Skip to content

Commit 43a282b

Browse files
committed
Add Node Manager
1 parent a6337e1 commit 43a282b

File tree

2 files changed

+449
-0
lines changed

2 files changed

+449
-0
lines changed

src/nodeManager.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { beforeEach, describe, expect, it } from "vitest";
2+
3+
import { NodeManager } from "./nodeManager";
4+
5+
describe("NodeManager", () => {
6+
let nodeManager: NodeManager;
7+
8+
beforeEach(() => {
9+
nodeManager = new NodeManager();
10+
});
11+
12+
describe("stable node ID generation", () => {
13+
it("should generate consistent node IDs for the same task", () => {
14+
const nodeId1 = nodeManager.getNodeId("test-task", "task");
15+
const nodeId2 = nodeManager.getNodeId("test-task", "task");
16+
17+
expect(nodeId1).toBe(nodeId2);
18+
expect(nodeId1).toMatch(/^task_/);
19+
});
20+
21+
it("should generate different IDs for different tasks", () => {
22+
const nodeId1 = nodeManager.getNodeId("task-1", "task");
23+
const nodeId2 = nodeManager.getNodeId("task-2", "task");
24+
25+
expect(nodeId1).not.toBe(nodeId2);
26+
});
27+
});
28+
29+
describe("task ID updates", () => {
30+
it("should update task ID while preserving node ID", () => {
31+
const originalNodeId = nodeManager.getNodeId("old-task", "task");
32+
nodeManager.updateTaskId("old-task", "new-task");
33+
34+
const newNodeId = nodeManager.getNodeId("new-task", "task");
35+
expect(newNodeId).toBe(originalNodeId);
36+
37+
const taskId = nodeManager.getTaskId(originalNodeId);
38+
expect(taskId).toBe("new-task");
39+
});
40+
});
41+
42+
describe("migration", () => {
43+
it("should migrate legacy nodes to stable IDs", () => {
44+
const legacyNodes = [
45+
{
46+
id: "task_legacy-task",
47+
type: "task",
48+
data: {},
49+
position: { x: 0, y: 0 },
50+
},
51+
{
52+
id: "input_legacy-input",
53+
type: "input",
54+
data: {},
55+
position: { x: 0, y: 0 },
56+
},
57+
];
58+
59+
const { updatedNodes, migrationMap } =
60+
nodeManager.migrateExistingNodes(legacyNodes);
61+
62+
expect(updatedNodes).toHaveLength(2);
63+
expect(updatedNodes[0].id).toMatch(/^task_/);
64+
expect(updatedNodes[1].id).toMatch(/^input_/);
65+
expect(updatedNodes[0].id).not.toBe("task_legacy-task");
66+
expect(updatedNodes[1].id).not.toBe("input_legacy-input");
67+
68+
expect(migrationMap["task_legacy-task"]).toBe(updatedNodes[0].id);
69+
expect(migrationMap["input_legacy-input"]).toBe(updatedNodes[1].id);
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)