diff --git a/src/app/index.ts b/src/app/index.ts index 076742d..6d1d966 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -562,11 +562,11 @@ function devGraphRoute(graph: () => GraphView): RouteDef { pattern: '/__graph__', transport: 'buffer', handler: async (req) => { - const mermaid = toMermaid(graph()); + const view = graph(); const accept = String(req.headers['accept'] ?? ''); return accept.includes('text/plain') - ? { status: 200, headers: { 'content-type': 'text/plain' }, body: mermaid } - : { status: 200, headers: { 'content-type': 'text/html' }, body: graphHtml(mermaid) }; + ? { status: 200, headers: { 'content-type': 'text/plain' }, body: toMermaid(view) } + : { status: 200, headers: { 'content-type': 'text/html' }, body: graphHtml(view) }; }, }; } diff --git a/src/graph-viz.ts b/src/graph-viz.ts index 686c2eb..a10809b 100644 --- a/src/graph-viz.ts +++ b/src/graph-viz.ts @@ -68,14 +68,142 @@ export function toDOT(view: GraphView): string { return out.join('\n'); } -/** Wraps Mermaid source in a self-contained HTML page that renders it in the browser. */ -export function graphHtml(mermaid: string): string { - return [ - '
', - mermaid, - '', - '', - ].join('\n'); +/** + * Renders the graph as a self-contained, interactive HTML page (Cytoscape.js + + * dagre, both from a CDN — no runtime dependency is added). Clicking a route + * lights up its exact transitive slice and dims everything else; clicking a + * provider/step shows its blast radius (every route that depends on it). + * + * The whole {@link GraphView} is embedded as JSON, so all interaction is + * client-side — the page needs nothing from the server after it loads. + */ +export function graphHtml(view: GraphView): string { + const data = JSON.stringify(view).replace(/ + +
');
- expect(html).toContain('flowchart LR');
- expect(html).toContain('mermaid');
+ it('emits an interactive Cytoscape page with the graph embedded as JSON', () => {
+ const html = graphHtml(view);
+ expect(html).toContain('cytoscape'); // loads the viz lib from CDN
+ expect(html).toContain('cytoscape-dagre'); // and the DAG layout
+ expect(html).toContain('"user"'); // node data is embedded client-side
+ expect(html).toContain('/users/:id'); // route data too
+ });
+
+ it('escapes embedded JSON so a node name cannot break out of the script tag', () => {
+ const evil = graphHtml({
+ nodes: [{ name: 'a