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('/')