test: build plugins as named objects
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+107
-63
No files matched your search
@@ -66,9 +66,12 @@ describe('app.upgrade', () => {
|
||||
|
||||
it('emits request:step:enter/leave for ws steps, like HTTP routes do', async () => {
|
||||
const seen: string[] = [];
|
||||
const logger = (api: any) => {
|
||||
api.bus.on('request:step:enter', (p: any) => seen.push(`enter:${p.name}`));
|
||||
api.bus.on('request:step:leave', (p: any) => seen.push(`leave:${p.name}`));
|
||||
const logger = {
|
||||
name: 'ws-step-logger',
|
||||
mount: (api: any) => {
|
||||
api.bus.on('request:step:enter', (p: any) => seen.push(`enter:${p.name}`));
|
||||
api.bus.on('request:step:leave', (p: any) => seen.push(`leave:${p.name}`));
|
||||
},
|
||||
};
|
||||
const wsApp = createApp({ modules: [Tagged], plugins: [logger] });
|
||||
const { socket, inbound, sent } = fakeSocket();
|
||||
|
||||
+32
-23
@@ -56,7 +56,10 @@ test('ready() is a no-op on a non-mesh app, so one protocol works for both', asy
|
||||
|
||||
test('a logger plugin observes step:enter without mutating the chain', async () => {
|
||||
const seen: string[] = [];
|
||||
const logger = (api: any) => api.bus.on('request:step:enter', (p: any) => seen.push(p.name));
|
||||
const logger = {
|
||||
name: 'step-logger',
|
||||
mount: (api: any) => api.bus.on('request:step:enter', (p: any) => seen.push(p.name)),
|
||||
};
|
||||
const app = createApp({ modules: [ApiModule], plugins: [logger] });
|
||||
const server = await app.listen(0);
|
||||
const port = (server.address() as any).port;
|
||||
@@ -256,17 +259,20 @@ describe('per-route subgraph slicing', () => {
|
||||
it('a side-effect step (provides: []) added by a plugin runs unconditionally', async () => {
|
||||
// NB: @Step always provides exactly one token (always sliceable). provides:[] only arises via a plugin's scope.add.
|
||||
const ran: string[] = [];
|
||||
const observer = (api: any) =>
|
||||
api.scope.add({
|
||||
kind: 'step',
|
||||
name: 'obs',
|
||||
needs: [],
|
||||
provides: [],
|
||||
run: () => {
|
||||
ran.push('obs');
|
||||
return {};
|
||||
},
|
||||
});
|
||||
const observer = {
|
||||
name: 'side-effect-observer',
|
||||
mount: (api: any) =>
|
||||
api.scope.add({
|
||||
kind: 'step',
|
||||
name: 'obs',
|
||||
needs: [],
|
||||
provides: [],
|
||||
run: () => {
|
||||
ran.push('obs');
|
||||
return {};
|
||||
},
|
||||
}),
|
||||
};
|
||||
@Route('/')
|
||||
class Ctl {
|
||||
@Get('/free') free() {
|
||||
@@ -285,17 +291,20 @@ describe('per-route subgraph slicing', () => {
|
||||
|
||||
it('an always-run observer step receives its declared needs even when the route does not need them', async () => {
|
||||
let seen: any;
|
||||
const observer = (api: any) =>
|
||||
api.scope.add({
|
||||
kind: 'step',
|
||||
name: 'audit',
|
||||
needs: ['user'],
|
||||
provides: [],
|
||||
run: (ctx: any) => {
|
||||
seen = ctx.user;
|
||||
return {};
|
||||
},
|
||||
});
|
||||
const observer = {
|
||||
name: 'audit-observer',
|
||||
mount: (api: any) =>
|
||||
api.scope.add({
|
||||
kind: 'step',
|
||||
name: 'audit',
|
||||
needs: ['user'],
|
||||
provides: [],
|
||||
run: (ctx: any) => {
|
||||
seen = ctx.user;
|
||||
return {};
|
||||
},
|
||||
}),
|
||||
};
|
||||
@Step({ provides: 'user', needs: [] })
|
||||
class Auth {
|
||||
run() {
|
||||
|
||||
@@ -22,8 +22,11 @@ describe('reaching the bus', () => {
|
||||
const fromOutside: string[] = [];
|
||||
const fromPlugin: string[] = [];
|
||||
|
||||
const observer: Plugin = (api) => {
|
||||
api.bus.on('request:end', (e) => fromPlugin.push(e.route ?? ''));
|
||||
const observer: Plugin = {
|
||||
name: 'bus-observer',
|
||||
mount(api) {
|
||||
api.bus.on('request:end', (e) => fromPlugin.push(e.route ?? ''));
|
||||
},
|
||||
};
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
|
||||
+28
-15
@@ -10,9 +10,12 @@ test('plugin can observe via bus.on and add to its own scope', () => {
|
||||
const added: string[] = [];
|
||||
const scope: ScopeApi = { add: (n) => added.push(n.name) };
|
||||
|
||||
const logger: Plugin = (api) => {
|
||||
api.bus.on('request:step:enter', () => {});
|
||||
api.scope.add({ kind: 'step', name: 'log', needs: [], provides: [], run: () => ({}) });
|
||||
const logger: Plugin = {
|
||||
name: 'logger',
|
||||
mount(api) {
|
||||
api.bus.on('request:step:enter', () => {});
|
||||
api.scope.add({ kind: 'step', name: 'log', needs: [], provides: [], run: () => ({}) });
|
||||
},
|
||||
};
|
||||
|
||||
const mounted = vi.fn();
|
||||
@@ -26,8 +29,11 @@ test('plugin can observe via bus.on and add to its own scope', () => {
|
||||
test('plugin api does NOT expose bus.emit', () => {
|
||||
const bus = new Bus();
|
||||
const scope: ScopeApi = { add: () => {} };
|
||||
const plugin: Plugin = (api) => {
|
||||
expect((api.bus as any).emit).toBeUndefined();
|
||||
const plugin: Plugin = {
|
||||
name: 'emit-probe',
|
||||
mount(api) {
|
||||
expect((api.bus as any).emit).toBeUndefined();
|
||||
},
|
||||
};
|
||||
mountPlugin(plugin, bus, scope, noTeardown());
|
||||
});
|
||||
@@ -35,8 +41,11 @@ test('plugin api does NOT expose bus.emit', () => {
|
||||
test('plugin can register a teardown, and it reaches the registry', () => {
|
||||
const bus = new Bus();
|
||||
const registered: TeardownFn[] = [];
|
||||
const plugin: Plugin = (api) => {
|
||||
api.onShutdown(() => {});
|
||||
const plugin: Plugin = {
|
||||
name: 'teardown-registrar',
|
||||
mount(api) {
|
||||
api.onShutdown(() => {});
|
||||
},
|
||||
};
|
||||
|
||||
mountPlugin(plugin, bus, { add: () => {} }, (fn) => registered.push(fn));
|
||||
@@ -44,22 +53,26 @@ test('plugin can register a teardown, and it reaches the registry', () => {
|
||||
expect(registered).toHaveLength(1);
|
||||
});
|
||||
|
||||
// D3's no-break guarantee, asserted rather than assumed: `Plugin`'s signature did not change, so a
|
||||
// plugin written before this capability existed must mount and behave exactly as it did.
|
||||
test('a plugin that never calls onShutdown behaves exactly as before', () => {
|
||||
// A plugin that opens nothing registers nothing: `onShutdown` is opt-in, and the teardown registry
|
||||
// stays empty rather than collecting no-ops. (The shape guarantee this test used to assert went
|
||||
// away with the move to `{ name, mount }` — see CHANGELOG, Breaking.)
|
||||
test('a plugin that never calls onShutdown registers no teardown', () => {
|
||||
const bus = new Bus();
|
||||
const added: string[] = [];
|
||||
const registered: TeardownFn[] = [];
|
||||
const legacy: Plugin = (api) => {
|
||||
api.bus.on('stream:open', () => {});
|
||||
api.scope.add({ kind: 'provider', name: 'thing', needs: [], provides: ['thing'], run: () => ({}) });
|
||||
const quiet: Plugin = {
|
||||
name: 'quiet',
|
||||
mount(api) {
|
||||
api.bus.on('stream:open', () => {});
|
||||
api.scope.add({ kind: 'provider', name: 'thing', needs: [], provides: ['thing'], run: () => ({}) });
|
||||
},
|
||||
};
|
||||
|
||||
const mounted = vi.fn();
|
||||
bus.on('plugin:mounted', mounted);
|
||||
mountPlugin(legacy, bus, { add: (n) => added.push(n.name) }, (fn) => registered.push(fn));
|
||||
mountPlugin(quiet, bus, { add: (n) => added.push(n.name) }, (fn) => registered.push(fn));
|
||||
|
||||
expect(added).toEqual(['thing']);
|
||||
expect(mounted).toHaveBeenCalled();
|
||||
expect(mounted).toHaveBeenCalledWith(expect.objectContaining({ name: 'quiet' }));
|
||||
expect(registered).toHaveLength(0);
|
||||
});
|
||||
+26
-14
@@ -17,11 +17,14 @@ class Ctl {
|
||||
describe('shutdown teardown', () => {
|
||||
it('awaits a plugin onShutdown before close() resolves', async () => {
|
||||
const order: string[] = [];
|
||||
const plugin: Plugin = (api) => {
|
||||
api.onShutdown(async () => {
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
order.push('plugin torn down');
|
||||
});
|
||||
const plugin: Plugin = {
|
||||
name: 'slow-teardown',
|
||||
mount(api) {
|
||||
api.onShutdown(async () => {
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
order.push('plugin torn down');
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
@@ -135,11 +138,14 @@ describe('shutdown teardown', () => {
|
||||
it('logs a failing teardown, runs the rest, and still resolves', async () => {
|
||||
const logger = silentLogger();
|
||||
const closed: string[] = [];
|
||||
const plugin: Plugin = (api) => {
|
||||
api.onShutdown(() => {
|
||||
throw new Error('teardown boom');
|
||||
});
|
||||
api.onShutdown(() => void closed.push('survivor'));
|
||||
const plugin: Plugin = {
|
||||
name: 'failing-teardown',
|
||||
mount(api) {
|
||||
api.onShutdown(() => {
|
||||
throw new Error('teardown boom');
|
||||
});
|
||||
api.onShutdown(() => void closed.push('survivor'));
|
||||
},
|
||||
};
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
@@ -153,8 +159,11 @@ describe('shutdown teardown', () => {
|
||||
});
|
||||
|
||||
it('does not let a hanging teardown push close() past its deadline', async () => {
|
||||
const plugin: Plugin = (api) => {
|
||||
api.onShutdown(() => new Promise<void>(() => {})); // never settles
|
||||
const plugin: Plugin = {
|
||||
name: 'hanging-teardown',
|
||||
mount(api) {
|
||||
api.onShutdown(() => new Promise<void>(() => {})); // never settles
|
||||
},
|
||||
};
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
@@ -199,8 +208,11 @@ describe('shutdown teardown', () => {
|
||||
|
||||
it('drains only once when close() is called twice', async () => {
|
||||
let runs = 0;
|
||||
const plugin: Plugin = (api) => {
|
||||
api.onShutdown(() => void runs++);
|
||||
const plugin: Plugin = {
|
||||
name: 'counting-teardown',
|
||||
mount(api) {
|
||||
api.onShutdown(() => void runs++);
|
||||
},
|
||||
};
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
|
||||
@@ -26,8 +26,8 @@ const prometheusText: TransformerFn = (value) => ({
|
||||
body: String(value),
|
||||
});
|
||||
|
||||
// A plugin split into named functions rather than one inline arrow: `Plugin` alone types the
|
||||
// arrow, and nothing typed its argument.
|
||||
// A plugin split into named functions rather than one inline `mount`: `Plugin` types the object,
|
||||
// and `PluginApi`/`ScopeApi`/`ScopeNode` type what the pieces receive.
|
||||
function subscribe(api: PluginApi): void {
|
||||
api.bus.on('request:end', () => {});
|
||||
api.onShutdown(flush);
|
||||
@@ -40,9 +40,12 @@ function contribute(scope: ScopeApi): void {
|
||||
scope.add(node);
|
||||
}
|
||||
|
||||
const metricsPlugin: Plugin = (api) => {
|
||||
subscribe(api);
|
||||
contribute(api.scope);
|
||||
const metricsPlugin: Plugin = {
|
||||
name: 'metrics',
|
||||
mount(api) {
|
||||
subscribe(api);
|
||||
contribute(api.scope);
|
||||
},
|
||||
};
|
||||
|
||||
// createApp({ hooks }) — declared as a value, which is the point of the option.
|
||||
@@ -59,7 +62,8 @@ const encoder: StreamEncoder = {
|
||||
|
||||
test('every extension point is typeable from the package barrel', () => {
|
||||
expect(prometheusText('x').body).toBe('x');
|
||||
expect(typeof metricsPlugin).toBe('function');
|
||||
expect(typeof metricsPlugin.mount).toBe('function');
|
||||
expect(metricsPlugin.name).toBe('metrics');
|
||||
expect(hooks).toHaveLength(1);
|
||||
expect(renderError(new Error('boom'), {} as never)?.status).toBe(500);
|
||||
expect(encoder.encode(1)).toBe('1');
|
||||
|
||||
Reference in new issue
Block a user