plugin-pnpm.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { createHash } from 'node:crypto'
  2. import { execFileSync } from 'node:child_process'
  3. import { createServer } from 'node:http'
  4. import { mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
  5. import { tmpdir } from 'node:os'
  6. import { join } from 'node:path'
  7. import { pathToFileURL } from 'node:url'
  8. import { c } from 'tar'
  9. import { expect, it } from 'vitest'
  10. import { DesktopProjectManager, type DesktopProjectHooks } from '../src/project-manager.ts'
  11. import { resolveDesktopPaths } from '../src/paths.ts'
  12. import { runtimeFixture, writePackage } from './runtime-fixture.ts'
  13. it('installs a real pnpm graph, then executes approved scripts with the shared host instance', async () => {
  14. const root = realpathSync(mkdtempSync(join(tmpdir(), 'desktop-real-pnpm-')))
  15. const server = createServer()
  16. const archives = new Map<string, Buffer>()
  17. try {
  18. for (const name of ['fixture-plugin', 'node-pty']) {
  19. const path = writePackage(join(root, 'packages'), name, name === 'fixture-plugin'
  20. ? { dependencies: { 'node-pty': '1.0.0' }, peerDependencies: { '@deepseek-ai/cordis': '^1.0.0' }, dsh: { bundle: { patch: 'bundle.yml' } } }
  21. : { scripts: { install: 'node install.cjs' } }, 'export {identity} from "@deepseek-ai/cordis"')
  22. writeFileSync(join(path, 'bundle.yml'), '[]\n')
  23. writeFileSync(join(path, 'install.cjs'), 'require("node:fs").writeFileSync("built.json", JSON.stringify({node:process.execPath, host:require.resolve("@deepseek-ai/cordis")}))')
  24. const tarball = join(root, `${name}.tgz`)
  25. await c({ file: tarball, cwd: join(path, '..'), gzip: true }, [name])
  26. archives.set(name, readFileSync(tarball))
  27. }
  28. await new Promise<void>((resolve, reject) => { server.once('error', reject); server.listen(0, '127.0.0.1', resolve) })
  29. const address = server.address()
  30. if (address === null || typeof address === 'string') throw new Error('fixture registry has no TCP address')
  31. const origin = `http://127.0.0.1:${address.port}`
  32. server.on('request', (request, response) => {
  33. const name = request.url?.slice(1).replace(/\.tgz$/u, '') ?? ''
  34. const archive = archives.get(name)
  35. if (archive === undefined) { response.writeHead(404); response.end(); return }
  36. if (request.url?.endsWith('.tgz')) { response.end(archive); return }
  37. response.setHeader('content-type', 'application/json')
  38. response.end(JSON.stringify({ name, 'dist-tags': { latest: '1.0.0' }, versions: { '1.0.0': {
  39. name, version: '1.0.0', dist: { tarball: `${origin}/${name}.tgz`, integrity: `sha512-${createHash('sha512').update(archive).digest('base64')}` },
  40. ...(name === 'fixture-plugin' ? { dependencies: { 'node-pty': '1.0.0' }, peerDependencies: { '@deepseek-ai/cordis': '^1.0.0' } } : {}),
  41. } }, time: { '1.0.0': '2020-01-01T00:00:00.000Z' } }))
  42. })
  43. const dsh = join(root, 'dsh')
  44. runtimeFixture(dsh)
  45. const pnpm = join(root, 'pnpm.mjs')
  46. const realPnpm = join(import.meta.dirname, '../node_modules/pnpm/bin/pnpm.mjs')
  47. writeFileSync(pnpm, `process.argv = process.argv.map(arg => arg === '--config.registry=https://registry.npmjs.org/' ? ${JSON.stringify(`--config.registry=${origin}`)} : arg); await import(${JSON.stringify(pathToFileURL(realPnpm).href)})`)
  48. const manager = new DesktopProjectManager(resolveDesktopPaths(join(root, '.dsh')), { node: process.execPath, pnpm, dsh })
  49. const hooks: DesktopProjectHooks = { beforeChange: async () => {}, afterChange: async () => {} }
  50. await manager.applyRelease()
  51. await manager.mutate({ type: 'plugin-add', spec: 'fixture-plugin@1.0.0' }, hooks)
  52. expect(manager.listPlugins()).toEqual([{ name: 'fixture-plugin', version: '1.0.0', enabled: true }])
  53. const built = JSON.parse(readFileSync(join(manager.paths.profile, 'node_modules/node-pty/built.json'), 'utf8')) as { node: string; host: string }
  54. expect(realpathSync(built.node)).toBe(realpathSync(process.execPath))
  55. expect(built.host).toBe(join(dsh, 'node_modules/@deepseek-ai/cordis/index.js'))
  56. const entry = join(dsh, 'identity.mjs')
  57. writeFileSync(entry, `import {identity} from '@deepseek-ai/cordis'; import {identity as plugin} from ${JSON.stringify(pathToFileURL(join(manager.paths.profile, 'node_modules/fixture-plugin/index.js')).href)}; console.log(identity === plugin)`)
  58. expect(execFileSync(process.execPath, [entry], { encoding: 'utf8' }).trim()).toBe('true')
  59. await manager.mutate({ type: 'plugin-remove', name: 'fixture-plugin' }, hooks)
  60. expect(manager.listPlugins()).toEqual([])
  61. } finally {
  62. server.closeAllConnections()
  63. if (server.listening) await new Promise<void>((resolve, reject) => {
  64. server.close((error) => {
  65. if (error !== undefined) reject(error)
  66. else resolve()
  67. })
  68. })
  69. rmSync(root, { recursive: true, force: true })
  70. }
  71. }, 30_000)