patcher.test.ts
2442 bytes
1import { describe, it } from "node:test";
2import assert from "node:assert/strict";
3import { SimpleFrontmatterCodec } from "./parser.ts";
4import { MinimalFrontmatterPatcher } from "./patcher.ts";
5import { getDuplicateFrontmatterKeys } from "./validation.ts";
6
7const codec = new SimpleFrontmatterCodec();
8const patcher = new MinimalFrontmatterPatcher();
9
10describe("MinimalFrontmatterPatcher", () => {
11 it("does not add a second disable-model-invocation key when a skill is already manual-only", () => {
12 const raw = [
13 "---",
14 "name: handoff",
15 "description: Compact the conversation.",
16 "disable-model-invocation: true",
17 "argument-hint: What next?",
18 "---",
19 "",
20 "# Handoff",
21 "",
22 ].join("\n");
23
24 const patch = patcher.patchInvocationMode(codec.parse(raw), "manual-only");
25
26 assert.equal(patch.newText, raw);
27 assert.equal(countDisableKeys(patch.newText), 1);
28 });
29
30 it("collapses duplicated disable-model-invocation keys when setting manual-only", () => {
31 const raw = [
32 "---",
33 "name: handoff",
34 "description: Compact the conversation.",
35 "disable-model-invocation: true",
36 "argument-hint: What next?",
37 "disable-model-invocation: true",
38 "---",
39 "",
40 "# Handoff",
41 "",
42 ].join("\n");
43
44 const patch = patcher.patchInvocationMode(codec.parse(raw), "manual-only");
45
46 assert.equal(countDisableKeys(patch.newText), 1);
47 assert.deepEqual(getDuplicateFrontmatterKeys(codec.parse(patch.newText)), []);
48 assert.ok(patch.newText.includes("disable-model-invocation: true\nargument-hint: What next?\n---"));
49 });
50
51 it("removes all disable-model-invocation keys when setting agent-invocable", () => {
52 const raw = [
53 "---",
54 "name: handoff",
55 "description: Compact the conversation.",
56 "disable-model-invocation: true",
57 "argument-hint: What next?",
58 "disable-model-invocation: true",
59 "---",
60 "",
61 "# Handoff",
62 "",
63 ].join("\n");
64
65 const patch = patcher.patchInvocationMode(codec.parse(raw), "agent-invocable");
66
67 assert.equal(countDisableKeys(patch.newText), 0);
68 assert.deepEqual(getDuplicateFrontmatterKeys(codec.parse(patch.newText)), []);
69 assert.ok(patch.newText.includes("argument-hint: What next?\n---"));
70 });
71});
72
73function countDisableKeys(raw: string): number {
74 return (raw.match(/^disable-model-invocation\s*:/gm) ?? []).length;
75}