import { Readable } from 'stream'; import arrayifyStream from 'arrayify-stream'; import streamifyArray from 'streamify-array'; import { RuntimeConfig } from '../../../src/init/RuntimeConfig'; import { BinaryRepresentation } from '../../../src/ldp/representation/BinaryRepresentation'; import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata'; import { InMemoryResourceStore } from '../../../src/storage/InMemoryResourceStore'; import { DATA_TYPE_BINARY } from '../../../src/util/ContentTypes'; import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError'; const base = 'http://test.com/'; describe('A InMemoryResourceStore', (): void => { let store: InMemoryResourceStore; let representation: BinaryRepresentation; const dataString = ' .'; beforeEach(async(): Promise => { store = new InMemoryResourceStore(new RuntimeConfig({ base })); representation = { data: streamifyArray([ dataString ]), dataType: DATA_TYPE_BINARY, metadata: {} as RepresentationMetadata, }; }); it('errors if a resource was not found.', async(): Promise => { await expect(store.getRepresentation({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError); await expect(store.addResource({ path: 'http://wrong.com/wrong' }, representation)) .rejects.toThrow(NotFoundHttpError); await expect(store.deleteResource({ path: 'wrong' })).rejects.toThrow(NotFoundHttpError); await expect(store.setRepresentation({ path: 'http://wrong.com/' }, representation)) .rejects.toThrow(NotFoundHttpError); }); it('errors when modifying resources.', async(): Promise => { await expect(store.modifyResource()).rejects.toThrow(Error); }); it('can write and read data.', async(): Promise => { const identifier = await store.addResource({ path: base }, representation); expect(identifier.path.startsWith(base)).toBeTruthy(); const result = await store.getRepresentation(identifier); expect(result).toEqual({ dataType: representation.dataType, data: expect.any(Readable), metadata: representation.metadata, }); await expect(arrayifyStream(result.data)).resolves.toEqual([ dataString ]); }); it('can add resources to previously added resources.', async(): Promise => { const identifier = await store.addResource({ path: base }, representation); representation.data = streamifyArray([ ]); const childIdentifier = await store.addResource(identifier, representation); expect(childIdentifier.path).toContain(identifier.path); }); it('can set data.', async(): Promise => { await store.setRepresentation({ path: base }, representation); const result = await store.getRepresentation({ path: base }); expect(result).toEqual({ dataType: representation.dataType, data: expect.any(Readable), metadata: representation.metadata, }); await expect(arrayifyStream(result.data)).resolves.toEqual([ dataString ]); }); it('can delete data.', async(): Promise => { await store.deleteResource({ path: base }); await expect(store.getRepresentation({ path: base })).rejects.toThrow(NotFoundHttpError); }); });