From fcee6892bfebe8bf523c4754097bef8a1e496e1b Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Mon, 12 Oct 2026 13:59:56 -0500 Subject: [PATCH] test(router): prove introspection parity after the radix index (CARD-09, #109) The frozen CARD-08 goldens already hold graph(), explain(), openapi() and the mesh teacup. This adds what they do not cover, with expectations captured from the pre-radix code at ed47836 in a detached checkout: - explain()/inspect() on a pattern shared by several methods with a non-GET declared first, and a concrete path that is not a declaration; - /__graph__ and /__openapi__ on app.fetch, a Node listener and a mesh teacup, and their agreement with the direct API; - a structural check that graph/explain/inspect/openapi never call the router lookup, return the same output before and after the index is built, and keep declaration order where the index ranks differently. Signed-off-by: Diego Resendez --- .../router-introspection-ed47836.json | 1091 +++++++++++++++++ test/router-introspection-parity.test.ts | 384 ++++++ 2 files changed, 1475 insertions(+) create mode 100644 test/fixtures/router-introspection-ed47836.json create mode 100644 test/router-introspection-parity.test.ts diff --git a/test/fixtures/router-introspection-ed47836.json b/test/fixtures/router-introspection-ed47836.json new file mode 100644 index 0000000..27bc439 --- /dev/null +++ b/test/fixtures/router-introspection-ed47836.json @@ -0,0 +1,1091 @@ +{ + "revision": "ed47836c9820c47bf4ed5e902d91e019abdb110a", + "data": { + "local": { + "direct": { + "graph": { + "nodes": [ + { + "name": "store", + "kind": "provider", + "origin": "module:DocsModule", + "needs": [], + "provides": ["store"] + }, + { + "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": "caller", + "kind": "step", + "origin": "module:DocsModule", + "needs": ["store"], + "provides": ["caller"] + } + ], + "routes": [ + { + "pattern": "/v1/docs/:id", + "method": "DELETE", + "transport": "buffer", + "chain": ["remove"] + }, + { + "pattern": "/v1/docs/:rest*", + "method": "GET", + "transport": "buffer", + "chain": ["rest"] + }, + { + "pattern": "/v1/docs/:id", + "method": "GET", + "transport": "buffer", + "chain": ["store", "caller", "get"] + }, + { + "pattern": "/v1/docs/latest", + "method": "GET", + "transport": "buffer", + "chain": ["latest"] + }, + { + "pattern": "/v1/docs/events", + "method": "GET", + "transport": "sse", + "chain": ["events"] + }, + { + "pattern": "/v1/docs/:id/live", + "method": "GET", + "transport": "ws", + "chain": ["store", "caller", "live"] + } + ] + }, + "explain": [ + { + "pattern": "/v1/docs/:id", + "value": { + "pattern": "/v1/docs/:id", + "method": "DELETE", + "transport": "buffer", + "chain": [ + { + "name": "remove", + "kind": "handler", + "origin": "module:DocsModule", + "needs": [], + "provides": [] + } + ] + } + }, + { + "pattern": "/v1/docs/latest", + "value": { + "pattern": "/v1/docs/latest", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "latest", + "kind": "handler", + "origin": "module:DocsModule", + "needs": [], + "provides": [] + } + ] + } + }, + { + "pattern": "/v1/docs/:rest*", + "value": { + "pattern": "/v1/docs/:rest*", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "rest", + "kind": "handler", + "origin": "module:DocsModule", + "needs": [], + "provides": [] + } + ] + } + }, + { + "pattern": "/v1/docs/:id/live", + "value": { + "pattern": "/v1/docs/:id/live", + "method": "GET", + "transport": "ws", + "chain": [ + { + "name": "store", + "kind": "provider", + "origin": "module:DocsModule", + "needs": [], + "provides": ["store"] + }, + { + "name": "caller", + "kind": "step", + "origin": "module:DocsModule", + "needs": ["store"], + "provides": ["caller"] + }, + { + "name": "live", + "kind": "handler", + "origin": "module:DocsModule", + "needs": ["caller"], + "provides": [] + } + ] + } + }, + { + "pattern": "/v1/docs/7", + "error": "no route: /v1/docs/7" + }, + { + "pattern": "/v1/docs/latest/", + "error": "no route: /v1/docs/latest/" + }, + { + "pattern": "/v1/missing", + "error": "no route: /v1/missing" + } + ], + "inspect": [ + { + "pattern": "/v1/docs/:id", + "value": [ + { + "name": "remove", + "kind": "handler", + "origin": "module:DocsModule" + } + ] + }, + { + "pattern": "/v1/docs/latest", + "value": [ + { + "name": "latest", + "kind": "handler", + "origin": "module:DocsModule" + } + ] + }, + { + "pattern": "/v1/docs/:rest*", + "value": [ + { + "name": "rest", + "kind": "handler", + "origin": "module:DocsModule" + } + ] + }, + { + "pattern": "/v1/docs/:id/live", + "value": [ + { + "name": "store", + "kind": "provider", + "origin": "module:DocsModule" + }, + { + "name": "caller", + "kind": "step", + "origin": "module:DocsModule" + }, + { + "name": "live", + "kind": "handler", + "origin": "module:DocsModule" + } + ] + }, + { + "pattern": "/v1/docs/7", + "error": "no route: /v1/docs/7" + }, + { + "pattern": "/v1/docs/latest/", + "error": "no route: /v1/docs/latest/" + }, + { + "pattern": "/v1/missing", + "error": "no route: /v1/missing" + } + ], + "mermaid": "flowchart LR\n n_store([\"store\"])\n n_logger([\"logger\"])\n n_rooms([\"rooms\"])\n n_events([\"events\"])\n n_caller[\"caller\"]\n n_store --> n_caller\n r__v1_docs__id{{\"DELETE /v1/docs/:id\"}}\n r__v1_docs__rest_{{\"GET /v1/docs/:rest*\"}}\n r__v1_docs__id{{\"GET /v1/docs/:id\"}}\n n_store --> r__v1_docs__id\n n_caller --> r__v1_docs__id\n r__v1_docs_latest{{\"GET /v1/docs/latest\"}}\n r__v1_docs_events{{\"GET /v1/docs/events\"}}\n n_events --> r__v1_docs_events\n r__v1_docs__id_live{{\"GET /v1/docs/:id/live\"}}\n n_store --> r__v1_docs__id_live\n n_caller --> r__v1_docs__id_live", + "openapi": { + "openapi": "3.1.0", + "info": { + "title": "Introspection", + "version": "1" + }, + "paths": { + "/v1/docs/{id}": { + "delete": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + }, + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/{rest}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "rest", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/latest": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/v1/docs/events": { + "get": { + "responses": { + "200": { + "description": "Event stream", + "content": { + "text/event-stream": {} + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + }, + "openapiNoInfo": { + "openapi": "3.1.0", + "info": { + "title": "green-tea API", + "version": "0.0.0" + }, + "paths": { + "/v1/docs/{id}": { + "delete": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + }, + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/{rest}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "rest", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/latest": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/v1/docs/events": { + "get": { + "responses": { + "200": { + "description": "Event stream", + "content": { + "text/event-stream": {} + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } + }, + "fetchDev": { + "graphHtml": { + "status": 200, + "type": "text/html", + "sha256": "746f6094eaa9c2850c614190a116f34e4689fd4b07aba2dcd40545f6cdd763b7" + }, + "graphText": { + "status": 200, + "type": "text/plain", + "body": "flowchart LR\n n_store([\"store\"])\n n_logger([\"logger\"])\n n_rooms([\"rooms\"])\n n_events([\"events\"])\n n_caller[\"caller\"]\n n_store --> n_caller\n r__v1_docs__id{{\"DELETE /v1/docs/:id\"}}\n r__v1_docs__rest_{{\"GET /v1/docs/:rest*\"}}\n r__v1_docs__id{{\"GET /v1/docs/:id\"}}\n n_store --> r__v1_docs__id\n n_caller --> r__v1_docs__id\n r__v1_docs_latest{{\"GET /v1/docs/latest\"}}\n r__v1_docs_events{{\"GET /v1/docs/events\"}}\n n_events --> r__v1_docs_events\n r__v1_docs__id_live{{\"GET /v1/docs/:id/live\"}}\n n_store --> r__v1_docs__id_live\n n_caller --> r__v1_docs__id_live" + }, + "openapi": { + "status": 200, + "type": "application/json", + "body": { + "openapi": "3.1.0", + "info": { + "title": "green-tea API", + "version": "0.0.0" + }, + "paths": { + "/v1/docs/{id}": { + "delete": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + }, + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/{rest}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "rest", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/latest": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/v1/docs/events": { + "get": { + "responses": { + "200": { + "description": "Event stream", + "content": { + "text/event-stream": {} + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } + } + }, + "nodeDev": { + "graphHtml": { + "status": 200, + "type": "text/html", + "sha256": "746f6094eaa9c2850c614190a116f34e4689fd4b07aba2dcd40545f6cdd763b7" + }, + "graphText": { + "status": 200, + "type": "text/plain", + "body": "flowchart LR\n n_store([\"store\"])\n n_logger([\"logger\"])\n n_rooms([\"rooms\"])\n n_events([\"events\"])\n n_caller[\"caller\"]\n n_store --> n_caller\n r__v1_docs__id{{\"DELETE /v1/docs/:id\"}}\n r__v1_docs__rest_{{\"GET /v1/docs/:rest*\"}}\n r__v1_docs__id{{\"GET /v1/docs/:id\"}}\n n_store --> r__v1_docs__id\n n_caller --> r__v1_docs__id\n r__v1_docs_latest{{\"GET /v1/docs/latest\"}}\n r__v1_docs_events{{\"GET /v1/docs/events\"}}\n n_events --> r__v1_docs_events\n r__v1_docs__id_live{{\"GET /v1/docs/:id/live\"}}\n n_store --> r__v1_docs__id_live\n n_caller --> r__v1_docs__id_live" + }, + "openapi": { + "status": 200, + "type": "application/json", + "body": { + "openapi": "3.1.0", + "info": { + "title": "green-tea API", + "version": "0.0.0" + }, + "paths": { + "/v1/docs/{id}": { + "delete": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + }, + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/{rest}": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + }, + "parameters": [ + { + "name": "rest", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/v1/docs/latest": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, + "/v1/docs/events": { + "get": { + "responses": { + "200": { + "description": "Event stream", + "content": { + "text/event-stream": {} + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } + } + }, + "lookups": [ + { + "method": "GET", + "path": "/v1/docs/latest", + "status": 200, + "allow": null, + "body": "{\"id\":\"latest\"}" + }, + { + "method": "GET", + "path": "/v1/docs/7", + "status": 200, + "allow": null, + "body": "{\"id\":\"get\",\"param\":\"7\"}" + }, + { + "method": "GET", + "path": "/v1/docs/a/b", + "status": 200, + "allow": null, + "body": "{\"id\":\"rest\",\"param\":\"a/b\"}" + }, + { + "method": "DELETE", + "path": "/v1/docs/7", + "status": 200, + "allow": null, + "body": "{\"id\":\"remove\",\"param\":\"7\"}" + }, + { + "method": "PUT", + "path": "/v1/docs/7", + "status": 405, + "allow": "GET, HEAD, DELETE, OPTIONS", + "body": "{\"error\":\"Method Not Allowed\"}" + } + ] + }, + "mesh": { + "before": { + "graph": { + "error": "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": "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" + }, + "inspect": { + "error": "inspect() 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": "green-tea API", + "version": "0.0.0" + }, + "paths": { + "/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" + } + } + ] + } + }, + "/api/local/who": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } + } + }, + "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/:id", + "method": "GET", + "transport": "buffer", + "chain": ["byId"] + }, + { + "pattern": "/api/local/who", + "method": "GET", + "transport": "buffer", + "chain": ["config", "who"] + } + ] + }, + "explain": [ + { + "pattern": "/api/local/who", + "value": { + "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": [] + } + ] + } + }, + { + "pattern": "/api/local/:id", + "value": { + "pattern": "/api/local/:id", + "method": "GET", + "transport": "buffer", + "chain": [ + { + "name": "byId", + "kind": "handler", + "origin": "module:TeacupModule", + "needs": [], + "provides": [] + } + ] + } + }, + { + "pattern": "/api/remote/ping", + "error": "no route: /api/remote/ping" + }, + { + "pattern": "/api/local/7", + "error": "no route: /api/local/7" + } + ], + "mermaid": "flowchart LR\n n_logger([\"logger\"])\n n_rooms([\"rooms\"])\n n_events([\"events\"])\n n_config[\"config\"]\n r__api_local__id{{\"GET /api/local/:id\"}}\n r__api_local_who{{\"GET /api/local/who\"}}\n n_config --> r__api_local_who", + "openapi": { + "openapi": "3.1.0", + "info": { + "title": "green-tea API", + "version": "0.0.0" + }, + "paths": { + "/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" + } + } + ] + } + }, + "/api/local/who": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } + }, + "dev": { + "graphHtml": { + "status": 200, + "type": "text/html", + "sha256": "7170130be7fa4ff3940eb35414f8d74b70a7d6b76fbc716552a26b581935d69a" + }, + "graphText": { + "status": 200, + "type": "text/plain", + "body": "flowchart LR\n n_logger([\"logger\"])\n n_rooms([\"rooms\"])\n n_events([\"events\"])\n n_config[\"config\"]\n r__api_local__id{{\"GET /api/local/:id\"}}\n r__api_local_who{{\"GET /api/local/who\"}}\n n_config --> r__api_local_who" + }, + "openapi": { + "status": 200, + "type": "application/json", + "body": { + "openapi": "3.1.0", + "info": { + "title": "green-tea API", + "version": "0.0.0" + }, + "paths": { + "/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" + } + } + ] + } + }, + "/api/local/who": { + "get": { + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + }, + "500": { + "description": "Internal Server Error" + } + } + } + } + } + } + } + }, + "lookup": { + "path": "/api/remote/ping", + "status": 200, + "body": "{\"from\":\"teapot\"}" + } + } + } +} diff --git a/test/router-introspection-parity.test.ts b/test/router-introspection-parity.test.ts new file mode 100644 index 0000000..a2b0aed --- /dev/null +++ b/test/router-introspection-parity.test.ts @@ -0,0 +1,384 @@ +/** + * Route introspection parity after the radix index (FPRD-001 CARD-09, #109). + * + * `test/router-parity/introspection.test.ts` already holds `graph()`, `explain()`, `openapi()` and the + * mesh teacup against goldens frozen at the pre-radix revision. This file covers what those goldens + * do not: + * + * - `explain()` / `inspect()` on a pattern declared under several methods with a non-GET first; + * - the dev endpoints (`/__graph__`, `/__openapi__`) on `app.fetch`, on a Node listener and on a + * mesh teacup, and their agreement with the direct API; + * - that `graph()` / `explain()` / `inspect()` / `openapi()` never consult the lookup index, and list + * routes in declaration order where the index ranks them differently. + * + * Expected values are in `test/fixtures/router-introspection-ed47836.json`. They were captured from + * the pre-radix code at `ed47836c9820c47bf4ed5e902d91e019abdb110a` (the CARD-08 pinned revision) by + * copying this file into a detached checkout of that commit and running + * `INTROSPECTION_PARITY_CAPTURE= npx vitest run test/router-introspection-parity.test.ts`. + * They were never generated from the radix implementation. A mismatch here is a compatibility + * failure, not a stale fixture: do not regenerate the file from current code. + */ +import 'reflect-metadata'; +import { createHash } from 'crypto'; +import { readFileSync, writeFileSync } from 'fs'; +import { join } from 'path'; +import type { AddressInfo } from 'net'; +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { createApp, Provider, Step, Route, Get, Delete, Sse, Ws, Module, needs, param, toMermaid } from '../src/index'; +import type { App } from '../src/app/types'; +import type { Logger } from '../src/logger'; +import { graphHtml } from '../src/graph-viz'; + +/** Counts every call into the router's lookup entry points, the only functions that build or read the index. */ +const lookups = vi.hoisted(() => ({ calls: 0 })); + +vi.mock('../src/http/router', async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + matchRoute: (...args: Parameters) => { + lookups.calls++; + return actual.matchRoute(...args); + }, + resolveRoute: (...args: Parameters) => { + lookups.calls++; + return actual.resolveRoute(...args); + }, + allowedMethods: (...args: Parameters) => { + lookups.calls++; + return actual.allowedMethods(...args); + }, + }; +}); + +const REVISION = 'ed47836c9820c47bf4ed5e902d91e019abdb110a'; +const FIXTURE = join(__dirname, 'fixtures', 'router-introspection-ed47836.json'); +const CAPTURE_TO = process.env.INTROSPECTION_PARITY_CAPTURE; + +// ── fixture app ───────────────────────────────────────────────────────────────────────────────── + +@Provider({ provides: 'store' }) +class Store { + provide() { + return { store: {} }; + } +} + +@Step({ provides: 'caller', needs: ['store'] }) +class Caller { + run() { + return { caller: {} }; + } +} + +// Declared least-specific first: DELETE precedes GET at `/:id`, and the catch-all and the param come +// before the static `/latest`. The index ranks `/latest` first for GET; declaration order does not. +@Route('/docs') +class Docs { + @Delete('/:id') remove(@param('id') id: string) { + return { id: 'remove', param: id }; + } + @Get('/:rest*') rest(@param('rest') rest: string) { + return { id: 'rest', param: rest }; + } + @Get('/:id') get(@param('id') id: string, @needs('caller') _c: unknown) { + return { id: 'get', param: id }; + } + @Get('/latest') latest() { + return { id: 'latest' }; + } + @Sse('/events') events() { + return (async function* () {})(); + } + @Ws('/:id/live') live(@needs('caller') _c: unknown) { + return (async function* () {})(); + } +} + +@Module({ mountpoint: '/v1', providers: [Store], steps: [Caller], controllers: [Docs] }) +class DocsModule {} + +const DECLARED_HANDLERS = ['remove', 'rest', 'get', 'latest', 'events', 'live']; + +const PATTERNS = [ + '/v1/docs/:id', // DELETE and GET share it; DELETE is declared first + '/v1/docs/latest', + '/v1/docs/:rest*', + '/v1/docs/:id/live', + '/v1/docs/7', // a concrete request path, not a declaration + '/v1/docs/latest/', + '/v1/missing', +]; + +const REQUESTS: Array<[string, string]> = [ + ['GET', '/v1/docs/latest'], + ['GET', '/v1/docs/7'], + ['GET', '/v1/docs/a/b'], + ['DELETE', '/v1/docs/7'], + ['PUT', '/v1/docs/7'], +]; + +function attempt(fn: () => T): { value: T } | { error: string } { + try { + return { value: fn() }; + } catch (error) { + return { error: (error as Error).message }; + } +} + +/** Everything introspection exposes, read straight from the app. Lookup-free by construction. */ +function introspect(app: App) { + return JSON.parse( + JSON.stringify({ + graph: app.graph(), + explain: PATTERNS.map((pattern) => ({ pattern, ...attempt(() => app.explain(pattern)) })), + inspect: PATTERNS.map((pattern) => ({ pattern, ...attempt(() => app.inspect(pattern)) })), + mermaid: app.toMermaid(), + openapi: app.openapi({ title: 'Introspection', version: '1' }), + openapiNoInfo: app.openapi(), + }), + ) as Record; +} + +const sha256 = (text: string) => createHash('sha256').update(text).digest('hex'); + +interface DevSeen { + graphHtml: { status: number; type: string | null; sha256: string }; + graphText: { status: number; type: string | null; body: string }; + openapi: { status: number; type: string | null; body: unknown }; +} + +/** Reads the three dev-endpoint responses through any `fetch`-shaped function. */ +async function devEndpoints(get: (path: string, accept?: string) => Promise): Promise { + const html = await get('/__graph__'); + const text = await get('/__graph__', 'text/plain'); + const doc = await get('/__openapi__'); + return { + graphHtml: { status: html.status, type: html.headers.get('content-type'), sha256: sha256(await html.text()) }, + graphText: { status: text.status, type: text.headers.get('content-type'), body: await text.text() }, + openapi: { status: doc.status, type: doc.headers.get('content-type'), body: JSON.parse(await doc.text()) }, + }; +} + +const viaFetch = (app: App) => (path: string, accept?: string) => + app.fetch(new Request(`http://parity.test${path}`, accept ? { headers: { accept } } : {})); + +async function lookupsThrough(app: App) { + const out = []; + + for (const [method, path] of REQUESTS) { + const res = await app.fetch(new Request(`http://parity.test${path}`, { method })); + out.push({ method, path, status: res.status, allow: res.headers.get('allow'), body: await res.text() }); + } + + return out; +} + +async function captureLocal() { + const app = createApp({ modules: [DocsModule], devGraph: true, devOpenapi: true }); + const direct = introspect(app); + const fetchDev = await devEndpoints(viaFetch(app)); + const server = await app.listen(0); + + try { + const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; + const nodeDev = await devEndpoints((path, accept) => + fetch(`${base}${path}`, accept ? { headers: { accept } } : {}), + ); + return { direct, fetchDev, nodeDev, lookups: await lookupsThrough(app) }; + } finally { + await app.close(); + } +} + +// ── mesh fixture ──────────────────────────────────────────────────────────────────────────────── + +const SECRET = 'introspection-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('/:id') byId() { + return {}; + } + @Get('/who') who(@needs('config') _c: unknown) { + return { from: 'teacup' }; + } +} +@Module({ mountpoint: '/api', controllers: [LocalCtl] }) +class TeacupModule {} + +const quiet: Logger = { debug: () => {}, info: () => {}, warn: () => {}, 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 = (text: string) => text.split(url).join(''); + const teacup = createApp({ + modules: [TeacupModule], + experimental: true, + logger: quiet, + devGraph: true, + devOpenapi: true, + mesh: { teapots: [{ url, secret: SECRET }] }, + }); + + try { + const before = { + graph: attempt(() => teacup.graph()), + explain: attempt(() => teacup.explain('/api/local/who')), + inspect: attempt(() => teacup.inspect('/api/local/who')), + openapi: attempt(() => teacup.openapi()), + }; + await teacup.ready(); + const after = introspectMesh(teacup); + const dev = await devEndpoints(async (path, accept) => { + const res = await viaFetch(teacup)(path, accept); + // The teapot URL carries an ephemeral port; replace it before hashing or comparing. + return new Response(anonymize(await res.text()), { status: res.status, headers: res.headers }); + }); + 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 { + result: JSON.parse(anonymize(JSON.stringify({ before, after, dev, lookup }))) as { + before: unknown; + after: ReturnType; + dev: DevSeen; + lookup: unknown; + }, + anonymize, + teacup, + }; + } finally { + await teacup.close(); + await teapot.close(); + } +} + +function introspectMesh(app: App) { + return { + graph: app.graph(), + explain: ['/api/local/who', '/api/local/:id', '/api/remote/ping', '/api/local/7'].map((pattern) => ({ + pattern, + ...attempt(() => app.explain(pattern)), + })), + mermaid: app.toMermaid(), + openapi: app.openapi(), + }; +} + +// ── tests ─────────────────────────────────────────────────────────────────────────────────────── + +/** Structural equality that also holds array order and object key order. */ +function expectSame(actual: unknown, expected: unknown, label: string): void { + const plain = JSON.parse(JSON.stringify(actual)); + expect(plain, label).toEqual(expected); + expect(JSON.stringify(plain), `${label} (key and array order)`).toBe(JSON.stringify(expected)); +} + +function loadExpected(): { local: Record; mesh: Record } { + const record = JSON.parse(readFileSync(FIXTURE, 'utf8')) as { + revision: string; + data: { local: Record; mesh: Record }; + }; + expect(record.revision, 'expectations must come from the pre-radix revision').toBe(REVISION); + return record.data; +} + +describe('router introspection parity (CARD-09)', () => { + beforeEach(() => { + lookups.calls = 0; + }); + + it('local app: direct introspection, dev endpoints and lookups equal the ed47836 capture', async () => { + const local = await captureLocal(); + const mesh = (await captureMesh()).result; + + if (CAPTURE_TO) { + writeFileSync(CAPTURE_TO, `${JSON.stringify({ revision: REVISION, data: { local, mesh } }, null, 2)}\n`); + return; + } + + const expected = loadExpected(); + for (const key of ['direct', 'fetchDev', 'nodeDev', 'lookups'] as const) + expectSame(local[key], expected.local[key], `local.${key}`); + for (const key of ['before', 'after', 'dev', 'lookup'] as const) + expectSame(mesh[key], expected.mesh[key], `mesh.${key}`); + }, 30_000); + + it('explain() and inspect() take the first declaration of a pattern shared by several methods', () => { + const app = createApp({ modules: [DocsModule] }); + expect(app.explain('/v1/docs/:id').method).toBe('DELETE'); + expect(app.inspect('/v1/docs/:id').at(-1)?.name).toBe('remove'); + // A concrete request path is never resolved against the patterns. + expect(() => app.explain('/v1/docs/7')).toThrow('no route: /v1/docs/7'); + expect(() => app.inspect('/v1/docs/latest/')).toThrow('no route: /v1/docs/latest/'); + }); + + it('dev endpoints agree with the direct API on app.fetch', async () => { + const app = createApp({ modules: [DocsModule], devGraph: true, devOpenapi: true }); + const dev = await devEndpoints(viaFetch(app)); + expect(dev.openapi.body).toEqual(app.openapi()); + expect(dev.graphText.body).toBe(toMermaid(app.graph())); + expect(dev.graphText.body).toBe(app.toMermaid()); + expect(dev.graphHtml.sha256).toBe(sha256(graphHtml(app.graph()))); + }); + + it('dev endpoints agree with the direct API on a mesh teacup after ready()', async () => { + const { result, anonymize, teacup } = await captureMesh(); + const anon = (value: T): T => JSON.parse(anonymize(JSON.stringify(value))); + expect(result.dev.openapi.body).toEqual(anon(teacup.openapi())); + expect(result.dev.graphText.body).toBe(anonymize(teacup.toMermaid())); + expect(result.dev.graphHtml.sha256).toBe(sha256(anonymize(graphHtml(teacup.graph())))); + // Remote HTTP routes are served but stay out of routePlans-based introspection. + expect(result.lookup).toMatchObject({ status: 200 }); + expect(result.after.graph.routes.map((route) => route.pattern)).toEqual(['/api/local/:id', '/api/local/who']); + expect(Object.keys(result.after.openapi.paths)).not.toContain('/api/remote/ping'); + }, 30_000); + + it('introspection never consults the lookup index and keeps declaration order where the index ranks differently', async () => { + const app = createApp({ modules: [DocsModule] }); + + const cold = introspect(app); + expect(lookups.calls, 'graph/explain/inspect/openapi must not call the router lookup').toBe(0); + + // Build and exercise the index. The ranked lookup answers `/latest` with the static route and + // `/7` with the param route, although `rest` is declared before both. + const served = await lookupsThrough(app); + expect(lookups.calls).toBeGreaterThan(0); + expect(JSON.parse(served[0].body)).toEqual({ id: 'latest' }); + expect(JSON.parse(served[1].body)).toEqual({ id: 'get', param: '7' }); + + const calls = lookups.calls; + const warm = introspect(app); + expect(lookups.calls, 'introspection after the index exists still does no lookup').toBe(calls); + expectSame(warm, cold, 'introspection before vs after the index was built'); + + // Declaration order, not rank order. + const graph = app.graph(); + expect(graph.routes.map((route) => route.chain.at(-1))).toEqual(DECLARED_HANDLERS); + expect(Object.keys(app.openapi().paths)).toEqual([ + '/v1/docs/{id}', + '/v1/docs/{rest}', + '/v1/docs/latest', + '/v1/docs/events', + ]); + expect(Object.keys(app.openapi().paths['/v1/docs/{id}'])).toEqual(['delete', 'get']); + }); +});