link-workspace.e2e.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { execFile } from 'node:child_process'
  2. import { existsSync } from 'node:fs'
  3. import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
  4. import { homedir, tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { fileURLToPath } from 'node:url'
  7. import { promisify } from 'node:util'
  8. import { afterEach, describe, expect, it } from 'vitest'
  9. import {
  10. LocalPluginBlueprint,
  11. featureId,
  12. createPackageManager,
  13. type PackageManagerName,
  14. } from '@deepseek-ai/dsh-helper'
  15. import { scrubEnvironment } from '../../helper/src/package-managers/package-manager.ts'
  16. import { scaffoldProject } from '../src/project-scaffolder.ts'
  17. const execFileAsync = promisify(execFile)
  18. const repoRoot = fileURLToPath(new URL('../../../../', import.meta.url))
  19. const builtScripts = join(repoRoot, 'packages/sdk/scripts/lib/bin.js')
  20. const temporary: string[] = []
  21. function resolveCorepackHome(): string {
  22. return process.env.COREPACK_HOME ?? join(
  23. process.env.XDG_CACHE_HOME
  24. ?? process.env.LOCALAPPDATA
  25. ?? join(homedir(), process.platform === 'win32' ? 'AppData/Local' : '.cache'),
  26. 'node/corepack',
  27. )
  28. }
  29. afterEach(async () => {
  30. await Promise.all(temporary.splice(0).map(path => rm(path, { recursive: true, force: true })))
  31. })
  32. async function managerVersion(name: PackageManagerName): Promise<string | undefined> {
  33. try {
  34. return (await execFileAsync(name, ['--version'], { encoding: 'utf8' })).stdout.trim()
  35. } catch {
  36. // An unavailable optional manager skips only its own live-link case.
  37. return undefined
  38. }
  39. }
  40. const managers: PackageManagerName[] = ['npm', 'pnpm', 'yarn']
  41. describe.skipIf(!existsSync(builtScripts))('live-linked generated projects', () => {
  42. for (const name of managers) {
  43. it(`${name}: installs the local closure and resolves plugin TypeScript in dev`, async (context) => {
  44. const version = await managerVersion(name)
  45. if (!version) {
  46. context.skip()
  47. return
  48. }
  49. const parent = await mkdtemp(join(tmpdir(), `dsh-link-${name}-`))
  50. const root = join(parent, 'project')
  51. temporary.push(parent)
  52. const manager = createPackageManager(name, version)
  53. await scaffoldProject(root, {
  54. name: `linked-${name}`,
  55. description: 'link e2e',
  56. runtime: { model: 'deepseek-v4-flash' },
  57. packageManager: manager,
  58. releaseVersion: '0.0.1',
  59. linkWorkspaceRoot: repoRoot,
  60. features: [
  61. { id: featureId('provider'), options: ['deepseek-official'], secrets: { apiKey: 'test-key' } },
  62. { id: featureId('bash'), options: ['local'] },
  63. { id: featureId('app'), options: ['embed'] },
  64. { id: featureId('persistence'), options: ['jsonl'] },
  65. ],
  66. localPlugins: [new LocalPluginBlueprint('probe', 'plugin')],
  67. })
  68. await writeFile(join(root, 'plugins/probe/src/index.ts'), `
  69. import { writeFileSync } from 'node:fs'
  70. import type { Context } from 'cordis'
  71. export const name = 'probe'
  72. export function apply(_ctx: Context): void {
  73. writeFileSync(new URL('../../../plugin-loaded', import.meta.url), 'loaded\\n')
  74. }
  75. `)
  76. const cacheRoot = join(tmpdir(), 'dsh-sdk-link-cache', name)
  77. const pnpmStore = name === 'pnpm'
  78. ? (await execFileAsync(name, ['store', 'path', '--silent'], { encoding: 'utf8' })).stdout.trim()
  79. : undefined
  80. const commandEnvironment = {
  81. ...scrubEnvironment(),
  82. COREPACK_HOME: resolveCorepackHome(),
  83. ...name === 'pnpm' ? {} : { XDG_CACHE_HOME: join(cacheRoot, 'cache') },
  84. XDG_DATA_HOME: join(cacheRoot, 'data'),
  85. npm_config_cache: join(cacheRoot, 'npm'),
  86. ...pnpmStore === undefined ? {} : { pnpm_config_store_dir: pnpmStore },
  87. // A generated project has no lockfile yet; ambient CI must not make its first Yarn install immutable.
  88. ...name === 'yarn' ? { YARN_ENABLE_IMMUTABLE_INSTALLS: 'false' } : {},
  89. }
  90. await execFileAsync(name, manager.installCommand(), {
  91. cwd: root,
  92. env: commandEnvironment,
  93. encoding: 'utf8',
  94. timeout: 120_000,
  95. })
  96. await execFileAsync(name, manager.buildCommand(), {
  97. cwd: root,
  98. env: commandEnvironment,
  99. encoding: 'utf8',
  100. timeout: 120_000,
  101. })
  102. expect(existsSync(join(root, 'index.js'))).toBe(true)
  103. expect(existsSync(join(root, 'plugins/probe/lib/index.js'))).toBe(true)
  104. const dshSdk = join(root, 'node_modules/@deepseek-ai/dsh-scripts/lib/bin.js')
  105. const run = await execFileAsync(process.execPath, [dshSdk, 'dev', 'index.ts'], {
  106. cwd: root,
  107. env: { ...commandEnvironment, DEEPSEEK_API_KEY: 'test-key' },
  108. encoding: 'utf8',
  109. timeout: 30_000,
  110. })
  111. expect(run.stderr).not.toContain('without inject')
  112. expect(await readFile(join(root, 'plugin-loaded'), 'utf8')).toBe('loaded\n')
  113. const manifest = JSON.parse(await readFile(join(root, 'package.json'), 'utf8')) as {
  114. dependencies: Record<string, string>
  115. }
  116. expect(manifest.dependencies.cordis).toMatch(name === 'npm' ? /^file:/ : name === 'pnpm' ? /^link:/ : /^portal:/)
  117. expect(manifest.dependencies).not.toHaveProperty('node-addon-require-builtin')
  118. }, 180_000)
  119. }
  120. })