feat(app): warn at boot when a route's dependency chain is unusually deep

A route whose need-closure resolves to more than 20 steps is almost always a modeling smell -- a step doing too much, or work that belongs in a provider. Per-request cost is linear, so this is a design nudge, not a perf guard (and a warning, not an error).

Tunable and silenceable via createApp({ warnGraphDepth }): a number sets the threshold, false quiets it. Default 20.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-17 16:25:31 -05:00
1 parent 4081106813
commit 642ef109b2
2 files changed
+77 -2

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
import 'reflect-metadata';
import { describe, expect, it, vi } from 'vitest';
import { createApp, Step, Route, Get, Module, needs } from '../src/index';
// Builds a module with an n-deep chain: s1..sn (each needs the previous) + a route that @needs the last.
function deepModule(n: number) {
const steps = Array.from({ length: n }, (_, i) => {
const k = i + 1;
@Step({ provides: `s${k}`, needs: k > 1 ? [`s${k - 1}`] : [] })
class S { run() { return { [`s${k}`]: k }; } }
return S;
});
@Route('/')
class C { @Get('/deep') deep(@needs(`s${n}`) s: number) { return { s }; } }
@Module({ mountpoint: '/', steps, controllers: [C] })
class M {}
return M;
}
describe('deep-graph warning', () => {
it('warns at boot when a route resolves to more than 20 steps', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
createApp({ modules: [deepModule(25)] });
expect(warn).toHaveBeenCalledWith(expect.stringContaining('25 steps'));
warn.mockRestore();
});
it('stays quiet under the threshold', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
createApp({ modules: [deepModule(10)] });
expect(warn).not.toHaveBeenCalled();
warn.mockRestore();
});
it('warnGraphDepth: false silences it', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
createApp({ modules: [deepModule(25)], warnGraphDepth: false });
expect(warn).not.toHaveBeenCalled();
warn.mockRestore();
});
it('warnGraphDepth tunes the threshold', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
createApp({ modules: [deepModule(10)], warnGraphDepth: 5 });
expect(warn).toHaveBeenCalledWith(expect.stringContaining('10 steps'));
warn.mockRestore();
});
});