refactor: Rename NotificationChannelInfo to NotificationChannel

This commit is contained in:
Joachim Van Herwegen
2023-02-07 14:35:02 +01:00
parent 61f04487a1
commit 8d31233075
41 changed files with 358 additions and 357 deletions

View File

@@ -3,8 +3,10 @@ import type { ResourceIdentifier } from '../../../../src/http/representation/Res
import type { Logger } from '../../../../src/logging/Logger';
import { getLoggerFor } from '../../../../src/logging/LogUtil';
import { KeyValueChannelStorage } from '../../../../src/server/notifications/KeyValueChannelStorage';
import type { NotificationChannel } from '../../../../src/server/notifications/NotificationChannel';
import type { NotificationChannelInfo } from '../../../../src/server/notifications/NotificationChannelStorage';
import type {
NotificationChannel,
NotificationChannelJson,
} from '../../../../src/server/notifications/NotificationChannel';
import type { KeyValueStorage } from '../../../../src/storage/keyvalue/KeyValueStorage';
import type { ReadWriteLocker } from '../../../../src/util/locking/ReadWriteLocker';
import resetAllMocks = jest.resetAllMocks;
@@ -19,13 +21,13 @@ describe('A KeyValueChannelStorage', (): void => {
const logger = getLoggerFor('mock');
const topic = 'http://example.com/foo';
const identifier = { path: topic };
const channel = {
const json = {
'@context': [ 'https://www.w3.org/ns/solid/notification/v1' ],
type: 'WebSocketSubscription2021',
topic,
} as NotificationChannel;
} as NotificationChannelJson;
const features = { aa: 'bb' };
let info: NotificationChannelInfo<Record<string, string>>;
let channel: NotificationChannel<Record<string, string>>;
let internalMap: Map<string, any>;
let internalStorage: KeyValueStorage<string, any>;
let locker: ReadWriteLocker;
@@ -33,7 +35,7 @@ describe('A KeyValueChannelStorage', (): void => {
beforeEach(async(): Promise<void> => {
resetAllMocks();
info = {
channel = {
id: `WebSocketSubscription2021:${v4()}:http://example.com/foo`,
topic,
type: 'WebSocketSubscription2021',
@@ -54,8 +56,8 @@ describe('A KeyValueChannelStorage', (): void => {
});
describe('#create', (): void => {
it('creates info based on a notification channel.', async(): Promise<void> => {
expect(storage.create(channel, features)).toEqual(info);
it('creates channel based on a notification channel.', async(): Promise<void> => {
expect(storage.create(json, features)).toEqual(channel);
});
});
@@ -64,15 +66,15 @@ describe('A KeyValueChannelStorage', (): void => {
await expect(storage.get('notexists')).resolves.toBeUndefined();
});
it('returns the matching info.', async(): Promise<void> => {
await storage.add(info);
await expect(storage.get(info.id)).resolves.toEqual(info);
it('returns the matching channel.', async(): Promise<void> => {
await storage.add(channel);
await expect(storage.get(channel.id)).resolves.toEqual(channel);
});
it('deletes expired info.', async(): Promise<void> => {
info.endAt = 0;
await storage.add(info);
await expect(storage.get(info.id)).resolves.toBeUndefined();
it('deletes expired channel.', async(): Promise<void> => {
channel.endAt = 0;
await storage.add(channel);
await expect(storage.get(channel.id)).resolves.toBeUndefined();
expect(internalMap.size).toBe(0);
});
});
@@ -82,93 +84,93 @@ describe('A KeyValueChannelStorage', (): void => {
await expect(storage.getAll(identifier)).resolves.toEqual([]);
});
it('returns the identifiers of all the matching infos.', async(): Promise<void> => {
await storage.add(info);
await expect(storage.getAll(identifier)).resolves.toEqual([ info.id ]);
it('returns the identifiers of all the matching channels.', async(): Promise<void> => {
await storage.add(channel);
await expect(storage.getAll(identifier)).resolves.toEqual([ channel.id ]);
});
});
describe('#add', (): void => {
it('adds the info and adds its id to the topic collection.', async(): Promise<void> => {
await expect(storage.add(info)).resolves.toBeUndefined();
it('adds the channel and adds its id to the topic collection.', async(): Promise<void> => {
await expect(storage.add(channel)).resolves.toBeUndefined();
expect(internalMap.size).toBe(2);
expect([ ...internalMap.values() ]).toEqual(expect.arrayContaining([
[ info.id ],
info,
[ channel.id ],
channel,
]));
});
});
describe('#update', (): void => {
it('changes the info.', async(): Promise<void> => {
await storage.add(info);
const newInfo = {
...info,
it('changes the channel.', async(): Promise<void> => {
await storage.add(channel);
const newChannel = {
...channel,
state: '123456',
};
await expect(storage.update(newInfo)).resolves.toBeUndefined();
await expect(storage.update(newChannel)).resolves.toBeUndefined();
expect([ ...internalMap.values() ]).toEqual(expect.arrayContaining([
[ info.id ],
newInfo,
[ channel.id ],
newChannel,
]));
});
it('rejects update requests that change the topic.', async(): Promise<void> => {
await storage.add(info);
const newInfo = {
...info,
await storage.add(channel);
const newChannel = {
...channel,
topic: 'http://example.com/other',
};
await expect(storage.update(newInfo)).rejects
.toThrow(`Trying to change the topic of a notification channel ${info.id}`);
await expect(storage.update(newChannel)).rejects
.toThrow(`Trying to change the topic of a notification channel ${channel.id}`);
});
it('rejects update request targeting a non-info value.', async(): Promise<void> => {
await storage.add(info);
it('rejects update request targeting a non-channel value.', async(): Promise<void> => {
await storage.add(channel);
// Looking for the key so this test doesn't depend on the internal keys used
const id = [ ...internalMap.entries() ].find((entry): boolean => Array.isArray(entry[1]))![0];
const newInfo = {
...info,
const newChannel = {
...channel,
id,
};
await expect(storage.update(newInfo)).rejects
.toThrow(`Trying to update ${id} which is not a NotificationChannelInfo.`);
await expect(storage.update(newChannel)).rejects
.toThrow(`Trying to update ${id} which is not a NotificationChannel.`);
});
});
describe('#delete', (): void => {
it('removes the info and its reference.', async(): Promise<void> => {
const info2 = {
...info,
it('removes the channel and its reference.', async(): Promise<void> => {
const channel2 = {
...channel,
id: 'differentId',
};
await storage.add(info);
await storage.add(info2);
await storage.add(channel);
await storage.add(channel2);
expect(internalMap.size).toBe(3);
await expect(storage.delete(info.id)).resolves.toBeUndefined();
await expect(storage.delete(channel.id)).resolves.toBeUndefined();
expect(internalMap.size).toBe(2);
expect([ ...internalMap.values() ]).toEqual(expect.arrayContaining([
[ info2.id ],
info2,
[ channel2.id ],
channel2,
]));
});
it('removes the references for an identifier if the array is empty.', async(): Promise<void> => {
await storage.add(info);
await expect(storage.delete(info.id)).resolves.toBeUndefined();
await storage.add(channel);
await expect(storage.delete(channel.id)).resolves.toBeUndefined();
expect(internalMap.size).toBe(0);
});
it('does nothing if the target does not exist.', async(): Promise<void> => {
await expect(storage.delete(info.id)).resolves.toBeUndefined();
await expect(storage.delete(channel.id)).resolves.toBeUndefined();
});
it('logs an error if the target can not be found in the list of references.', async(): Promise<void> => {
await storage.add(info);
await storage.add(channel);
// Looking for the key so this test doesn't depend on the internal keys used
const id = [ ...internalMap.entries() ].find((entry): boolean => Array.isArray(entry[1]))![0];
internalMap.set(id, []);
await expect(storage.delete(info.id)).resolves.toBeUndefined();
await expect(storage.delete(channel.id)).resolves.toBeUndefined();
expect(logger.error).toHaveBeenCalledTimes(2);
});
});