windows-shell.spec.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { mkdtempSync, writeFileSync, rmSync, mkdirSync } 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-local
  17. name: '@deepseek-ai/dsh-pwsh-local'
  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-local', name: '@deepseek-ai/dsh-pwsh-local' }] },
  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 danger-full-access 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. for (const id of ['bash-sandbox', 'tool-bash', 'permission', 'ui-permission',
  81. 'sandbox', 'sandbox-policy', 'fs-sandbox', 'approval']) {
  82. expect(byId.get(id)?.disabled, `row ${id}`).toBe(true)
  83. }
  84. for (const id of ['pwsh-local', 'tool-pwsh', 'fs-local']) {
  85. expect(byId.has(id), `inserted row ${id}`).toBe(true)
  86. }
  87. // The web-app layer provides ui-permission, so the full web profile
  88. // composes without any no-match warning.
  89. expect(warnings).toEqual([])
  90. })
  91. it('leaves POSIX untouched and base-only profiles warned but harmless', () => {
  92. home = mkdtempSync(join(tmpdir(), 'dsh-windows-home-'))
  93. initProfile(join(home, PROFILES_DIR, 'web'), ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  94. const profile = loadProfile('dsh', 'web', anchor, home)
  95. // POSIX: no platform layer, the bash stack stays enabled.
  96. const posixRows = composeEntries(profile.layers.map(layer => layer.patches))
  97. const posixById = new Map(posixRows.map(row => [row.id, row]))
  98. expect(posixById.get('bash-sandbox')?.disabled).not.toBe(true)
  99. expect(posixById.has('pwsh-local')).toBe(false)
  100. // A base-only custom profile (the DEFAULT_PROFILE_BUNDLES template):
  101. // ui-permission has no row to patch, so the shipped layer warns once per
  102. // composition — never fails — exactly as its header comment documents.
  103. initProfile(join(home, PROFILES_DIR, 'base-only'), ['@deepseek-ai/dsh-base'])
  104. const baseOnly = loadProfile('dsh', 'base-only', anchor, home)
  105. const baseWarnings: string[] = []
  106. const win32 = resolveWindowsShellLayer('win32', baseOnly.layers, 'dsh')
  107. expect(win32).toBeDefined()
  108. composeEntries(
  109. [...baseOnly.layers.map(layer => layer.patches), win32!.patches],
  110. message => baseWarnings.push(message),
  111. )
  112. expect(baseWarnings.some(message => message.includes('ui-permission'))).toBe(true)
  113. })
  114. })