windows-shell.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { mkdtempSync, writeFileSync, rmSync, mkdirSync, readFileSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import type { ProfileLayer } from '@deepseek-ai/dsh-app-boot'
  7. import { composeEntries, initProfile, loadProfile, PROFILES_DIR } from '@deepseek-ai/dsh-app-boot'
  8. import {
  9. BASE_BUNDLE,
  10. resolveWindowsShellLayer,
  11. WINDOWS_SHELL_PATCH_FILENAME,
  12. } from '../src/windows-shell.ts'
  13. const WINDOWS_PATCH = `- id: bash-sandbox
  14. disabled: true
  15. - insert:
  16. - id: pwsh-sandbox
  17. name: '@deepseek-ai/dsh-pwsh-sandbox'
  18. `
  19. /** One fake bundle layer rooted in a temp directory. */
  20. function fakeLayer(packageName: string, dir: string): ProfileLayer {
  21. return { packageName, packageDir: dir, patchPath: join(dir, 'cordis.patch.yml'), patches: [] }
  22. }
  23. /** A base bundle layer whose package carries the Windows shell patch. */
  24. function baseLayerWithPatch(dir: string): ProfileLayer {
  25. writeFileSync(join(dir, WINDOWS_SHELL_PATCH_FILENAME), WINDOWS_PATCH)
  26. return fakeLayer(BASE_BUNDLE, dir)
  27. }
  28. describe('resolveWindowsShellLayer', () => {
  29. let base: string
  30. afterEach(() => { if (base !== undefined) rmSync(base, { recursive: true, force: true }) })
  31. const tempBase = (): string => {
  32. base = mkdtempSync(join(tmpdir(), 'dsh-windows-shell-'))
  33. return base
  34. }
  35. it('never applies on POSIX hosts', () => {
  36. expect(resolveWindowsShellLayer('linux', [baseLayerWithPatch(tempBase())], 'dsh')).toBeUndefined()
  37. expect(resolveWindowsShellLayer('darwin', [baseLayerWithPatch(tempBase())], 'dsh')).toBeUndefined()
  38. })
  39. it('defaults Windows hosts to the pwsh platform layer', () => {
  40. const layer = resolveWindowsShellLayer('win32', [baseLayerWithPatch(tempBase())], 'dsh')
  41. expect(layer).toBeDefined()
  42. expect(layer?.label.endsWith(WINDOWS_SHELL_PATCH_FILENAME)).toBe(true)
  43. expect(layer?.patches).toEqual([
  44. { id: 'bash-sandbox', disabled: true },
  45. { insert: [{ id: 'pwsh-sandbox', name: '@deepseek-ai/dsh-pwsh-sandbox' }] },
  46. ])
  47. })
  48. it('skips custom profiles without a base bundle', () => {
  49. const other = fakeLayer('@deepseek-ai/dsh-custom', tempBase())
  50. expect(resolveWindowsShellLayer('win32', [other], 'dsh')).toBeUndefined()
  51. })
  52. it('fails loud when the base bundle ships no Windows shell patch', () => {
  53. const base = tempBase()
  54. mkdirSync(base, { recursive: true })
  55. // The overlay loader owns the fail-loud contract: the caller named this
  56. // file, so its absence is a misconfiguration, not "no overlay".
  57. expect(() => resolveWindowsShellLayer('win32', [fakeLayer(BASE_BUNDLE, base)], 'dsh'))
  58. .toThrow(/dsh: failed to read overlay .*windows\.cordis\.patch\.yml/)
  59. })
  60. })
  61. describe('the shipped Windows composition (real bundle layers)', () => {
  62. let home: string
  63. afterEach(() => { if (home !== undefined) rmSync(home, { recursive: true, force: true }) })
  64. // The app installation anchor, mirroring profile-boot.ts: the bundle layers
  65. // resolve from the REAL dsh-base/dsh-web-app packages through it, so this
  66. // suite composes the shipped patch files, not test fixtures.
  67. const anchor = fileURLToPath(new URL('../package.json', import.meta.url))
  68. it('composes the win32 confined roster through the real patch layers', () => {
  69. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  70. initProfile(join(home, PROFILES_DIR, 'web'), ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  71. const profile = loadProfile('dsh', 'web', anchor, home)
  72. const warnings: string[] = []
  73. const win32 = resolveWindowsShellLayer('win32', profile.layers, 'dsh')
  74. expect(win32).toBeDefined()
  75. const rows = composeEntries(
  76. [...profile.layers.map(layer => layer.patches), win32!.patches],
  77. message => warnings.push(message),
  78. )
  79. const byId = new Map(rows.map(row => [row.id, row]))
  80. // Only the POSIX bash stack leaves the roster: the permission surface
  81. // (sandbox/sandbox-policy/fs-sandbox, permission, approval) stays enabled
  82. // exactly as on POSIX — the confined pwsh executor is what changes.
  83. for (const id of ['bash-sandbox', 'tool-bash']) {
  84. expect(byId.get(id)?.disabled, `row ${id}`).toBe(true)
  85. }
  86. for (const id of ['permission', 'ui-permission', 'sandbox', 'sandbox-policy', 'fs-sandbox', 'approval']) {
  87. expect(byId.get(id)?.disabled, `row ${id}`).not.toBe(true)
  88. }
  89. for (const id of ['pwsh-sandbox', 'tool-pwsh']) {
  90. expect(byId.has(id), `inserted row ${id}`).toBe(true)
  91. }
  92. // The launcher's cold-start module fallback BFS-links the apps/cli
  93. // dependency closure into the profile's node_modules (the pwsh-local
  94. // precedent), so every inserted bare plugin must resolve from there.
  95. const cliManifest = JSON.parse(readFileSync(anchor, 'utf8')) as { dependencies?: Record<string, string> }
  96. for (const name of ['@deepseek-ai/dsh-pwsh-sandbox', '@deepseek-ai/dsh-tool-pwsh']) {
  97. expect(cliManifest.dependencies?.[name], `cold-start closure must reach ${name}`).toBeDefined()
  98. }
  99. // The patch touches only base-owned rows plus inserts, so the full web
  100. // profile composes without any no-match warning.
  101. expect(warnings).toEqual([])
  102. })
  103. it('leaves POSIX untouched and base-only profiles compose without warnings', () => {
  104. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  105. initProfile(join(home, PROFILES_DIR, 'web'), ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  106. const profile = loadProfile('dsh', 'web', anchor, home)
  107. // POSIX: no platform layer, the bash stack stays enabled.
  108. const posixRows = composeEntries(profile.layers.map(layer => layer.patches))
  109. const posixById = new Map(posixRows.map(row => [row.id, row]))
  110. expect(posixById.get('bash-sandbox')?.disabled).not.toBe(true)
  111. expect(posixById.has('pwsh-local')).toBe(false)
  112. expect(posixById.has('pwsh-sandbox')).toBe(false)
  113. // A base-only custom profile (the DEFAULT_PROFILE_BUNDLES template): the
  114. // patch touches only base-owned rows (bash-sandbox/tool-bash) plus its
  115. // inserts, so the composition produces no no-match warning.
  116. initProfile(join(home, PROFILES_DIR, 'base-only'), ['@deepseek-ai/dsh-base'])
  117. const baseOnly = loadProfile('dsh', 'base-only', anchor, home)
  118. const baseWarnings: string[] = []
  119. const win32 = resolveWindowsShellLayer('win32', baseOnly.layers, 'dsh')
  120. expect(win32).toBeDefined()
  121. composeEntries(
  122. [...baseOnly.layers.map(layer => layer.patches), win32!.patches],
  123. message => baseWarnings.push(message),
  124. )
  125. expect(baseWarnings).toEqual([])
  126. })
  127. })