diff --git a/src/app/index.ts b/src/app/index.ts index f89abe5..f6c34e5 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -466,6 +466,7 @@ async function spliceRemoteScopes( ): Promise<{ remoteRoutes: RouteDef[]; meshLinks: Link[] }> { const remoteRoutes: RouteDef[] = []; const meshLinks: Link[] = []; + const routeOwners = new Map(); // "METHOD /pattern" -> the teapot that exported it try { for (const teapot of mesh.teapots ?? []) { @@ -486,6 +487,22 @@ async function spliceRemoteScopes( } for (const route of routes) { + const key = `${route.method} ${route.pattern}`; + const owner = routeOwners.get(key); + + // Fail the boot rather than pick one. Route matching keeps the first of two identical + // patterns, so the loser would be silently dead — and callers could come to depend on + // "first teapot wins", which would make adding balancing later a breaking change. + // Scope tokens already fail this way in setRunner; routes now match. + if (owner) { + throw new Error( + `mesh: route '${key}' is exported by more than one teapot (${owner} and ${teapot.url}) — ` + + 'load balancing across teapots is not implemented yet, so green-tea will not choose one for you. ' + + 'Export it from a single teapot.', + ); + } + + routeOwners.set(key, teapot.url); remoteRoutes.push({ method: route.method, pattern: route.pattern, diff --git a/test/mesh/integration.test.ts b/test/mesh/integration.test.ts index 98bbf37..3108417 100644 --- a/test/mesh/integration.test.ts +++ b/test/mesh/integration.test.ts @@ -23,6 +23,28 @@ class LocalCtl { @Module({ mountpoint: '/api', controllers: [LocalCtl] }) class TeacupModule {} +// Two teapots exporting the SAME route and no scopes — a scope clash would throw first +// (setRunner enforces unique tokens), which would hide the route clash under test. +@Route('/dup') +class DupCtl { + @Get('/ping', { export: true }) + ping() { + return { pong: true }; + } +} +@Module({ mountpoint: '/api', controllers: [DupCtl] }) +class DupTeapotModule {} + +@Route('/c') +class PlainCtl { + @Get('/ok') + ok() { + return { ok: true }; + } +} +@Module({ mountpoint: '/', controllers: [PlainCtl] }) +class PlainTeacupModule {} + describe('mesh skeleton integration', () => { it('teacup resolves remote app-scope + request-scope into a local handler', async () => { const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } }); @@ -52,6 +74,51 @@ describe('mesh skeleton integration', () => { s.close(); }); + it('refuses to boot when two teapots export the same route, naming both', async () => { + // No load balancing exists yet, so picking one silently would be a wrong answer that + // callers could come to depend on — and it would make adding LB later a breaking change. + const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } }); + const b = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } }); + const aServer = await a.listen(0); + const bServer = await b.listen(0); + const url = (s: any) => `ws://127.0.0.1:${(s.address() as any).port}/__mesh__/control`; + + const teacup = createApp({ + modules: [PlainTeacupModule], + experimental: true, + mesh: { + teapots: [ + { url: url(aServer), secret: SECRET }, + { url: url(bServer), secret: SECRET }, + ], + }, + }); + + await expect(teacup.fetch(new Request('http://x/c/ok'))).rejects.toThrow(/GET \/api\/dup\/ping/); + await expect(teacup.fetch(new Request('http://x/c/ok'))).rejects.toThrow(/load balancing/i); + + aServer.close(); + bServer.close(); + }); + + it('accepts the same route from a single teapot (regression: no false positive)', async () => { + const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } }); + const aServer = await a.listen(0); + const url = `ws://127.0.0.1:${(aServer.address() as any).port}/__mesh__/control`; + + const teacup = createApp({ + modules: [PlainTeacupModule], + experimental: true, + mesh: { teapots: [{ url, secret: SECRET }] }, + }); + + const res = await teacup.fetch(new Request('http://x/api/dup/ping')); + expect(await res.json()).toEqual({ pong: true }); + + await teacup.close(); + aServer.close(); + }); + it('rejects a bad secret (teacup boot fails)', async () => { const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } }); const tServer = await teapot.listen(0); const tPort = (tServer.address() as any).port;