client-bundle-purity.spec.ts 7.5 KB

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