windows-shell.spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * The shipped shell composition: the base bundle gates both shell stacks by
  3. * platform on its own rows (`disabled: !!js process.platform`), so exactly
  4. * one shell stack mounts per host and no separate platform layer exists —
  5. * the launcher applies nothing beyond the bundle layers. The spec composes
  6. * the REAL shipped bundle layers (dsh-base + dsh-web-app resolved from the
  7. * app installation anchor) through the boot's patch algorithm and pins the
  8. * effective per-platform roster, the preset-level gates that keep tool-bash
  9. * out of win32 sessions and tool-pwsh out of POSIX sessions, and the
  10. * cold-start resolution closure for the pwsh rows' bare plugin names.
  11. */
  12. import { afterEach, describe, expect, it } from 'vitest'
  13. import { mkdtempSync, rmSync, readFileSync } from 'node:fs'
  14. import { tmpdir } from 'node:os'
  15. import { join, resolve } from 'node:path'
  16. import { fileURLToPath } from 'node:url'
  17. import yaml from 'js-yaml'
  18. import { entryListSchema } from '@deepseek-ai/cordis-plugin-include'
  19. import { evaluate } from '@deepseek-ai/cordis-plugin-loader'
  20. import { composeEntries, initProfile, loadProfile, PROFILES_DIR } from '@deepseek-ai/dsh-app-boot'
  21. /**
  22. * The effective disabled state of one row on one platform: a `!!js` expression
  23. * evaluates with a platform-scoped `process` so both outcomes pin on any host.
  24. */
  25. function disabledOn(row: { disabled?: unknown }, platform: 'win32' | 'linux'): boolean {
  26. const value = row.disabled
  27. if (value !== null && typeof value === 'object' && '__jsExpr' in value) {
  28. return Boolean(evaluate({ process: { platform } }, (value as { __jsExpr: string }).__jsExpr))
  29. }
  30. return value === true
  31. }
  32. describe('the shipped shell composition (real bundle layers)', () => {
  33. let home: string
  34. afterEach(() => { if (home !== undefined) rmSync(home, { recursive: true, force: true }) })
  35. // The app installation anchor, mirroring profile-boot.ts: the bundle layers
  36. // resolve from the REAL dsh-base/dsh-web-app packages through it, so this
  37. // suite composes the shipped patch files, not test fixtures.
  38. const anchor = fileURLToPath(new URL('../package.json', import.meta.url))
  39. it('composes the confined pwsh roster on win32 and the bash roster on POSIX from the same rows', () => {
  40. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  41. initProfile(join(home, PROFILES_DIR, 'web'), ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  42. const profile = loadProfile('dsh', 'web', anchor, home)
  43. const warnings: string[] = []
  44. const rows = composeEntries(
  45. profile.layers.map(layer => layer.patches),
  46. message => warnings.push(message),
  47. )
  48. const byId = new Map(rows.map(row => [row.id, row]))
  49. // One shared patch set, two rosters: the shell stacks gate themselves.
  50. for (const id of ['bash-sandbox', 'pwsh-sandbox', 'tool-bash', 'tool-pwsh']) {
  51. expect(byId.has(id), `row ${id}`).toBe(true)
  52. }
  53. expect(disabledOn(byId.get('bash-sandbox')!, 'win32'), 'bash-sandbox on win32').toBe(true)
  54. expect(disabledOn(byId.get('bash-sandbox')!, 'linux'), 'bash-sandbox on linux').toBe(false)
  55. expect(disabledOn(byId.get('pwsh-sandbox')!, 'win32'), 'pwsh-sandbox on win32').toBe(false)
  56. expect(disabledOn(byId.get('pwsh-sandbox')!, 'linux'), 'pwsh-sandbox on linux').toBe(true)
  57. // Host shell-tool rows are disabled on every platform; sessions mount
  58. // their own rows instead.
  59. expect(byId.get('tool-bash')?.disabled).toBe(true)
  60. expect(byId.get('tool-pwsh')?.disabled).toBe(true)
  61. // The permission surface never moves: the sandbox/policy rows, the
  62. // permission switcher, fs-sandbox, and the approval service stay enabled
  63. // exactly as on POSIX — the confined pwsh executor is what changes.
  64. for (const id of ['permission', 'ui-permission', 'sandbox', 'sandbox-policy', 'fs-sandbox', 'approval']) {
  65. expect(byId.get(id)?.disabled, `row ${id}`).not.toBe(true)
  66. }
  67. // The launcher's cold-start module fallback BFS-links the apps/cli
  68. // dependency closure into the profile's node_modules, so every bare
  69. // plugin name in the base patch must resolve from there.
  70. const cliManifest = JSON.parse(readFileSync(anchor, 'utf8')) as { dependencies?: Record<string, string> }
  71. for (const name of ['@deepseek-ai/dsh-pwsh-sandbox', '@deepseek-ai/dsh-tool-pwsh']) {
  72. expect(cliManifest.dependencies?.[name], `cold-start closure must reach ${name}`).toBeDefined()
  73. }
  74. expect(warnings).toEqual([])
  75. })
  76. it('base-only profiles carry both stacks with the same platform gating', () => {
  77. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  78. initProfile(join(home, PROFILES_DIR, 'base-only'), ['@deepseek-ai/dsh-base'])
  79. const profile = loadProfile('dsh', 'base-only', anchor, home)
  80. const warnings: string[] = []
  81. const rows = composeEntries(
  82. profile.layers.map(layer => layer.patches),
  83. message => warnings.push(message),
  84. )
  85. const byId = new Map(rows.map(row => [row.id, row]))
  86. for (const id of ['bash-sandbox', 'tool-bash', 'pwsh-sandbox', 'tool-pwsh']) {
  87. expect(byId.has(id), `row ${id}`).toBe(true)
  88. }
  89. // No web overlay: the tool rows keep their own gating too.
  90. expect(disabledOn(byId.get('tool-bash')!, 'win32'), 'tool-bash on win32').toBe(true)
  91. expect(disabledOn(byId.get('tool-bash')!, 'linux'), 'tool-bash on linux').toBe(false)
  92. expect(disabledOn(byId.get('tool-pwsh')!, 'win32'), 'tool-pwsh on win32').toBe(false)
  93. expect(disabledOn(byId.get('tool-pwsh')!, 'linux'), 'tool-pwsh on linux').toBe(true)
  94. expect(warnings).toEqual([])
  95. })
  96. })
  97. describe('shipped agent presets gate both shell tools by platform', () => {
  98. const presetRoot = resolve(fileURLToPath(new URL('../package.json', import.meta.url)), '..', 'config', 'agent-presets')
  99. it.each(['standard', 'code', 'cordis'])('preset %s gates its shell tool rows by platform', (preset) => {
  100. const entries: unknown = yaml.load(
  101. readFileSync(join(presetRoot, preset, 'agent.cordis.yml'), 'utf8'),
  102. { schema: entryListSchema },
  103. )
  104. if (!Array.isArray(entries)) throw new TypeError(`preset ${preset} must parse to an entry array`)
  105. for (const [id, win32] of [['tool-bash', true], ['tool-pwsh', false]] as const) {
  106. const row = entries.find((entry): entry is Record<string, unknown> => (
  107. typeof entry === 'object' && entry !== null && (entry as Record<string, unknown>).id === id
  108. ))
  109. if (row === undefined) throw new TypeError(`preset ${preset} must mount ${id}`)
  110. expect(row.disabled).toMatchObject({ __jsExpr: expect.any(String) as string })
  111. // A platform-scoped context pins both outcomes on every host.
  112. const expression = (row.disabled as { __jsExpr: string }).__jsExpr
  113. expect(Boolean(evaluate({ process: { platform: 'win32' } }, expression)), `${id} on win32`).toBe(win32)
  114. expect(Boolean(evaluate({ process: { platform: 'linux' } }, expression)), `${id} on linux`).toBe(!win32)
  115. }
  116. })
  117. it('minimal mounts no shell tool row and gates its persistent shell stack by platform', () => {
  118. const entries: unknown = yaml.load(
  119. readFileSync(join(presetRoot, 'minimal', 'agent.cordis.yml'), 'utf8'),
  120. { schema: entryListSchema },
  121. )
  122. if (!Array.isArray(entries)) throw new TypeError('minimal preset must parse to an entry array')
  123. for (const id of ['tool-bash', 'tool-pwsh']) {
  124. expect(entries.some(entry => (
  125. typeof entry === 'object' && entry !== null && (entry as Record<string, unknown>).id === id
  126. )), `${id} must be absent from minimal`).toBe(false)
  127. }
  128. const group = entries.find((entry): entry is Record<string, unknown> => (
  129. typeof entry === 'object' && entry !== null && (entry as Record<string, unknown>).id === 'persistent-shell'
  130. ))
  131. if (group === undefined) throw new TypeError('minimal preset must mount persistent-shell')
  132. const rows = group.config as unknown[]
  133. if (!Array.isArray(rows)) throw new TypeError('persistent-shell must carry a row list')
  134. const byId = new Map(rows
  135. .filter((entry): entry is Record<string, unknown> => typeof entry === 'object' && entry !== null)
  136. .map(entry => [entry.id, entry]))
  137. // The bash stack (terminal-bash + persistent-bash) mounts on POSIX only; the
  138. // pwsh twin (terminal-bash with shellDialect pwsh + persistent-pwsh) mounts on
  139. // win32 only — exactly one persistent shell per host.
  140. for (const id of ['terminal-bash', 'persistent-bash']) {
  141. expect(disabledOn(byId.get(id)!, 'win32'), `${id} on win32`).toBe(true)
  142. expect(disabledOn(byId.get(id)!, 'linux'), `${id} on linux`).toBe(false)
  143. }
  144. for (const id of ['terminal-pwsh', 'persistent-pwsh']) {
  145. expect(disabledOn(byId.get(id)!, 'win32'), `${id} on win32`).toBe(false)
  146. expect(disabledOn(byId.get(id)!, 'linux'), `${id} on linux`).toBe(true)
  147. }
  148. expect(byId.get('terminal-pwsh')?.config).toMatchObject({ shellDialect: 'pwsh' })
  149. })
  150. })