loader-smoke.spec.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { existsSync } from 'node:fs'
  2. import { readFile, writeFile } from 'node:fs/promises'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { describe, expect, it } from 'vitest'
  6. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  7. const configPath = '/tmp/fixture.cordis.yml'
  8. const tsconfigPath = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  9. const fixture = (name: string): string => fileURLToPath(new URL(`./fixtures/${name}.ts`, import.meta.url))
  10. const canonicalTempPath = (path: string): string => path.replace(/^\/private(?=\/var\/)/, '')
  11. describe('runLoaderSmoke', () => {
  12. it('isolates the process, closes stdin, captures output, and removes the cwd', async () => {
  13. const result = await runLoaderSmoke({
  14. label: 'success fixture',
  15. tempDirPrefix: 'loader-smoke-success-',
  16. binScript: fixture('success'),
  17. configPath,
  18. tsconfigPath,
  19. mode: 'src',
  20. env: { LOADER_SMOKE_MARKER: 'present' },
  21. })
  22. const output = JSON.parse(result.stdout) as {
  23. configPath: string
  24. args: string[]
  25. cwd: string
  26. dshHome: string
  27. agentsHome: string
  28. marker: string
  29. input: string
  30. }
  31. expect(output).toMatchObject({
  32. configPath,
  33. args: [configPath],
  34. marker: 'present',
  35. input: '',
  36. })
  37. expect(canonicalTempPath(output.dshHome)).toBe(canonicalTempPath(join(output.cwd, '.dsh')))
  38. expect(canonicalTempPath(output.agentsHome)).toBe(canonicalTempPath(join(output.cwd, '.agents')))
  39. expect(result.stderr).toContain('fixture stderr')
  40. expect(existsSync(output.cwd)).toBe(false)
  41. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  42. it('passes an arbitrary bin argv and inspects world state before cleanup', async () => {
  43. let inspected = ''
  44. let marker = ''
  45. const result = await runLoaderSmoke({
  46. label: 'argv fixture',
  47. tempDirPrefix: 'loader-smoke-argv-',
  48. binScript: fixture('success'),
  49. libBinScript: fixture('success'),
  50. configPath,
  51. binArgs: ['--config', configPath, '--output-format', 'json', 'task with spaces'],
  52. tsconfigPath,
  53. prepare: cwd => writeFile(join(cwd, 'marker.txt'), 'prepared'),
  54. inspect: async (cwd) => {
  55. inspected = cwd
  56. marker = await readFile(join(cwd, 'marker.txt'), 'utf8')
  57. },
  58. })
  59. const output = JSON.parse(result.stdout) as { args: string[]; cwd: string }
  60. expect(output.args).toEqual(['--config', configPath, '--output-format', 'json', 'task with spaces'])
  61. expect(canonicalTempPath(inspected)).toBe(canonicalTempPath(output.cwd))
  62. expect(marker).toBe('prepared')
  63. expect(existsSync(inspected)).toBe(false)
  64. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  65. it('rejects a non-zero exit with captured diagnostics', async () => {
  66. await expect(runLoaderSmoke({
  67. label: 'failure fixture',
  68. tempDirPrefix: 'loader-smoke-fail-',
  69. binScript: fixture('fail'),
  70. libBinScript: fixture('fail'),
  71. configPath,
  72. tsconfigPath,
  73. })).rejects.toThrow('failure fixture exited 7. stdout:\n\nstderr:\nfixture failed')
  74. })
  75. it('kills a process at its deadline and reports captured output', async () => {
  76. await expect(runLoaderSmoke({
  77. label: 'hanging fixture',
  78. tempDirPrefix: 'loader-smoke-hang-',
  79. binScript: fixture('hang'),
  80. libBinScript: fixture('hang'),
  81. configPath,
  82. tsconfigPath,
  83. processTimeoutMs: 100,
  84. })).rejects.toThrow('hanging fixture did not exit within 0.1s.')
  85. })
  86. })