startup.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * The one-shot app's ordinary command-line provider over a real Loader tree:
  3. * the task becomes injected runner config, while help and usage errors leave
  4. * the consumer pending.
  5. */
  6. import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  7. import { tmpdir } from 'node:os'
  8. import { join } from 'node:path'
  9. import { pathToFileURL } from 'node:url'
  10. import { Context } from '@deepseek-ai/cordis'
  11. import Loader from '@deepseek-ai/cordis-plugin-loader'
  12. import Include from '@deepseek-ai/cordis-plugin-include'
  13. import { internals, provideCmdline } from '@deepseek-ai/dsh-cmdline'
  14. import { afterEach, describe, expect, it } from 'vitest'
  15. import { apply, HEADLESS_STARTUP_SERVICE, type HeadlessStartupValues } from '../src/startup.ts'
  16. /** What one boot of the fixture tree observed. */
  17. interface Observed {
  18. exits: number[]
  19. out: string
  20. runnerConfig?: unknown
  21. }
  22. const disposers: (() => Promise<void>)[] = []
  23. /** Fixture tree roots, removed after their booted tree has been disposed. */
  24. const tempDirs: string[] = []
  25. afterEach(async () => {
  26. for (const dispose of disposers.splice(0)) await dispose()
  27. for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true })
  28. internals.stdout = process.stdout
  29. internals.stderr = process.stderr
  30. })
  31. /**
  32. * Mount the real provider over a runner stand-in.
  33. * @param args - the invocation's inner arguments.
  34. * @returns the resolved service value and observed runner/process effects.
  35. */
  36. async function bootStartup(args: string[]): Promise<{ task: HeadlessStartupValues | undefined; observed: Observed }> {
  37. const dir = mkdtempSync(join(tmpdir(), 'dsh-headless-startup-'))
  38. tempDirs.push(dir)
  39. const observed: Observed = { exits: [], out: '' }
  40. writeFileSync(join(dir, 'row.mjs'), 'export function apply(_ctx, config) { globalThis.__headlessStartupObserved.runnerConfig = config }\n')
  41. // Loader imports through Node's resolver, so this fixture delegates to the
  42. // source-plane plugin already imported by the test.
  43. writeFileSync(join(dir, 'startup.mjs'), `
  44. export const name = 'headless-startup'
  45. export const inject = ['cmdlineArgs']
  46. export const apply = ctx => globalThis.__headlessStartupApply(ctx)
  47. `)
  48. const rowUrl = pathToFileURL(join(dir, 'row.mjs')).href
  49. writeFileSync(join(dir, 'cordis.yml'), [
  50. '- id: headless-runner',
  51. ` name: ${rowUrl}`,
  52. ` inject: [${HEADLESS_STARTUP_SERVICE}]`,
  53. ' config:',
  54. ' task: !!js ctx.headlessStartup.task',
  55. '- id: headless-startup',
  56. ` name: ${pathToFileURL(join(dir, 'startup.mjs')).href}`,
  57. '',
  58. ].join('\n'))
  59. const observing = { write: (chunk: string) => { observed.out += chunk; return true } }
  60. internals.stdout = observing
  61. internals.stderr = observing
  62. const globals = globalThis as unknown as {
  63. __headlessStartupApply: typeof apply
  64. __headlessStartupObserved: Observed
  65. }
  66. globals.__headlessStartupApply = apply
  67. globals.__headlessStartupObserved = observed
  68. const ctx = new Context()
  69. await ctx.plugin(Loader)
  70. ctx.loader.builtins.include = Include
  71. provideCmdline(ctx, { args, exit: code => void observed.exits.push(code) })
  72. await ctx.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(join(dir, 'cordis.yml')).href } })
  73. await ctx.loader.await()
  74. disposers.push(async () => { await ctx.fiber.dispose() })
  75. return {
  76. task: ctx.get(HEADLESS_STARTUP_SERVICE) as HeadlessStartupValues | undefined,
  77. observed,
  78. }
  79. }
  80. describe('headless command-line provider', () => {
  81. it('joins the task positional into the runner config', async () => {
  82. const { task, observed } = await bootStartup(['run', 'the', 'tests'])
  83. expect(task).toEqual({ task: 'run the tests' })
  84. expect(observed.runnerConfig).toEqual({ task: 'run the tests' })
  85. expect(observed.exits).toEqual([])
  86. })
  87. it.each([{ args: [] }, { args: [' '] }])('rejects an invocation with no non-whitespace task ($args)', async ({ args }) => {
  88. const { task, observed } = await bootStartup(args)
  89. expect(observed.out).toContain('a task is required')
  90. expect(task).toBeUndefined()
  91. expect(observed.runnerConfig).toBeUndefined()
  92. expect(observed.exits).toEqual([1])
  93. })
  94. it('prints its own help and leaves the runner pending', async () => {
  95. const { task, observed } = await bootStartup(['--help'])
  96. expect(observed.out).toContain('dsh --profile headless')
  97. expect(observed.out).toContain('stream reasoning to stderr')
  98. expect(task).toBeUndefined()
  99. expect(observed.runnerConfig).toBeUndefined()
  100. expect(observed.exits).toEqual([0])
  101. })
  102. })