diff --git a/test/router-parity/frozen/golden-explain.json b/test/router-parity/frozen/golden-explain.json new file mode 100644 index 0000000..44e22f0 --- /dev/null +++ b/test/router-parity/frozen/golden-explain.json @@ -0,0 +1,138 @@ +{ + "revision": "ed47836c9820c47bf4ed5e902d91e019abdb110a", + "data": [ + { + "pattern": "/api/items/:id(\\d+)", + "explain": { + "pattern": "/api/items/:id(\\d+)", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "get", + "kind": "handler", + "origin": "module:IntrospectionModule", + "needs": [], + "provides": [] + } + ] + } + }, + { + "pattern": "/api/items", + "error": { + "name": "Error", + "message": "no route: /api/items" + } + }, + { + "pattern": "/api/live/:room", + "explain": { + "pattern": "/api/live/:room", + "method": "GET", + "transport": "ws", + "chain": [ + { + "name": "db", + "kind": "provider", + "origin": "module:IntrospectionModule", + "needs": [], + "provides": [ + "db" + ] + }, + { + "name": "user", + "kind": "step", + "origin": "module:IntrospectionModule", + "needs": [ + "db" + ], + "provides": [ + "user" + ] + }, + { + "name": "live", + "kind": "handler", + "origin": "module:IntrospectionModule", + "needs": [ + "user" + ], + "provides": [] + } + ] + } + }, + { + "pattern": "/api/items/:id/files/:path*", + "explain": { + "pattern": "/api/items/:id/files/:path*", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "files", + "kind": "handler", + "origin": "module:IntrospectionModule", + "needs": [], + "provides": [] + } + ] + } + }, + { + "pattern": "/api/missing", + "error": { + "name": "Error", + "message": "no route: /api/missing" + } + }, + { + "pattern": "/api/items/42", + "error": { + "name": "Error", + "message": "no route: /api/items/42" + } + }, + { + "pattern": "/api/items/", + "explain": { + "pattern": "/api/items/", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "db", + "kind": "provider", + "origin": "module:IntrospectionModule", + "needs": [], + "provides": [ + "db" + ] + }, + { + "name": "user", + "kind": "step", + "origin": "module:IntrospectionModule", + "needs": [ + "db" + ], + "provides": [ + "user" + ] + }, + { + "name": "list", + "kind": "handler", + "origin": "module:IntrospectionModule", + "needs": [ + "user" + ], + "provides": [] + } + ] + } + } + ] +} diff --git a/test/router-parity/frozen/golden-graph.json b/test/router-parity/frozen/golden-graph.json new file mode 100644 index 0000000..213b341 --- /dev/null +++ b/test/router-parity/frozen/golden-graph.json @@ -0,0 +1,168 @@ +{ + "revision": "ed47836c9820c47bf4ed5e902d91e019abdb110a", + "data": { + "nodes": [ + { + "name": "db", + "kind": "provider", + "origin": "module:IntrospectionModule", + "needs": [], + "provides": [ + "db" + ] + }, + { + "name": "logger", + "kind": "provider", + "origin": "builtin", + "needs": [], + "provides": [ + "logger" + ] + }, + { + "name": "rooms", + "kind": "provider", + "origin": "builtin", + "needs": [], + "provides": [ + "rooms" + ] + }, + { + "name": "events", + "kind": "provider", + "origin": "builtin", + "needs": [], + "provides": [ + "events" + ] + }, + { + "name": "user", + "kind": "step", + "origin": "module:IntrospectionModule", + "needs": [ + "db" + ], + "provides": [ + "user" + ] + }, + { + "name": "audit", + "kind": "step", + "origin": "module:IntrospectionModule", + "needs": [], + "provides": [ + "audit" + ] + } + ], + "routes": [ + { + "pattern": "/api/items/:id/files/:path*", + "method": "GET", + "transport": "buffer", + "chain": [ + "files" + ] + }, + { + "pattern": "/api/items/", + "method": "GET", + "transport": "buffer", + "chain": [ + "db", + "user", + "list" + ] + }, + { + "pattern": "/api/items/", + "method": "POST", + "transport": "buffer", + "chain": [ + "db", + "user", + "create" + ] + }, + { + "pattern": "/api/items/:id(\\d+)", + "method": "GET", + "transport": "buffer", + "chain": [ + "get" + ] + }, + { + "pattern": "/api/items/:id(\\d+)", + "method": "HEAD", + "transport": "buffer", + "chain": [ + "head" + ] + }, + { + "pattern": "/api/items/:id(\\d+)", + "method": "OPTIONS", + "transport": "buffer", + "chain": [ + "options" + ] + }, + { + "pattern": "/api/items/:id(\\d+)", + "method": "DELETE", + "transport": "buffer", + "chain": [ + "audit", + "remove" + ] + }, + { + "pattern": "/api/items/:slug([a-z0-9-]+)", + "method": "PUT", + "transport": "buffer", + "chain": [ + "upsert" + ] + }, + { + "pattern": "/api/items/search", + "method": "GET", + "transport": "buffer", + "chain": [ + "search" + ] + }, + { + "pattern": "/api/events", + "method": "GET", + "transport": "sse", + "chain": [ + "events" + ] + }, + { + "pattern": "/api/feed", + "method": "GET", + "transport": "negotiate", + "chain": [ + "feed" + ] + }, + { + "pattern": "/api/live/:room", + "method": "GET", + "transport": "ws", + "chain": [ + "db", + "user", + "live" + ] + } + ] + } +} diff --git a/test/router-parity/frozen/golden-mesh.json b/test/router-parity/frozen/golden-mesh.json new file mode 100644 index 0000000..b47271b --- /dev/null +++ b/test/router-parity/frozen/golden-mesh.json @@ -0,0 +1,233 @@ +{ + "revision": "ed47836c9820c47bf4ed5e902d91e019abdb110a", + "data": { + "before": { + "graph": { + "error": { + "name": "Error", + "message": "graph() unavailable until a mesh app has booted: a mesh graph is not knowable until its teapots are asked. Call `await app.ready()` first — it is a no-op on a non-mesh app" + } + }, + "explain": { + "error": { + "name": "Error", + "message": "explain() unavailable until a mesh app has booted: a mesh graph is not knowable until its teapots are asked. Call `await app.ready()` first — it is a no-op on a non-mesh app" + } + }, + "openapi": { + "value": { + "openapi": "3.1.0", + "info": { + "title": "Parity", + "version": "1" + }, + "paths": { + "/api/local/who": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/api/local/{id}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + } + } + } + } + }, + "after": { + "graph": { + "nodes": [ + { + "name": "logger", + "kind": "provider", + "origin": "builtin", + "needs": [], + "provides": [ + "logger" + ] + }, + { + "name": "rooms", + "kind": "provider", + "origin": "builtin", + "needs": [], + "provides": [ + "rooms" + ] + }, + { + "name": "events", + "kind": "provider", + "origin": "builtin", + "needs": [], + "provides": [ + "events" + ] + }, + { + "name": "config", + "kind": "step", + "origin": "mesh:", + "needs": [], + "provides": [ + "config" + ] + } + ], + "routes": [ + { + "pattern": "/api/local/who", + "method": "GET", + "transport": "buffer", + "chain": [ + "config", + "who" + ] + }, + { + "pattern": "/api/local/:id", + "method": "GET", + "transport": "buffer", + "chain": [ + "byId" + ] + } + ] + }, + "explain": [ + { + "pattern": "/api/missing", + "error": { + "name": "Error", + "message": "no route: /api/missing" + } + } + ], + "explainLocal": { + "pattern": "/api/local/who", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "config", + "kind": "step", + "origin": "mesh:", + "needs": [], + "provides": [ + "config" + ] + }, + { + "name": "who", + "kind": "handler", + "origin": "module:TeacupModule", + "needs": [ + "config" + ], + "provides": [] + } + ] + }, + "explainRemote": { + "error": { + "name": "Error", + "message": "no route: /api/remote/ping" + } + }, + "openapi": { + "openapi": "3.1.0", + "info": { + "title": "Parity", + "version": "1" + }, + "paths": { + "/api/local/who": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/api/local/{id}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + } + } + } + }, + "lookup": { + "path": "/api/remote/ping", + "status": 200, + "body": "{\"from\":\"teapot\"}" + } + } +} diff --git a/test/router-parity/frozen/golden-openapi.json b/test/router-parity/frozen/golden-openapi.json new file mode 100644 index 0000000..4d471df --- /dev/null +++ b/test/router-parity/frozen/golden-openapi.json @@ -0,0 +1,285 @@ +{ + "revision": "ed47836c9820c47bf4ed5e902d91e019abdb110a", + "data": { + "openapi": "3.1.0", + "info": { + "title": "Parity", + "version": "1" + }, + "paths": { + "/api/items/{id}/files/{path}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "path", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/api/items": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + }, + "post": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "requestBody": { + "content": { + "application/json": { + "schema": {} + } + } + } + } + }, + "/api/items/{id}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "pattern": "\\d+" + } + }, + { + "name": "expand", + "in": "query", + "required": false, + "schema": { + "type": "string" + } + } + ] + }, + "head": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "pattern": "\\d+" + } + } + ] + }, + "options": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "pattern": "\\d+" + } + } + ] + }, + "delete": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "pattern": "\\d+" + } + } + ] + } + }, + "/api/items/{slug}": { + "put": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "422": { + "description": "Validation failed" + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "slug", + "in": "path", + "required": true, + "schema": { + "type": "string", + "pattern": "[a-z0-9-]+" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": {} + } + } + } + } + }, + "/api/items/search": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/api/events": { + "get": { + "responses": { + "200": { + "description": "Event stream", + "content": { + "text/event-stream": {} + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/api/feed": { + "get": { + "responses": { + "200": { + "description": "Stream", + "content": { + "application/x-ndjson": {} + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } +} diff --git a/test/router-parity/introspection.test.ts b/test/router-parity/introspection.test.ts new file mode 100644 index 0000000..5102d7c --- /dev/null +++ b/test/router-parity/introspection.test.ts @@ -0,0 +1,227 @@ +/** + * Introspection goldens (ROUTER-CORPUS §3, CARD-09). `graph()`, `explain()` and `openapi()` read + * `routePlans` in declaration order and must never be derived from a lookup index. Compared as + * parsed structures with array order and key order held — see `expectFrozenEqual`. + */ +import 'reflect-metadata'; +import type { AddressInfo } from 'net'; +import { describe, it } from 'vitest'; +import { + createApp, + Provider, + Step, + Route, + Get, + Head, + Post, + Put, + Delete, + Options, + Sse, + Stream, + Ws, + Module, + needs, + param, + query, + body, +} from '../../src/index'; +import type { Logger } from '../../src/logger'; +import { errorOutcome } from './matcher'; +import { frozen, expectFrozenEqual } from './frozen'; + +const slugSchema = { + '~standard': { version: 1 as const, vendor: 'parity', validate: (v: unknown) => ({ value: v }) }, +}; + +@Provider({ provides: 'db' }) +class Db { + provide() { + return { db: {} }; + } +} + +@Step({ provides: 'user', needs: ['db'] }) +class Auth { + run() { + return { user: {} }; + } +} + +@Step({ provides: 'audit', needs: [] }) +class Audit { + run() { + return { audit: {} }; + } +} + +// Declaration order is deliberately not specificity order, and paths repeat across methods, so +// any traversal-ordered output would differ from this one. +@Route('/items') +class Items { + @Get('/:id/files/:path*') files(@param('path') _p: string) { + return {}; + } + @Get('/') list(@needs('user') _u: unknown) { + return []; + } + @Post('/') create(@body() _b: unknown, @needs('user') _u: unknown) { + return {}; + } + @Get('/:id(\\d+)') get(@param('id') _id: string, @query('expand') _e: string) { + return {}; + } + @Head('/:id(\\d+)') head() { + return {}; + } + @Options('/:id(\\d+)') options() { + return {}; + } + @Delete('/:id(\\d+)') remove(@needs('audit') _a: unknown) { + return {}; + } + @Put('/:slug([a-z0-9-]+)') upsert(@body(slugSchema) _b: unknown) { + return {}; + } + @Get('/search') search() { + return []; + } +} + +@Route('/') +class Streams { + @Sse('/events') events() { + return (async function* () {})(); + } + @Stream('/feed') feed() { + return (async function* () {})(); + } + @Ws('/live/:room') live(@needs('user') _u: unknown) { + return (async function* () {})(); + } +} + +@Module({ mountpoint: '/api', providers: [Db], steps: [Auth, Audit], controllers: [Items, Streams] }) +class IntrospectionModule {} + +const EXPLAINED = ['/api/items/:id(\\d+)', '/api/items', '/api/live/:room', '/api/items/:id/files/:path*']; +const NOT_DECLARED = ['/api/missing', '/api/items/42', '/api/items/']; + +function explainAll(explain: (pattern: string) => unknown) { + return [...EXPLAINED, ...NOT_DECLARED].map((pattern) => { + try { + return { pattern, explain: explain(pattern) }; + } catch (error) { + return { pattern, error: errorOutcome(error) }; + } + }); +} + +function capture() { + const app = createApp({ modules: [IntrospectionModule] }); + return { + graph: app.graph(), + explain: explainAll(app.explain), + openapi: app.openapi({ title: 'Parity', version: '1' }), + }; +} + +// ── mesh ──────────────────────────────────────────────────────────────────────────────────────── + +const SECRET = 'parity-secret'; + +@Step({ provides: 'config', needs: [], export: true }) +class Config { + run() { + return { config: {} }; + } +} + +@Route('/remote') +class RemoteCtl { + @Get('/ping', { export: true }) ping() { + return { from: 'teapot' }; + } +} +@Module({ mountpoint: '/api', steps: [Config], controllers: [RemoteCtl] }) +class TeapotModule {} + +@Route('/local') +class LocalCtl { + @Get('/who') who(@needs('config') _c: unknown) { + return { from: 'teacup' }; + } + @Get('/:id') byId() { + return {}; + } +} +@Module({ mountpoint: '/api', controllers: [LocalCtl] }) +class TeacupModule {} + +const quiet: Logger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} }; + +function attempt(fn: () => T): { value: T } | { error: ReturnType } { + try { + return { value: fn() }; + } catch (error) { + return { error: errorOutcome(error) }; + } +} + +async function captureMesh() { + const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } }); + const server = await teapot.listen(0); + const url = `ws://127.0.0.1:${(server.address() as AddressInfo).port}/__mesh__/control`; + const anonymize = (value: unknown) => JSON.parse(JSON.stringify(value).split(url).join('')); + const teacup = createApp({ + modules: [TeacupModule], + experimental: true, + logger: quiet, + mesh: { teapots: [{ url, secret: SECRET }] }, + }); + + try { + const before = { + graph: attempt(() => teacup.graph()), + explain: attempt(() => teacup.explain('/api/local/who')), + openapi: attempt(() => teacup.openapi({ title: 'Parity', version: '1' })), + }; + await teacup.ready(); + const after = { + graph: teacup.graph(), + explain: explainAll(teacup.explain).filter(({ pattern }) => pattern === '/api/missing'), + explainLocal: teacup.explain('/api/local/who'), + explainRemote: attempt(() => teacup.explain('/api/remote/ping')), + openapi: teacup.openapi({ title: 'Parity', version: '1' }), + }; + // Remote routes are reachable for lookup even though they are absent from routePlans. + const res = await teacup.fetch(new Request('http://parity.test/api/remote/ping')); + const lookup = { path: '/api/remote/ping', status: res.status, body: await res.text() }; + return anonymize({ before, after, lookup }); + } finally { + await teacup.close(); + await teapot.close(); + } +} + +describe('router parity: introspection goldens', () => { + it('graph() equals the golden, routes in declaration order', async () => { + const expected = await frozen('golden-graph', () => capture().graph); + expectFrozenEqual(capture().graph, expected, 'graph()'); + }); + + it('explain() for declared patterns and the `no route:` error equals the golden', async () => { + const expected = await frozen('golden-explain', () => capture().explain); + expectFrozenEqual(capture().explain, expected, 'explain()'); + }); + + it("openapi({ title: 'Parity', version: '1' }) equals the golden", async () => { + const expected = await frozen('golden-openapi', () => capture().openapi); + expectFrozenEqual(capture().openapi, expected, 'openapi()'); + }); + + it('a mesh teacup before and after ready() equals the golden; remote routes stay out of routePlans', async () => { + const expected = await frozen('golden-mesh', captureMesh); + expectFrozenEqual(await captureMesh(), expected, 'mesh introspection'); + }, 30_000); +});