smoke-runtime.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /** Boot the materialized target runtime without access to a user's Harness profile. */
  2. import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { DesktopHostProcess } from '../src/host-process.ts'
  6. import { createPluginProfile } from '../src/project-manager.ts'
  7. import { linkDesktopHostPackages, validateDesktopPluginGraph } from '../src/profile-packages.ts'
  8. import type { DesktopRuntimeDescriptor } from '../src/runtime-tree.ts'
  9. /**
  10. * Prove the final resource tree boots and serves its matching Web frontend.
  11. * @param root - Materialized dsh resources.
  12. * @param node - Prepared target Node executable.
  13. * @param runtime - Verified resource descriptor.
  14. */
  15. export async function smokeDesktopRuntime(root: string, node: string, runtime: DesktopRuntimeDescriptor): Promise<void> {
  16. const home = mkdtempSync(join(tmpdir(), 'dsh-desktop-smoke-'))
  17. const profile = join(home, 'profiles', 'desktop')
  18. const host = new DesktopHostProcess(node, root, profile, undefined, { ...process.env, DSH_HOME: home })
  19. try {
  20. createPluginProfile(profile)
  21. const pluginName = 'desktop-runtime-smoke-plugin'
  22. const plugin = join(profile, 'node_modules', pluginName)
  23. mkdirSync(plugin, { recursive: true })
  24. const cordis = runtime.sharedPackages.find(entry => entry.name === '@deepseek-ai/cordis')
  25. if (cordis === undefined) throw new Error('desktop runtime: missing shared Cordis package')
  26. writeFileSync(join(plugin, 'package.json'), JSON.stringify({
  27. name: pluginName, version: '1.0.0', type: 'module', exports: './index.js',
  28. peerDependencies: { '@deepseek-ai/cordis': cordis.version }, dsh: { bundle: { patch: './bundle.yml' } },
  29. }))
  30. writeFileSync(join(plugin, 'index.js'), `
  31. import { Context } from '@deepseek-ai/cordis'
  32. export function apply(ctx) {
  33. if (!(ctx instanceof Context)) throw new Error('desktop runtime: external plugin loaded another Cordis instance')
  34. }
  35. `)
  36. writeFileSync(join(plugin, 'bundle.yml'), '- insert:\n - id: desktop-runtime-smoke-plugin\n name: desktop-runtime-smoke-plugin\n')
  37. const manifest = JSON.parse(readFileSync(join(profile, 'package.json'), 'utf8')) as {
  38. dependencies: Record<string, string>
  39. dsh: { profile: { bundles: string[] } }
  40. }
  41. manifest.dependencies[pluginName] = '1.0.0'
  42. manifest.dsh.profile.bundles.push(pluginName)
  43. writeFileSync(join(profile, 'package.json'), JSON.stringify(manifest))
  44. linkDesktopHostPackages(profile, root, runtime)
  45. validateDesktopPluginGraph(profile, root, runtime, [pluginName])
  46. const ready = await host.start()
  47. if (ready.dshVersion !== runtime.release.version) throw new Error('desktop runtime: Host reported another dsh release')
  48. const response = await host.fetch(new Request('dsh-app://app/'))
  49. if (response.status !== 200 || !(await response.text()).includes('<html')) {
  50. throw new Error('desktop runtime: packaged frontend smoke failed')
  51. }
  52. } finally {
  53. await host.stop()
  54. rmSync(home, { recursive: true, force: true })
  55. }
  56. }