auth-yes/.forum/poc-g1/dependency_graph_poc.ts

67 lines
2.1 KiB
TypeScript

import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: Dependency Graphing (Adjacency Matrices)
*
* Demonstrates mathematically calculating the "blast radius" of a code change
* by representing component dependencies as an adjacency matrix and using
* graph traversal to find impacted nodes.
*/
// Adjacency matrix for a simple app:
// Nodes: [0: Auth, 1: Database, 2: API Route, 3: UI Component]
// Matrix[i][j] = 1 means Node i depends on Node j
const matrix = [
[0, 1, 0, 0], // Auth depends on Database
[0, 0, 0, 0], // Database has no outgoing dependencies
[1, 1, 0, 0], // API Route depends on Auth and Database
[0, 0, 1, 0], // UI Component depends on API Route
];
const nodes = ["Auth", "Database", "API Route", "UI Component"];
// Find all nodes that depend on a given node (blast radius)
function calculateBlastRadius(changedNodeIndex: number): number[] {
const impacted = new Set<number>();
const queue = [changedNodeIndex];
while (queue.length > 0) {
const current = queue.shift()!;
// Find who depends on `current`
for (let i = 0; i < matrix.length; i++) {
if (matrix[i][current] === 1 && !impacted.has(i)) {
impacted.add(i);
queue.push(i);
}
}
}
return Array.from(impacted);
}
if (import.meta.main) {
console.log("Running Dependency Graphing PoC tests...");
try {
const changedNode = 1; // "Database" changed
console.log(
`If '${nodes[changedNode]}' changes, calculating blast radius...`,
);
const impactedIndices = calculateBlastRadius(changedNode);
const impactedNames = impactedIndices.map((i) => nodes[i]);
console.log(`Impacted components:`, impactedNames);
// If Database changes, Auth and API Route depend on it directly.
// UI Component depends on API Route. So all others should be impacted.
assertEquals(impactedIndices.length, 3);
console.log(
"✅ Dependency Graphing PoC successful: Blast radius calculated correctly.",
);
} catch (err) {
console.error("❌ Dependency Graphing PoC failed:", err);
Deno.exit(1);
}
}