feat(app): add app.ready() so introspection works on any app
inspect()/graph()/explain() are synchronous, but a mesh graph is not knowable
until its teapots have been asked, so on a mesh app they threw and the caller had
no way to fix it. Code holding an `App` could not tell which kind it had — the
Liskov complaint from the audit, and the last of it: fetch/upgrade stopped
throwing when the boot moved into the gate.
app.ready() resolves the graph and returns. On a mesh app it connects the
teapots; on every other app it is a no-op. So one shape works for both:
await app.ready();
app.graph();
which is what actually restores substitutability — a uniform protocol, not a type
that describes the asymmetry.
Deliberately not a full boot: it does not run provider factories. Resolving the
graph and being ready to serve are different things, and a devtool drawing a
diagram should not open your database connections. Serving boots them too and
shares the same memoized step, so ready() then fetch() resolves the graph once.
The guards stay — sync code cannot await network I/O — but the message names the
fix instead of describing the trap.
createApp hit 26 on the complexity gate with ready() added. Both boot stages had
their own copy of the same memoisation, so it collapses into one `once` helper
used by both, which is a smaller createApp than before this change.
Refs .specs/2026-07-15-green-tea-solid-audit (App/Mesh LSP)
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
7 files changed
+147
-11
No files matched your search
@@ -166,6 +166,81 @@ describe('mesh over app.fetch (no listen)', () => {
|
||||
tServer.close();
|
||||
});
|
||||
|
||||
it('resolves the graph via ready() without serving a request', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = await teapot.listen(0);
|
||||
const url = teapotUrl((tServer.address() as any).port);
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
mesh: { teapots: [{ url, secret: SECRET }] },
|
||||
});
|
||||
|
||||
// a mesh graph is not knowable without asking the teapots, so it throws until resolved
|
||||
expect(() => teacup.graph()).toThrow(/until a mesh app has booted/);
|
||||
|
||||
await teacup.ready();
|
||||
|
||||
expect(teacup.graph().nodes.some((n) => n.name === 'config')).toBe(true);
|
||||
expect(teacup.inspect('/api/local/who').map((l) => `${l.kind}:${l.name}`)).toContain('provider:config');
|
||||
expect(teacup.explain('/api/local/who')).toBeTruthy();
|
||||
|
||||
await teacup.close();
|
||||
tServer.close();
|
||||
});
|
||||
|
||||
it('ready() resolves the graph but does NOT boot providers', async () => {
|
||||
// the point of ready() over a full boot: a devtool asking for the graph must not run
|
||||
// provider factories and open their connections as a side effect
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = await teapot.listen(0);
|
||||
const url = teapotUrl((tServer.address() as any).port);
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
mesh: { teapots: [{ url, secret: SECRET }] },
|
||||
});
|
||||
const booted: string[] = [];
|
||||
teacup.bus.on('boot:provider:start', (p) => booted.push(p.name));
|
||||
|
||||
await teacup.ready();
|
||||
expect(teacup.graph().nodes.length).toBeGreaterThan(0); // graph is resolved
|
||||
expect(booted).toEqual([]); // and nothing was booted to get it
|
||||
|
||||
await teacup.fetch(new Request('http://x/api/local/who'));
|
||||
expect(booted.length).toBeGreaterThan(0); // serving does boot them
|
||||
|
||||
await teacup.close();
|
||||
tServer.close();
|
||||
});
|
||||
|
||||
it('ready() is memoized and shared with the boot: a later fetch does not re-splice', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = await teapot.listen(0);
|
||||
const url = teapotUrl((tServer.address() as any).port);
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
mesh: { teapots: [{ url, secret: SECRET }] },
|
||||
});
|
||||
let connects = 0;
|
||||
teacup.bus.on('mesh:connect', () => {
|
||||
connects += 1;
|
||||
});
|
||||
|
||||
await Promise.all([teacup.ready(), teacup.ready()]);
|
||||
await teacup.fetch(new Request('http://x/api/local/who'));
|
||||
await teacup.ready();
|
||||
|
||||
expect(connects).toBe(1);
|
||||
|
||||
await teacup.close();
|
||||
tServer.close();
|
||||
});
|
||||
|
||||
it('surfaces a failed mesh boot as a request error, not a silent success', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = await teapot.listen(0);
|
||||
|
||||
Reference in new issue
Block a user