deepseek-harness/scripts/verify-cordis-config.spec.ts

130 lines
5.2 KiB
TypeScript

/**
* The verify-cordis-config metadata contract: `disabled` is the one entry
* metadata field whose `!!js` expression the Loader interpolates; every other
* metadata field must stay static, and a disabled expression must parse.
*/
import { globSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
bundleManifestPaths,
bundlePluginDependencyErrors,
metadataExpressionErrors,
} from './verify-cordis-config.ts'
interface WorkspaceManifest {
name?: string
dependencies?: Record<string, string>
optionalDependencies?: Record<string, string>
peerDependencies?: Record<string, string>
}
const repoRoot = fileURLToPath(new URL('..', import.meta.url))
function productionClosure(entry: string): Set<string> {
const manifests = new Map<string, WorkspaceManifest>()
for (const path of globSync(['apps/*/package.json', 'packages/*/*/package.json'], { cwd: repoRoot })) {
const manifest = JSON.parse(readFileSync(join(repoRoot, path), 'utf8')) as WorkspaceManifest
if (manifest.name !== undefined) manifests.set(manifest.name, manifest)
}
const visited = new Set<string>()
const pending = [entry]
for (let name = pending.pop(); name !== undefined; name = pending.pop()) {
if (visited.has(name)) continue
visited.add(name)
const manifest = manifests.get(name)
pending.push(
...Object.keys(manifest?.dependencies ?? {}),
...Object.keys(manifest?.optionalDependencies ?? {}),
...Object.keys(manifest?.peerDependencies ?? {}),
)
}
return visited
}
describe('verify-cordis-config metadata expressions', () => {
it('accepts a disabled !!js expression', () => {
const problems = metadataExpressionErrors(
{ id: 'tool-bash', name: '@deepseek-ai/dsh-tool-bash', disabled: { __jsExpr: "process.platform === 'win32'" } },
'[0]',
)
expect(problems).toEqual([])
})
it('rejects an expression in a static metadata field', () => {
const problems = metadataExpressionErrors({ id: { __jsExpr: 'process.platform' }, name: 'pkg' }, '[0]')
expect(problems).toContain('[0].id: !!js is not interpolated here')
})
it('rejects an expression nested below disabled (only the field itself interpolates)', () => {
const problems = metadataExpressionErrors(
{ id: 'tool-bash', name: 'pkg', disabled: { when: { __jsExpr: 'process.platform' } } },
'[0]',
)
expect(problems).toContain('[0].disabled.when: !!js is not interpolated here')
})
it('rejects a disabled expression that does not parse (the loader would fail the boot)', () => {
const problems = metadataExpressionErrors(
{ id: 'tool-bash', name: 'pkg', disabled: { __jsExpr: 'process.platform ===' } },
'[0]',
)
expect(problems.some(problem => problem.includes('[0].disabled: disabled expression does not parse'))).toBe(true)
})
})
describe('workspace Bundle discovery and product dependency closures', () => {
it('discovers a Bundle outside packages/bundle from its manifest declaration', () => {
const fixture = mkdtempSync(join(tmpdir(), 'dsh-bundle-discovery-'))
try {
const bundleDir = join(fixture, 'packages/subagent/example')
const plainDir = join(fixture, 'packages/bundle/plain')
mkdirSync(bundleDir, { recursive: true })
mkdirSync(plainDir, { recursive: true })
writeFileSync(join(bundleDir, 'package.json'), JSON.stringify({
name: '@deepseek-ai/dsh-subagent-example',
dsh: { bundle: { patch: './cordis.patch.yml' } },
}))
writeFileSync(join(plainDir, 'package.json'), JSON.stringify({
name: '@deepseek-ai/dsh-plain',
}))
expect(bundleManifestPaths(fixture)).toEqual([
'packages/subagent/example/package.json',
])
} finally {
rmSync(fixture, { recursive: true, force: true })
}
})
it('allows a Bundle to mount itself but rejects an undeclared plugin package', () => {
const manifestPath = 'packages/subagent/example/package.json'
const file = 'packages/subagent/example/cordis.patch.yml'
const manifest = {
name: '@deepseek-ai/dsh-subagent-example',
dependencies: {},
}
const self = { file, name: '@deepseek-ai/dsh-subagent-example' }
expect(bundlePluginDependencyErrors(manifestPath, manifest, [self])).toEqual([])
expect(bundlePluginDependencyErrors(manifestPath, manifest, [
self,
{ file, name: '@deepseek-ai/dsh-missing-plugin' },
])).toEqual([
`${file}: @deepseek-ai/dsh-missing-plugin must be declared in ${manifestPath} dependencies`,
])
})
it('keeps the default and optional Claude Code closure independent', () => {
const shipped = productionClosure('@deepseek-ai/dsh')
expect(shipped).not.toContain('@deepseek-ai/dsh-subagent-codex')
expect(shipped).not.toContain('@deepseek-ai/dsh-subagent-claude-code')
expect(shipped).not.toContain('@anthropic-ai/claude-agent-sdk')
const claudeCode = productionClosure('@deepseek-ai/dsh-subagent-claude-code')
expect(claudeCode).toContain('@anthropic-ai/claude-agent-sdk')
expect(claudeCode).not.toContain('@deepseek-ai/dsh-subagent-codex')
})
})