claude-harness/dsh/tests/bootstrap.test.mjs

91 lines
3.3 KiB
JavaScript

import assert from 'node:assert/strict'
import { join } from 'node:path'
import test from 'node:test'
import {
buildBootstrapText,
isBootstrapVisible,
prepareBootstrap,
resolveBootstrapSkill,
stripFrontmatter,
truncateUtf8,
} from '../bundle/lib/bootstrap.js'
import { cleanup, tempDir, write } from './helpers.mjs'
test('stripFrontmatter removes a YAML header', () => {
assert.equal(stripFrontmatter('---\nname: using-superpowers\n---\nbody\n'), 'body')
assert.equal(stripFrontmatter('body\n'), 'body')
})
test('truncateUtf8 respects the byte budget', () => {
const out = truncateUtf8(`<!-- marker -->\n${'x'.repeat(5000)}`, 2000)
assert.ok(Buffer.byteLength(out, 'utf8') <= 2000)
assert.match(out, /bootstrap truncated/)
assert.match(out, /^<!-- marker -->/)
})
test('prepareBootstrap builds a marker-first message with the mapping', () => {
const dir = tempDir()
try {
const skillPath = join(dir, 'SKILL.md')
const mappingPath = join(dir, 'dsh-tools.md')
write(skillPath, '---\nname: using-superpowers\ndescription: test\n---\n# Use skills\nNever skip the skill check.\n')
write(mappingPath, '| Read a file | read |\n')
const prepared = prepareBootstrap({ skillPath, mappingPath, maxBytes: 65_536 })
assert.ok(prepared)
assert.match(prepared.marker, /^<!-- DSH-HARNESS-BOOTSTRAP:[0-9a-f]{16} -->$/)
assert.ok(prepared.text.startsWith(prepared.marker))
assert.match(prepared.text, /Never skip the skill check/)
assert.match(prepared.text, /\| Read a file \| read \|/)
assert.doesNotMatch(prepared.text, /^---/)
assert.match(prepared.text, /EXTREMELY_IMPORTANT/)
} finally {
cleanup(dir)
}
})
test('buildBootstrapText is deterministic for the same content', () => {
const args = {
skillBody: '# Skill',
mapping: '| Read | read |',
marker: '<!-- DSH-HARNESS-BOOTSTRAP:0000000000000000 -->',
}
assert.equal(buildBootstrapText(args), buildBootstrapText(args))
})
test('isBootstrapVisible sees the marker in messages and in the visible surface', () => {
const marker = '<!-- DSH-HARNESS-BOOTSTRAP:abc -->'
assert.equal(isBootstrapVisible({}, [{ content: [{ type: 'text', text: marker }] }], marker), true)
assert.equal(isBootstrapVisible({}, [{ content: [{ type: 'text', text: 'hello' }] }], marker), false)
const event = { type: 'user/message', data: { content: [{ type: 'text', text: `prefix ${marker}` }] } }
const visibleAgent = {
session: {
seq: 2,
surface: { nodes: new Set([1]) },
eventAt: (seq) => (seq === 1 ? event : undefined),
},
}
assert.equal(isBootstrapVisible(visibleAgent, [], marker), true)
const compactedAgent = {
session: { seq: 3, surface: { nodes: new Set() }, eventAt: () => undefined },
}
assert.equal(isBootstrapVisible(compactedAgent, [], marker), false)
})
test('resolveBootstrapSkill prefers an explicit path and finds the user root', () => {
const home = tempDir()
try {
const installed = join(home, '.dsh', 'skills', 'using-superpowers', 'SKILL.md')
write(installed, '---\nname: using-superpowers\n---\nbody\n')
assert.equal(resolveBootstrapSkill({ home, env: {} }), installed)
const explicit = join(home, 'explicit.md')
write(explicit, 'explicit\n')
assert.equal(resolveBootstrapSkill({ configuredPath: explicit, home, env: {} }), explicit)
} finally {
cleanup(home)
}
})