compose-stack.spec.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Tree-wide row-id ownership across the profile stack: built-in layers claim
  3. * first and fail loud on a duplicate, an external bundle that collides is left
  4. * out and recorded, a bundle that repeats one of its own ids is left out the
  5. * same way, a user insert of a taken id is dropped, and every conflict
  6. * carries its message.
  7. */
  8. import { describe, expect, it } from 'vitest'
  9. import type { EntryOptions } from '@deepseek-ai/cordis-plugin-loader'
  10. import type { PatchOptions } from '@deepseek-ai/cordis-plugin-include'
  11. import { claimLayerIds, composeProfileStack, CONTAINED_GROUP_MODULE, formatRowConflict, type ProfileLayer } from '../src/index.ts'
  12. const NAME = 'dsh-test-bin'
  13. function layer(
  14. packageName: string, trust: ProfileLayer['trust'], patches: PatchOptions[], stage: ProfileLayer['stage'] = 'runtime',
  15. ): ProfileLayer {
  16. return { packageName, version: '1.0.0', packageDir: '/nowhere', patchPath: '/nowhere/cordis.patch.yml', trust, stage, patches }
  17. }
  18. const base = layer('@deepseek-ai/dsh-base', 'builtin', [{ insert: [
  19. { id: 'settings', name: 'settings' },
  20. { id: 'tools', name: 'cordis:group', group: true, config: [{ id: 'tool-bash', name: 'bash' }] },
  21. ] }])
  22. describe('claimLayerIds', () => {
  23. it('lets built-in layers own their ids, including group children, before any external layer', () => {
  24. const ext = layer('ext', 'external', [{ insert: [{ id: 'tool-bash', name: 'ext' }] }])
  25. const { owners, skipped } = claimLayerIds([ext, base])
  26. expect(owners.get('tool-bash')?.packageName).toBe('@deepseek-ai/dsh-base')
  27. expect(owners.get('tools')?.packageName).toBe('@deepseek-ai/dsh-base')
  28. expect(skipped.get('ext')).toEqual([
  29. {
  30. rowId: 'tool-bash', moduleName: 'ext', layer: 'ext', packageName: 'ext', declaredBy: '@deepseek-ai/dsh-base',
  31. message: 'row "tool-bash" is already declared by @deepseek-ai/dsh-base',
  32. },
  33. ])
  34. })
  35. it('throws when two built-in or boot-staged layers declare one id, or one declares it twice', () => {
  36. const twin = layer('twin', 'external', [{ insert: [{ id: 'settings', name: 'twin' }] }], 'boot')
  37. expect(() => claimLayerIds([base, twin])).toThrow(/row "settings" is declared by both @deepseek-ai\/dsh-base and twin/)
  38. const stutter = layer('stutter', 'builtin', [{ insert: [{ id: 'x', name: 'a' }] }, { insert: [{ id: 'x', name: 'b' }] }])
  39. expect(() => claimLayerIds([stutter])).toThrow(/row "x" is declared twice by stutter/)
  40. })
  41. it('leaves out a bundle that declares one of its own ids twice and composes each mounted bundle once', () => {
  42. const stutter = layer('stutter', 'external', [{ insert: [{ id: 'x', name: 'stutter/a' }, { id: 'x', name: 'stutter/b' }] }])
  43. const clean = layer('clean', 'external', [{ insert: [{ id: 'y', name: 'clean' }] }])
  44. const { owners, skipped, composed } = claimLayerIds([base, stutter, clean])
  45. expect(skipped.get('stutter')).toEqual([
  46. { rowId: 'x', moduleName: 'stutter/b', layer: 'stutter', packageName: 'stutter', declaredBy: 'stutter', message: 'row "x" is declared twice by stutter' },
  47. ])
  48. expect(owners.has('x')).toBe(false)
  49. expect(owners.get('y')?.packageName).toBe('clean')
  50. expect([...composed.keys()]).toEqual(['clean'])
  51. expect(composed.get('clean')?.patches[1]).toEqual({ id: 'bundle/clean', insert: [{ id: 'y', name: 'clean' }] })
  52. })
  53. it('gives the earlier external bundle the id and leaves the later one out whole', () => {
  54. const first = layer('first', 'external', [{ insert: [{ id: 'hello', name: 'first' }, { id: 'only-first', name: 'first/x' }] }])
  55. const second = layer('second', 'external', [{ insert: [{ id: 'hello', name: 'second' }, { id: 'only-second', name: 'second/x' }] }])
  56. const { owners, skipped } = claimLayerIds([base, first, second])
  57. expect(owners.get('hello')?.packageName).toBe('first')
  58. expect(owners.get('bundle/first')?.packageName).toBe('first')
  59. expect(owners.has('only-second')).toBe(false)
  60. expect(skipped.get('second')?.map(conflict => conflict.rowId)).toEqual(['hello'])
  61. })
  62. })
  63. describe('composeProfileStack', () => {
  64. it('mounts owning layers in manifest order and drops a user insert of a taken id', () => {
  65. const ext = layer('ext', 'external', [{ insert: [{ id: 'ext-tool', name: 'ext' }] }])
  66. const stack = composeProfileStack(NAME, [base, ext], [
  67. { label: '/p/cordis.patch.yml', patches: [
  68. { id: 'settings', config: { path: '/x' } },
  69. { insert: [{ id: 'mine', name: 'mine' }, { id: 'ext-tool', name: 'clash' }] },
  70. { insert: [{ id: 'g', name: 'cordis:group', group: true, config: [{ id: 'tool-bash', name: 'nested-clash' }] }] },
  71. ] },
  72. { label: '/home/cordis.patch.yml', patches: [
  73. { insert: [{ id: 'mine', name: 'twice' }] },
  74. { insert: [{ id: 'clean', name: 'clean' }, { name: 'anonymous' } as EntryOptions] },
  75. ] },
  76. ])
  77. expect(stack.layers.map(current => current.label)).toEqual(['@deepseek-ai/dsh-base', 'ext', '/p/cordis.patch.yml', '/home/cordis.patch.yml'])
  78. expect(stack.layers[1]?.patches[0]?.insert?.[0]).toMatchObject({ id: 'bundle/ext', name: CONTAINED_GROUP_MODULE })
  79. expect([...stack.owners.keys()]).toEqual(['settings', 'tools', 'tool-bash', 'ext-tool', 'bundle/ext'])
  80. expect(stack.layers[2]?.patches).toEqual([
  81. { id: 'settings', config: { path: '/x' } },
  82. { insert: [{ id: 'mine', name: 'mine' }] },
  83. ])
  84. // An insert with no conflict passes through as the same object; an anonymous row claims nothing.
  85. expect(stack.layers[3]?.patches).toEqual([{ insert: [{ id: 'clean', name: 'clean' }, { name: 'anonymous' }] }])
  86. expect(stack.patches).toEqual(stack.layers.flatMap(current => current.patches))
  87. expect(stack.skippedBundles).toEqual([])
  88. expect(stack.conflicts).toEqual([
  89. { rowId: 'ext-tool', moduleName: 'clash', layer: '/p/cordis.patch.yml', declaredBy: 'ext', message: 'row "ext-tool" is already declared by ext' },
  90. {
  91. rowId: 'tool-bash', moduleName: 'cordis:group', layer: '/p/cordis.patch.yml', declaredBy: '@deepseek-ai/dsh-base',
  92. message: 'row "tool-bash" is already declared by @deepseek-ai/dsh-base',
  93. },
  94. {
  95. rowId: 'mine', moduleName: 'twice', layer: '/home/cordis.patch.yml', declaredBy: '/p/cordis.patch.yml',
  96. message: 'row "mine" is already declared by /p/cordis.patch.yml',
  97. },
  98. ])
  99. })
  100. it('leaves a colliding external bundle out of the stack and reports it', () => {
  101. const clash = layer('clash', 'external', [{ insert: [{ id: 'settings', name: 'clash' }] }])
  102. const stack = composeProfileStack(NAME, [base, clash], [])
  103. expect(stack.layers.map(current => current.label)).toEqual(['@deepseek-ai/dsh-base'])
  104. expect(stack.skippedBundles).toEqual(['clash'])
  105. expect(stack.conflicts).toEqual([
  106. {
  107. rowId: 'settings', moduleName: 'clash', layer: 'clash', packageName: 'clash', declaredBy: '@deepseek-ai/dsh-base',
  108. message: 'row "settings" is already declared by @deepseek-ai/dsh-base',
  109. },
  110. ])
  111. })
  112. it('prefixes a built-in duplicate with the binary name', () => {
  113. const twin = layer('twin', 'builtin', [{ insert: [{ id: 'settings', name: 'twin' }] }])
  114. expect(() => composeProfileStack(NAME, [base, twin], [])).toThrow(/^dsh-test-bin: row "settings" is declared by both/)
  115. })
  116. })
  117. describe('formatRowConflict', () => {
  118. it('names the bundle left out, or the user layer whose insert was skipped', () => {
  119. const message = 'row "x" is already declared by base'
  120. expect(formatRowConflict({ rowId: 'x', moduleName: 'm', layer: 'pkg', packageName: 'pkg', declaredBy: 'base', message }))
  121. .toBe('bundle pkg left out — row "x" is already declared by base')
  122. expect(formatRowConflict({ rowId: 'x', moduleName: 'm', layer: '/p/cordis.patch.yml', declaredBy: 'base', message }))
  123. .toBe('/p/cordis.patch.yml: insert of m skipped — row "x" is already declared by base')
  124. })
  125. })