feat: per-route subgraph slicing — routes run only their needed providers/steps

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-28 01:09:47 -05:00
1 parent 68caca2cc1
commit df01bbfc41
4 files changed
+82 -4

No files matched your search

+39
View File
@@ -154,6 +154,45 @@ describe('app streaming wiring', () => {
});
});
describe('per-route subgraph slicing', () => {
it('a route that does not @needs a step does not run it', async () => {
const ran: string[] = [];
@Step({ provides: 'tracked', needs: [] })
class Tracked { run() { ran.push('tracked'); return { tracked: true }; } }
@Route('/')
class Ctl {
@Get('/needs') needs(@needs('tracked') t: any) { return { t }; }
@Get('/free') free() { return { ok: true }; }
}
@Module({ mountpoint: '/', steps: [Tracked], controllers: [Ctl] }) class M {}
const app = createApp({ modules: [M] });
const server = await app.listen(0);
const port = (server.address() as any).port;
await fetch(`http://127.0.0.1:${port}/free`).then((r) => r.json());
expect(ran).toEqual([]); // step did NOT run for /free
await fetch(`http://127.0.0.1:${port}/needs`).then((r) => r.json());
expect(ran).toEqual(['tracked']); // step ran for /needs
expect(app.inspect('/free').map((l) => l.kind)).toEqual(['handler']); // no step in the chain
server.close();
});
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 {}; } });
@Route('/')
class Ctl { @Get('/free') free() { return { ok: true }; } }
@Module({ mountpoint: '/', controllers: [Ctl] }) class M {}
const app = createApp({ modules: [M], plugins: [observer] });
const server = await app.listen(0);
const port = (server.address() as any).port;
await fetch(`http://127.0.0.1:${port}/free`).then((r) => r.json());
expect(ran).toEqual(['obs']); // ran despite the route @needs-ing nothing
server.close();
});
});
describe('graceful shutdown', () => {
it('app.close() stops accepting and resolves, even with an SSE stream open', async () => {
@Route('/')
+20 -2
View File
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { topoSort, GraphNode } from '../src/graph';
import { describe, expect, it, test } from 'vitest';
import { topoSort, GraphNode, subgraphFor } from '../src/graph';
const node = (name: string, needs: string[], provides: string[]): GraphNode =>
({ name, needs, provides, origin: 'test' });
@@ -23,3 +23,21 @@ test('throws on cycle', () => {
const nodes = [node('a', ['b'], ['a']), node('b', ['a'], ['b'])];
expect(() => topoSort(nodes, [])).toThrow(/cycle detected/);
});
const N = (name: string, needs: string[] = []) => ({ name, needs, provides: [name], origin: 'm' });
describe('subgraphFor', () => {
const ordered = [N('config'), N('db', ['config']), N('user', ['db', 'req']), N('audit', ['db'])];
it('returns the transitive closure of needs, in the given order', () => {
expect(subgraphFor(['user'], ordered).map((n) => n.name)).toEqual(['config', 'db', 'user']);
});
it('returns empty for no needs', () => {
expect(subgraphFor([], ordered)).toEqual([]);
});
it('ignores tokens with no producer (seeds/envelope)', () => {
expect(subgraphFor(['user', 'query', 'req'], ordered).map((n) => n.name)).toEqual(['config', 'db', 'user']);
});
it('does not pull in unrelated nodes', () => {
expect(subgraphFor(['user'], ordered).map((n) => n.name)).not.toContain('audit');
});
});