@usesketchbook/opencode-plugin 0.0.0-stage → 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -2
- package/dist/generated/baseline-manifest.d.ts +2 -0
- package/dist/generated/baseline-manifest.js +1590 -0
- package/dist/generated/catalog-baseline.d.ts +2 -0
- package/dist/generated/catalog-baseline.js +255 -0
- package/dist/gui-experimental.cjs +111 -0
- package/dist/gui.d.ts +22 -0
- package/dist/gui.js +34 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/rpc.d.ts +1173 -0
- package/dist/rpc.js +212 -0
- package/dist/server/catalog.d.ts +9 -0
- package/dist/server/catalog.js +74 -0
- package/dist/server/decision-guard-hooks.d.ts +2 -0
- package/dist/server/decision-guard-hooks.js +57 -0
- package/dist/server/decision-guards.d.ts +11 -0
- package/dist/server/decision-guards.js +36 -0
- package/dist/server/engine.d.ts +6 -0
- package/dist/server/engine.js +38 -0
- package/dist/server/guard-hooks.d.ts +3 -0
- package/dist/server/guard-hooks.js +35 -0
- package/dist/server/guards.d.ts +13 -0
- package/dist/server/guards.js +42 -0
- package/dist/server/operations.d.ts +93 -0
- package/dist/server/operations.js +300 -0
- package/dist/server/permissions.d.ts +29 -0
- package/dist/server/permissions.js +97 -0
- package/dist/server/rpc.d.ts +126 -0
- package/dist/server/rpc.js +308 -0
- package/dist/server/runner.d.ts +27 -0
- package/dist/server/runner.js +136 -0
- package/dist/server/schema.d.ts +4 -0
- package/dist/server/schema.js +66 -0
- package/dist/server/tools.d.ts +2 -0
- package/dist/server/tools.js +176 -0
- package/dist/server/tui.d.ts +70 -0
- package/dist/server/tui.js +172 -0
- package/dist/shared/catalog.d.ts +52 -0
- package/dist/shared/catalog.js +84 -0
- package/dist/shared/envelope.d.ts +24 -0
- package/dist/shared/envelope.js +28 -0
- package/dist/shared/manifest.d.ts +53 -0
- package/dist/shared/manifest.js +135 -0
- package/dist/test/baseline.test.d.ts +1 -0
- package/dist/test/baseline.test.js +11 -0
- package/dist/test/catalog.test.d.ts +1 -0
- package/dist/test/catalog.test.js +232 -0
- package/dist/test/decision-guards.test.d.ts +1 -0
- package/dist/test/decision-guards.test.js +30 -0
- package/dist/test/engine.test.d.ts +1 -0
- package/dist/test/engine.test.js +23 -0
- package/dist/test/fixtures.d.ts +12 -0
- package/dist/test/fixtures.js +55 -0
- package/dist/test/guard-hooks.integration.test.d.ts +1 -0
- package/dist/test/guard-hooks.integration.test.js +321 -0
- package/dist/test/guard-hooks.test.d.ts +1 -0
- package/dist/test/guard-hooks.test.js +14 -0
- package/dist/test/guards.test.d.ts +1 -0
- package/dist/test/guards.test.js +24 -0
- package/dist/test/gui.test.d.ts +1 -0
- package/dist/test/gui.test.js +61 -0
- package/dist/test/manifest.test.d.ts +1 -0
- package/dist/test/manifest.test.js +86 -0
- package/dist/test/operations.test.d.ts +1 -0
- package/dist/test/operations.test.js +263 -0
- package/dist/test/parity.test.d.ts +1 -0
- package/dist/test/parity.test.js +56 -0
- package/dist/test/permissions.integration.test.d.ts +1 -0
- package/dist/test/permissions.integration.test.js +535 -0
- package/dist/test/permissions.test.d.ts +1 -0
- package/dist/test/permissions.test.js +42 -0
- package/dist/test/rpc.test.d.ts +1 -0
- package/dist/test/rpc.test.js +389 -0
- package/dist/test/runner.test.d.ts +1 -0
- package/dist/test/runner.test.js +47 -0
- package/dist/test/schema.test.d.ts +1 -0
- package/dist/test/schema.test.js +51 -0
- package/dist/test/tui.test.d.ts +1 -0
- package/dist/test/tui.test.js +226 -0
- package/dist/tprobe.d.ts +1 -0
- package/dist/tprobe.js +2 -0
- package/dist/tui.d.ts +10 -0
- package/dist/tui.js +21 -0
- package/dist/views/operations.d.ts +50 -0
- package/dist/views/operations.js +80 -0
- package/package.json +71 -4
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { setupGuardHooks } from "../server/guard-hooks.js";
|
|
7
|
+
import { setupDecisionGuardHooks } from "../server/decision-guard-hooks.js";
|
|
8
|
+
const capture = async (setup, root) => {
|
|
9
|
+
let hook = () => undefined;
|
|
10
|
+
const ctx = {
|
|
11
|
+
location: { directory: root },
|
|
12
|
+
tool: {
|
|
13
|
+
hook: async (_event, fn) => {
|
|
14
|
+
hook = fn;
|
|
15
|
+
return { dispose: async () => { } };
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
await setup(ctx);
|
|
20
|
+
return {
|
|
21
|
+
hook: (event) => {
|
|
22
|
+
// Guard listeners may throw synchronously (generated-path guard) or
|
|
23
|
+
// return a rejected promise (decision guard, which reads the file
|
|
24
|
+
// first). Routing the call through a promise normalizes both into
|
|
25
|
+
// rejections the tests can assert on.
|
|
26
|
+
return Promise.resolve().then(() => hook(event));
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const rejectsGuard = async (harness, tool, input, match) => {
|
|
31
|
+
await assert.rejects(() => Promise.resolve(harness.hook({ tool, input })), (error) => {
|
|
32
|
+
assert.equal(error._tag, "Tool.Error", "guards raise the host Tool.Error");
|
|
33
|
+
assert.match(error.message ?? "", match);
|
|
34
|
+
return true;
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const tmpProject = async (prefix) => mkdtemp(join(tmpdir(), prefix));
|
|
38
|
+
// ---------------------------------------------------------------- generated
|
|
39
|
+
test("generated-path guard blocks native writes to generated output", async () => {
|
|
40
|
+
const root = await tmpProject("sketchbook-plugin-guard-");
|
|
41
|
+
try {
|
|
42
|
+
const harness = await capture(setupGuardHooks, root);
|
|
43
|
+
for (const target of [
|
|
44
|
+
join(root, "generated/css/tokens.css"),
|
|
45
|
+
join(root, "generated", "js/app.js"),
|
|
46
|
+
join(root, "src/components/button/button.styles.ts"),
|
|
47
|
+
join(root, "src/index.ts"),
|
|
48
|
+
join(root, "spec/tokens/foundation/color.json"),
|
|
49
|
+
join(root, "spec/tokens/interface.json"),
|
|
50
|
+
]) {
|
|
51
|
+
await rejectsGuard(harness, "write", { file_path: target, content: "x" }, /generated output/i);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
await rm(root, { recursive: true, force: true });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
test("generated-path guard leaves owned sources alone", async () => {
|
|
59
|
+
const root = await tmpProject("sketchbook-plugin-guard-own-");
|
|
60
|
+
try {
|
|
61
|
+
const harness = await capture(setupGuardHooks, root);
|
|
62
|
+
for (const target of [
|
|
63
|
+
join(root, "src/components/button/Button.ts"),
|
|
64
|
+
join(root, "spec/tokens/semantic/light.tokens.json"),
|
|
65
|
+
join(root, "spec/components/button/contract.json"),
|
|
66
|
+
join(root, "src/generated-helpers/readme.md"),
|
|
67
|
+
]) {
|
|
68
|
+
await harness.hook({ tool: "write", input: { file_path: target } });
|
|
69
|
+
await harness.hook({ tool: "edit", input: { file_path: target } });
|
|
70
|
+
await harness.hook({ tool: "patch", input: { path: target } });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await rm(root, { recursive: true, force: true });
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
test("generated-path guard resolves and normalizes the target path", async () => {
|
|
78
|
+
const root = await tmpProject("sketchbook-plugin-guard-res-");
|
|
79
|
+
try {
|
|
80
|
+
const harness = await capture(setupGuardHooks, root);
|
|
81
|
+
// Relative and traversal spellings of the same generated file are caught.
|
|
82
|
+
await rejectsGuard(harness, "edit", { file_path: join(root, "src", "..", "generated", "css", "tokens.css") }, /generated output/i);
|
|
83
|
+
// The generated directory itself is not a file the guard names, but
|
|
84
|
+
// everything under it is.
|
|
85
|
+
await harness.hook({ tool: "write", input: { file_path: join(root, "generated") } });
|
|
86
|
+
await rejectsGuard(harness, "write", { file_path: join(root, "generated", "a") }, /generated output/i);
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
await rm(root, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
test("generated-path guard is textual: symlink aliases follow their path spelling", async () => {
|
|
93
|
+
const root = await tmpProject("sketchbook-plugin-guard-link-");
|
|
94
|
+
try {
|
|
95
|
+
await mkdir(join(root, "generated/css"), { recursive: true });
|
|
96
|
+
await mkdir(join(root, "src"), { recursive: true });
|
|
97
|
+
await writeFile(join(root, "generated/css/tokens.css"), "real output");
|
|
98
|
+
await writeFile(join(root, "src/theme.source.ts"), "real source");
|
|
99
|
+
// An alias whose path lands on a protected pattern is blocked even though
|
|
100
|
+
// the link target is an owned source file.
|
|
101
|
+
await symlink(join(root, "src/theme.source.ts"), join(root, "src/index.ts"));
|
|
102
|
+
// An alias that avoids every protected pattern points at generated output
|
|
103
|
+
// without matching one; this guard is a path-policy net, not a filesystem
|
|
104
|
+
// boundary — the host's own file permissions still apply to the target.
|
|
105
|
+
await symlink(join(root, "generated/css/tokens.css"), join(root, "src/theme.alias.ts"));
|
|
106
|
+
const harness = await capture(setupGuardHooks, root);
|
|
107
|
+
await rejectsGuard(harness, "write", { file_path: join(root, "src/index.ts") }, /src\/index\.ts/);
|
|
108
|
+
await harness.hook({
|
|
109
|
+
tool: "write",
|
|
110
|
+
input: { file_path: join(root, "src/theme.alias.ts") },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
await rm(root, { recursive: true, force: true });
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
test("generated-path guard scopes to its registered location, including nested projects", async () => {
|
|
118
|
+
const root = await tmpProject("sketchbook-plugin-guard-nest-");
|
|
119
|
+
try {
|
|
120
|
+
const nested = join(root, "examples", "web");
|
|
121
|
+
await mkdir(join(nested, "generated/css"), { recursive: true });
|
|
122
|
+
// A guard registered at the outer root does not reach a nested project's
|
|
123
|
+
// generated tree: patterns are anchored to the guard's own location.
|
|
124
|
+
const outer = await capture(setupGuardHooks, root);
|
|
125
|
+
await outer.hook({
|
|
126
|
+
tool: "write",
|
|
127
|
+
input: { file_path: join(nested, "generated/css/tokens.css") },
|
|
128
|
+
});
|
|
129
|
+
// A guard registered at the nested location protects it.
|
|
130
|
+
const inner = await capture(setupGuardHooks, nested);
|
|
131
|
+
await rejectsGuard(inner, "edit", { file_path: join(nested, "generated/css/tokens.css") }, /generated output/i);
|
|
132
|
+
// And the inner guard still ignores the outer project's sources.
|
|
133
|
+
await inner.hook({
|
|
134
|
+
tool: "write",
|
|
135
|
+
input: { file_path: join(root, "src/index.ts") },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
await rm(root, { recursive: true, force: true });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
test("generated-path guard only inspects file tools and inputs with a path", async () => {
|
|
143
|
+
const root = await tmpProject("sketchbook-plugin-guard-tool-");
|
|
144
|
+
try {
|
|
145
|
+
const harness = await capture(setupGuardHooks, root);
|
|
146
|
+
// Non-file tools never trip the guard, even on generated paths.
|
|
147
|
+
await harness.hook({
|
|
148
|
+
tool: "bash",
|
|
149
|
+
input: { command: `echo x > ${join(root, "generated/css/tokens.css")}` },
|
|
150
|
+
});
|
|
151
|
+
// File tools without a usable path are ignored.
|
|
152
|
+
await harness.hook({ tool: "write", input: {} });
|
|
153
|
+
await harness.hook({ tool: "edit", input: { file_path: " " } });
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
await rm(root, { recursive: true, force: true });
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
test("disposing the generated-path guard unregisters the hook", async () => {
|
|
160
|
+
const root = await tmpProject("sketchbook-plugin-guard-dispose-");
|
|
161
|
+
try {
|
|
162
|
+
let hook = () => undefined;
|
|
163
|
+
let disposed = false;
|
|
164
|
+
const ctx = {
|
|
165
|
+
location: { directory: root },
|
|
166
|
+
tool: {
|
|
167
|
+
hook: async (_event, fn) => {
|
|
168
|
+
hook = fn;
|
|
169
|
+
return {
|
|
170
|
+
dispose: async () => {
|
|
171
|
+
disposed = true;
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
const dispose = await setupGuardHooks(ctx);
|
|
178
|
+
assert.equal(disposed, false);
|
|
179
|
+
await dispose();
|
|
180
|
+
assert.equal(disposed, true);
|
|
181
|
+
// After disposal the captured listener still exists in this mock, but the
|
|
182
|
+
// plugin no longer holds a live registration; a compliant host stops
|
|
183
|
+
// delivering events once dispose resolves.
|
|
184
|
+
assert.equal(typeof hook, "function");
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
await rm(root, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
// ------------------------------------------------------- settled decisions
|
|
191
|
+
const accepted = `### SB-0001 · 2026-01-01 · Use a native button
|
|
192
|
+
**Status:** accepted
|
|
193
|
+
**Decision:** Use a native button.
|
|
194
|
+
`;
|
|
195
|
+
const proposed = `### SB-0002 · 2026-01-02 · Try a div
|
|
196
|
+
**Status:** proposed
|
|
197
|
+
**Decision:** Use a div.
|
|
198
|
+
`;
|
|
199
|
+
const writeDecisionLog = async (root, body) => {
|
|
200
|
+
await mkdir(join(root, "spec/decisions"), { recursive: true });
|
|
201
|
+
await writeFile(join(root, "spec/decisions/DECISIONS.md"), body);
|
|
202
|
+
};
|
|
203
|
+
const decisionHarness = async (root, body) => {
|
|
204
|
+
await writeDecisionLog(root, body);
|
|
205
|
+
return capture(setupDecisionGuardHooks, root);
|
|
206
|
+
};
|
|
207
|
+
test("decision guard blocks a write that drops a settled decision", async () => {
|
|
208
|
+
const root = await tmpProject("sketchbook-plugin-dec-");
|
|
209
|
+
try {
|
|
210
|
+
const harness = await decisionHarness(root, `# Decisions\n\n${accepted}\n${proposed}`);
|
|
211
|
+
await rejectsGuard(harness, "write", { file_path: join(root, "spec/decisions/DECISIONS.md"), content: `# Decisions\n\n${proposed}` }, /settled decisions stay settled.*SB-0001/is);
|
|
212
|
+
// The file on disk is untouched: the guard fires before the tool runs.
|
|
213
|
+
const onDisk = await readFile(join(root, "spec/decisions/DECISIONS.md"), "utf8");
|
|
214
|
+
assert.match(onDisk, /SB-0001/);
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
await rm(root, { recursive: true, force: true });
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
test("decision guard allows writes that keep settled entries and add new ones", async () => {
|
|
221
|
+
const root = await tmpProject("sketchbook-plugin-dec-keep-");
|
|
222
|
+
try {
|
|
223
|
+
const harness = await decisionHarness(root, `# Decisions\n\n${accepted}\n${proposed}`);
|
|
224
|
+
await harness.hook({
|
|
225
|
+
tool: "write",
|
|
226
|
+
input: {
|
|
227
|
+
file_path: join(root, "spec/decisions/DECISIONS.md"),
|
|
228
|
+
content: `# Decisions\n\n${accepted}\n${proposed}\n### SB-0003 · 2026-03-01 · New\n**Status:** proposed\n**Decision:** New.\n`,
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
await rm(root, { recursive: true, force: true });
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
test("decision guard blocks rewording a settled entry and allows appending after it", async () => {
|
|
237
|
+
const root = await tmpProject("sketchbook-plugin-dec-edit-");
|
|
238
|
+
try {
|
|
239
|
+
const harness = await decisionHarness(root, `# Decisions\n\n${accepted}\n${proposed}`);
|
|
240
|
+
const decisionPath = join(root, "spec/decisions/DECISIONS.md");
|
|
241
|
+
await rejectsGuard(harness, "edit", {
|
|
242
|
+
file_path: decisionPath,
|
|
243
|
+
old_string: "**Decision:** Use a native button.",
|
|
244
|
+
new_string: "**Decision:** Use a div.",
|
|
245
|
+
}, /SB-0001/);
|
|
246
|
+
// Appending a whole new entry after a settled one is the sanctioned flow.
|
|
247
|
+
await harness.hook({
|
|
248
|
+
tool: "edit",
|
|
249
|
+
input: {
|
|
250
|
+
file_path: decisionPath,
|
|
251
|
+
old_string: "**Decision:** Use a native button.",
|
|
252
|
+
new_string: "**Decision:** Use a native button.\n### SB-0003 · 2026-03-01 · New\n",
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
// Proposed entries may still be reworded freely.
|
|
256
|
+
await harness.hook({
|
|
257
|
+
tool: "edit",
|
|
258
|
+
input: {
|
|
259
|
+
file_path: decisionPath,
|
|
260
|
+
old_string: "**Decision:** Use a div.",
|
|
261
|
+
new_string: "**Decision:** Use a span.",
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
await rm(root, { recursive: true, force: true });
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
test("decision guard reads the structured edits form and resolves path aliases", async () => {
|
|
270
|
+
const root = await tmpProject("sketchbook-plugin-dec-edits-");
|
|
271
|
+
try {
|
|
272
|
+
const harness = await decisionHarness(root, `# Decisions\n\n${accepted}`);
|
|
273
|
+
// The `edits` array form (patch-style tools) is inspected too.
|
|
274
|
+
await rejectsGuard(harness, "patch", {
|
|
275
|
+
file_path: join(root, "spec/decisions/DECISIONS.md"),
|
|
276
|
+
edits: [{ oldString: "**Decision:** Use a native button.", newString: "gone" }],
|
|
277
|
+
}, /SB-0001/);
|
|
278
|
+
// Non-canonical spellings that resolve to the decision file are guarded.
|
|
279
|
+
await rejectsGuard(harness, "edit", {
|
|
280
|
+
file_path: join(root, "spec", "decisions", "..", "decisions", "DECISIONS.md"),
|
|
281
|
+
old_string: "**Decision:** Use a native button.",
|
|
282
|
+
new_string: "gone",
|
|
283
|
+
}, /SB-0001/);
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
await rm(root, { recursive: true, force: true });
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
test("decision guard ignores other paths, other tools, and a missing log", async () => {
|
|
290
|
+
const root = await tmpProject("sketchbook-plugin-dec-other-");
|
|
291
|
+
try {
|
|
292
|
+
const harness = await capture(setupDecisionGuardHooks, root);
|
|
293
|
+
// No DECISIONS.md exists yet: nothing to protect, nothing to throw.
|
|
294
|
+
await harness.hook({
|
|
295
|
+
tool: "write",
|
|
296
|
+
input: { file_path: join(root, "spec/decisions/DECISIONS.md"), content: "x" },
|
|
297
|
+
});
|
|
298
|
+
await writeDecisionLog(root, `# Decisions\n\n${accepted}`);
|
|
299
|
+
// Same basename outside the project is not the decision file.
|
|
300
|
+
await harness.hook({
|
|
301
|
+
tool: "write",
|
|
302
|
+
input: {
|
|
303
|
+
file_path: join(root, "other", "spec", "decisions", "DECISIONS.md"),
|
|
304
|
+
content: "x",
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
// Non-file tools are not intercepted even on the decision path.
|
|
308
|
+
await harness.hook({
|
|
309
|
+
tool: "bash",
|
|
310
|
+
input: { command: "cat spec/decisions/DECISIONS.md" },
|
|
311
|
+
});
|
|
312
|
+
// Non-destructive edit shapes (no old_string) pass through.
|
|
313
|
+
await harness.hook({
|
|
314
|
+
tool: "edit",
|
|
315
|
+
input: { file_path: join(root, "spec/decisions/DECISIONS.md"), new_string: "### SB-0009 · New\n" },
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
finally {
|
|
319
|
+
await rm(root, { recursive: true, force: true });
|
|
320
|
+
}
|
|
321
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { filePathFromToolInput } from "../server/guard-hooks.js";
|
|
4
|
+
test("guard hooks read the common file-path fields", () => {
|
|
5
|
+
assert.equal(filePathFromToolInput({ file_path: "/project/src/index.ts" }), "/project/src/index.ts");
|
|
6
|
+
assert.equal(filePathFromToolInput({ filePath: "/project/src/index.ts" }), "/project/src/index.ts");
|
|
7
|
+
assert.equal(filePathFromToolInput({ path: "/project/src/index.ts" }), "/project/src/index.ts");
|
|
8
|
+
});
|
|
9
|
+
test("guard hooks ignore inputs without a usable path", () => {
|
|
10
|
+
assert.equal(filePathFromToolInput(undefined), undefined);
|
|
11
|
+
assert.equal(filePathFromToolInput({}), undefined);
|
|
12
|
+
assert.equal(filePathFromToolInput({ file_path: " " }), undefined);
|
|
13
|
+
assert.equal(filePathFromToolInput({ file_path: 42 }), undefined);
|
|
14
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { inspectProtectedPath, isProtectedPath, PROTECTED_PATH_PATTERNS } from "../server/guards.js";
|
|
4
|
+
test("generated outputs are protected", () => {
|
|
5
|
+
assert.equal(isProtectedPath("/project", "/project/generated/css/tokens.css"), true);
|
|
6
|
+
assert.equal(isProtectedPath("/project", "/project/src/components/button/button.styles.ts"), true);
|
|
7
|
+
assert.equal(isProtectedPath("/project", "/project/src/index.ts"), true);
|
|
8
|
+
assert.equal(isProtectedPath("/project", "/project/spec/tokens/foundation/color.json"), true);
|
|
9
|
+
assert.equal(isProtectedPath("/project", "/project/spec/tokens/interface.json"), true);
|
|
10
|
+
});
|
|
11
|
+
test("owned project files are not protected", () => {
|
|
12
|
+
assert.equal(isProtectedPath("/project", "/project/src/components/button/Button.ts"), false);
|
|
13
|
+
assert.equal(isProtectedPath("/project", "/project/spec/tokens/semantic/light.tokens.json"), false);
|
|
14
|
+
assert.equal(isProtectedPath("/project", "/project/spec/components/button/contract.json"), false);
|
|
15
|
+
});
|
|
16
|
+
test("paths outside the project follow the external boundary instead", () => {
|
|
17
|
+
assert.equal(isProtectedPath("/project", "/shared/generated/css/tokens.css"), false);
|
|
18
|
+
assert.equal(inspectProtectedPath("/project", "/shared/generated/css/tokens.css").pattern, undefined);
|
|
19
|
+
});
|
|
20
|
+
test("protected results name the matching pattern", () => {
|
|
21
|
+
const inspected = inspectProtectedPath("/project", "/project/generated/css/tokens.css");
|
|
22
|
+
assert.equal(inspected.protected, true);
|
|
23
|
+
assert.equal(inspected.pattern, PROTECTED_PATH_PATTERNS[0]);
|
|
24
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
const packageRoot = fileURLToPath(new URL("../..", import.meta.url));
|
|
8
|
+
const bundlePath = join(packageRoot, "dist", "gui-experimental.cjs");
|
|
9
|
+
const ensureBundle = () => {
|
|
10
|
+
if (!existsSync(bundlePath)) {
|
|
11
|
+
const built = spawnSync(process.execPath, [join(packageRoot, "scripts/build-gui.mjs")], {
|
|
12
|
+
cwd: packageRoot,
|
|
13
|
+
encoding: "utf8",
|
|
14
|
+
});
|
|
15
|
+
assert.equal(built.status, 0, `the GUI bundle failed to build: ${built.stderr}`);
|
|
16
|
+
}
|
|
17
|
+
return bundlePath;
|
|
18
|
+
};
|
|
19
|
+
test("the experimental Desktop bundle builds to a CommonJS entry", () => {
|
|
20
|
+
const path = ensureBundle();
|
|
21
|
+
const bundle = readFileSync(path, "utf8");
|
|
22
|
+
// CommonJS module shape, loadable by a CJS host.
|
|
23
|
+
assert.match(bundle, /__esModule/, "the bundle must be CommonJS, not ESM");
|
|
24
|
+
assert.doesNotMatch(bundle, /^\s*import\s/, "the bundle must not contain ESM syntax");
|
|
25
|
+
// The expected entry point: the experimental marker and a window setup.
|
|
26
|
+
assert.match(bundle, /sketchbook-desktop-experimental/);
|
|
27
|
+
assert.match(bundle, /Sketchbook \(experimental\)/);
|
|
28
|
+
assert.match(bundle, /setup/);
|
|
29
|
+
});
|
|
30
|
+
test("the gui-experimental export resolves the CommonJS bundle through a require condition", () => {
|
|
31
|
+
const manifest = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"));
|
|
32
|
+
const entry = manifest.exports["./gui-experimental"];
|
|
33
|
+
assert.ok(entry, "the package exposes ./gui-experimental");
|
|
34
|
+
assert.equal(entry.require, "./dist/gui-experimental.cjs");
|
|
35
|
+
assert.equal(entry.import, "./dist/gui.js");
|
|
36
|
+
assert.ok(existsSync(ensureBundle()));
|
|
37
|
+
assert.equal(existsSync(join(packageRoot, "dist", "gui.js")), true, "the ESM entry is also built");
|
|
38
|
+
});
|
|
39
|
+
test("the Desktop bundle is marked experimental and generated from the shared views", () => {
|
|
40
|
+
// The bundle is compiled from src/gui.tsx: the compiled text carries the
|
|
41
|
+
// markers, and the shared view sources are the compiled-in formatters.
|
|
42
|
+
const bundle = readFileSync(ensureBundle(), "utf8");
|
|
43
|
+
assert.match(bundle, /sketchbook-desktop-experimental/);
|
|
44
|
+
const source = readFileSync(join(packageRoot, "src", "gui.tsx"), "utf8");
|
|
45
|
+
assert.match(source, /EXPERIMENTAL/);
|
|
46
|
+
// Generated from the same shared sources as the TUI: the formatters live
|
|
47
|
+
// in one module both entries import.
|
|
48
|
+
const views = readFileSync(join(packageRoot, "src", "views", "operations.ts"), "utf8");
|
|
49
|
+
assert.match(views, /formatStatusOverview/);
|
|
50
|
+
assert.match(readFileSync(join(packageRoot, "src", "server", "tui.ts"), "utf8"), /views\/operations\.js/);
|
|
51
|
+
});
|
|
52
|
+
test("Desktop setup performs no engine execution and no project writes", () => {
|
|
53
|
+
// The setup body is statically free of engine or write paths: it neither
|
|
54
|
+
// spawns the engine nor writes anything — the only apply path is the
|
|
55
|
+
// preview-then-apply RPC flow shared with the TUI.
|
|
56
|
+
const source = readFileSync(join(packageRoot, "src", "gui.tsx"), "utf8");
|
|
57
|
+
assert.doesNotMatch(source, /spawn\(|execFile\(|writeFile\(|executeOperation\(|runSketchbook\(/);
|
|
58
|
+
// The only documented apply path is the shared preview → confirm → apply
|
|
59
|
+
// flow, so writes only ever happen after validation.
|
|
60
|
+
assert.match(source, /preview.*apply/s);
|
|
61
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { validateManifest, CompatibilityError } from "../shared/manifest.js";
|
|
4
|
+
const baseManifest = () => ({
|
|
5
|
+
protocolVersion: "1.0.0",
|
|
6
|
+
envelopeVersion: 1,
|
|
7
|
+
engine: { name: "@usesketchbook/cli", version: "0.9.1" },
|
|
8
|
+
groups: { meta: "Meta commands" },
|
|
9
|
+
commands: [
|
|
10
|
+
{
|
|
11
|
+
name: "capabilities",
|
|
12
|
+
group: "meta",
|
|
13
|
+
summary: "List capabilities.",
|
|
14
|
+
usage: "sketchbook capabilities",
|
|
15
|
+
args: [],
|
|
16
|
+
options: {},
|
|
17
|
+
examples: [],
|
|
18
|
+
mutates: false,
|
|
19
|
+
project: false,
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
test("validateManifest accepts a current manifest", () => {
|
|
24
|
+
const manifest = validateManifest(baseManifest());
|
|
25
|
+
assert.equal(manifest.engine.version, "0.9.1");
|
|
26
|
+
});
|
|
27
|
+
test("validateManifest rejects an unsupported protocol version", () => {
|
|
28
|
+
const manifest = baseManifest();
|
|
29
|
+
manifest.protocolVersion = "2.0.0";
|
|
30
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
31
|
+
});
|
|
32
|
+
test("validateManifest rejects an unsupported envelope version", () => {
|
|
33
|
+
const manifest = baseManifest();
|
|
34
|
+
manifest.envelopeVersion = 99;
|
|
35
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
36
|
+
});
|
|
37
|
+
test("validateManifest rejects a manifest missing engine info", () => {
|
|
38
|
+
const manifest = { ...baseManifest(), engine: { name: "", version: "" } };
|
|
39
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
40
|
+
});
|
|
41
|
+
test("validateManifest normalizes and validates every command", () => {
|
|
42
|
+
const manifest = baseManifest();
|
|
43
|
+
manifest.commands[0].risk = "read";
|
|
44
|
+
manifest.commands[0].args = [
|
|
45
|
+
{ name: "target", required: true, variadic: true, description: "Target path" },
|
|
46
|
+
];
|
|
47
|
+
manifest.commands[0].options = {
|
|
48
|
+
yes: { type: "boolean", description: "Confirm" },
|
|
49
|
+
};
|
|
50
|
+
manifest.commands[0].subcommands = [
|
|
51
|
+
{ name: "apply", usage: "capabilities apply", summary: "Apply.", mutates: true, risk: "write", examples: [] },
|
|
52
|
+
];
|
|
53
|
+
const validated = validateManifest(manifest);
|
|
54
|
+
assert.equal(validated.commands[0].risk, "read");
|
|
55
|
+
assert.equal(validated.commands[0].args[0].variadic, true);
|
|
56
|
+
assert.equal(validated.commands[0].options.yes?.type, "boolean");
|
|
57
|
+
assert.equal(validated.commands[0].subcommands?.[0].risk, "write");
|
|
58
|
+
});
|
|
59
|
+
test("validateManifest rejects malformed command metadata", () => {
|
|
60
|
+
const manifest = baseManifest();
|
|
61
|
+
manifest.commands[0].usage = "";
|
|
62
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
63
|
+
});
|
|
64
|
+
test("validateManifest rejects an invalid risk", () => {
|
|
65
|
+
const manifest = baseManifest();
|
|
66
|
+
manifest.commands[0].risk = "dangerous";
|
|
67
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
68
|
+
});
|
|
69
|
+
test("validateManifest rejects duplicate commands", () => {
|
|
70
|
+
const manifest = baseManifest();
|
|
71
|
+
manifest.commands = [...manifest.commands, ...manifest.commands];
|
|
72
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
73
|
+
});
|
|
74
|
+
test("validateManifest rejects unknown command groups", () => {
|
|
75
|
+
const manifest = baseManifest();
|
|
76
|
+
manifest.commands[0].group = "unknown";
|
|
77
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
78
|
+
});
|
|
79
|
+
test("validateManifest rejects duplicate subcommands", () => {
|
|
80
|
+
const manifest = baseManifest();
|
|
81
|
+
manifest.commands[0].subcommands = [
|
|
82
|
+
{ name: "apply", usage: "capabilities apply", summary: "Apply.", mutates: true, examples: [] },
|
|
83
|
+
{ name: "apply", usage: "capabilities apply", summary: "Apply.", mutates: true, examples: [] },
|
|
84
|
+
];
|
|
85
|
+
assert.throws(() => validateManifest(manifest), CompatibilityError);
|
|
86
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|