From 27a984f07046bede5f6733829b320d5e74ec6f62 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 9 Jul 2026 10:44:03 -0500 Subject: [PATCH] feat(graph): interactive Cytoscape viewer for /__graph__ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mermaid's static diagram overlaps badly as the graph grows. Replace the /__graph__ HTML with an interactive Cytoscape.js + dagre viewer (both loaded from a CDN, so no runtime dependency is added — the one-dep pitch holds). Clicking a route lights up its real transitive slice and dims the rest; clicking a provider/step shows its blast radius. Edges are colour-coded by kind (needs vs feeds-endpoint), provider/step/endpoint nodes are visually distinct, and nodes are draggable. toMermaid()/toDOT() and the text/plain path are unchanged. Signed-off-by: Diego Resendez --- src/app/index.ts | 6 +- src/graph-viz.ts | 148 ++++++++++++++++++++++++++++++++++++++--- test/app.test.ts | 4 +- test/graph-viz.test.ts | 28 +++++--- 4 files changed, 162 insertions(+), 24 deletions(-) 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 [ - 'green-tea graph', - '', - '
',
-    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(/ + +green-tea graph + + + + +
🍵 green-tea · click a route for its slice · click a provider/step for its blast radius · drag to rearrange · click canvas to reset +
providerstependpointneedsfeeds endpoint
+
+
+`; } diff --git a/test/app.test.ts b/test/app.test.ts index dcea67f..4b52a50 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -376,8 +376,8 @@ describe('devGraph endpoint', () => { const app = createApp({ modules: [M], devGraph: true }); const s = await app.listen(0); const html = await fetch(`http://127.0.0.1:${portOf(s)}/__graph__`).then((r) => r.text()); - expect(html).toContain('class="mermaid"'); - expect(html).toContain('flowchart'); + expect(html).toContain('cytoscape'); + expect(html).toContain('/api/u/me'); const mmd = await fetch(`http://127.0.0.1:${portOf(s)}/__graph__`, { headers: { accept: 'text/plain' } }); expect(mmd.headers.get('content-type')).toContain('text/plain'); expect(await mmd.text()).toContain('flowchart'); diff --git a/test/graph-viz.test.ts b/test/graph-viz.test.ts index 940c0d3..bcbee3a 100644 --- a/test/graph-viz.test.ts +++ b/test/graph-viz.test.ts @@ -16,10 +16,10 @@ describe('toMermaid', () => { expect(m).toContain('flowchart'); expect(m).toContain('"db"'); expect(m).toContain('"user"'); - expect(m).toContain('n_config --> n_db'); // db needs config - expect(m).toContain('n_db --> n_user'); // user needs db - expect(m).not.toContain('n_req'); // 'req' is a seed (no node) -> no edge - expect(m).toContain('GET /users/:id'); // route node label + expect(m).toContain('n_config --> n_db'); // db needs config + expect(m).toContain('n_db --> n_user'); // user needs db + expect(m).not.toContain('n_req'); // 'req' is a seed (no node) -> no edge + expect(m).toContain('GET /users/:id'); // route node label }); }); @@ -32,10 +32,20 @@ describe('toDOT', () => { }); describe('graphHtml', () => { - it('wraps mermaid source in an HTML page that loads mermaid', () => { - const html = graphHtml('flowchart LR\n a-->b'); - expect(html).toContain('
');
-    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