Skip to content

Commit

Permalink
Add ReadableStream support for importActorProfile and validateExportS…
Browse files Browse the repository at this point in the history
…tream; update dependencies
  • Loading branch information
0marSalah committed Jan 9, 2025
1 parent c869227 commit 6afa20d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 38 deletions.
Binary file modified out/test-export-2024-01-01.tar
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"./package.json": "./package.json"
},
"dependencies": {
"eslint-plugin-n": "^17.15.1",
"stream": "^0.0.3",
"tar-stream": "^3.1.7",
"yaml": "^2.5.1"
},
Expand Down
Binary file added test/fixtures/tarball-samples/invalid-actor.tar
Binary file not shown.
Binary file not shown.
Binary file added test/fixtures/tarball-samples/missing-outbox.tar
Binary file not shown.
4 changes: 3 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { exportActorProfile, importActorProfile } from '../src'
import { outbox } from './fixtures/outbox'
import { actorProfile } from './fixtures/actorProfile'
import { expect } from 'chai'
import { Readable } from 'node:stream'

describe('exportActorProfile', () => {
it('calls function', async () => {
Expand Down Expand Up @@ -40,7 +41,8 @@ describe('importActorProfile', () => {
)

// Use the importActorProfile function to parse the tar contents
const importedData = await importActorProfile(tarBuffer)
const tarStream = Readable.from(tarBuffer)
const importedData = await importActorProfile(tarStream)

// Log or inspect the imported data structure
// console.log('Imported Data:', importedData)
Expand Down
74 changes: 37 additions & 37 deletions test/verify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { expect } from 'chai'
import { readFileSync } from 'fs'
import { validateExportStream } from '../src/verify'
import { validateExportStream } from '../dist'
import { Readable } from 'stream'

describe('validateExportStream', () => {
it('should validate a valid tarball', async () => {
// Load a valid tarball (e.g., exported-profile-valid.tar)
const tarBuffer = readFileSync(
'test/fixtures/tarball-samples/valid-export.tar'
)
const result = await validateExportStream(tarBuffer)
const tarStream = Readable.from(tarBuffer)
const result = await validateExportStream(tarStream)
console.log('🚀 ~ it ~ valid result:', result)

expect(result.valid).to.be.true
expect(result.errors).to.be.an('array').that.is.empty
Expand All @@ -19,7 +22,9 @@ describe('validateExportStream', () => {
const tarBuffer = readFileSync(
'test/fixtures/tarball-samples/missing-manifest.tar'
)
const result = await validateExportStream(tarBuffer)
const tarStream = Readable.from(tarBuffer)
const result = await validateExportStream(tarStream)
console.log('🚀 ~ it ~ miss mani result:', result)

expect(result.valid).to.be.false
})
Expand All @@ -29,48 +34,43 @@ describe('validateExportStream', () => {
const tarBuffer = readFileSync(
'test/fixtures/tarball-samples/missing-actor.tar'
)
const result = await validateExportStream(tarBuffer)
const tarStream = Readable.from(tarBuffer)
const result = await validateExportStream(tarStream)

expect(result.valid).to.be.false
console.log(JSON.stringify(result.errors))
})

// it('should fail if outbox.json is missing', async () => {
// // Load a tarball with missing outbox.json
// const tarBuffer = readFileSync(
// 'test/fixtures/exported-profile-missing-outbox.tar'
// )
// const result = await validateExportStream(tarBuffer)
it('should fail if outbox.json is missing', async () => {
// Load a tarball with missing outbox.json
const tarBuffer = readFileSync(
'test/fixtures/tarball-samples/missing-outbox.tar'
)
const tarStream = Readable.from(tarBuffer)
const result = await validateExportStream(tarStream)

// expect(result.valid).to.be.false
// expect(result.errors).to.include(
// 'Missing required file: activitypub/outbox.json'
// )
// })
expect(result.valid).to.be.false
})

// it('should fail if actor.json contains invalid JSON', async () => {
// // Load a tarball with invalid JSON in actor.json
// const tarBuffer = readFileSync(
// 'test/fixtures/exported-profile-invalid-actor-json.tar'
// )
// const result = await validateExportStream(tarBuffer)
it('should fail if actor.json contains invalid JSON', async () => {
// Load a tarball with invalid JSON in actor.json
const tarBuffer = readFileSync(
'test/fixtures/tarball-samples/invalid-actor.tar'
)
const tarStream = Readable.from(tarBuffer)
const result = await validateExportStream(tarStream)

// expect(result.valid).to.be.false
// expect(result.errors).to.include(
// 'Error processing file activitypub/actor.json: Unexpected token } in JSON at position 42'
// )
// })
expect(result.valid).to.be.false
})

// it('should fail if manifest.yaml is invalid', async () => {
// // Load a tarball with invalid manifest.yaml
// const tarBuffer = readFileSync(
// 'test/fixtures/exported-profile-invalid-manifest.tar'
// )
// const result = await validateExportStream(tarBuffer)
it('should fail if manifest.yaml is invalid', async () => {
// Load a tarball with invalid manifest.yaml
const tarBuffer = readFileSync(
'test/fixtures/tarball-samples/invalid-manifest.tar'
)
const tarStream = Readable.from(tarBuffer)
const result = await validateExportStream(tarStream)

// expect(result.valid).to.be.false
// expect(result.errors).to.include(
// 'Manifest is missing required field: ubc-version'
// )
// })
expect(result.valid).to.be.false
})
})

0 comments on commit 6afa20d

Please sign in to comment.