forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-catalog-source-pr.test.mjs
More file actions
142 lines (131 loc) · 4.64 KB
/
Copy pathcheck-catalog-source-pr.test.mjs
File metadata and controls
142 lines (131 loc) · 4.64 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import assert from "node:assert/strict";
import { execFileSync, spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { test } from "node:test";
const guard = fileURLToPath(new URL("./check-catalog-source-pr.mjs", import.meta.url));
const old = { name: "old", type: "hyperframes:component" };
const kept = { name: "kept", type: "hyperframes:block" };
function fixture(t) {
const root = mkdtempSync(join(tmpdir(), "catalog-source-pr-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
const git = (...args) => execFileSync("git", args, { cwd: root, encoding: "utf8" }).trim();
const write = (path, contents) => {
mkdirSync(dirname(join(root, path)), { recursive: true });
writeFileSync(join(root, path), contents);
};
const item = (directory, entry) => {
write(`registry/${directory}/${entry.name}/registry-item.json`, JSON.stringify(entry));
write(`registry/${directory}/${entry.name}/index.html`, "<div>item</div>");
};
const index = (items, revision = "published") =>
write(
"registry/registry.json",
JSON.stringify({ name: "catalog", items, vectorRevision: revision }),
);
const commit = () => {
git("add", "-A");
git(
"-c",
"user.name=Test",
"-c",
"user.email=test@example.invalid",
"-c",
"commit.gpgsign=false",
"-c",
"core.hooksPath=/dev/null",
"commit",
"-qm",
"fixture",
);
};
git("init", "-q");
item("components", old);
write("registry/components/old/assets/registry-item.json", "asset metadata");
item("blocks", kept);
index([old, kept]);
commit();
const base = git("rev-parse", "HEAD");
return {
root,
item,
index,
removeOld: () => rmSync(join(root, "registry/components/old"), { recursive: true }),
check: () => {
commit();
return spawnSync(process.execPath, [guard, base], { cwd: root, encoding: "utf8" });
},
};
}
test("a deletion removes its index entry in the same source PR", (t) => {
const repo = fixture(t);
repo.removeOld();
repo.index([kept]);
const result = repo.check();
assert.equal(result.status, 0, result.stderr);
});
test("a rename unpublishes the old name and leaves the new name for automation", (t) => {
const repo = fixture(t);
repo.removeOld();
repo.item("components", { ...old, name: "renamed" });
repo.index([kept]);
const result = repo.check();
assert.equal(result.status, 0, result.stderr);
});
test("an add-plus-delete PR carries only the old entry removal", (t) => {
const repo = fixture(t);
repo.removeOld();
repo.item("blocks", { name: "new", type: "hyperframes:block" });
repo.index([kept]);
const result = repo.check();
assert.equal(result.status, 0, result.stderr);
});
test("a deletion cannot leave a dangling index entry", (t) => {
const repo = fixture(t);
repo.removeOld();
const result = repo.check();
assert.equal(result.status, 1);
assert.match(result.stderr, /must remove deleted items/);
});
test("the removal exception cannot publish an added entry", (t) => {
const repo = fixture(t);
repo.removeOld();
const added = { name: "new", type: "hyperframes:block" };
repo.item("blocks", added);
repo.index([kept, added]);
const result = repo.check();
assert.equal(result.status, 1);
assert.match(result.stderr, /Do not add entries/);
});
test("the removal exception cannot unpublish a directory that still exists", (t) => {
const repo = fixture(t);
repo.item("blocks", { name: "new", type: "hyperframes:block" });
repo.index([kept]);
const result = repo.check();
assert.equal(result.status, 1);
assert.match(result.stderr, /must remove deleted items/);
});
test("the removal exception preserves index metadata", (t) => {
const repo = fixture(t);
repo.removeOld();
repo.index([kept], "changed");
const result = repo.check();
assert.equal(result.status, 1);
assert.match(result.stderr, /change index metadata/);
});
test("deleting only the manifest does not qualify as deleting an item directory", (t) => {
const repo = fixture(t);
rmSync(join(repo.root, "registry/components/old/registry-item.json"));
repo.index([kept]);
const result = repo.check();
assert.equal(result.status, 1);
assert.match(result.stderr, /complete item directory/);
});
test("deleting nested asset metadata does not unpublish its item", (t) => {
const repo = fixture(t);
rmSync(join(repo.root, "registry/components/old/assets/registry-item.json"));
const result = repo.check();
assert.equal(result.status, 0, result.stderr);
});