patch.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /** Profile patch edits preserve user-authored syntax and are idempotent. */
  2. import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
  3. import { join } from 'node:path'
  4. import { tmpdir } from 'node:os'
  5. import { expect, it, onTestFinished } from 'vitest'
  6. import { loadOptionalPatches } from '@deepseek-ai/dsh-app-boot'
  7. import { writePluginEnabled } from '../src/patch.ts'
  8. async function fixture(text?: string): Promise<string> {
  9. const dir = await mkdtemp(join(tmpdir(), 'manager-patch-'))
  10. onTestFinished(() => rm(dir, { recursive: true, force: true }))
  11. const file = join(dir, 'cordis.patch.yml')
  12. if (text !== undefined) await writeFile(file, text)
  13. return file
  14. }
  15. it('preserves comments, expressions, unrelated configuration and the last override', async () => {
  16. const file = await fixture('# personal configuration\n- id: tool\n config:\n value: !!js process.platform\n- id: tool\n disabled: false # availability\n')
  17. expect(await writePluginEnabled(file, 'tool', false)).toBe(true)
  18. const text = await readFile(file, 'utf8')
  19. expect(text).toContain('# personal configuration')
  20. expect(text).toContain('!!js process.platform')
  21. expect(text).toContain('# availability')
  22. expect(loadOptionalPatches('test', file)).toEqual([
  23. { id: 'tool', config: { value: { __jsExpr: 'process.platform' } } }, { id: 'tool', disabled: true },
  24. ])
  25. expect(await writePluginEnabled(file, 'tool', false)).toBe(false)
  26. expect(await readFile(file, 'utf8')).toBe(text)
  27. expect(await writePluginEnabled(file, 'tool', true)).toBe(true)
  28. })
  29. it('creates a missing patch file and appends after insertions', async () => {
  30. const file = await fixture()
  31. await writePluginEnabled(file, 'tool', false)
  32. expect(loadOptionalPatches('test', file)).toEqual([{ id: 'tool', disabled: true }])
  33. await writeFile(file, '- insert:\n - id: tool\n name: package\n')
  34. await writePluginEnabled(file, 'tool', true)
  35. expect(loadOptionalPatches('test', file)).toEqual([
  36. { insert: [{ id: 'tool', name: 'package' }] }, { id: 'tool', disabled: false },
  37. ])
  38. })
  39. it.each(['- id: [broken', 'mapping: true\n'])('refuses malformed documents without overwriting %s', async (text) => {
  40. const file = await fixture(text)
  41. await expect(writePluginEnabled(file, 'tool', true)).rejects.toThrow()
  42. expect(await readFile(file, 'utf8')).toBe(text)
  43. })
  44. it('retains name-asserting overrides and appends an unambiguous switch', async () => {
  45. const file = await fixture('- id: tool\n name: another-package\n disabled: false\n')
  46. await writePluginEnabled(file, 'tool', false)
  47. expect(loadOptionalPatches('test', file)).toEqual([
  48. { id: 'tool', name: 'another-package', disabled: false }, { id: 'tool', disabled: true },
  49. ])
  50. expect(await writePluginEnabled(file, 'tool', false)).toBe(false)
  51. })
  52. it('reports read failures without replacing a directory with configuration', async () => {
  53. const file = await fixture()
  54. await mkdir(file)
  55. await expect(writePluginEnabled(file, 'tool', true)).rejects.toThrow()
  56. })