verify-runtime-closure.spec.ts 6.1 KB

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