Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed Dec 12, 2024
1 parent 615324f commit 42cae41
Showing 1 changed file with 100 additions and 48 deletions.
148 changes: 100 additions & 48 deletions lib/tests/mocha/unit/seekable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { createSandbox, SinonSandbox } from 'sinon';

import { type Chunker, sourceToStream } from '../../../src/seekable.js';
import { type Chunker, fromSource, sourceToStream } from '../../../src/seekable.js';

function range(a: number, b?: number): number[] {
if (!b) {
Expand Down Expand Up @@ -137,61 +137,113 @@ describe('chunkers', () => {
});
});

describe('sourceToStream', () => {
describe('sourceToStream', () => {
it('should return a ReadableStream for buffer source', async () => {
const b = new Uint8Array(range(256));
const stream = await sourceToStream({ type: 'buffer', location: b });
expect(stream).to.be.an.instanceOf(ReadableStream);
});
describe('fromSource', () => {
it('should return a chunker for buffer source', async () => {
const b = new Uint8Array(range(256));
const chunker = await fromSource({ type: 'buffer', location: b });
const result = await chunker();
expect(result).to.deep.equal(b);
});

it('should return a ReadableStream for file-browser source', async () => {
const file = new Blob([new Uint8Array(range(256))]);
const stream = await sourceToStream({ type: 'file-browser', location: file });
expect(stream).to.be.an.instanceOf(ReadableStream);
});
it('should return a chunker for chunker source', async () => {
const b = new Uint8Array(range(256));
const { fromBuffer } = await import('../../../src/seekable.js');
const originalChunker = fromBuffer(b);
const chunker = await fromSource({ type: 'chunker', location: originalChunker });
const result = await chunker();
expect(result).to.deep.equal(b);
});

it('should return a ReadableStream for chunker source', async () => {
const { fromBuffer } = await import('../../../src/seekable.js');
const b = new Uint8Array(range(256));
const chunker = fromBuffer(b);
const stream = await sourceToStream({ type: 'chunker', location: chunker });
expect(stream).to.be.an.instanceOf(ReadableStream);
const result = await saveToBuffer(stream);
expect(result).to.deep.equal(b);
});
it('should return a chunker for file-browser source', async () => {
const file = new Blob([new Uint8Array(range(256))]);
const chunker = await fromSource({ type: 'file-browser', location: file });
const result = await chunker();
expect(result).to.deep.equal(new Uint8Array(await file.arrayBuffer()));
});

it('should return a chunker for remote source', async () => {
const chunker = await fromSource({ type: 'remote', location: 'http://localhost:3000/file' });
const result = await chunker();
expect(result).to.deep.equal(new Uint8Array(range(256)));
});

it('should return a ReadableStream for remote source', async () => {
const stream = await sourceToStream({
type: 'remote',
location: 'http://localhost:3000/file',
});
expect(stream).to.be.an.instanceOf(ReadableStream);
it('should return a chunker for stream source', async () => {
const b = new Uint8Array(range(256));
const readableStream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(b);
controller.close();
},
});
const chunker = await fromSource({ type: 'stream', location: readableStream });
const result = await chunker();
expect(result).to.deep.equal(b);
});

it('should throw an error for unsupported source type', async () => {
try {
await fromSource({ type: 'unsupported', location: 'unsupported' } as any);
expect.fail();
} catch (e) {
expect(e).to.be.an('error');
expect(e.message).to.include('Data source type not defined, or not supported');
}
});
});

describe('sourceToStream', () => {
it('should return a ReadableStream for buffer source', async () => {
const b = new Uint8Array(range(256));
const stream = await sourceToStream({ type: 'buffer', location: b });
expect(stream).to.be.an.instanceOf(ReadableStream);
});

it('should return a ReadableStream for file-browser source', async () => {
const file = new Blob([new Uint8Array(range(256))]);
const stream = await sourceToStream({ type: 'file-browser', location: file });
expect(stream).to.be.an.instanceOf(ReadableStream);
});

it('should return a ReadableStream for stream source', async () => {
const b = new Uint8Array(range(256));
const readableStream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(b);
controller.close();
},
});
const stream = await sourceToStream({ type: 'stream', location: readableStream });
expect(stream).to.be.an.instanceOf(ReadableStream);
const result = await saveToBuffer(stream);
expect(result).to.deep.equal(b);
it('should return a ReadableStream for chunker source', async () => {
const { fromBuffer } = await import('../../../src/seekable.js');
const b = new Uint8Array(range(256));
const chunker = fromBuffer(b);
const stream = await sourceToStream({ type: 'chunker', location: chunker });
expect(stream).to.be.an.instanceOf(ReadableStream);
const result = await saveToBuffer(stream);
expect(result).to.deep.equal(b);
});

it('should return a ReadableStream for remote source', async () => {
const stream = await sourceToStream({
type: 'remote',
location: 'http://localhost:3000/file',
});
expect(stream).to.be.an.instanceOf(ReadableStream);
});

it('should return a ReadableStream', async () => {
const { fromBuffer } = await import('../../../src/seekable.js');
const b = new Uint8Array(range(256));
const chunker = fromBuffer(b);
const stream = await sourceToStream({ type: 'chunker', location: chunker });
expect(stream).to.be.an.instanceOf(ReadableStream);
const result = await saveToBuffer(stream);
expect(result).to.deep.equal(b);
it('should return a ReadableStream for stream source', async () => {
const b = new Uint8Array(range(256));
const readableStream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(b);
controller.close();
},
});
const stream = await sourceToStream({ type: 'stream', location: readableStream });
expect(stream).to.be.an.instanceOf(ReadableStream);
const result = await saveToBuffer(stream);
expect(result).to.deep.equal(b);
});

it('should return a ReadableStream', async () => {
const { fromBuffer } = await import('../../../src/seekable.js');
const b = new Uint8Array(range(256));
const chunker = fromBuffer(b);
const stream = await sourceToStream({ type: 'chunker', location: chunker });
expect(stream).to.be.an.instanceOf(ReadableStream);
const result = await saveToBuffer(stream);
expect(result).to.deep.equal(b);
});
});

Expand Down

0 comments on commit 42cae41

Please sign in to comment.