startup-bundle.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /** Installed bundle provenance for product-profile startup acceptance tests. */
  2. import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
  3. import { join } from 'node:path'
  4. import {
  5. initProfile, loadOverlayPatches, PROFILE_TEMPLATES, readProfileManifest, writeProfileManifest, type ProfileLayer,
  6. } from '@deepseek-ai/dsh-app-boot'
  7. /**
  8. * Install a patch as a real profile dependency, preserving its module resolution anchors.
  9. * @param home - the scenario's isolated Harness home.
  10. * @param profile - the shipped profile under test.
  11. * @param patchFile - the fixture patch whose relative module names resolve at its source.
  12. * @param stage - whether this external bundle is required or optional at startup.
  13. */
  14. export function stageStartupBundle(
  15. home: string,
  16. profile: 'web' | 'sdk' | 'headless',
  17. patchFile: string,
  18. stage: ProfileLayer['stage'] = 'runtime',
  19. ): void {
  20. const dir = join(home, 'profiles', profile)
  21. const template = PROFILE_TEMPLATES[profile]
  22. if (template === undefined) throw new Error(`Missing shipped profile template: ${profile}`)
  23. if (!existsSync(join(dir, 'package.json'))) initProfile(dir, template.bundles, template.patchReload)
  24. const name = '@fixture/startup-policy'
  25. const bundle = join(dir, 'node_modules', name)
  26. mkdirSync(bundle, { recursive: true })
  27. writeFileSync(join(bundle, 'package.json'), JSON.stringify({
  28. name, version: '1.0.0', private: true, type: 'module', dsh: { bundle: { patch: './cordis.patch.json', stage } },
  29. }))
  30. writeFileSync(join(bundle, 'cordis.patch.json'), JSON.stringify(loadOverlayPatches('startup fixture', patchFile)))
  31. const manifest = readProfileManifest('startup fixture', dir)
  32. writeProfileManifest(dir, {
  33. ...manifest,
  34. dependencies: { ...manifest.dependencies, [name]: '1.0.0' },
  35. dsh: { ...manifest.dsh, profile: { ...manifest.dsh?.profile, bundles: [...manifest.dsh?.profile?.bundles ?? [], name] } },
  36. })
  37. }