diff options
Diffstat (limited to 'tests/opencode')
| -rwxr-xr-x | tests/opencode/run-tests.sh | 2 | ||||
| -rw-r--r-- | tests/opencode/test-bootstrap-caching.mjs | 22 | ||||
| -rw-r--r-- | tests/opencode/test-hook-registration.mjs | 98 | ||||
| -rwxr-xr-x | tests/opencode/test-hook-registration.sh | 4 | ||||
| -rw-r--r-- | tests/opencode/test-session-bootstrap.mjs | 10 |
5 files changed, 136 insertions, 0 deletions
diff --git a/tests/opencode/run-tests.sh b/tests/opencode/run-tests.sh index 43e1a3a..4cdf546 100755 --- a/tests/opencode/run-tests.sh +++ b/tests/opencode/run-tests.sh @@ -46,6 +46,7 @@ while [[ $# -gt 0 ]]; do echo " test-plugin-loading.sh Verify plugin installation and structure" echo " test-bootstrap-caching.sh Verify bootstrap content caching" echo " test-session-bootstrap.sh Verify session classification and lookup recovery" + echo " test-hook-registration.sh Verify V2 bootstrap registers on context, compaction, generate" echo " test-skill-registration.sh Verify V2 skill registration contract (2.0.4 path field)" echo " test-tools.sh Test use_skill and find_skills tools (integration)" echo " test-priority.sh Test skill priority resolution (integration)" @@ -64,6 +65,7 @@ tests=( "test-plugin-loading.sh" "test-bootstrap-caching.sh" "test-session-bootstrap.sh" + "test-hook-registration.sh" "test-skill-registration.sh" ) diff --git a/tests/opencode/test-bootstrap-caching.mjs b/tests/opencode/test-bootstrap-caching.mjs index 386b7b1..39e4daa 100644 --- a/tests/opencode/test-bootstrap-caching.mjs +++ b/tests/opencode/test-bootstrap-caching.mjs @@ -76,6 +76,9 @@ const failures = scenario === 'present' if (scenario === 'present') { failures.push(...assertV2Bootstrap(result)); + // #2583: both flavors register bare skill ids and look them up exactly. + failures.push(...assertTeachesBareSkillIds('V1', bootstrapText(firstOutput), 'name')); + failures.push(...assertTeachesBareSkillIds('V2', v2Result.text, 'id')); } failures.push(...mappingFailures); @@ -202,6 +205,25 @@ function assertMappingConstants(mod) { return failures; } +// #2583: skills reference each other as `superpowers:<name>` (the Claude Code +// plugin namespace), but OpenCode V1 and V2 register bare ids (`brainstorming`) +// and their `skill` tools do an exact lookup, so a namespaced call fails with +// "Unable to load skill". When the injected bootstrap contains namespaced +// references, its tool mapping must tell the model to drop the prefix, using +// the flavor's `skill` parameter (V1 `name`, V2 `id`). +function assertTeachesBareSkillIds(flavor, text, param) { + const failures = []; + const refs = [...new Set(text.match(/superpowers:[a-z0-9-]+/g) || [])]; + if (refs.length === 0) return failures; + if (!text.includes('drop the `superpowers:` prefix')) { + failures.push(`expected ${flavor} bootstrap to tell the model to drop the superpowers: prefix (bootstrap references ${refs.join(', ')})`); + } + if (!text.includes(`\`superpowers:brainstorming\` → \`skill\` with \`${param}: "brainstorming"\``)) { + failures.push(`expected ${flavor} bootstrap to show superpowers:brainstorming loading as \`skill\` with \`${param}: "brainstorming"\``); + } + return failures; +} + // Drive setup() with a mock V2 ctx and fire the captured "context" hook on a // top-level (parentID-less) session. Returns the injected-part count and the // injected bootstrap text ('' when nothing was injected). diff --git a/tests/opencode/test-hook-registration.mjs b/tests/opencode/test-hook-registration.mjs new file mode 100644 index 0000000..9df6a6f --- /dev/null +++ b/tests/opencode/test-hook-registration.mjs @@ -0,0 +1,98 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import { pathToFileURL } from 'node:url'; + +// Issue #2391: the V2 plugin must register bootstrap injection on +// `context`, `compaction`, and `generate`. OpenCode dispatches each request +// shape through a different hook, so a handler on `context` only means +// compaction and generate requests diverge from the cached prefix at the +// tail — a cold re-prefill of the whole session on the largest request of +// its lifetime. + +const [, , inputPath] = process.argv; +assert.ok(inputPath, 'pass the plugin module path'); +const pluginURL = pathToFileURL(fs.realpathSync(inputPath)); +const marker = '<EXTREMELY_IMPORTANT>\nYou have superpowers.'; +let generation = 0; + +async function captureHooks(fetchSession) { + const mod = await import(`${pluginURL.href}?hooks-test=${++generation}`); + const hooks = new Map(); + await mod.default.setup({ + skill: { transform: async (transform) => transform({ add: () => {} }) }, + session: { + get: ({ sessionID }) => fetchSession(sessionID), + hook: async (name, callback) => { hooks.set(name, callback); }, + }, + }); + return hooks; +} + +function event(sessionID) { + return { + sessionID, + messages: [{ role: 'user', content: [{ type: 'text', text: 'continue' }] }], + }; +} + +const session = (id) => ({ id }); + +// All three request hooks must be registered. +for (const required of ['context', 'compaction', 'generate']) { + const hooks = await captureHooks(async (id) => session(id)); + assert.ok(hooks.has(required), `plugin registers '${required}' hook`); +} + +// One shared handler: the three hooks resolve to the same function so +// behaviour cannot drift between request shapes. +const shared = await captureHooks(async (id) => session(id)); +assert.equal( + shared.get('compaction'), + shared.get('context'), + 'compaction handler is the context handler', +); +assert.equal( + shared.get('generate'), + shared.get('context'), + 'generate handler is the context handler', +); + +// Each hook actually injects the bootstrap into an eligible session. +for (const name of ['context', 'compaction', 'generate']) { + const hooks = await captureHooks(async (id) => session(id)); + const ev = event(`${name}-root`); + await hooks.get(name)(ev); + assert.equal( + ev.messages[0].content.filter((p) => p.text?.startsWith(marker)).length, + 1, + `'${name}' hook injects bootstrap exactly once`, + ); +} + +// Child sessions stay suppressed on every hook (regression guard against +// only 'context' checking parentID while compaction/generate skip the check). +for (const name of ['context', 'compaction', 'generate']) { + const hooks = await captureHooks(async (id) => ({ id, parentID: 'parent' })); + const ev = event(`${name}-child`); + await hooks.get(name)(ev); + assert.equal( + ev.messages[0].content.filter((p) => p.text?.startsWith(marker)).length, + 0, + `'${name}' hook skips bootstrap for child sessions`, + ); +} + +// Idempotence: a second delivery of the same event does not double-inject. +for (const name of ['context', 'compaction', 'generate']) { + const hooks = await captureHooks(async (id) => session(id)); + const ev = event(`${name}-root`); + await hooks.get(name)(ev); + await hooks.get(name)(ev); + assert.equal( + ev.messages[0].content.filter((p) => p.text?.startsWith(marker)).length, + 1, + `'${name}' hook stays idempotent on repeat`, + ); +} + +console.log('V2 hook registration (context/compaction/generate) passed'); diff --git a/tests/opencode/test-hook-registration.sh b/tests/opencode/test-hook-registration.sh new file mode 100755 index 0000000..47191ef --- /dev/null +++ b/tests/opencode/test-hook-registration.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +node "$SCRIPT_DIR/test-hook-registration.mjs" "$SCRIPT_DIR/../../.opencode/plugins/superpowers.js" diff --git a/tests/opencode/test-session-bootstrap.mjs b/tests/opencode/test-session-bootstrap.mjs index 31658d5..67b3118 100644 --- a/tests/opencode/test-session-bootstrap.mjs +++ b/tests/opencode/test-session-bootstrap.mjs @@ -139,6 +139,16 @@ for (const flavor of ['v1', 'v2']) { await unknown.invoke(noID); assert.equal(bootstrapCount(noID), 1); assert.deepEqual(unknown.lookups, []); + + // #2197: a prompt that merely mentions the bootstrap's tag is not the bootstrap. + const mentions = await makeHarness(flavor, (id) => reply(flavor, { id })); + const probe = makeEvent(flavor, `${flavor}-mentions-tag`); + (probe.messages[0].parts ?? probe.messages[0].content)[0].text = + "Is there an 'EXTREMELY_IMPORTANT' block in your context?"; + await mentions.invoke(probe); + assert.equal(bootstrapCount(probe), 1, `${flavor}: prompt mentioning the tag still gets bootstrap`); + await mentions.invoke(probe); + assert.equal(bootstrapCount(probe), 1, `${flavor}: prompt mentioning the tag, repeated event`); } function compactedEvent(sessionID) { |
