keyless-smoke.e2e.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { cp, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { zstdDecompress } from 'node:zlib'
  4. import { promisify } from 'node:util'
  5. import { join } from 'node:path'
  6. import { fileURLToPath } from 'node:url'
  7. import { describe, expect, it } from 'vitest'
  8. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  9. import { PREPARED_ENTRY_FILENAME, prepareDshPlugin } from '@deepseek-ai/dsh-repository-plugin'
  10. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  11. const binScript = fileURLToPath(new URL('../../../packages/examples/cli-demo/src/bin.ts', import.meta.url))
  12. const configPath = fileURLToPath(new URL('./fixtures/cli.cordis.yml', import.meta.url))
  13. const tsconfigPath = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  14. const decompress = promisify(zstdDecompress)
  15. describe('headless-agent keyless smoke', () => {
  16. it('boots the real Loader tree, runs a real bash tool round trip, and persists the turn', async () => {
  17. let persistedHeader: Record<string, unknown> | undefined
  18. const { stdout, stderr } = await runLoaderSmoke({
  19. label: 'headless-agent',
  20. tempDirPrefix: 'headless-agent-smoke-',
  21. binScript,
  22. configPath,
  23. binArgs: ['--config', configPath, '--output-format', 'stream-json', 'prove the tool path'],
  24. tsconfigPath,
  25. inspect: async (cwd) => {
  26. const files = await readdir(cwd, { recursive: true })
  27. const relativePath = files.find(file => file.endsWith('.jsonl.zstd'))
  28. if (relativePath === undefined) return
  29. const compressed = await readFile(join(cwd, relativePath))
  30. expect(compressed.subarray(0, 4).toString('hex')).toBe('28b52ffd')
  31. persistedHeader = JSON.parse((await decompress(compressed)).toString()) as Record<string, unknown>
  32. },
  33. })
  34. const lines = stdout.trimEnd().split('\n').map(line => JSON.parse(line) as Record<string, unknown>)
  35. const events = lines.slice(0, -1).map(line => line['event'] as SessionEvent)
  36. const result = lines.at(-1)
  37. expect(stderr).toBe('')
  38. expect(events.some(event => event.type === 'tool/call' && event.data.name === 'bash')).toBe(true)
  39. const catalogMessage = events.find(event => event.type === 'user/message'
  40. && event.data.source.kind === 'skill-catalog')
  41. const catalog = catalogMessage?.type === 'user/message'
  42. ? catalogMessage.data.content.filter(block => block.type === 'text').map(block => block.text).join('\n')
  43. : ''
  44. expect(catalog.split('\n').find(line => line.includes('repository-fixture'))).toMatchInlineSnapshot(
  45. `
  46. "- \`repository-fixture\`: Repository fixture skill."
  47. `,
  48. )
  49. const toolResult = events.find(event => event.type === 'tool/result')
  50. expect(JSON.stringify(toolResult)).toContain('CLI_TOOL_ROUND_TRIP')
  51. expect(result).toMatchObject({
  52. type: 'result',
  53. usage: { inputTokens: 18, outputTokens: 8, cacheReadTokens: 2, reasoningTokens: 1 },
  54. })
  55. expect(String(result?.['output'])).toContain('CLI_TOOL_ROUND_TRIP')
  56. expect(persistedHeader).toMatchObject({ type: 'session' })
  57. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  58. it('keeps the checked-in prepared wrapper identical to the generator output for its manifest', async () => {
  59. // The fixture claims "Generated by dsh-plugin-prepare"; this pin makes the
  60. // claim true — a wrapper-template change fails here until the fixture is
  61. // regenerated, so the assembled smoke can never exercise a stale shape.
  62. const fixture = fileURLToPath(new URL('./fixtures/repository-plugin/', import.meta.url))
  63. const root = await mkdtemp(join(tmpdir(), 'dsh-fixture-drift-'))
  64. try {
  65. const plugin = join(root, '.dsh-plugin')
  66. await mkdir(plugin, { recursive: true })
  67. await cp(join(fixture, 'dsh-plugin-assets/skills/0'), join(root, 'skills'), { recursive: true })
  68. await writeFile(join(plugin, 'package.json'), `${JSON.stringify({
  69. name: 'headless-repository-fixture',
  70. version: '0.0.0',
  71. dsh: { skills: ['../skills'] },
  72. }, undefined, 2)}\n`)
  73. await prepareDshPlugin(plugin)
  74. const generated = await readFile(join(plugin, PREPARED_ENTRY_FILENAME), 'utf8')
  75. const checkedIn = await readFile(join(fixture, PREPARED_ENTRY_FILENAME), 'utf8')
  76. expect(checkedIn).toBe(generated)
  77. } finally {
  78. await rm(root, { recursive: true, force: true })
  79. }
  80. })
  81. })