The best way to control Claude Code, Codex and more from any computer or phone

main
ts 106 lines 3.02 KB
Raw
1 import { describe, expect, test } from "@alchemy.run/test";
2 import { fromPath, FQN_SEPARATOR, parseFqn, toFqn, toPath } from "../src/FQN";
3 import type { NamespaceNode } from "../src/Namespace";
4
5 describe("FQN", { tags: ["unit", "local"] }, () => {
6 describe("toPath", () => {
7 test("returns empty array for undefined namespace", () => {
8 expect(toPath(undefined)).toEqual([]);
9 });
10
11 test("returns single element for root namespace", () => {
12 const ns: NamespaceNode = { Id: "Root" };
13 expect(toPath(ns)).toEqual(["Root"]);
14 });
15
16 test("returns path from root to leaf", () => {
17 const ns: NamespaceNode = {
18 Id: "Child",
19 Parent: {
20 Id: "Parent",
21 Parent: { Id: "Root" },
22 },
23 };
24 expect(toPath(ns)).toEqual(["Root", "Parent", "Child"]);
25 });
26 });
27
28 describe("toFqn", () => {
29 test("returns logicalId for undefined namespace", () => {
30 expect(toFqn(undefined, "MyResource")).toBe("MyResource");
31 });
32
33 test("returns namespace-qualified name", () => {
34 const ns: NamespaceNode = { Id: "Parent" };
35 expect(toFqn(ns, "MyResource")).toBe(`Parent${FQN_SEPARATOR}MyResource`);
36 });
37
38 test("handles deep namespace", () => {
39 const ns: NamespaceNode = {
40 Id: "Child",
41 Parent: { Id: "Parent" },
42 };
43 expect(toFqn(ns, "MyResource")).toBe(`Parent${FQN_SEPARATOR}Child${FQN_SEPARATOR}MyResource`);
44 });
45 });
46
47 describe("parseFqn", () => {
48 test("parses simple logicalId", () => {
49 expect(parseFqn("MyResource")).toEqual({
50 path: [],
51 logicalId: "MyResource",
52 });
53 });
54
55 test("parses namespaced FQN", () => {
56 expect(parseFqn("Parent/Child/MyResource")).toEqual({
57 path: ["Parent", "Child"],
58 logicalId: "MyResource",
59 });
60 });
61
62 test("parses single namespace FQN", () => {
63 expect(parseFqn("Parent/MyResource")).toEqual({
64 path: ["Parent"],
65 logicalId: "MyResource",
66 });
67 });
68 });
69
70 describe("fromPath", () => {
71 test("returns undefined for empty path", () => {
72 expect(fromPath([])).toBeUndefined();
73 });
74
75 test("returns single node for single element", () => {
76 const result = fromPath(["Root"]);
77 expect(result).toEqual({ Id: "Root", Parent: undefined });
78 });
79
80 test("returns nested nodes", () => {
81 const result = fromPath(["Root", "Parent", "Child"]);
82 expect(result).toEqual({
83 Id: "Child",
84 Parent: {
85 Id: "Parent",
86 Parent: { Id: "Root", Parent: undefined },
87 },
88 });
89 });
90 });
91
92 describe("roundtrip", () => {
93 test("toFqn -> parseFqn -> fromPath -> toFqn", () => {
94 const ns: NamespaceNode = {
95 Id: "Child",
96 Parent: { Id: "Parent" },
97 };
98 const logicalId = "MyResource";
99 const fqn = toFqn(ns, logicalId);
100 const parsed = parseFqn(fqn);
101 const reconstructedNs = fromPath(parsed.path);
102 const roundtripFqn = toFqn(reconstructedNs, parsed.logicalId);
103 expect(roundtripFqn).toBe(fqn);
104 });
105 });
106 });