compose-stack.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, formatRowConflict, type ProfileLayer } from '../src/index.ts'
  12. import { CONTAINED_GROUP_MODULE } from '../src/external-bundles.ts'
  13. const NAME = 'dsh-test-bin'
  14. function layer(
  15. packageName: string, trust: ProfileLayer['trust'], patches: PatchOptions[], stage: ProfileLayer['stage'] = 'runtime',
  16. ): ProfileLayer {
  17. return { packageName, version: '1.0.0', packageDir: '/nowhere', patchPath: '/nowhere/cordis.patch.yml', trust, stage, patches }
  18. }
  19. const base = layer('@deepseek-ai/dsh-base', 'builtin', [{ insert: [
  20. { id: 'settings', name: 'settings' },
  21. { id: 'tools', name: 'cordis:group', group: true, config: [{ id: 'tool-bash', name: 'bash' }] },
  22. ] }])
  23. describe('claimLayerIds', () => {
  24. it('lets built-in layers own their ids, including group children, before any external layer', () => {
  25. const ext = layer('ext', 'external', [{ insert: [{ id: 'tool-bash', name: 'ext' }] }])
  26. const { owners, skipped } = claimLayerIds([ext, base])
  27. expect(owners.get('tool-bash')?.packageName).toBe('@deepseek-ai/dsh-base')
  28. expect(owners.get('tools')?.packageName).toBe('@deepseek-ai/dsh-base')
  29. expect(skipped.get('ext')).toEqual([
  30. {
  31. rowId: 'tool-bash', moduleName: 'ext', layer: 'ext', packageName: 'ext', declaredBy: '@deepseek-ai/dsh-base',
  32. message: 'row "tool-bash" is already declared by @deepseek-ai/dsh-base',
  33. },
  34. ])
  35. })
  36. it('throws when two built-in or boot-staged layers declare one id, or one declares it twice', () => {
  37. const twin = layer('twin', 'external', [{ insert: [{ id: 'settings', name: 'twin' }] }], 'boot')
  38. expect(() => claimLayerIds([base, twin])).toThrow(/row "settings" is declared by both @deepseek-ai\/dsh-base and twin/)
  39. const stutter = layer('stutter', 'builtin', [{ insert: [{ id: 'x', name: 'a' }] }, { insert: [{ id: 'x', name: 'b' }] }])
  40. expect(() => claimLayerIds([stutter])).toThrow(/row "x" is declared twice by stutter/)
  41. })
  42. it('leaves out a bundle whose config override sets a row another layer owns', () => {
  43. const hijack = layer('hijack', 'external', [
  44. { insert: [{ id: 'own', name: 'cordis:group', group: true, config: [] }] },
  45. { id: 'own', config: [{ id: 'settings', name: 'hijack/impostor' }] },
  46. ])
  47. const { skipped } = claimLayerIds([base, hijack])
  48. expect(skipped.get('hijack')).toEqual([
  49. {
  50. rowId: 'settings', moduleName: 'hijack/impostor', layer: 'hijack', packageName: 'hijack', declaredBy: '@deepseek-ai/dsh-base',
  51. message: 'row "settings" is already declared by @deepseek-ai/dsh-base',
  52. },
  53. ])
  54. const stack = composeProfileStack(NAME, [base, hijack], [])
  55. expect(stack.skippedBundles).toEqual(['hijack'])
  56. expect(stack.owners.get('settings')?.packageName).toBe('@deepseek-ai/dsh-base')
  57. })
  58. it('lets a built-in layer restate its own rows through a config override, and rejects another layer\'s', () => {
  59. const restating = layer('restating', 'builtin', [
  60. { insert: [{ id: 'g', name: 'cordis:group', group: true, config: [{ id: 'a', name: 'a' }] }] },
  61. { id: 'g', config: [{ id: 'a', name: 'a' }, { id: 'b', name: 'b' }] },
  62. ])
  63. expect([...claimLayerIds([restating]).owners.keys()]).toEqual(['g', 'a', 'b'])
  64. const taking = layer('taking', 'builtin', [
  65. { insert: [{ id: 'h', name: 'cordis:group', group: true, config: [] }] },
  66. { id: 'h', config: [{ id: 'settings', name: 'taking/impostor' }] },
  67. ])
  68. expect(() => claimLayerIds([base, taking])).toThrow(/row "settings" is declared by both @deepseek-ai\/dsh-base and taking/)
  69. // Setting a row under another group would move it; listing it twice would fail the group's update.
  70. const moving = layer('moving', 'builtin', [
  71. { insert: [
  72. { id: 'g', name: 'cordis:group', group: true, config: [{ id: 'a', name: 'a' }] },
  73. { id: 'h', name: 'cordis:group', group: true, config: [] },
  74. ] },
  75. { id: 'h', config: [{ id: 'a', name: 'a' }] },
  76. ])
  77. expect(() => claimLayerIds([moving])).toThrow(/row "a" is declared twice by moving/)
  78. const twice = layer('twice', 'builtin', [
  79. { insert: [{ id: 'g', name: 'cordis:group', group: true, config: [] }] },
  80. { id: 'g', config: [{ id: 'a', name: 'a' }, { id: 'a', name: 'a' }] },
  81. ])
  82. expect(() => claimLayerIds([twice])).toThrow(/row "a" is declared twice by twice/)
  83. // A child of a group without an id cannot be restated: no patch can address that group.
  84. const anonymous = layer('anonymous', 'builtin', [
  85. { insert: [
  86. // A row leaves its id to the Loader, as YAML rows may; the type asks for one.
  87. { name: 'cordis:group', group: true, config: [{ id: 'a', name: 'a' }] } as unknown as EntryOptions,
  88. { id: 'h', name: 'cordis:group', group: true, config: [] },
  89. ] },
  90. { id: 'h', config: [{ id: 'a', name: 'a' }] },
  91. ])
  92. expect(() => claimLayerIds([anonymous])).toThrow(/row "a" is declared twice by anonymous/)
  93. })
  94. it('leaves out a bundle whose config override of its own group lists an id twice', () => {
  95. const self = layer('self', 'external', [
  96. { insert: [{ id: 'row', name: 'self/row' }] },
  97. { id: 'bundle/self', config: [{ id: 'row', name: 'self/row' }, { id: 'row', name: 'self/row-again' }] },
  98. ])
  99. const { skipped, composed } = claimLayerIds([base, self])
  100. expect(skipped.get('self')?.map(conflict => conflict.message)).toEqual(['row "row" is declared twice by self'])
  101. expect(composed.has('self')).toBe(false)
  102. })
  103. it('leaves out a bundle that declares one of its own ids twice and composes each mounted bundle once', () => {
  104. const stutter = layer('stutter', 'external', [{ insert: [{ id: 'x', name: 'stutter/a' }, { id: 'x', name: 'stutter/b' }] }])
  105. const clean = layer('clean', 'external', [{ insert: [{ id: 'y', name: 'clean' }] }])
  106. const { owners, skipped, composed } = claimLayerIds([base, stutter, clean])
  107. expect(skipped.get('stutter')).toEqual([
  108. { rowId: 'x', moduleName: 'stutter/b', layer: 'stutter', packageName: 'stutter', declaredBy: 'stutter', message: 'row "x" is declared twice by stutter' },
  109. ])
  110. expect(owners.has('x')).toBe(false)
  111. expect(owners.get('y')?.packageName).toBe('clean')
  112. expect([...composed.keys()]).toEqual(['clean'])
  113. expect(composed.get('clean')?.patches[1]).toEqual({ id: 'bundle/clean', insert: [{ id: 'y', name: 'clean' }] })
  114. })
  115. it('gives the earlier external bundle the id and leaves the later one out whole', () => {
  116. const first = layer('first', 'external', [{ insert: [{ id: 'hello', name: 'first' }, { id: 'only-first', name: 'first/x' }] }])
  117. const second = layer('second', 'external', [{ insert: [{ id: 'hello', name: 'second' }, { id: 'only-second', name: 'second/x' }] }])
  118. const { owners, skipped } = claimLayerIds([base, first, second])
  119. expect(owners.get('hello')?.packageName).toBe('first')
  120. expect(owners.get('bundle/first')?.packageName).toBe('first')
  121. expect(owners.has('only-second')).toBe(false)
  122. expect(skipped.get('second')?.map(conflict => conflict.rowId)).toEqual(['hello'])
  123. })
  124. })
  125. describe('composeProfileStack', () => {
  126. it('collects the rows the user layers disable with a literal disabled: true', () => {
  127. const stack = composeProfileStack(NAME, [base], [{ label: '/p/cordis.patch.yml', patches: [
  128. { id: 'a', disabled: true },
  129. // A gate read from disk is an expression node: a condition of the composition, not a decision.
  130. { id: 'b', disabled: { __jsExpr: 'true' } as unknown as boolean },
  131. { id: 'c', config: {} },
  132. { insert: [{ id: 'd', name: 'x', disabled: true }] },
  133. ] }])
  134. expect([...stack.userDisabledRowIds]).toEqual(['a'])
  135. })
  136. it('mounts owning layers in manifest order and drops a user insert of a taken id', () => {
  137. const ext = layer('ext', 'external', [{ insert: [{ id: 'ext-tool', name: 'ext' }] }])
  138. const stack = composeProfileStack(NAME, [base, ext], [
  139. { label: '/p/cordis.patch.yml', patches: [
  140. { id: 'settings', config: { path: '/x' } },
  141. { insert: [{ id: 'mine', name: 'mine' }, { id: 'ext-tool', name: 'clash' }] },
  142. { insert: [{ id: 'g', name: 'cordis:group', group: true, config: [{ id: 'tool-bash', name: 'nested-clash' }] }] },
  143. ] },
  144. { label: '/home/cordis.patch.yml', patches: [
  145. { insert: [{ id: 'mine', name: 'twice' }] },
  146. { insert: [{ id: 'clean', name: 'clean' }, { name: 'anonymous' } as EntryOptions] },
  147. ] },
  148. ])
  149. expect(stack.layers.map(current => current.label)).toEqual(['@deepseek-ai/dsh-base', 'ext', '/p/cordis.patch.yml', '/home/cordis.patch.yml'])
  150. expect(stack.layers[1]?.patches[0]?.insert?.[0]).toMatchObject({ id: 'bundle/ext', name: CONTAINED_GROUP_MODULE })
  151. expect([...stack.owners.keys()]).toEqual(['settings', 'tools', 'tool-bash', 'ext-tool', 'bundle/ext'])
  152. expect(stack.layers[2]?.patches).toEqual([
  153. { id: 'settings', config: { path: '/x' } },
  154. { insert: [{ id: 'mine', name: 'mine' }] },
  155. ])
  156. // An insert with no conflict passes through as the same object; an anonymous row claims nothing.
  157. expect(stack.layers[3]?.patches).toEqual([{ insert: [{ id: 'clean', name: 'clean' }, { name: 'anonymous' }] }])
  158. expect(stack.patches).toEqual(stack.layers.flatMap(current => current.patches))
  159. expect(stack.skippedBundles).toEqual([])
  160. expect(stack.conflicts).toEqual([
  161. { rowId: 'ext-tool', moduleName: 'clash', layer: '/p/cordis.patch.yml', declaredBy: 'ext', message: 'row "ext-tool" is already declared by ext' },
  162. {
  163. rowId: 'tool-bash', moduleName: 'cordis:group', layer: '/p/cordis.patch.yml', declaredBy: '@deepseek-ai/dsh-base',
  164. message: 'row "tool-bash" is already declared by @deepseek-ai/dsh-base',
  165. },
  166. {
  167. rowId: 'mine', moduleName: 'twice', layer: '/home/cordis.patch.yml', declaredBy: '/p/cordis.patch.yml',
  168. message: 'row "mine" is already declared by /p/cordis.patch.yml',
  169. },
  170. ])
  171. })
  172. it('leaves a colliding external bundle out of the stack and reports it', () => {
  173. const clash = layer('clash', 'external', [{ insert: [{ id: 'settings', name: 'clash' }] }])
  174. const stack = composeProfileStack(NAME, [base, clash], [])
  175. expect(stack.layers.map(current => current.label)).toEqual(['@deepseek-ai/dsh-base'])
  176. expect(stack.skippedBundles).toEqual(['clash'])
  177. expect(stack.conflicts).toEqual([
  178. {
  179. rowId: 'settings', moduleName: 'clash', layer: 'clash', packageName: 'clash', declaredBy: '@deepseek-ai/dsh-base',
  180. message: 'row "settings" is already declared by @deepseek-ai/dsh-base',
  181. },
  182. ])
  183. })
  184. it('prefixes a built-in duplicate with the binary name', () => {
  185. const twin = layer('twin', 'builtin', [{ insert: [{ id: 'settings', name: 'twin' }] }])
  186. expect(() => composeProfileStack(NAME, [base, twin], [])).toThrow(/^dsh-test-bin: row "settings" is declared by both/)
  187. })
  188. })
  189. describe('formatRowConflict', () => {
  190. it('names the bundle left out, or the user layer whose insert was skipped', () => {
  191. const message = 'row "x" is already declared by base'
  192. expect(formatRowConflict({ rowId: 'x', moduleName: 'm', layer: 'pkg', packageName: 'pkg', declaredBy: 'base', message }))
  193. .toBe('bundle pkg left out — row "x" is already declared by base')
  194. expect(formatRowConflict({ rowId: 'x', moduleName: 'm', layer: '/p/cordis.patch.yml', declaredBy: 'base', message }))
  195. .toBe('/p/cordis.patch.yml: insert of m skipped — row "x" is already declared by base')
  196. })
  197. })