summaryrefslogtreecommitdiff
path: root/tests/opencode/test-skill-registration.mjs
blob: a31fe4abdd4cd8d92d3c84e0f3d0041bb93edcbc (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import fs from 'fs';
import os from 'os';
import path from 'path';
import { pathToFileURL } from 'url';

// Verifies the V2 skill registration payload matches OpenCode 2.0.4's
// Skill.Info contract (packages/schema/src/skill.ts):
//   { id, name, description?, autoinvoke?, path, content }
// Upstream commit 199aabe9e2 (first released in v2.0.4) renamed the required
// file field `location` -> `path`. A wrong field name makes draft.add()
// throw inside the host's transform rebuild, which asynchronously disables
// the whole plugin ("Plugin disabled after skill.transform failed") and
// takes the bootstrap hook down with it — see PR #2106 review by 80avin.

const [, , inputPath] = process.argv;

if (!inputPath) {
  console.error('Usage: node test-skill-registration.mjs PLUGIN_PATH');
  process.exit(2);
}

const pluginPath = fs.realpathSync(inputPath);
const skillsDir = path.resolve(path.dirname(pluginPath), '../../skills');
const mod = await import(pathToFileURL(pluginPath).href);

const failures = [];

// --- Run 1: passive capture of every draft.add payload -------------------
const added = [];
await mod.default.setup(makeCtx({ add: (skill) => added.push(skill) }));

const expectedIds = fs.existsSync(skillsDir)
  ? fs.readdirSync(skillsDir, { withFileTypes: true })
      .filter((e) => e.isDirectory() && !e.name.startsWith('.'))
      .filter((e) => fs.existsSync(path.join(skillsDir, e.name, 'SKILL.md')))
      .map((e) => e.name)
      .sort()
  : [];

if (added.length === 0) {
  failures.push('expected setup() to register at least one skill via draft.add()');
}
if (JSON.stringify(added.map((s) => s.id).sort()) !== JSON.stringify(expectedIds)) {
  failures.push(`expected draft.add() ids to match skills dir contents, got ${JSON.stringify(added.map((s) => s.id))}`);
}

for (const skill of added) {
  if (typeof skill.path !== 'string' || !path.isAbsolute(skill.path)) {
    failures.push(`skill "${skill.id}": expected required absolute Skill.Info field "path", got ${JSON.stringify(skill.path)}`);
  } else if (skill.path !== path.join(skillsDir, skill.id, 'SKILL.md')) {
    failures.push(`skill "${skill.id}": expected path ${path.join(skillsDir, skill.id, 'SKILL.md')}, got ${skill.path}`);
  } else if (!fs.existsSync(skill.path)) {
    failures.push(`skill "${skill.id}": path does not exist on disk: ${skill.path}`);
  }
  // Stale 2.0.3-era fields must not leak into the payload: the host strips
  // unknown keys, but keeping them would silently mask a future regression
  // to a schema that no longer accepts `path`.
  if ('location' in skill) {
    failures.push(`skill "${skill.id}": payload still carries the pre-2.0.4 field "location"`);
  }
  if ('slash' in skill) {
    failures.push(`skill "${skill.id}": payload carries "slash", removed from Skill.Info in 2.0.4`);
  }
  if (typeof skill.id !== 'string' || skill.id.length === 0) failures.push(`skill payload missing non-empty "id"`);
  if (typeof skill.name !== 'string' || skill.name.length === 0) failures.push(`skill "${skill.id}" missing non-empty "name"`);
  if (typeof skill.content !== 'string' || !skill.content.trim()) failures.push(`skill "${skill.id}" missing non-empty "content"`);
  if ('description' in skill && typeof skill.description !== 'string') {
    failures.push(`skill "${skill.id}": "description" must be a string when present`);
  } else if ('description' in skill && /["']$/.test(skill.description)) {
    failures.push(`skill "${skill.id}": description ends with a dangling quote: ${JSON.stringify(skill.description)}`);
  }
  if (typeof skill.content === 'string' && skill.content.startsWith('---')) {
    failures.push(`skill "${skill.id}": content still starts with the frontmatter delimiter`);
  }
}

// --- Run 2: hostile draft.add must not abort the remaining registrations --
// The real host swallows a throw escaping the transform callback and then
// hard-disables the plugin asynchronously. Locally we can only observe the
// synchronous half of that contract: when draft.add() rejects one skill, the
// plugin must keep registering the rest instead of aborting the loop.
const hostileId = added.length > 1 ? added[Math.floor(added.length / 2)].id : null;
const survived = [];
let setupThrew = null;
let survivingContextHook;
try {
  await mod.default.setup(makeCtx({
    add: (skill) => {
      if (skill.id === hostileId) throw new Error('Simulated Skill.Info decode failure');
      survived.push(skill.id);
    },
    onHook: (name, callback) => {
      if (name === 'context') survivingContextHook = callback;
    },
  }));
} catch (err) {
  setupThrew = err;
}
if (setupThrew) {
  failures.push(`expected setup() to contain draft.add() failures, but it threw: ${setupThrew.message}`);
} else if (hostileId) {
  const expectedSurvivors = added.map((s) => s.id).filter((id) => id !== hostileId);
  if (JSON.stringify(survived.sort()) !== JSON.stringify(expectedSurvivors.sort())) {
    failures.push(`expected all non-rejected skills to still register when one draft.add() throws, got ${JSON.stringify(survived)}`);
  }
}
if (typeof survivingContextHook !== 'function') {
  failures.push('expected bootstrap hook to survive a rejected skill');
} else {
  const event = {
    sessionID: 'registration-survival-root',
    messages: [{ role: 'user', content: [{ type: 'text', text: 'Continue' }] }],
  };
  await survivingContextHook(event);
  const count = event.messages.flatMap((message) => message.content).filter(
    (part) => part.type === 'text' && part.text.startsWith('<EXTREMELY_IMPORTANT>\nYou have superpowers.')
  ).length;
  if (count !== 1) failures.push(`expected surviving bootstrap once, got ${count}`);
}

// --- Run 3: quoted and multi-line frontmatter values ---------------------
// The description is what the host shows in its skill list. A quoted value
// that wraps onto indented continuation lines must register as one unquoted
// line, so exercise each layout against a synthetic install: a copy of the
// plugin next to fixture skills, laid out like a real package root.
const frontmatterFixtures = {
  'multi-line-double': {
    frontmatter: 'description: "Use when foo happens\n  and bar continues\n  and baz ends"',
    expected: 'Use when foo happens and bar continues and baz ends',
  },
  'multi-line-single': {
    frontmatter: "description: 'Use when foo happens\n  and bar continues\n  and baz ends'",
    expected: 'Use when foo happens and bar continues and baz ends',
  },
  'single-line-quoted': {
    frontmatter: 'description: "Plain quoted"',
    expected: 'Plain quoted',
  },
  'block-scalar': {
    frontmatter: 'description: >\n  Folded line one\n  line two',
    expected: 'Folded line one line two',
  },
};
const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'superpowers-frontmatter-'));
try {
  const fixturePlugin = path.join(fixtureRoot, '.opencode', 'plugins', 'superpowers.js');
  fs.mkdirSync(path.dirname(fixturePlugin), { recursive: true });
  fs.copyFileSync(pluginPath, fixturePlugin);
  for (const [id, { frontmatter }] of Object.entries(frontmatterFixtures)) {
    const skillDir = path.join(fixtureRoot, 'skills', id);
    fs.mkdirSync(skillDir, { recursive: true });
    fs.writeFileSync(path.join(skillDir, 'SKILL.md'), `---\nname: ${id}\n${frontmatter}\n---\n# Title\n\nBody.\n`);
  }
  const fixtureMod = await import(pathToFileURL(fixturePlugin).href);
  const fixtureAdded = [];
  await fixtureMod.default.setup(makeCtx({ add: (skill) => fixtureAdded.push(skill) }));
  for (const [id, { expected }] of Object.entries(frontmatterFixtures)) {
    const skill = fixtureAdded.find((s) => s.id === id);
    if (!skill) {
      failures.push(`fixture "${id}": expected setup() to register it`);
      continue;
    }
    if (skill.description !== expected) {
      failures.push(`fixture "${id}": expected description ${JSON.stringify(expected)}, got ${JSON.stringify(skill.description)}`);
    }
    if (skill.content.startsWith('---')) {
      failures.push(`fixture "${id}": content still starts with the frontmatter delimiter`);
    }
  }
} finally {
  fs.rmSync(fixtureRoot, { recursive: true, force: true });
}

const result = {
  registered: added.length,
  ids: added.map((s) => s.id),
  allPathsValid: added.every((s) => s.path === path.join(skillsDir, s.id, 'SKILL.md') && fs.existsSync(s.path)),
  staleLocationField: added.some((s) => 'location' in s),
  hostileRejectedId: hostileId,
  survivedHostileAdd: JSON.stringify(survived.sort()) === JSON.stringify(added.map((s) => s.id).filter((id) => id !== hostileId).sort()),
};

if (failures.length > 0) {
  console.error(JSON.stringify(result, null, 2));
  for (const failure of failures) {
    console.error(`FAIL: ${failure}`);
  }
  process.exit(1);
}

console.log(JSON.stringify(result, null, 2));

function makeCtx({ add, onHook = () => {} }) {
  return {
    skill: {
      transform: async (fn) => {
        await fn({ list: () => [], get: () => undefined, add, update: () => {}, remove: () => {} });
      },
    },
    session: {
      hook: async (name, callback) => onHook(name, callback),
      get: async ({ sessionID }) => ({ id: sessionID }), // top-level: no parentID
    },
  };
}