feat: pure Mermaid/DOT/HTML graph renderers

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-28 02:28:39 -05:00
1 parent 8f3aef9edc
commit d67a4080dd
2 files changed
+86

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
export interface GraphNodeView { name: string; kind: 'provider' | 'step'; origin: string; needs: string[]; provides: string[] }
export interface GraphRouteView { pattern: string; method: string; transport: string; chain: string[] }
export interface GraphView { nodes: GraphNodeView[]; routes: GraphRouteView[] }
const nid = (s: string) => 'n_' + s.replace(/[^a-zA-Z0-9]/g, '_');
const rid = (s: string) => 'r_' + s.replace(/[^a-zA-Z0-9]/g, '_');
export function toMermaid(view: GraphView): string {
const known = new Set(view.nodes.map((n) => n.name));
const out: string[] = ['flowchart LR'];
for (const n of view.nodes) {
out.push(n.kind === 'provider' ? ` ${nid(n.name)}(["${n.name}"])` : ` ${nid(n.name)}["${n.name}"]`);
}
for (const n of view.nodes) for (const need of n.needs) if (known.has(need)) out.push(` ${nid(need)} --> ${nid(n.name)}`);
for (const r of view.routes) {
out.push(` ${rid(r.pattern)}{{"${r.method} ${r.pattern}"}}`);
for (const c of r.chain) if (known.has(c)) out.push(` ${nid(c)} --> ${rid(r.pattern)}`);
}
return out.join('\n');
}
export function toDOT(view: GraphView): string {
const known = new Set(view.nodes.map((n) => n.name));
const out: string[] = ['digraph green_tea {', ' rankdir=LR;'];
for (const n of view.nodes) {
out.push(` "${n.name}" [shape=${n.kind === 'provider' ? 'ellipse' : 'box'}];`);
}
for (const n of view.nodes) for (const need of n.needs) if (known.has(need)) out.push(` "${need}" -> "${n.name}";`);
for (const r of view.routes) {
const label = `${r.method} ${r.pattern}`;
out.push(` "${label}" [shape=hexagon];`);
for (const c of r.chain) if (known.has(c)) out.push(` "${c}" -> "${label}";`);
}
out.push('}');
return out.join('\n');
}
export function graphHtml(mermaid: string): string {
return [
'<!doctype html><html><head><meta charset="utf-8"><title>green-tea graph</title>',
'<script src="https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"></script></head>',
'<body><pre class="mermaid">', mermaid, '</pre>',
'<script>mermaid.initialize({ startOnLoad: true });</script></body></html>',
].join('\n');
}
+41
View File
@@ -0,0 +1,41 @@
import { describe, it, expect } from 'vitest';
import { toMermaid, toDOT, graphHtml, type GraphView } from '../src/graph-viz';
const view: GraphView = {
nodes: [
{ name: 'db', kind: 'provider', origin: 'module:M', needs: ['config'], provides: ['db'] },
{ name: 'config', kind: 'provider', origin: 'module:M', needs: [], provides: ['config'] },
{ name: 'user', kind: 'step', origin: 'module:M', needs: ['db', 'req'], provides: ['user'] },
],
routes: [{ pattern: '/users/:id', method: 'GET', transport: 'buffer', chain: ['config', 'db', 'user', 'getUser'] }],
};
describe('toMermaid', () => {
it('declares nodes and draws needs edges between known nodes', () => {
const m = toMermaid(view);
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
});
});
describe('toDOT', () => {
it('emits a digraph with nodes and edges', () => {
const d = toDOT(view);
expect(d).toContain('digraph');
expect(d).toContain('"config" -> "db"');
});
});
describe('graphHtml', () => {
it('wraps mermaid source in an HTML page that loads mermaid', () => {
const html = graphHtml('flowchart LR\n a-->b');
expect(html).toContain('<pre class="mermaid">');
expect(html).toContain('flowchart LR');
expect(html).toContain('mermaid');
});
});