client-bundle-purity.spec.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Pins shared client-bundle preset rules: the module-edge purity gate and
  3. * the physical watch dependencies hidden behind virtual CSS Modules.
  4. */
  5. import { fileURLToPath } from 'node:url'
  6. import { describe, expect, it, vi } from 'vitest'
  7. import { CLIENT_EXTERNALS, clientBundle } from '../packages/client/tsdown.client.ts'
  8. type ResolveId = (source: string) => null | { id: string; external: boolean }
  9. interface CssModulePlugin {
  10. name: string
  11. resolveId?: (source: string, importer: string | undefined) => null | string
  12. load?: (this: { addWatchFile: (id: string) => void }, id: string) => Promise<unknown>
  13. }
  14. function clientConfigs(id = '@deepseek-ai/dsh-client-test') {
  15. return clientBundle(id, ['lib/types/index.js', 'lib/types/invariant.js'])(
  16. { env: { DSH_BUILD_FACE: 'client' } },
  17. ).filter(config => config.platform === 'browser')
  18. }
  19. describe('client bundle build faces', () => {
  20. it('watches source in development and consumes emitted JavaScript in the Client build', () => {
  21. const bundle = clientBundle('@deepseek-ai/dsh-client-test', ['lib/types/index.js'])
  22. const development = bundle({ env: {} }).find(config => config.platform === 'browser')
  23. const artifact = bundle({ env: { DSH_BUILD_FACE: 'client' } })
  24. .find(config => config.platform === 'browser')
  25. expect(development?.entry).toEqual({ client: 'src/client/index.ts' })
  26. expect(artifact?.entry).toEqual({ client: 'lib/types/client/index.js' })
  27. })
  28. })
  29. function clientSourceMapPath(packagePath: string): string {
  30. return fileURLToPath(new URL(`../packages/${packagePath}/lib/client.js.map`, import.meta.url))
  31. }
  32. function purityResolveId(): ResolveId {
  33. // libEntry is spelled at every call site (no default) so the
  34. // package-invariants text check can see the invariant entry per package.
  35. const configs = clientConfigs()
  36. const plugins = (configs[0] as { plugins: { name: string; resolveId?: unknown }[] }).plugins
  37. const gate = plugins.find(p => p.name === 'dsh-client-bundle-purity')
  38. if (gate?.resolveId === undefined) throw new Error('purity plugin missing from client config')
  39. return gate.resolveId as ResolveId
  40. }
  41. function cssModulePlugin(): CssModulePlugin {
  42. const configs = clientConfigs()
  43. const plugins = (configs[0] as { plugins: CssModulePlugin[] }).plugins
  44. const plugin = plugins.find(candidate => candidate.name === 'dsh-css-modules-inline')
  45. if (plugin?.resolveId === undefined || plugin.load === undefined) {
  46. throw new Error('CSS Modules plugin missing from client config')
  47. }
  48. return plugin
  49. }
  50. describe('client bundle purity gate', () => {
  51. const resolveId = purityResolveId()
  52. it('leaves platform table entries and non-scoped specifiers alone', () => {
  53. expect(resolveId('@deepseek-ai/dsh-client-ui-slots')).toBeNull()
  54. expect(resolveId('@deepseek-ai/dsh-client-web-react')).toBeNull()
  55. expect(resolveId('@deepseek-ai/dsh-client-ui-primitives')).toBeNull()
  56. expect(resolveId('react')).toBeNull()
  57. expect(resolveId('zod')).toBeNull()
  58. })
  59. it('rejects retired table entries (web-react/store left the 8-entry seed)', () => {
  60. expect(() => resolveId('@deepseek-ai/dsh-client-web-react/store')).toThrow(/purity/)
  61. })
  62. it('lets inline-safe wire layers inline', () => {
  63. expect(resolveId('@deepseek-ai/dsh-host-apiproxy/api')).toBeNull()
  64. expect(resolveId('@deepseek-ai/dsh-session/surface')).toBeNull()
  65. expect(resolveId('@deepseek-ai/dsh-brand')).toBeNull()
  66. })
  67. it('lets exact generated Remote contributions inline without admitting their package implementation', () => {
  68. expect(resolveId('@deepseek-ai/dsh-goal/remote')).toBeNull()
  69. expect(() => resolveId('@deepseek-ai/dsh-goal')).toThrow(/purity/)
  70. expect(() => resolveId('@deepseek-ai/dsh-goal/client')).toThrow(/purity/)
  71. expect(() => resolveId('@deepseek-ai/dsh-goal/remote/nested')).toThrow(/purity/)
  72. })
  73. it('throws on any other @deepseek-ai leak', () => {
  74. expect(() => resolveId('@deepseek-ai/dsh-agent')).toThrow(/purity/)
  75. expect(() => resolveId('@deepseek-ai/dsh-client-web')).toThrow(/purity/)
  76. })
  77. it('throws on cross-plugin value imports — bare plugin names and /client subpaths alike (the rewrite arm is gone)', () => {
  78. expect(() => resolveId('@deepseek-ai/dsh-client-connection')).toThrow(/purity/)
  79. expect(() => resolveId('@deepseek-ai/dsh-client-runtime')).toThrow(/purity/)
  80. expect(() => resolveId('@deepseek-ai/dsh-client-ui-layout/client')).toThrow(/purity/)
  81. })
  82. it('carries exactly one documented temporary exemption: runtime/client (store engine pending rehoming)', () => {
  83. expect(resolveId('@deepseek-ai/dsh-client-runtime/client')).toBeNull()
  84. const clientChannels = CLIENT_EXTERNALS.filter(
  85. entry => entry.startsWith('@deepseek-ai/') && entry.endsWith('/client'))
  86. expect(clientChannels).toEqual(['@deepseek-ai/dsh-client-runtime/client'])
  87. })
  88. })
  89. describe('client bundle debug artifacts', () => {
  90. it('emits source maps for plugin TS and TSX outside the Vite module graph', () => {
  91. const configs = clientConfigs()
  92. expect(configs[0]?.sourcemap).toBe(true)
  93. })
  94. it('maps first-party sources to their repository package paths', () => {
  95. const configs = clientConfigs('@deepseek-ai/dsh-client-ui-goal')
  96. const outputOptions = configs[0]?.outputOptions
  97. if (typeof outputOptions !== 'object' || outputOptions === null) throw new Error('client output options missing')
  98. const transform = outputOptions.sourcemapPathTransform
  99. if (transform === undefined) throw new Error('client sourcemap path transform missing')
  100. const source = transform('../src/client/GoalBar.tsx', clientSourceMapPath('client/ui-goal'))
  101. expect(source).toBe('../../../packages/client/ui-goal/src/client/GoalBar.tsx')
  102. const resolved = new URL(source, 'https://dsh.test/plugins/@deepseek-ai/dsh-client-ui-goal/client.js.map')
  103. expect(resolved.pathname).toBe('/packages/client/ui-goal/src/client/GoalBar.tsx')
  104. })
  105. it('maps dual-face host sources to the host package group', () => {
  106. const configs = clientConfigs('@deepseek-ai/dsh-host-directory-picker-native')
  107. const outputOptions = configs[0]?.outputOptions
  108. if (typeof outputOptions !== 'object' || outputOptions === null) throw new Error('client output options missing')
  109. const transform = outputOptions.sourcemapPathTransform
  110. if (transform === undefined) throw new Error('client sourcemap path transform missing')
  111. const source = transform('../src/client/index.ts', clientSourceMapPath('host/directory-picker-native'))
  112. expect(source).toBe('../../../packages/host/directory-picker-native/src/client/index.ts')
  113. })
  114. it('maps inlined workspace sources to packages and leaves dependencies outside it unchanged', () => {
  115. const configs = clientConfigs('@deepseek-ai/dsh-client-connection')
  116. const outputOptions = configs[0]?.outputOptions
  117. if (typeof outputOptions !== 'object' || outputOptions === null) throw new Error('client output options missing')
  118. const transform = outputOptions.sourcemapPathTransform
  119. if (transform === undefined) throw new Error('client sourcemap path transform missing')
  120. const sourceMapPath = clientSourceMapPath('client/connection')
  121. const workspaceSource = transform('../../../host/apiproxy/src/api/rpc.ts', sourceMapPath)
  122. expect(workspaceSource).toBe('../../../packages/host/apiproxy/src/api/rpc.ts')
  123. const resolved = new URL(workspaceSource, 'https://dsh.test/plugins/@deepseek-ai/dsh-client-connection/client.js.map')
  124. expect(resolved.pathname).toBe('/packages/host/apiproxy/src/api/rpc.ts')
  125. const dependencySource = '../../../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/index.js'
  126. expect(transform(dependencySource, sourceMapPath)).toBe(dependencySource)
  127. })
  128. })
  129. describe('client bundle CSS Modules watch graph', () => {
  130. it('registers the physical stylesheet read behind a virtual module', async () => {
  131. const plugin = cssModulePlugin()
  132. const importer = fileURLToPath(new URL(
  133. '../packages/client/ui-conversation/src/client/queue/QueueDock.tsx',
  134. import.meta.url,
  135. ))
  136. const stylesheet = fileURLToPath(new URL(
  137. '../packages/client/ui-conversation/src/client/queue/QueueDock.module.css',
  138. import.meta.url,
  139. ))
  140. const virtualId = plugin.resolveId?.('./QueueDock.module.css', importer)
  141. if (virtualId === null || virtualId === undefined) throw new Error('CSS Modules import was not resolved')
  142. const addWatchFile = vi.fn()
  143. await plugin.load?.call({ addWatchFile }, virtualId)
  144. expect(addWatchFile).toHaveBeenCalledExactlyOnceWith(stylesheet)
  145. })
  146. })