startup.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, 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. afterEach(async () => {
  24. for (const dispose of disposers.splice(0)) await dispose()
  25. internals.stdout = process.stdout
  26. internals.stderr = process.stderr
  27. })
  28. /**
  29. * Mount the real provider over a runner stand-in.
  30. * @param args - the invocation's inner arguments.
  31. * @returns the resolved service value and observed runner/process effects.
  32. */
  33. async function bootStartup(args: string[]): Promise<{ task: HeadlessStartupValues | undefined; observed: Observed }> {
  34. const dir = mkdtempSync(join(tmpdir(), 'dsh-headless-startup-'))
  35. const observed: Observed = { exits: [], out: '' }
  36. writeFileSync(join(dir, 'row.mjs'), 'export function apply(_ctx, config) { globalThis.__headlessStartupObserved.runnerConfig = config }\n')
  37. // Loader imports through Node's resolver, so this fixture delegates to the
  38. // source-plane plugin already imported by the test.
  39. writeFileSync(join(dir, 'startup.mjs'), `
  40. export const name = 'headless-startup'
  41. export const inject = ['cmdlineArgs']
  42. export const apply = ctx => globalThis.__headlessStartupApply(ctx)
  43. `)
  44. const rowUrl = pathToFileURL(join(dir, 'row.mjs')).href
  45. writeFileSync(join(dir, 'cordis.yml'), [
  46. '- id: headless-runner',
  47. ` name: ${rowUrl}`,
  48. ` inject: [${HEADLESS_STARTUP_SERVICE}]`,
  49. ' config:',
  50. ' task: !!js ctx.headlessStartup.task',
  51. '- id: headless-startup',
  52. ` name: ${pathToFileURL(join(dir, 'startup.mjs')).href}`,
  53. '',
  54. ].join('\n'))
  55. const observing = { write: (chunk: string) => { observed.out += chunk; return true } }
  56. internals.stdout = observing
  57. internals.stderr = observing
  58. const globals = globalThis as unknown as {
  59. __headlessStartupApply: typeof apply
  60. __headlessStartupObserved: Observed
  61. }
  62. globals.__headlessStartupApply = apply
  63. globals.__headlessStartupObserved = observed
  64. const ctx = new Context()
  65. await ctx.plugin(Loader)
  66. ctx.loader.builtins.include = Include
  67. provideCmdline(ctx, { args, exit: code => void observed.exits.push(code) })
  68. await ctx.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(join(dir, 'cordis.yml')).href } })
  69. await ctx.loader.await()
  70. disposers.push(async () => { await ctx.fiber.dispose() })
  71. return {
  72. task: ctx.get(HEADLESS_STARTUP_SERVICE) as HeadlessStartupValues | undefined,
  73. observed,
  74. }
  75. }
  76. describe('headless command-line provider', () => {
  77. it('joins the task positional into the runner config', async () => {
  78. const { task, observed } = await bootStartup(['run', 'the', 'tests'])
  79. expect(task).toEqual({ task: 'run the tests' })
  80. expect(observed.runnerConfig).toEqual({ task: 'run the tests' })
  81. expect(observed.exits).toEqual([])
  82. })
  83. it.each([{ args: [] }, { args: [' '] }])('rejects an invocation with no non-whitespace task ($args)', async ({ args }) => {
  84. const { task, observed } = await bootStartup(args)
  85. expect(observed.out).toContain('a task is required')
  86. expect(task).toBeUndefined()
  87. expect(observed.runnerConfig).toBeUndefined()
  88. expect(observed.exits).toEqual([1])
  89. })
  90. it('prints its own help and leaves the runner pending', async () => {
  91. const { task, observed } = await bootStartup(['--help'])
  92. expect(observed.out).toContain('dsh --profile headless')
  93. expect(task).toBeUndefined()
  94. expect(observed.runnerConfig).toBeUndefined()
  95. expect(observed.exits).toEqual([0])
  96. })
  97. })