startup.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * The Web command-line provider over a real Loader tree: its ordinary service
  3. * releases a consumer whose config reads `ctx.webStartup` directly.
  4. */
  5. import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  6. import { tmpdir } from 'node:os'
  7. import { join } from 'node:path'
  8. import { pathToFileURL } from 'node:url'
  9. import { Context } from '@deepseek-ai/cordis'
  10. import Loader from '@deepseek-ai/cordis-plugin-loader'
  11. import Include from '@deepseek-ai/cordis-plugin-include'
  12. import { internals, provideCmdline } from '@deepseek-ai/dsh-cmdline'
  13. import { afterEach, describe, expect, it } from 'vitest'
  14. import { apply, WEB_STARTUP_SERVICE, type WebStartupValues } from '../src/startup.ts'
  15. /** What one fixture boot observed. */
  16. interface Observed {
  17. exits: number[]
  18. out: string
  19. readerConfig?: unknown
  20. }
  21. const disposers: (() => Promise<void>)[] = []
  22. /** Fixture tree roots, removed after their booted tree has been disposed. */
  23. const tempDirs: string[] = []
  24. afterEach(async () => {
  25. for (const dispose of disposers.splice(0)) await dispose()
  26. for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true })
  27. internals.stdout = process.stdout
  28. internals.stderr = process.stderr
  29. })
  30. /**
  31. * Mount the real provider and a consumer using injection-ordered config.
  32. * @param args - the invocation's inner arguments.
  33. * @returns the service value and observed consumer/process effects.
  34. */
  35. async function bootProvider(args: string[]): Promise<{
  36. values: WebStartupValues | undefined
  37. observed: Observed
  38. }> {
  39. const dir = mkdtempSync(join(tmpdir(), 'dsh-web-startup-'))
  40. tempDirs.push(dir)
  41. const observed: Observed = { exits: [], out: '' }
  42. writeFileSync(join(dir, 'reader.mjs'), `
  43. export function apply(_ctx, config) { globalThis.__webStartupObserved.readerConfig = config }
  44. `)
  45. // Node imports the fixture row outside Vite's source resolver, so delegate
  46. // to the source-plane plugin already imported by this test.
  47. writeFileSync(join(dir, 'provider.mjs'), `
  48. export const name = 'web-startup'
  49. export const inject = ['cmdlineArgs']
  50. export const apply = ctx => globalThis.__webStartupApply(ctx)
  51. `)
  52. writeFileSync(join(dir, 'cordis.yml'), [
  53. '- id: reader',
  54. ` name: ${pathToFileURL(join(dir, 'reader.mjs')).href}`,
  55. ` inject: [${WEB_STARTUP_SERVICE}]`,
  56. ' config:',
  57. " host: !!js ctx.webStartup.host ?? '127.0.0.1'",
  58. ' openBrowser: !!js ctx.webStartup.openBrowser',
  59. ' port: !!js ctx.webStartup.port ?? 3080',
  60. ' trustedHosts: !!js ctx.webStartup.trustedHosts',
  61. '- id: provider',
  62. ` name: ${pathToFileURL(join(dir, 'provider.mjs')).href}`,
  63. '',
  64. ].join('\n'))
  65. const observing = { write: (chunk: string) => { observed.out += chunk; return true } }
  66. internals.stdout = observing
  67. internals.stderr = observing
  68. const globals = globalThis as unknown as {
  69. __webStartupApply: typeof apply
  70. __webStartupObserved: Observed
  71. }
  72. globals.__webStartupApply = apply
  73. globals.__webStartupObserved = observed
  74. const ctx = new Context()
  75. await ctx.plugin(Loader)
  76. ctx.loader.builtins.include = Include
  77. provideCmdline(ctx, { args, exit: code => void observed.exits.push(code) })
  78. await ctx.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(join(dir, 'cordis.yml')).href } })
  79. await ctx.loader.await()
  80. disposers.push(async () => { await ctx.fiber.dispose() })
  81. return {
  82. values: ctx.get(WEB_STARTUP_SERVICE) as WebStartupValues | undefined,
  83. observed,
  84. }
  85. }
  86. describe('web command-line provider', () => {
  87. it('publishes each flag and releases direct service expressions', async () => {
  88. const { values, observed } = await bootProvider([
  89. '--host', '127.0.0.1',
  90. '--no-open',
  91. '--port', '8080',
  92. '--trusted-host', 'lab.internal', 'lab-2.internal',
  93. '--trusted-host', '10.0.0.9',
  94. ])
  95. expect(values).toEqual({
  96. host: '127.0.0.1',
  97. openBrowser: false,
  98. port: 8080,
  99. trustedHosts: ['lab.internal', 'lab-2.internal', '10.0.0.9'],
  100. })
  101. expect(observed.readerConfig).toEqual(values)
  102. expect(observed.exits).toEqual([])
  103. })
  104. it('leaves deployment values to each consumer when flags omit them', async () => {
  105. const { values, observed } = await bootProvider([])
  106. expect(values).toEqual({ openBrowser: true, trustedHosts: [] })
  107. expect(observed.readerConfig).toEqual({
  108. host: '127.0.0.1',
  109. openBrowser: true,
  110. port: 3080,
  111. trustedHosts: [],
  112. })
  113. })
  114. it('prints its own help and leaves the consumer pending', async () => {
  115. const { values, observed } = await bootProvider(['--help'])
  116. expect(observed.out).toContain('dsh --profile web')
  117. expect(observed.out).toContain('--no-open')
  118. expect(observed.out).toContain('--trusted-host')
  119. expect(values).toBeUndefined()
  120. expect(observed.readerConfig).toBeUndefined()
  121. expect(observed.exits).toEqual([0])
  122. })
  123. it('rejects a non-numeric port before the consumer activates', async () => {
  124. const { values, observed } = await bootProvider(['--port', 'abc'])
  125. expect(observed.out).toContain('--port must be a number')
  126. expect(values).toBeUndefined()
  127. expect(observed.readerConfig).toBeUndefined()
  128. expect(observed.exits).toEqual([1])
  129. })
  130. it('rejects the intentionally unsupported all-interfaces host before the consumer activates', async () => {
  131. const { values, observed } = await bootProvider(['--host', '0.0.0.0'])
  132. expect(observed.out).toContain('--host 0.0.0.0 is intentionally not supported yet for safety: it would expose remote code execution to the network; use 127.0.0.1 instead')
  133. expect(values).toBeUndefined()
  134. expect(observed.readerConfig).toBeUndefined()
  135. expect(observed.exits).toEqual([1])
  136. })
  137. })