import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation'; import type { Representation } from '../../../../src/http/representation/Representation'; import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata'; import { SingleContainerJsonStorage } from '../../../../src/init/migration/SingleContainerJsonStorage'; import type { ResourceStore } from '../../../../src/storage/ResourceStore'; import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError'; import { isContainerIdentifier } from '../../../../src/util/PathUtil'; import { LDP } from '../../../../src/util/Vocabularies'; describe('A SingleContainerJsonStorage', (): void => { const baseUrl = 'http://example.com/'; const container = '.internal/accounts/'; let store: jest.Mocked; let storage: SingleContainerJsonStorage; beforeEach(async(): Promise => { store = { getRepresentation: jest.fn(async(id): Promise => { if (isContainerIdentifier(id)) { const metadata = new RepresentationMetadata(id); metadata.add(LDP.terms.contains, 'http://example.com/.internal/accounts/foo'); metadata.add(LDP.terms.contains, 'http://example.com/.internal/accounts/bar/'); metadata.add(LDP.terms.contains, 'http://example.com/.internal/accounts/baz'); metadata.add(LDP.terms.contains, 'http://example.com/.internal/accounts/unknown'); return new BasicRepresentation('', metadata); } if (id.path.endsWith('unknown')) { throw new NotFoundHttpError(); } return new BasicRepresentation(`{ "id": "${id.path}" }`, 'text/plain'); }), } satisfies Partial as any; storage = new SingleContainerJsonStorage(store, baseUrl, container); }); it('only iterates over the documents in the base container.', async(): Promise => { const entries = []; for await (const entry of storage.entries()) { entries.push(entry); } expect(entries).toEqual([ [ 'foo', { id: 'http://example.com/.internal/accounts/foo' }], [ 'baz', { id: 'http://example.com/.internal/accounts/baz' }], ]); expect(store.getRepresentation).toHaveBeenCalledTimes(4); expect(store.getRepresentation).toHaveBeenNthCalledWith( 1, { path: 'http://example.com/.internal/accounts/' }, {}, ); expect(store.getRepresentation).toHaveBeenNthCalledWith( 2, { path: 'http://example.com/.internal/accounts/foo' }, { type: { 'application/json': 1 }}, ); expect(store.getRepresentation).toHaveBeenNthCalledWith( 3, { path: 'http://example.com/.internal/accounts/baz' }, { type: { 'application/json': 1 }}, ); expect(store.getRepresentation).toHaveBeenNthCalledWith( 4, { path: 'http://example.com/.internal/accounts/unknown' }, { type: { 'application/json': 1 }}, ); }); it('does nothing if the container does not exist.', async(): Promise => { store.getRepresentation.mockRejectedValueOnce(new NotFoundHttpError()); const entries = []; for await (const entry of storage.entries()) { entries.push(entry); } expect(entries).toHaveLength(0); }); });