1
0

dev-web.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { expect, it } from 'vitest'
  5. import type { TsdownBundle } from 'tsdown'
  6. import { writeClientBuildRecord } from './client-build-environment.ts'
  7. import {
  8. devWebBuildEnvironment,
  9. discoverLibraryDirs,
  10. discoverPluginDirs,
  11. watchClientPlugins,
  12. } from './dev-web.ts'
  13. it('samples one local environment at startup without validating watcher outputs', async () => {
  14. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-environment-'))
  15. try {
  16. await mkdir(join(root, 'apps/web/dist'), { recursive: true })
  17. await mkdir(join(root, 'packages/client/example/lib'), { recursive: true })
  18. await writeFile(join(root, 'package.json'), JSON.stringify({ version: '1.2.3' }))
  19. await writeFile(join(root, 'apps/web/dist/index.html'), '<main></main>')
  20. await writeFile(join(root, 'packages/client/example/lib/client.js'), 'module.exports = {}\n')
  21. writeClientBuildRecord(root, {
  22. DSH_CLIENT_BUILD_PROFILE: 'official',
  23. DSH_CLIENT_COMMIT_HASH: 'fffffff',
  24. DSH_CLIENT_TITLE: 'DeepSeek Harness',
  25. DSH_CLIENT_VERSION: '1.2.2',
  26. })
  27. await writeFile(join(root, 'packages/client/example/lib/client.js'), 'module.exports = { changed: true }\n')
  28. expect(devWebBuildEnvironment(root, {
  29. PATH: '/bin',
  30. DSH_BUILD_CLIENT_PROFILE: 'official',
  31. DSH_CLIENT_COMMIT_HASH: 'abc1234',
  32. DSH_CLIENT_EXTRA: 'launch-value',
  33. })).toEqual({
  34. PATH: '/bin',
  35. DSH_CLIENT_COMMIT_HASH: 'abc1234',
  36. DSH_CLIENT_EXTRA: 'launch-value',
  37. DSH_CLIENT_VERSION: '1.2.3',
  38. })
  39. } finally {
  40. await rm(root, { recursive: true, force: true })
  41. }
  42. })
  43. it('discovers dsh.client packages with sibling roles', async () => {
  44. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-discovery-'))
  45. try {
  46. const current = join(root, 'packages', 'client', 'current')
  47. await mkdir(current, { recursive: true })
  48. await writeFile(join(current, 'package.json'), JSON.stringify({
  49. dsh: {
  50. bundle: { patch: './cordis.patch.yml' },
  51. client: { platform: 'web' },
  52. profile: { bundles: [] },
  53. },
  54. }))
  55. expect(discoverPluginDirs(root)).toEqual(['packages/client/current'])
  56. } finally {
  57. await rm(root, { recursive: true, force: true })
  58. }
  59. })
  60. it('discovers client-preset packages the shell links, excluding loader-delivered and test infrastructure', async () => {
  61. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-library-'))
  62. try {
  63. const write = async (dir: string, manifest: unknown, config: string): Promise<void> => {
  64. await mkdir(join(root, dir), { recursive: true })
  65. await writeFile(join(root, dir, 'package.json'), JSON.stringify(manifest))
  66. await writeFile(join(root, dir, 'tsdown.config.ts'), config)
  67. }
  68. const clientPreset = "import { clientLibrary } from '../tsdown.client.ts'\nexport default clientLibrary('x', [])\n"
  69. // Linked by the compile shell: client preset, no loader-delivered half.
  70. await write('packages/client/linked', {}, clientPreset)
  71. // Loader-delivered: discoverPluginDirs owns it, so it must not appear twice.
  72. await write('packages/client/delivered', { dsh: { client: { platform: 'web' } } }, clientPreset)
  73. // Test infrastructure builds through the preset but never enters the shell graph.
  74. await write('packages/test-support/harness', {}, clientPreset)
  75. // Host package with its own config: not a client-face build at all.
  76. await write('packages/host/server', {}, "import { defineConfig } from 'tsdown'\nexport default defineConfig({})\n")
  77. expect(discoverLibraryDirs(root)).toEqual(['packages/client/linked'])
  78. } finally {
  79. await rm(root, { recursive: true, force: true })
  80. }
  81. })
  82. it('rebuilds a client-plugin bundle after its source changes', async () => {
  83. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-watch-'))
  84. let bundles: TsdownBundle[] = []
  85. try {
  86. await symlink(join(import.meta.dirname, '..', 'node_modules'), join(root, 'node_modules'), 'dir')
  87. await writeFile(join(root, 'package.json'), JSON.stringify({ name: '@dsh-test/dev-web-watch', private: true, type: 'module' }))
  88. await writeFile(join(root, 'tsdown.config.ts'), `
  89. import { defineConfig } from 'tsdown'
  90. export default defineConfig({
  91. entry: { client: 'src.ts' }, outDir: 'lib', format: 'cjs', platform: 'browser', dts: false, clean: false,
  92. outputOptions: { entryFileNames: 'client.js' },
  93. })
  94. `)
  95. const sourcePath = join(root, 'src.ts')
  96. const bundlePath = join(root, 'lib/client.js')
  97. await writeFile(sourcePath, 'export const version = "watch-v1"\n')
  98. bundles = await watchClientPlugins(root, ['.'], 50)
  99. expect(await readFile(bundlePath, 'utf8')).toContain('watch-v1')
  100. await new Promise(resolve => setTimeout(resolve, 1_000))
  101. await writeFile(sourcePath, `export const version = "watch-v2-${'x'.repeat(100)}"\n`)
  102. await expect.poll(async () => (await readFile(bundlePath, 'utf8')).includes('watch-v2-'), {
  103. timeout: 10_000,
  104. }).toBe(true)
  105. } finally {
  106. for (const bundle of bundles) await bundle[Symbol.asyncDispose]()
  107. await rm(root, { recursive: true, force: true })
  108. }
  109. }, 20_000)