import { BasicRepresentation } from '../../../../../src/http/representation/BasicRepresentation'; import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata'; import type { ResourceIdentifier } from '../../../../../src/http/representation/ResourceIdentifier'; import { ActivityNotificationGenerator, } from '../../../../../src/server/notifications/generate/ActivityNotificationGenerator'; import type { NotificationChannel } from '../../../../../src/server/notifications/NotificationChannel'; import type { ResourceStore } from '../../../../../src/storage/ResourceStore'; import { AS, DC, LDP, RDF } from '../../../../../src/util/Vocabularies'; describe('An ActivityNotificationGenerator', (): void => { const topic: ResourceIdentifier = { path: 'http://example.com/foo' }; const channel: NotificationChannel = { id: 'id', topic: topic.path, type: 'type', features: {}, lastEmit: 0, }; const activity = AS.terms.Update; const metadata = new RepresentationMetadata({ [RDF.type]: LDP.terms.Resource, // Needed for ETag [DC.modified]: new Date().toISOString(), }); let store: jest.Mocked; let generator: ActivityNotificationGenerator; beforeEach(async(): Promise => { store = { getRepresentation: jest.fn().mockResolvedValue(new BasicRepresentation('', metadata)), } as any; generator = new ActivityNotificationGenerator(store); }); it('only handles defined activities.', async(): Promise => { await expect(generator.canHandle({ topic, channel })).rejects.toThrow('Only defined activities are supported.'); await expect(generator.canHandle({ topic, channel, activity })).resolves.toBeUndefined(); }); it('generates a notification.', async(): Promise => { const date = '1988-03-09T14:48:00.000Z'; const ms = Date.parse(date); jest.useFakeTimers(); jest.setSystemTime(ms); await expect(generator.handle({ topic, channel, activity })).resolves.toEqual({ '@context': [ 'https://www.w3.org/ns/activitystreams', 'https://www.w3.org/ns/solid/notification/v1', ], id: `urn:${ms}:http://example.com/foo`, type: 'Update', object: 'http://example.com/foo', state: expect.stringMatching(/"\d+"/u), published: date, }); jest.useRealTimers(); }); });