client-bundle-purity.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Pins shared client-bundle preset rules: module-edge purity, source-map
  3. * chaining, and physical watch dependencies hidden behind virtual CSS Modules.
  4. */
  5. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  6. import { tmpdir } from 'node:os'
  7. import { join } from 'node:path'
  8. import { fileURLToPath } from 'node:url'
  9. import { describe, expect, it, vi } from 'vitest'
  10. import { clientBundle, requestedExternals } from '../packages/client/tsdown.client.ts'
  11. type ResolveId = (source: string) => null | { id: string; external: boolean }
  12. interface CssModulePlugin {
  13. name: string
  14. resolveId?: (source: string, importer: string | undefined) => null | string
  15. load?: (this: { addWatchFile: (id: string) => void }, id: string) => Promise<unknown>
  16. }
  17. interface SourceMapPlugin {
  18. name: string
  19. load?: (id: string) => Promise<unknown>
  20. }
  21. /** A representative dynamic bundle using the shared client baseline. */
  22. const REQUESTING_PACKAGE = '@deepseek-ai/dsh-client-ui-conversation'
  23. function clientConfigs(id = REQUESTING_PACKAGE) {
  24. return clientBundle(id, ['lib/types/index.js', 'lib/types/invariant.js'])(
  25. { env: { DSH_BUILD_FACE: 'client' } },
  26. ).filter(config => config.platform === 'browser')
  27. }
  28. describe('client bundle build faces', () => {
  29. it('watches source in development and consumes emitted JavaScript in the Client build', () => {
  30. const bundle = clientBundle('@deepseek-ai/dsh-client-test', ['lib/types/index.js'])
  31. const development = bundle({ env: {} }).find(config => config.platform === 'browser')
  32. const artifact = bundle({ env: { DSH_BUILD_FACE: 'client' } })
  33. .find(config => config.platform === 'browser')
  34. expect(development?.entry).toEqual({ client: 'src/client/index.ts' })
  35. expect(artifact?.entry).toEqual({ client: 'lib/types/client/index.js' })
  36. })
  37. })
  38. function clientSourceMapPath(packagePath: string): string {
  39. return fileURLToPath(new URL(`../packages/${packagePath}/lib/client.js.map`, import.meta.url))
  40. }
  41. function purityResolveId(id = REQUESTING_PACKAGE): ResolveId {
  42. // libEntry is spelled at every call site (no default) so the
  43. // package-invariants text check can see the invariant entry per package.
  44. const configs = clientConfigs(id)
  45. const plugins = (configs[0] as { plugins: { name: string; resolveId?: unknown }[] }).plugins
  46. const gate = plugins.find(p => p.name === 'dsh-client-bundle-purity')
  47. if (gate?.resolveId === undefined) throw new Error('purity plugin missing from client config')
  48. return gate.resolveId as ResolveId
  49. }
  50. function cssModulePlugin(): CssModulePlugin {
  51. const configs = clientConfigs()
  52. const plugins = (configs[0] as { plugins: CssModulePlugin[] }).plugins
  53. const plugin = plugins.find(candidate => candidate.name === 'dsh-css-modules-inline')
  54. if (plugin?.resolveId === undefined || plugin.load === undefined) {
  55. throw new Error('CSS Modules plugin missing from client config')
  56. }
  57. return plugin
  58. }
  59. function sourceMapPlugin(): SourceMapPlugin {
  60. const configs = clientConfigs()
  61. const plugins = (configs[0] as { plugins: SourceMapPlugin[] }).plugins
  62. const plugin = plugins.find(candidate => candidate.name === 'dsh-tsc-sourcemap')
  63. if (plugin?.load === undefined) throw new Error('tsc sourcemap plugin missing from client config')
  64. return plugin
  65. }
  66. describe('client bundle purity gate', () => {
  67. const resolveId = purityResolveId()
  68. it('leaves default externals and non-scoped specifiers alone', () => {
  69. expect(resolveId('@deepseek-ai/dsh-client-store')).toBeNull()
  70. expect(resolveId('@deepseek-ai/dsh-client-ui-slots')).toBeNull()
  71. expect(resolveId('@deepseek-ai/dsh-client-ui-primitives')).toBeNull()
  72. expect(resolveId('react')).toBeNull()
  73. expect(resolveId('zod')).toBeNull()
  74. })
  75. it('rejects the retired web-react platform package', () => {
  76. expect(() => resolveId('@deepseek-ai/dsh-client-web-react')).toThrow(/purity/)
  77. expect(() => resolveId('@deepseek-ai/dsh-client-web-react/store')).toThrow(/purity/)
  78. })
  79. it('lets inline-safe wire layers inline', () => {
  80. expect(resolveId('@deepseek-ai/dsh-host-apiproxy/api')).toBeNull()
  81. expect(resolveId('@deepseek-ai/dsh-session/surface')).toBeNull()
  82. expect(resolveId('@deepseek-ai/dsh-brand')).toBeNull()
  83. })
  84. it('lets exact generated Remote contributions inline without admitting their package implementation', () => {
  85. expect(resolveId('@deepseek-ai/dsh-goal/remote')).toBeNull()
  86. expect(() => resolveId('@deepseek-ai/dsh-goal')).toThrow(/purity/)
  87. expect(() => resolveId('@deepseek-ai/dsh-goal/client')).toThrow(/purity/)
  88. expect(() => resolveId('@deepseek-ai/dsh-goal/remote/nested')).toThrow(/purity/)
  89. })
  90. it('throws on any other @deepseek-ai leak', () => {
  91. expect(() => resolveId('@deepseek-ai/dsh-agent')).toThrow(/purity/)
  92. expect(() => resolveId('@deepseek-ai/dsh-client-web')).toThrow(/purity/)
  93. })
  94. it('throws on cross-plugin value imports — bare plugin names and /client subpaths alike', () => {
  95. expect(() => resolveId('@deepseek-ai/dsh-client-connection')).toThrow(/purity/)
  96. expect(() => resolveId('@deepseek-ai/dsh-client-ui-session')).toThrow(/purity/)
  97. expect(() => resolveId('@deepseek-ai/dsh-client-ui-layout/client')).toThrow(/purity/)
  98. })
  99. it('admits package-specific requests only for the declaring bundle', () => {
  100. const requesting = purityResolveId('@deepseek-ai/dsh-api-session-controller')
  101. expect(requesting('@deepseek-ai/dsh-api-gateway/client')).toBeNull()
  102. expect(() => resolveId('@deepseek-ai/dsh-api-gateway/client')).toThrow(/purity/)
  103. })
  104. it('externalizes the baseline independently of each package manifest', () => {
  105. const requesting = clientConfigs()[0]?.deps as { neverBundle: (specifier: string) => boolean }
  106. const plain = clientConfigs('@deepseek-ai/dsh-client-connection')[0]?.deps as {
  107. neverBundle: (specifier: string) => boolean
  108. }
  109. expect(requesting.neverBundle('react')).toBe(true)
  110. expect(requesting.neverBundle('zod')).toBe(false)
  111. expect(plain.neverBundle('react')).toBe(true)
  112. expect(plain.neverBundle('@deepseek-ai/dsh-client-store')).toBe(true)
  113. })
  114. })
  115. describe('client bundle module requests', () => {
  116. it('requests what the declaration lists', () => {
  117. const requests = requestedExternals('@deepseek-ai/dsh-client-fixture', {
  118. external: ['react', 'react/jsx-runtime', '@deepseek-ai/dsh-client-ui-slots'],
  119. })
  120. expect([...requests].sort()).toEqual([
  121. '@deepseek-ai/dsh-client-ui-slots', 'react', 'react/jsx-runtime',
  122. ])
  123. })
  124. it('requests nothing when the declaration is absent', () => {
  125. expect(requestedExternals('@deepseek-ai/dsh-client-fixture', {}).size).toBe(0)
  126. })
  127. it('rejects a malformed declaration instead of reading past it', () => {
  128. expect(() => requestedExternals('@deepseek-ai/dsh-client-fixture', { external: 'react' }))
  129. .toThrow(/dsh\.client\.external must be a string array/)
  130. })
  131. })
  132. describe('client bundle debug artifacts', () => {
  133. it('emits source maps for plugin TS and TSX outside the Vite module graph', () => {
  134. const configs = clientConfigs()
  135. expect(configs[0]?.sourcemap).toBe(true)
  136. expect(configs[0]?.outputOptions).toMatchObject({ sourcemapExcludeSources: false })
  137. })
  138. it('chains emitted tsc maps when the production Client build consumes lib/types', async () => {
  139. const root = mkdtempSync(join(tmpdir(), 'dsh-client-sourcemap-'))
  140. try {
  141. const entry = join(root, 'lib', 'types', 'client', 'index.js')
  142. const source = join(root, 'src', 'client', 'index.ts')
  143. const map = { version: 3, names: [], mappings: 'AAAA', sources: ['../../../src/client/index.ts'] }
  144. mkdirSync(join(root, 'lib', 'types', 'client'), { recursive: true })
  145. mkdirSync(join(root, 'src', 'client'), { recursive: true })
  146. writeFileSync(entry, 'export const marker = true\n//# sourceMappingURL=index.js.map\n')
  147. writeFileSync(`${entry}.map`, JSON.stringify(map))
  148. writeFileSync(source, 'export const marker: true = true\n')
  149. await expect(sourceMapPlugin().load!(entry)).resolves.toEqual({
  150. code: 'export const marker = true',
  151. map: { ...map, sourcesContent: ['export const marker: true = true\n'] },
  152. })
  153. } finally {
  154. rmSync(root, { recursive: true, force: true })
  155. }
  156. })
  157. it('maps first-party sources to their repository package paths', () => {
  158. const configs = clientConfigs('@deepseek-ai/dsh-client-ui-goal')
  159. const outputOptions = configs[0]?.outputOptions
  160. if (typeof outputOptions !== 'object' || outputOptions === null) throw new Error('client output options missing')
  161. const transform = outputOptions.sourcemapPathTransform
  162. if (transform === undefined) throw new Error('client sourcemap path transform missing')
  163. const source = transform('../src/client/GoalBar.tsx', clientSourceMapPath('client/ui-goal'))
  164. expect(source).toBe('../../../packages/client/ui-goal/src/client/GoalBar.tsx')
  165. const resolved = new URL(source, 'https://dsh.test/plugins/@deepseek-ai/dsh-client-ui-goal/client.js.map')
  166. expect(resolved.pathname).toBe('/packages/client/ui-goal/src/client/GoalBar.tsx')
  167. })
  168. it('maps dual-face host sources to the host package group', () => {
  169. const configs = clientConfigs('@deepseek-ai/dsh-host-directory-picker-native')
  170. const outputOptions = configs[0]?.outputOptions
  171. if (typeof outputOptions !== 'object' || outputOptions === null) throw new Error('client output options missing')
  172. const transform = outputOptions.sourcemapPathTransform
  173. if (transform === undefined) throw new Error('client sourcemap path transform missing')
  174. const source = transform('../src/client/index.ts', clientSourceMapPath('host/directory-picker-native'))
  175. expect(source).toBe('../../../packages/host/directory-picker-native/src/client/index.ts')
  176. })
  177. it('maps inlined workspace sources to packages and leaves dependencies outside it unchanged', () => {
  178. const configs = clientConfigs('@deepseek-ai/dsh-client-connection')
  179. const outputOptions = configs[0]?.outputOptions
  180. if (typeof outputOptions !== 'object' || outputOptions === null) throw new Error('client output options missing')
  181. const transform = outputOptions.sourcemapPathTransform
  182. if (transform === undefined) throw new Error('client sourcemap path transform missing')
  183. const sourceMapPath = clientSourceMapPath('client/connection')
  184. const workspaceSource = transform('../../../host/apiproxy/src/api/rpc.ts', sourceMapPath)
  185. expect(workspaceSource).toBe('../../../packages/host/apiproxy/src/api/rpc.ts')
  186. const resolved = new URL(workspaceSource, 'https://dsh.test/plugins/@deepseek-ai/dsh-client-connection/client.js.map')
  187. expect(resolved.pathname).toBe('/packages/host/apiproxy/src/api/rpc.ts')
  188. const dependencySource = '../../../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/index.js'
  189. expect(transform(dependencySource, sourceMapPath)).toBe(dependencySource)
  190. })
  191. })
  192. describe('client bundle CSS Modules watch graph', () => {
  193. it('registers the physical stylesheet read behind a virtual module', async () => {
  194. const plugin = cssModulePlugin()
  195. const importer = fileURLToPath(new URL(
  196. '../packages/client/ui-conversation/src/client/queue/QueueDock.tsx',
  197. import.meta.url,
  198. ))
  199. const stylesheet = fileURLToPath(new URL(
  200. '../packages/client/ui-conversation/src/client/queue/QueueDock.module.css',
  201. import.meta.url,
  202. ))
  203. const virtualId = plugin.resolveId?.('./QueueDock.module.css', importer)
  204. if (virtualId === null || virtualId === undefined) throw new Error('CSS Modules import was not resolved')
  205. const addWatchFile = vi.fn()
  206. await plugin.load?.call({ addWatchFile }, virtualId)
  207. expect(addWatchFile).toHaveBeenCalledExactlyOnceWith(stylesheet)
  208. })
  209. })