compose-stack.spec.ts 7.6 KB

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