Repository navigation
Expand file tree
/
Copy pathdesign-docs.test.ts
More file actions
121 lines (111 loc) · 3.73 KB
/
Copy pathdesign-docs.test.ts
File metadata and controls
121 lines (111 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
collectChangedPaths,
expandInputPaths,
matchDesignDocuments,
normalizeProjectPath,
parseDesignDocument,
type DesignDocument,
} from "./design-docs";
describe("parseDesignDocument", () => {
test("reads a list of project-relative globs", () => {
expect(
parseDesignDocument(
"design/example.md",
`---
applies_to:
- packages/example/**
- packages/shared/src/contract.ts
---
# Example
`,
),
).toEqual({
path: "design/example.md",
patterns: ["packages/example/**", "packages/shared/src/contract.ts"],
});
});
test("requires non-empty applies_to front matter", () => {
expect(() => parseDesignDocument("design/example.md", "# Example\n")).toThrow(
"missing YAML front matter",
);
expect(() =>
parseDesignDocument("design/example.md", "---\napplies_to: []\n---\n"),
).toThrow("applies_to must be a non-empty list");
});
test("rejects paths that are not project-root-relative", () => {
expect(() =>
parseDesignDocument(
"design/example.md",
"---\napplies_to:\n - ../outside/**\n---\n",
),
).toThrow("must be project-root-relative");
});
});
describe("matchDesignDocuments", () => {
const documents: DesignDocument[] = [
{
path: "design/engine.md",
patterns: ["inference/crates/icn-engine/**", "inference/shared.rs"],
},
{
path: "design/api.md",
patterns: ["inference/crates/icn-api/**"],
},
];
test("returns all overlapping documents once", () => {
const result = matchDesignDocuments(documents, [
"inference/crates/icn-engine/src/lib.rs",
"inference/shared.rs",
]);
expect(result.map((match) => match.document.path)).toEqual(["design/engine.md"]);
expect(result[0]?.matches).toHaveLength(2);
});
test("treats a changed design document as pertinent to itself", () => {
const result = matchDesignDocuments(documents, ["design/api.md"]);
expect(result.map((match) => match.document.path)).toEqual(["design/api.md"]);
});
});
describe("normalizeProjectPath", () => {
test("normalizes paths under the project root", () => {
expect(normalizeProjectPath("/repo", "/repo/packages/example.ts")).toBe(
"packages/example.ts",
);
});
test("rejects paths outside the project root", () => {
expect(() => normalizeProjectPath("/repo", "/other/example.ts")).toThrow(
"outside the project root",
);
});
});
describe("Git path collection", () => {
test("includes staged, unstaged, and untracked files and expands directories", () => {
const root = mkdtempSync(join(tmpdir(), "magnitude-design-docs-"));
try {
Bun.spawnSync(["git", "init", "-q"], { cwd: root });
mkdirSync(join(root, "src", "nested"), { recursive: true });
writeFileSync(join(root, "src", "tracked.ts"), "initial\n");
Bun.spawnSync(["git", "add", "src/tracked.ts"], { cwd: root });
writeFileSync(join(root, "src", "tracked.ts"), "changed\n");
writeFileSync(join(root, "src", "staged.ts"), "staged\n");
Bun.spawnSync(["git", "add", "src/staged.ts"], { cwd: root });
writeFileSync(join(root, "src", "nested", "untracked.ts"), "untracked\n");
expect(collectChangedPaths(root)).toEqual([
"src/nested/untracked.ts",
"src/staged.ts",
"src/tracked.ts",
]);
expect(expandInputPaths(root, ["src"])).toEqual([
"src/",
"src/nested/untracked.ts",
"src/staged.ts",
"src/tracked.ts",
]);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
});