example-launch.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import {
  3. EXAMPLE_MODE_ENV,
  4. resolveExampleLaunch,
  5. resolveExampleMode,
  6. } from '@deepseek-ai/dsh-loader-smoke'
  7. const SRC_BIN = '/repo/apps/cli/src/bin.ts'
  8. const TSCONFIG = '/repo/tsconfig.json'
  9. const originalMode = process.env[EXAMPLE_MODE_ENV]
  10. afterEach(() => {
  11. if (originalMode === undefined) Reflect.deleteProperty(process.env, EXAMPLE_MODE_ENV)
  12. else process.env[EXAMPLE_MODE_ENV] = originalMode
  13. })
  14. describe('resolveExampleMode', () => {
  15. it('defaults absent/empty/src to src', () => {
  16. Reflect.deleteProperty(process.env, EXAMPLE_MODE_ENV)
  17. expect(resolveExampleMode()).toBe('src')
  18. expect(resolveExampleMode('')).toBe('src')
  19. expect(resolveExampleMode('src')).toBe('src')
  20. })
  21. it('accepts lib', () => {
  22. expect(resolveExampleMode('lib')).toBe('lib')
  23. })
  24. it('throws on any other value', () => {
  25. expect(() => resolveExampleMode('prod')).toThrow(/must be 'src' or 'lib'/)
  26. })
  27. it('reads the environment when no argument is given', () => {
  28. process.env[EXAMPLE_MODE_ENV] = 'lib'
  29. expect(resolveExampleMode()).toBe('lib')
  30. Reflect.deleteProperty(process.env, EXAMPLE_MODE_ENV)
  31. expect(resolveExampleMode()).toBe('src')
  32. })
  33. })
  34. describe('resolveExampleLaunch', () => {
  35. it('src mode: --import tsx on the source bin with the tsconfig paths env', () => {
  36. const { command, args, env } = resolveExampleLaunch({
  37. srcBin: SRC_BIN,
  38. configArgs: ['./cordis.yml'],
  39. mode: 'src',
  40. tsconfigPath: TSCONFIG,
  41. })
  42. expect(command).toBe(process.execPath)
  43. expect(args).toContain('--import')
  44. expect(args).toContain(SRC_BIN)
  45. expect(args[args.length - 1]).toBe('./cordis.yml')
  46. expect(env.TSX_TSCONFIG_PATH).toBe(TSCONFIG)
  47. })
  48. it('src mode: throws without a tsconfig path', () => {
  49. expect(() => resolveExampleLaunch({ srcBin: SRC_BIN, mode: 'src' })).toThrow(/needs tsconfigPath/)
  50. })
  51. it('src mode: resolves an app-specific tsx import hook', () => {
  52. const { args } = resolveExampleLaunch({
  53. srcBin: SRC_BIN,
  54. mode: 'src',
  55. sourceImport: 'tsx/esm',
  56. tsconfigPath: TSCONFIG,
  57. })
  58. expect(args[0]).toBe('--import')
  59. expect(args[1]).toContain('/tsx/dist/esm/index.mjs')
  60. })
  61. it('lib mode: plain node on the derived lib bin, no tsx and no paths env', () => {
  62. const { args, env } = resolveExampleLaunch({
  63. srcBin: SRC_BIN,
  64. configArgs: ['--config', './cordis.yml'],
  65. mode: 'lib',
  66. env: { DSH_HOME: '/tmp/home' },
  67. })
  68. expect(args).not.toContain('--import')
  69. expect(args).toContain('/repo/apps/cli/lib/bin.js')
  70. expect(args.slice(-2)).toEqual(['--config', './cordis.yml'])
  71. expect(env.TSX_TSCONFIG_PATH).toBeUndefined()
  72. expect(env.DSH_HOME).toBe('/tmp/home')
  73. })
  74. it('lib mode: uses an explicit plain-Node bin when provided', () => {
  75. const fixture = '/repo/fixture.ts'
  76. const { args } = resolveExampleLaunch({ srcBin: fixture, libBin: fixture, mode: 'lib' })
  77. expect(args).toContain(fixture)
  78. })
  79. it('lib mode: rewrites only the last /src/ segment', () => {
  80. const { args } = resolveExampleLaunch({
  81. srcBin: '/repo/src/apps/cli/src/bin.ts',
  82. mode: 'lib',
  83. })
  84. expect(args).toContain('/repo/src/apps/cli/lib/bin.js')
  85. })
  86. it('lib mode: derives the built bin from a Windows source path', () => {
  87. const { args } = resolveExampleLaunch({
  88. srcBin: String.raw`D:\repo\src\apps\cli\src\bin.ts`,
  89. mode: 'lib',
  90. })
  91. expect(args).toContain(String.raw`D:\repo\src\apps\cli\lib\bin.js`)
  92. })
  93. it('lib mode: throws when the bin has no /src/ segment', () => {
  94. expect(() => resolveExampleLaunch({ srcBin: '/repo/lib/bin.js', mode: 'lib' })).toThrow(/"\/src\/" segment/)
  95. })
  96. it('defaults the mode from the environment', () => {
  97. process.env[EXAMPLE_MODE_ENV] = 'lib'
  98. const { args } = resolveExampleLaunch({ srcBin: SRC_BIN })
  99. expect(args).toContain('/repo/apps/cli/lib/bin.js')
  100. })
  101. })