windows-shell.spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 gate that keeps tool-bash
  9. * out of win32 sessions, and the cold-start resolution closure for the pwsh
  10. * 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 composed row on one platform: a `!!js`
  23. * expression evaluates with a platform-scoped context (the `with` scope
  24. * shadows the global `process`) so both outcomes pin on every host; a plain
  25. * boolean is the value itself.
  26. */
  27. function disabledOn(row: { disabled?: unknown }, platform: 'win32' | 'linux'): boolean {
  28. const value = row.disabled
  29. if (value !== null && typeof value === 'object' && '__jsExpr' in value) {
  30. return Boolean(evaluate({ process: { platform } }, (value as { __jsExpr: string }).__jsExpr))
  31. }
  32. return value === true
  33. }
  34. describe('the shipped shell composition (real bundle layers)', () => {
  35. let home: string
  36. afterEach(() => { if (home !== undefined) rmSync(home, { recursive: true, force: true }) })
  37. // The app installation anchor, mirroring profile-boot.ts: the bundle layers
  38. // resolve from the REAL dsh-base/dsh-web-app packages through it, so this
  39. // suite composes the shipped patch files, not test fixtures.
  40. const anchor = fileURLToPath(new URL('../package.json', import.meta.url))
  41. it('composes the confined pwsh roster on win32 and the bash roster on POSIX from the same rows', () => {
  42. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  43. initProfile(join(home, PROFILES_DIR, 'web'), ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  44. const profile = loadProfile('dsh', 'web', anchor, home)
  45. const warnings: string[] = []
  46. const rows = composeEntries(
  47. profile.layers.map(layer => layer.patches),
  48. message => warnings.push(message),
  49. )
  50. const byId = new Map(rows.map(row => [row.id, row]))
  51. // One shared patch set, two rosters: the shell stacks gate themselves.
  52. for (const id of ['bash-sandbox', 'pwsh-sandbox', 'tool-pwsh']) {
  53. expect(byId.has(id), `row ${id}`).toBe(true)
  54. }
  55. expect(disabledOn(byId.get('bash-sandbox')!, 'win32'), 'bash-sandbox on win32').toBe(true)
  56. expect(disabledOn(byId.get('bash-sandbox')!, 'linux'), 'bash-sandbox on linux').toBe(false)
  57. expect(disabledOn(byId.get('pwsh-sandbox')!, 'win32'), 'pwsh-sandbox on win32').toBe(false)
  58. expect(disabledOn(byId.get('pwsh-sandbox')!, 'linux'), 'pwsh-sandbox on linux').toBe(true)
  59. expect(disabledOn(byId.get('tool-pwsh')!, 'win32'), 'tool-pwsh on win32').toBe(false)
  60. expect(disabledOn(byId.get('tool-pwsh')!, 'linux'), 'tool-pwsh on linux').toBe(true)
  61. // The Web surface owns the host tool-bash row on every platform: sessions
  62. // mount their own preset rows instead.
  63. expect(byId.get('tool-bash')?.disabled).toBe(true)
  64. // The permission surface never moves: the sandbox/policy rows, the
  65. // permission switcher, fs-sandbox, and the approval service stay enabled
  66. // exactly as on POSIX — the confined pwsh executor is what changes.
  67. for (const id of ['permission', 'ui-permission', 'sandbox', 'sandbox-policy', 'fs-sandbox', 'approval']) {
  68. expect(byId.get(id)?.disabled, `row ${id}`).not.toBe(true)
  69. }
  70. // The launcher's cold-start module fallback BFS-links the apps/cli
  71. // dependency closure into the profile's node_modules, so every bare
  72. // plugin name in the base patch must resolve from there.
  73. const cliManifest = JSON.parse(readFileSync(anchor, 'utf8')) as { dependencies?: Record<string, string> }
  74. for (const name of ['@deepseek-ai/dsh-pwsh-sandbox', '@deepseek-ai/dsh-tool-pwsh']) {
  75. expect(cliManifest.dependencies?.[name], `cold-start closure must reach ${name}`).toBeDefined()
  76. }
  77. expect(warnings).toEqual([])
  78. })
  79. it('base-only profiles carry both stacks with the same platform gating', () => {
  80. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  81. initProfile(join(home, PROFILES_DIR, 'base-only'), ['@deepseek-ai/dsh-base'])
  82. const profile = loadProfile('dsh', 'base-only', anchor, home)
  83. const warnings: string[] = []
  84. const rows = composeEntries(
  85. profile.layers.map(layer => layer.patches),
  86. message => warnings.push(message),
  87. )
  88. const byId = new Map(rows.map(row => [row.id, row]))
  89. for (const id of ['bash-sandbox', 'tool-bash', 'pwsh-sandbox', 'tool-pwsh']) {
  90. expect(byId.has(id), `row ${id}`).toBe(true)
  91. }
  92. // No web overlay: the tool rows keep their own gating too.
  93. expect(disabledOn(byId.get('tool-bash')!, 'win32'), 'tool-bash on win32').toBe(true)
  94. expect(disabledOn(byId.get('tool-bash')!, 'linux'), 'tool-bash on linux').toBe(false)
  95. expect(disabledOn(byId.get('tool-pwsh')!, 'win32'), 'tool-pwsh on win32').toBe(false)
  96. expect(disabledOn(byId.get('tool-pwsh')!, 'linux'), 'tool-pwsh on linux').toBe(true)
  97. expect(warnings).toEqual([])
  98. })
  99. })
  100. describe('shipped agent presets keep tool-bash off the win32 roster', () => {
  101. const presetRoot = resolve(fileURLToPath(new URL('../package.json', import.meta.url)), '..', 'config', 'agent-presets')
  102. it.each(['standard', 'code', 'cordis'])('preset %s gates its tool-bash row by platform', (preset) => {
  103. const entries: unknown = yaml.load(
  104. readFileSync(join(presetRoot, preset, 'agent.cordis.yml'), 'utf8'),
  105. { schema: entryListSchema },
  106. )
  107. if (!Array.isArray(entries)) throw new TypeError(`preset ${preset} must parse to an entry array`)
  108. const row = entries.find((entry): entry is Record<string, unknown> => (
  109. typeof entry === 'object' && entry !== null && (entry as Record<string, unknown>).id === 'tool-bash'
  110. ))
  111. if (row === undefined) throw new TypeError(`preset ${preset} must mount tool-bash`)
  112. expect(row.disabled).toMatchObject({ __jsExpr: expect.any(String) as string })
  113. // The base patch gates the host tool-bash row on win32; the preset row
  114. // must not re-enable it there. Evaluate the shipped expression with a
  115. // platform-scoped context (the `with` scope shadows the global
  116. // `process`) so both outcomes pin on every host.
  117. const expression = (row.disabled as { __jsExpr: string }).__jsExpr
  118. expect(Boolean(evaluate({ process: { platform: 'win32' } }, expression))).toBe(true)
  119. expect(Boolean(evaluate({ process: { platform: 'linux' } }, expression))).toBe(false)
  120. })
  121. it('minimal mounts no tool-bash row at all (its shell is the PTY stack)', () => {
  122. const entries: unknown = yaml.load(
  123. readFileSync(join(presetRoot, 'minimal', 'agent.cordis.yml'), 'utf8'),
  124. { schema: entryListSchema },
  125. )
  126. if (!Array.isArray(entries)) throw new TypeError('minimal preset must parse to an entry array')
  127. expect(entries.some(entry => (
  128. typeof entry === 'object' && entry !== null && (entry as Record<string, unknown>).id === 'tool-bash'
  129. ))).toBe(false)
  130. })
  131. })