backend-app-api: throw error if duplicate service implementations are provided

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-01-03 11:39:44 +01:00
parent 2ba88fe2df
commit 015a6dced6
7 changed files with 142 additions and 45 deletions
@@ -64,33 +64,34 @@ describe('TestBackend', () => {
const extensionPoint3 = createExtensionPoint<Obj>({ id: 'b3' });
const extensionPoint4 = createExtensionPoint<Obj>({ id: 'b4' });
const extensionPoint5 = createExtensionPoint<Obj>({ id: 'b5' });
await startTestBackend({
services: [
// @ts-expect-error
[extensionPoint1, { a: 'a' }],
[serviceRef, { a: 'a' }],
[serviceRef, { a: 'a', b: 'b' }],
// @ts-expect-error
[serviceRef, { c: 'c' }],
// @ts-expect-error
[serviceRef, { a: 'a', c: 'c' }],
// @ts-expect-error
[serviceRef, { a: 'a', b: 'b', c: 'c' }],
],
extensionPoints: [
// @ts-expect-error
[serviceRef, { a: 'a' }],
[extensionPoint1, { a: 'a' }],
[extensionPoint2, { a: 'a', b: 'b' }],
// @ts-expect-error
[extensionPoint3, { c: 'c' }],
// @ts-expect-error
[extensionPoint4, { a: 'a', c: 'c' }],
// @ts-expect-error
[extensionPoint5, { a: 'a', b: 'b', c: 'c' }],
],
});
expect(1).toBe(1);
await expect(
startTestBackend({
services: [
// @ts-expect-error
[extensionPoint1, { a: 'a' }],
[serviceRef, { a: 'a' }],
[serviceRef, { a: 'a', b: 'b' }],
// @ts-expect-error
[serviceRef, { c: 'c' }],
// @ts-expect-error
[serviceRef, { a: 'a', c: 'c' }],
// @ts-expect-error
[serviceRef, { a: 'a', b: 'b', c: 'c' }],
],
extensionPoints: [
// @ts-expect-error
[serviceRef, { a: 'a' }],
[extensionPoint1, { a: 'a' }],
[extensionPoint2, { a: 'a', b: 'b' }],
// @ts-expect-error
[extensionPoint3, { c: 'c' }],
// @ts-expect-error
[extensionPoint4, { a: 'a', c: 'c' }],
// @ts-expect-error
[extensionPoint5, { a: 'a', b: 'b', c: 'c' }],
],
}),
).rejects.toThrow();
});
it('should start the test backend', async () => {
@@ -93,11 +93,14 @@ export async function startTestBackend<
factory: async () => impl,
})();
}
if (typeof serviceDef === 'function') {
return serviceDef();
}
return serviceDef as ServiceFactory;
});
for (const factory of defaultServiceFactories) {
if (!factories.some(f => f.service === factory.service)) {
if (!factories.some(f => f.service.id === factory.service.id)) {
factories.push(factory);
}
}