CommunitySolidServer/test/unit/server/notifications/generate/ActivityNotificationGenerator.test.ts
Joachim Van Herwegen d6be724a12 Merge branch 'main' into versions/6.0.0
# Conflicts:
#	config/ldp/authorization/readers/access-checkers/agent-group.json
2023-04-24 11:21:59 +02:00

62 lines
2.4 KiB
TypeScript

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, CONTENT_TYPE, 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',
};
const activity = AS.terms.Update;
const metadata = new RepresentationMetadata({
[RDF.type]: LDP.terms.Resource,
// Needed for ETag
[DC.modified]: new Date().toISOString(),
[CONTENT_TYPE]: 'text/turtle',
});
let store: jest.Mocked<ResourceStore>;
let generator: ActivityNotificationGenerator;
beforeEach(async(): Promise<void> => {
store = {
getRepresentation: jest.fn().mockResolvedValue(new BasicRepresentation('', metadata)),
} as any;
generator = new ActivityNotificationGenerator(store);
});
it('only handles defined activities.', async(): Promise<void> => {
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<void> => {
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+-text\/turtle"/u),
published: date,
});
jest.useRealTimers();
});
});