Files
core/test/mesh/fetch-boot.test.ts
T
chrnx db9a7712af feat(mesh): boot the mesh graph from the app boot gate, not listen()
Only listen() spliced remote scopes and finalized the graph, and listen() builds
a Node http.Server. Deno/Bun/edge serve through app.fetch and can never call it,
so mesh was Node-only by construction: fetch() threw "unavailable before
listen()" forever on those runtimes, telling operators to call something they
had no way to call. buildAppFetch also hardcoded remoteRoutes to [], so even past
the guard it would not have served a remote route.

Move splice+finalize into the memoized boot gate that fetch, upgrade and listen
already share, ahead of the providers — remote nodes must join before the topo
sort. Await it before building the route table rather than after, since for a
mesh app the table is not knowable until the splice has run. listen() keeps
working by awaiting the same gate; the memoization means a fetch followed by a
listen boots once, and concurrent first requests splice once.

The fetch/upgrade guards are gone rather than reworded: they are now unreachable.

inspect()/graph()/explain() keep theirs — they are synchronous and cannot await
network I/O — but their message no longer claims listen() is the only way in.

test/fetch.test.ts asserted the old contract (fetch throws until listen), i.e.
the trap itself; it now pins that a mesh app serves without listen() and that
listen() afterwards does not re-run provider factories.

Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 5

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-07-15 15:04:49 -05:00

124 lines
4.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { createApp, Provider, Step, Route, Get, Module, needs } from '../../src/index';
const SECRET = 's3cr3t';
@Provider({ provides: 'config', export: true })
class Config {
provide() {
return { config: { region: 'mx' } };
}
}
@Step({ provides: 'auth', needs: [], export: true })
class Auth {
run(ctx: any) {
return { auth: { token: ctx.headers?.['x-token'] ?? 'anon' } };
}
}
@Module({ mountpoint: '/api', providers: [Config], steps: [Auth] })
class TeapotModule {}
@Route('/local')
class LocalCtl {
@Get('/who')
who(@needs('config') config: any, @needs('auth') auth: any) {
return { config, auth };
}
}
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
class TeacupModule {}
const teapotUrl = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
/**
* A mesh teacup driven purely through `app.fetch` — the entry point Deno/Bun/edge use.
* `listen()` is never called on the teacup, because on those runtimes it cannot be:
* it builds a Node http.Server.
*/
describe('mesh over app.fetch (no listen)', () => {
it('boots the mesh graph on the first request and resolves remote scopes', 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 }] },
});
const res = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'abc' } }));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ config: { region: 'mx' }, auth: { token: 'abc' } });
await teacup.close();
tServer.close();
});
it('boots exactly once across concurrent first requests', 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);
let connects = 0;
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url, secret: SECRET }] },
});
teacup.bus.on('mesh:connect', () => {
connects += 1;
});
const hit = () => teacup.fetch(new Request('http://x/api/local/who'));
const all = await Promise.all([hit(), hit(), hit()]);
expect(all.map((r) => r.status)).toEqual([200, 200, 200]);
expect(connects).toBe(1); // the boot gate is memoized: three racing requests splice once
await teacup.close();
tServer.close();
});
it('serves the remote scope in inspect() once a request has booted the graph', 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 }] },
});
await teacup.fetch(new Request('http://x/api/local/who'));
const chain = teacup.inspect('/api/local/who').map((line) => `${line.kind}:${line.name}`);
// the remote provider and step spliced into the local route's chain
expect(chain).toContain('provider:config');
expect(chain).toContain('step:auth');
expect(teacup.graph().nodes.some((n) => n.name === 'config')).toBe(true);
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);
const url = teapotUrl((tServer.address() as any).port);
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url, secret: 'WRONG' }], timeoutMs: 800 },
});
await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow();
await teacup.close();
tServer.close();
});
});