verify-runtime-closure.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { dirname, join } from 'node:path'
  4. import { afterEach, describe, expect, it } from 'vitest'
  5. import { verifyRuntimeClosure } from './verify-runtime-closure.ts'
  6. const roots: string[] = []
  7. function fixture(files: Record<string, string | Record<string, unknown>>): string {
  8. const root = mkdtempSync(join(tmpdir(), 'dsh-runtime-closure-'))
  9. roots.push(root)
  10. for (const [relative, value] of Object.entries(files)) {
  11. const path = join(root, relative)
  12. mkdirSync(dirname(path), { recursive: true })
  13. writeFileSync(path, typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`)
  14. }
  15. return root
  16. }
  17. const platforms = {
  18. 'linux-x64': { tag: 'manylinux_2_28_x86_64', executable: 'runtime-linux-x64' },
  19. 'linux-arm64': { tag: 'manylinux_2_28_aarch64', executable: 'runtime-linux-arm64' },
  20. 'macos-arm64': { tag: 'macosx_14_0_arm64', executable: 'runtime-macos-arm64' },
  21. 'macos-x64': { tag: 'macosx_14_0_x86_64', executable: 'runtime-macos-x64' },
  22. 'win-x64': { tag: 'win_amd64', executable: 'runtime-win-x64.exe' },
  23. }
  24. function workspace(root: string, name: string, manifest: Record<string, unknown>): void {
  25. const packageName = name.replace('@scope/', '')
  26. const path = join(root, 'packages/core', packageName, 'package.json')
  27. mkdirSync(dirname(path), { recursive: true })
  28. writeFileSync(path, `${JSON.stringify({ name, ...manifest }, null, 2)}\n`)
  29. }
  30. afterEach(() => {
  31. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  32. })
  33. describe('verifyRuntimeClosure', () => {
  34. it('requires only plugins active for each published target', async () => {
  35. const root = fixture({
  36. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: { '@scope/shared': 'workspace:^' } },
  37. 'python/sdk-runtime/platforms.json': platforms,
  38. 'packages/preset/agent-presets/presets/standard/agent.cordis.yml': `
  39. - id: tools
  40. name: cordis:group
  41. group: true
  42. config:
  43. - id: shared
  44. name: '@scope/shared'
  45. - id: linux
  46. name: '@scope/linux'
  47. disabled: !!js process.platform !== 'linux'
  48. - id: macos
  49. name: '@scope/macos'
  50. disabled: !!js process.platform !== 'darwin'
  51. - id: windows
  52. name: '@scope/windows'
  53. disabled: !!js process.platform !== 'win32'
  54. `,
  55. })
  56. const result = await verifyRuntimeClosure(root)
  57. expect(result.presetCount).toBe(1)
  58. expect(result.failures).toEqual([
  59. 'standard preset -> @scope/linux (linux-arm64, linux-x64)',
  60. 'standard preset -> @scope/macos (macos-arm64, macos-x64)',
  61. 'standard preset -> @scope/windows (win-x64)',
  62. ])
  63. })
  64. it('treats an unsupported disabled expression as active on every target', async () => {
  65. const root = fixture({
  66. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: {} },
  67. 'python/sdk-runtime/platforms.json': platforms,
  68. 'packages/preset/agent-presets/presets/standard/agent.cordis.yml': `
  69. - id: conditional
  70. name: '@scope/conditional'
  71. disabled: !!js process.env.DSH_DISABLE_CONDITIONAL === '1'
  72. `,
  73. })
  74. const result = await verifyRuntimeClosure(root)
  75. expect(result.failures).toEqual([
  76. 'standard preset -> @scope/conditional (linux-arm64, linux-x64, macos-arm64, macos-x64, win-x64)',
  77. ])
  78. })
  79. it('does not interpret an ordinary plugin array config as nested Loader entries', async () => {
  80. const root = fixture({
  81. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: { '@scope/plugin': 'workspace:^' } },
  82. 'python/sdk-runtime/platforms.json': platforms,
  83. 'packages/preset/agent-presets/presets/standard/agent.cordis.yml': `
  84. - id: plugin
  85. name: '@scope/plugin'
  86. config:
  87. - name: '@scope/config-value'
  88. `,
  89. })
  90. const result = await verifyRuntimeClosure(root)
  91. expect(result.failures).toEqual([])
  92. })
  93. it('requires preset plugins to be linked from the workspace', async () => {
  94. const root = fixture({
  95. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: { '@scope/plugin': '1.2.3' } },
  96. 'python/sdk-runtime/platforms.json': platforms,
  97. 'packages/preset/agent-presets/presets/standard/agent.cordis.yml': `
  98. - id: plugin
  99. name: '@scope/plugin'
  100. `,
  101. })
  102. const result = await verifyRuntimeClosure(root)
  103. expect(result.failures).toEqual([
  104. 'standard preset -> @scope/plugin [runtime dependency is "1.2.3"; expected workspace:] (linux-arm64, linux-x64, macos-arm64, macos-x64, win-x64)',
  105. ])
  106. })
  107. it('fails when no shipped preset is discovered', async () => {
  108. const root = fixture({
  109. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: {} },
  110. 'python/sdk-runtime/platforms.json': platforms,
  111. })
  112. const result = await verifyRuntimeClosure(root)
  113. expect(result.presetCount).toBe(0)
  114. expect(result.failures).toEqual([
  115. 'no agent presets matched packages/preset/agent-presets/presets/*/agent.cordis.yml',
  116. ])
  117. })
  118. it('fails when the runtime platform manifest has no targets', async () => {
  119. const root = fixture({
  120. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: {} },
  121. 'python/sdk-runtime/platforms.json': {},
  122. 'packages/preset/agent-presets/presets/standard/agent.cordis.yml': '[]\n',
  123. })
  124. const result = await verifyRuntimeClosure(root)
  125. expect(result.failures).toEqual([
  126. 'python/sdk-runtime/platforms.json defines no runtime targets',
  127. ])
  128. })
  129. it('retains the required workspace-peer closure check', async () => {
  130. const root = fixture({
  131. 'python/sdk-runtime/package.json': { name: 'runtime', dependencies: { '@scope/root': 'workspace:^' } },
  132. 'python/sdk-runtime/platforms.json': platforms,
  133. 'packages/preset/agent-presets/presets/minimal/agent.cordis.yml': '[]\n',
  134. })
  135. workspace(root, '@scope/root', {
  136. peerDependencies: { '@scope/required': 'workspace:^', '@scope/optional': 'workspace:^' },
  137. peerDependenciesMeta: { '@scope/optional': { optional: true } },
  138. })
  139. workspace(root, '@scope/required', {})
  140. workspace(root, '@scope/optional', {})
  141. const result = await verifyRuntimeClosure(root)
  142. expect(result.workspacePackageCount).toBe(1)
  143. expect(result.failures).toEqual(['runtime -> @scope/root -> @scope/required'])
  144. })
  145. })