github-repository-plugin.built.e2e.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
  2. import { createRequire } from 'node:module'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import { startMockLlmServer } from '@deepseek-ai/dsh-llm-mock-server'
  7. import { execa } from 'execa'
  8. import { describe, expect, it } from 'vitest'
  9. const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
  10. const dshBin = join(repoRoot, 'apps/cli/lib/bin.js')
  11. const source = process.env.DSH_GITHUB_REPOSITORY_PLUGIN_SOURCE
  12. const required = process.env.DSH_REQUIRE_GITHUB_REPOSITORY_PLUGIN_E2E === '1'
  13. const enabled = required || source !== undefined
  14. describe.skipIf(!enabled)('dsh run GitHub repository Plugin installation', () => {
  15. it('installs, builds, and runs skill, MCP, and TypeScript Plugin contributions from a private exact GitHub source', async () => {
  16. expect(existsSync(dshBin), 'the repository Plugin acceptance must run the built dsh entry').toBe(true)
  17. expect(source, 'DSH_GITHUB_REPOSITORY_PLUGIN_SOURCE is required by this CI lane').toMatch(
  18. /^github:[^/\s#&]+\/[^/\s#&]+#[0-9a-f]{40}&path:\/.*\/\.dsh-plugin$/u,
  19. )
  20. const apiKey = 'github-repository-plugin-e2e-key'
  21. const server = await startMockLlmServer({
  22. sequence: ['tool_call_success', 'success'],
  23. apiKey,
  24. toolName: 'mcp__github_repository__proof',
  25. toolArguments: '{}',
  26. successText: 'trusted GitHub repository package reached dsh run',
  27. })
  28. const home = mkdtempSync(join(tmpdir(), 'dsh-github-repository-plugin-'))
  29. const patch = join(home, 'github-repository-plugin.cordis.patch.yml')
  30. writeFileSync(patch, [
  31. '- id: repository-plugins',
  32. ' config:',
  33. ' repositories:',
  34. ` - ${JSON.stringify(source)}`,
  35. '',
  36. ].join('\n'))
  37. try {
  38. const result = await execa(process.execPath, [
  39. dshBin,
  40. 'run',
  41. '--patch',
  42. patch,
  43. 'prove the private GitHub repository Plugin is active',
  44. ], {
  45. cwd: repoRoot,
  46. input: '',
  47. timeout: 180_000,
  48. killSignal: 'SIGKILL',
  49. reject: false,
  50. env: {
  51. ...process.env,
  52. DSH_HOME: home,
  53. DSH_TELEMETRY_DISABLED: '1',
  54. DEEPSEEK_API_KEY: apiKey,
  55. DEEPSEEK_BASE_URL: server.baseURL,
  56. },
  57. })
  58. if (result.timedOut) {
  59. throw new Error(`dsh GitHub repository Plugin run did not exit within 180s. stdout:\n${result.stdout}\nstderr:\n${result.stderr}`)
  60. }
  61. expect(result.exitCode, `${result.stderr}\nstdout:\n${result.stdout}`).toBe(0)
  62. expect(result.stdout).toBe('trusted GitHub repository package reached dsh run')
  63. expect(server.requests).toHaveLength(2)
  64. const firstRequest = JSON.stringify(server.requests[0]!.body)
  65. const secondRequest = JSON.stringify(server.requests[1]!.body)
  66. expect(firstRequest).toContain(
  67. 'Proves that dsh installed a private repository Plugin from an exact GitHub source.',
  68. )
  69. expect(firstRequest).toContain('mcp__github_repository__proof')
  70. expect(firstRequest).toContain('Proves that an MCP server compiled from the exact GitHub repository package is active.')
  71. expect(secondRequest).toContain('MCP_FROM_GITHUB_REPOSITORY')
  72. expect(secondRequest).toContain('TS_PLUGIN_FROM_GITHUB_REPOSITORY')
  73. const cacheRoot = join(home, 'cache', 'repository-plugins')
  74. const generations = readdirSync(cacheRoot, { withFileTypes: true }).filter(entry => entry.isDirectory())
  75. expect(generations).toHaveLength(1)
  76. const installed = join(cacheRoot, generations[0]!.name, 'node_modules', 'repository')
  77. const manifest = JSON.parse(readFileSync(join(installed, 'package.json'), 'utf8')) as Record<string, unknown>
  78. expect(manifest).toMatchObject({
  79. name: 'dsh-github-repository-plugin-e2e-fixture',
  80. private: true,
  81. scripts: {
  82. prepack: 'tsc --noEmit && tsdown src/plugin.ts src/mcp-server.ts --no-config --tsconfig tsconfig.json --out-dir lib --platform node --target es2024 --clean && dsh-plugin-prepare',
  83. },
  84. dsh: {
  85. skills: ['../skills'],
  86. mcpServers: './.mcp.json',
  87. entry: './lib/plugin.mjs',
  88. },
  89. dependencies: {
  90. '@modelcontextprotocol/sdk': '1.29.0',
  91. },
  92. devDependencies: {
  93. cordis: '4.0.0-rc.7',
  94. tsdown: '0.22.2',
  95. typescript: '6.0.3',
  96. },
  97. })
  98. expect(readFileSync(join(installed, 'dsh-plugin-assets/skills/0/github-source-proof/SKILL.md'), 'utf8'))
  99. .toContain('This skill exists only in the GitHub repository source fixture.')
  100. expect(readFileSync(join(installed, 'dsh-plugin-assets/.mcp.json'), 'utf8')).toContain('lib/mcp-server.mjs')
  101. expect(readFileSync(join(installed, 'lib/plugin.mjs'), 'utf8')).toContain('TS_PLUGIN_FROM_GITHUB_REPOSITORY')
  102. expect(readFileSync(join(installed, 'lib/mcp-server.mjs'), 'utf8')).toContain('MCP_FROM_GITHUB_REPOSITORY')
  103. expect(existsSync(join(installed, 'src'))).toBe(false)
  104. const installedRequire = createRequire(join(installed, 'lib/mcp-server.mjs'))
  105. expect(existsSync(installedRequire.resolve('@modelcontextprotocol/sdk/server/mcp.js'))).toBe(true)
  106. const wrapper = readFileSync(join(installed, 'dsh-plugin.mjs'), 'utf8')
  107. expect(wrapper).toContain('dsh-repository-plugin')
  108. expect(wrapper).toContain('await import(manifest.entry)')
  109. expect(wrapper).toContain('"entry":"./lib/plugin.mjs"')
  110. } finally {
  111. await server.close()
  112. rmSync(home, { recursive: true, force: true })
  113. }
  114. }, 190_000)
  115. })