verify-package-dependencies.spec.ts 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { dirname, join } from 'node:path'
  4. import { afterEach, describe, expect, it } from 'vitest'
  5. import {
  6. PACKAGE_DEPENDENCY_POLICY,
  7. type PackageDependencyPolicy,
  8. } from './package-dependency-policy.ts'
  9. import {
  10. collectHostDependencyExportPolicyViolations,
  11. collectPackageDependencyViolations,
  12. collectRuntimeSourceExportUses,
  13. discoverPackageDependencyScope,
  14. expectedPackageDependencies,
  15. fixPackageDependencies,
  16. formatManagedRuntimeDependencies,
  17. formatPeerRequiredRuntimeDependencies,
  18. readPackageDependencyFacts,
  19. readPackageDependencyState,
  20. repairPackageDependencyManifest,
  21. type PackageDependencyFacts,
  22. type PackageDependencyManifest,
  23. type PackageDependencyRole,
  24. type WorkspacePackageManifest,
  25. } from './verify-package-dependencies.ts'
  26. const CORDIS = '@deepseek-ai/cordis'
  27. const roots: string[] = []
  28. afterEach(() => {
  29. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  30. })
  31. function pkg(
  32. name: string,
  33. manifestPath: string,
  34. manifest: Partial<PackageDependencyManifest> = {},
  35. ): WorkspacePackageManifest {
  36. return {
  37. name,
  38. manifestPath,
  39. dir: dirname(manifestPath),
  40. manifest: { name, ...manifest },
  41. }
  42. }
  43. function policy(fields: Partial<PackageDependencyPolicy> = {}): PackageDependencyPolicy {
  44. return {
  45. clientFaceInclude: [],
  46. clientFaceExclude: [],
  47. hostPackages: [],
  48. configurationOnlyDevDependencies: {},
  49. safeHostDependencyExports: {},
  50. peerRequiredHostExports: {},
  51. ...fields,
  52. }
  53. }
  54. function facts(manifest: PackageDependencyManifest): PackageDependencyFacts {
  55. return {
  56. manifestPath: 'packages/core/probe/package.json',
  57. role: 'configured-host',
  58. manifest,
  59. workspaceNames: new Set([
  60. CORDIS,
  61. '@deepseek-ai/dsh-runtime',
  62. '@deepseek-ai/dsh-types',
  63. '@deepseek-ai/dsh-stale',
  64. '@deepseek-ai/schemastery',
  65. ]),
  66. allSourceUses: new Map([
  67. ['@deepseek-ai/dsh-runtime', ['packages/core/probe/src/index.ts']],
  68. ['@deepseek-ai/dsh-types', ['packages/core/probe/src/types.ts']],
  69. ]),
  70. hostRuntimeSourceUses: new Map([
  71. ['@deepseek-ai/dsh-runtime', ['packages/core/probe/src/index.ts']],
  72. ]),
  73. hostRuntimeExportUses: [{
  74. packageName: '@deepseek-ai/dsh-runtime',
  75. specifier: '@deepseek-ai/dsh-runtime',
  76. exportName: 'runtimeValue',
  77. sourcePath: 'packages/core/probe/src/index.ts',
  78. line: 1,
  79. column: 10,
  80. sourceLine: "import { runtimeValue } from '@deepseek-ai/dsh-runtime'",
  81. }],
  82. peerRequiredHostDependencies: new Set(),
  83. configurationOnlyDevDependencies: new Set(),
  84. clientInject: new Set(),
  85. }
  86. }
  87. function sourceFacts(
  88. files: Readonly<Record<string, string>>,
  89. manifest: Partial<PackageDependencyManifest> = {},
  90. role: PackageDependencyRole = 'client-host',
  91. ): PackageDependencyFacts {
  92. const root = mkdtempSync(join(tmpdir(), 'dsh-dependency-source-'))
  93. roots.push(root)
  94. const subject = pkg('@f/probe', 'packages/g/probe/package.json', manifest)
  95. for (const [path, source] of Object.entries(files)) {
  96. const absolute = join(root, subject.dir, path)
  97. mkdirSync(dirname(absolute), { recursive: true })
  98. writeFileSync(absolute, source)
  99. }
  100. return readPackageDependencyFacts(root, subject, role, new Set([CORDIS, subject.name]), policy())
  101. }
  102. function generatedHostFixture(mode: 'schema' | 'object'): { root: string; manifestPath: string; source: string } {
  103. const root = mkdtempSync(join(tmpdir(), 'dsh-generated-host-dependencies-'))
  104. roots.push(root)
  105. const manifestPath = 'packages/client/probe/package.json'
  106. const source = `/** @typert ${mode} */\nexport interface Payload { value: string }\n`
  107. const manifest = {
  108. name: '@fixture/generated',
  109. type: 'module',
  110. dsh: { client: {} },
  111. exports: {
  112. '.': { types: './lib/types/index.d.ts', default: './lib/index.js' },
  113. './typert': { types: './lib/typert.host.d.ts', default: './lib/typert.host.js' },
  114. },
  115. files: ['lib/typert.host.js', 'lib/typert.host.d.ts'],
  116. dependencies: { zod: '^4.0.0' },
  117. devDependencies: { [CORDIS]: 'workspace:^' },
  118. peerDependencies: { [CORDIS]: 'workspace:^' },
  119. }
  120. const files = {
  121. 'tsconfig.base.json': JSON.stringify({
  122. compilerOptions: {
  123. target: 'ES2024', module: 'ESNext', moduleResolution: 'Bundler', strict: true,
  124. composite: true, noEmit: true, types: [], skipLibCheck: true,
  125. },
  126. }),
  127. 'tsconfig.host.json': JSON.stringify({
  128. extends: './tsconfig.base.json', files: [], references: [{ path: './packages/client/probe' }],
  129. }),
  130. 'packages/client/probe/tsconfig.json': JSON.stringify({
  131. extends: '../../../tsconfig.base.json', compilerOptions: { rootDir: 'src' }, include: ['src'],
  132. }),
  133. [manifestPath]: JSON.stringify(manifest),
  134. 'packages/client/probe/src/index.ts': source,
  135. }
  136. for (const [path, content] of Object.entries(files)) {
  137. mkdirSync(dirname(join(root, path)), { recursive: true })
  138. writeFileSync(join(root, path), content)
  139. }
  140. return { root, manifestPath, source }
  141. }
  142. function hostRuntimeFixture(): {
  143. provider: WorkspacePackageManifest
  144. workspaceNames: Set<string>
  145. consumerFacts: PackageDependencyFacts
  146. } {
  147. const consumer = pkg('@f/consumer', 'packages/core/consumer/package.json')
  148. const provider = pkg('@f/provider', 'packages/core/provider/package.json')
  149. const sourcePath = 'packages/core/consumer/src/index.ts'
  150. const specifier = `${provider.name}/api`
  151. const workspaceNames = new Set([CORDIS, consumer.name, provider.name])
  152. const consumerFacts: PackageDependencyFacts = {
  153. manifestPath: consumer.manifestPath,
  154. role: 'configured-host',
  155. manifest: consumer.manifest,
  156. workspaceNames,
  157. allSourceUses: new Map(),
  158. hostRuntimeSourceUses: new Map([[provider.name, [sourcePath]]]),
  159. hostRuntimeExportUses: [{
  160. packageName: provider.name,
  161. specifier,
  162. exportName: 'safeValue',
  163. sourcePath,
  164. line: 1,
  165. column: 10,
  166. sourceLine: `import { safeValue } from '${specifier}'`,
  167. }],
  168. peerRequiredHostDependencies: new Set(),
  169. configurationOnlyDevDependencies: new Set(),
  170. clientInject: new Set(),
  171. }
  172. return { provider, workspaceNames, consumerFacts }
  173. }
  174. describe('package dependency scope', () => {
  175. it('keeps the measured Host relay roster explicit', () => {
  176. expect(PACKAGE_DEPENDENCY_POLICY.clientFaceExclude).toEqual([
  177. '@deepseek-ai/dsh-api-session-controller',
  178. '@deepseek-ai/dsh-api-workspace-controller',
  179. ])
  180. expect(PACKAGE_DEPENDENCY_POLICY.hostPackages).toEqual([
  181. '@deepseek-ai/dsh-llm',
  182. '@deepseek-ai/dsh-session',
  183. ])
  184. expect(PACKAGE_DEPENDENCY_POLICY.configurationOnlyDevDependencies).toEqual({
  185. '@deepseek-ai/dsh-client-locale': ['@deepseek-ai/dsh-api-remotes'],
  186. '@deepseek-ai/dsh-client-ui-conversation': [
  187. '@deepseek-ai/dsh-api-remotes',
  188. '@deepseek-ai/dsh-client-ui-workspace',
  189. ],
  190. '@deepseek-ai/dsh-client-ui-model-selection': ['@deepseek-ai/dsh-client-ui-input-trigger'],
  191. '@deepseek-ai/dsh-client-ui-sidebar': ['@deepseek-ai/dsh-client-ui-workspace'],
  192. '@deepseek-ai/dsh-client-ui-subagent': ['@deepseek-ai/dsh-client-ui-input-trigger'],
  193. '@deepseek-ai/dsh-client-ui-theme': ['@deepseek-ai/dsh-api-remotes'],
  194. '@deepseek-ai/dsh-client-ui-tool': ['@deepseek-ai/dsh-api-remotes'],
  195. })
  196. expect(PACKAGE_DEPENDENCY_POLICY.duplicateSafePackages).toEqual([
  197. '@deepseek-ai/dsh-brand',
  198. '@deepseek-ai/dsh-typert-protocol',
  199. '@deepseek-ai/dsh-util-crypto',
  200. '@deepseek-ai/dsh-util-values',
  201. ])
  202. expect(PACKAGE_DEPENDENCY_POLICY.safeHostDependencyExports['@deepseek-ai/dsh-deque']).toEqual(['Deque'])
  203. expect(PACKAGE_DEPENDENCY_POLICY.safeHostDependencyExports['@deepseek-ai/schemastery']).toEqual(['default'])
  204. expect(PACKAGE_DEPENDENCY_POLICY.safeHostDependencyExports['@deepseek-ai/dsh-session/types']).toBeUndefined()
  205. expect(PACKAGE_DEPENDENCY_POLICY.safeHostDependencyExports['@deepseek-ai/dsh-typert-protocol']).toBeUndefined()
  206. expect(PACKAGE_DEPENDENCY_POLICY.peerRequiredHostExports['@deepseek-ai/dsh-scope']).toEqual([
  207. 'carrierKeyOf', 'scopeOf', 'scopeTarget',
  208. ])
  209. expect(PACKAGE_DEPENDENCY_POLICY.peerRequiredHostExports['@deepseek-ai/dsh-typert-protocol']).toBeUndefined()
  210. })
  211. it('discovers the Client directory, dsh.client declarations, and configured Host packages', () => {
  212. const packages = [
  213. pkg('@f/static', 'packages/client/static/package.json'),
  214. pkg('@f/dynamic-client', 'packages/client/dynamic/package.json', { dsh: { client: {} } }),
  215. pkg('@f/dual', 'packages/api/dual/package.json', { dsh: { client: {} } }),
  216. pkg('@f/export-only', 'packages/api/export-only/package.json', { exports: { './client': './lib/client.js' } }),
  217. pkg('@f/forced-client', 'packages/api/forced/package.json'),
  218. pkg('@f/excluded', 'packages/api/excluded/package.json', { dsh: { client: {} } }),
  219. pkg('@f/host', 'packages/core/host/package.json'),
  220. ]
  221. const found = discoverPackageDependencyScope(packages, policy({
  222. clientFaceInclude: ['@f/forced-client'],
  223. clientFaceExclude: ['@f/excluded'],
  224. hostPackages: ['@f/host'],
  225. }))
  226. expect(found.violations).toEqual([])
  227. expect(found.selected.map(item => [item.name, item.role])).toEqual([
  228. ['@f/dual', 'client-host'],
  229. ['@f/forced-client', 'client-host'],
  230. ['@f/dynamic-client', 'client-host'],
  231. ['@f/static', 'client-only'],
  232. ['@f/host', 'configured-host'],
  233. ])
  234. })
  235. it('rejects stale, redundant, overlapping, and unknown configuration', () => {
  236. const packages = [
  237. pkg('@f/client', 'packages/client/client/package.json'),
  238. pkg('@f/dual', 'packages/api/dual/package.json', { dsh: { client: {} } }),
  239. pkg('@f/host', 'packages/core/host/package.json'),
  240. ]
  241. const found = discoverPackageDependencyScope(packages, policy({
  242. clientFaceInclude: ['@f/dual', '@f/missing', '@f/host'],
  243. clientFaceExclude: ['@f/client', '@f/host', '@f/missing'],
  244. hostPackages: ['@f/dual'],
  245. }))
  246. expect(found.violations).toEqual(expect.arrayContaining([
  247. expect.stringContaining('clientFaceInclude redundantly names automatically discovered package @f/dual'),
  248. expect.stringContaining('@f/host appears in both clientFaceInclude and clientFaceExclude'),
  249. expect.stringContaining('clientFaceExclude cannot exempt packages/client package @f/client'),
  250. expect.stringContaining('clientFaceExclude names @f/host, which declares no dsh.client entry'),
  251. expect.stringContaining('hostPackages redundantly names Client-faced package @f/dual'),
  252. expect.stringContaining('unknown release package @f/missing'),
  253. ]))
  254. })
  255. it('rejects stale, duplicate, and unbounded safe Host export entries', () => {
  256. const { provider, workspaceNames, consumerFacts } = hostRuntimeFixture()
  257. expect(collectHostDependencyExportPolicyViolations(
  258. [consumerFacts],
  259. workspaceNames,
  260. {
  261. safeHostDependencyExports: {
  262. [`${provider.name}/api`]: ['safeValue', 'safeValue', '*', 'staleValue'],
  263. },
  264. peerRequiredHostExports: {
  265. [`${provider.name}/api`]: ['safeValue'],
  266. },
  267. },
  268. )).toEqual(expect.arrayContaining([
  269. expect.stringContaining('export safeValue more than once'),
  270. expect.stringContaining('cannot classify unbounded'),
  271. expect.stringContaining('unused @f/provider/api export staleValue'),
  272. expect.stringContaining('appears in both Host export classifications'),
  273. ]))
  274. })
  275. it('applies a duplicate-safe package classification to its subpaths', () => {
  276. const { provider, workspaceNames, consumerFacts } = hostRuntimeFixture()
  277. expect(collectHostDependencyExportPolicyViolations(
  278. [consumerFacts],
  279. workspaceNames,
  280. {
  281. duplicateSafePackages: [provider.name],
  282. safeHostDependencyExports: {},
  283. peerRequiredHostExports: {},
  284. },
  285. )).toEqual([])
  286. expect(collectHostDependencyExportPolicyViolations(
  287. [consumerFacts],
  288. workspaceNames,
  289. {
  290. duplicateSafePackages: [provider.name],
  291. safeHostDependencyExports: { [`${provider.name}/api`]: ['safeValue'] },
  292. peerRequiredHostExports: {},
  293. },
  294. )).toContain(`safeHostDependencyExports redundantly classifies duplicate-install-safe package ${provider.name}/api`)
  295. })
  296. })
  297. describe('face-aware source classification', () => {
  298. it('keeps generated Host schema imports in dependencies without reading or writing lib', () => {
  299. const { root, manifestPath, source } = generatedHostFixture('schema')
  300. const before = readFileSync(join(root, manifestPath), 'utf8')
  301. const state = readPackageDependencyState(root, policy())
  302. const subject = state.facts[0]
  303. if (subject === undefined) throw new Error('generated Host fixture was not classified')
  304. expect(subject.allSourceUses.has('zod')).toBe(false)
  305. expect(subject.hostRuntimeExportUses).toContainEqual(expect.objectContaining({
  306. packageName: 'zod', specifier: 'zod', exportName: 'z',
  307. sourcePath: 'packages/client/probe/lib/typert.host.js',
  308. }))
  309. const standalone = readPackageDependencyFacts(root, pkg('@fixture/generated', manifestPath, subject.manifest),
  310. subject.role, state.workspaceNames, policy())
  311. expect(standalone.hostRuntimeExportUses).toEqual(subject.hostRuntimeExportUses)
  312. expect(collectPackageDependencyViolations(state)).toEqual([])
  313. repairPackageDependencyManifest(subject)
  314. expect(subject.manifest.dependencies?.zod).toBe('^4.0.0')
  315. expect(subject.manifest.devDependencies?.zod).toBeUndefined()
  316. delete subject.manifest.dependencies?.zod
  317. subject.manifest.devDependencies = { ...subject.manifest.devDependencies, zod: '^4.0.0' }
  318. expect(collectPackageDependencyViolations(state)).toContainEqual(
  319. expect.stringContaining('must be dependencies-only; found devDependencies'),
  320. )
  321. repairPackageDependencyManifest(subject)
  322. expect(subject.manifest.dependencies?.zod).toBe('^4.0.0')
  323. expect(subject.manifest.devDependencies?.zod).toBeUndefined()
  324. delete subject.manifest.dependencies?.zod
  325. expect(() => { repairPackageDependencyManifest(subject) }).toThrow('undeclared third-party dependency zod')
  326. expect(existsSync(join(root, 'packages/client/probe/lib'))).toBe(false)
  327. expect(readFileSync(join(root, manifestPath), 'utf8')).toBe(before)
  328. expect(readFileSync(join(root, 'packages/client/probe/src/index.ts'), 'utf8')).toBe(source)
  329. })
  330. it('does not infer a zod runtime dependency from a metadata-only Typert export', () => {
  331. const { root } = generatedHostFixture('object')
  332. const state = readPackageDependencyState(root, policy())
  333. const subject = state.facts[0]
  334. if (subject === undefined) throw new Error('generated Host fixture was not classified')
  335. expect(subject.hostRuntimeSourceUses.has('zod')).toBe(false)
  336. expect(expectedPackageDependencies(subject).get('zod')?.section).toBe('devDependencies')
  337. repairPackageDependencyManifest(subject)
  338. expect(subject.manifest.dependencies?.zod).toBeUndefined()
  339. expect(subject.manifest.devDependencies?.zod).toBe('^4.0.0')
  340. expect(existsSync(join(root, 'packages/client/probe/lib'))).toBe(false)
  341. })
  342. it('rejects a declared Host Typert module absent from the Host program', () => {
  343. const { root } = generatedHostFixture('schema')
  344. rmSync(join(root, 'tsconfig.host.json'))
  345. expect(() => readPackageDependencyState(root, policy())).toThrow(
  346. 'packages/client/probe/package.json: declared Host Typert export has no generated module',
  347. )
  348. expect(existsSync(join(root, 'packages/client/probe/lib'))).toBe(false)
  349. })
  350. it('propagates generator publication errors without writing or repairing manifests', () => {
  351. const { root, manifestPath } = generatedHostFixture('schema')
  352. const manifest = JSON.parse(readFileSync(join(root, manifestPath), 'utf8')) as { files: string[] }
  353. manifest.files = []
  354. const before = JSON.stringify(manifest)
  355. writeFileSync(join(root, manifestPath), before)
  356. expect(() => readPackageDependencyState(root, policy())).toThrow(
  357. 'package files must include lib/typert.host.js',
  358. )
  359. expect(readFileSync(join(root, manifestPath), 'utf8')).toBe(before)
  360. expect(existsSync(join(root, 'packages/client/probe/lib'))).toBe(false)
  361. })
  362. it('counts browser imports, JSX, type-only references, and augmentations as development inputs', () => {
  363. const subject = sourceFacts({
  364. 'src/index.ts': [
  365. "import { readFile } from 'node:fs'",
  366. "import { join } from 'path'",
  367. "import type { HostType } from 'host-types'",
  368. "import { type MixedType } from 'mixed-types'",
  369. "import type { Hidden } from './type-helper.ts'",
  370. ].join('\n'),
  371. 'src/type-helper.ts': "import { hidden } from 'hidden-value'; export type Hidden = typeof hidden",
  372. 'src/client/index.tsx': [
  373. "import { browser } from '@browser/kit/subpath'",
  374. "import 'react-dom/client'",
  375. "import '#local'",
  376. "import 'https://example.test/browser.js'",
  377. 'export const view = <div />',
  378. ].join('\n'),
  379. 'src/client/augmentation.d.ts': [
  380. "declare module 'augmented' { interface Extra {} }",
  381. "declare module '*.css' {}",
  382. "declare module '*.module.css' {}",
  383. ].join('\n'),
  384. })
  385. expect([...subject.hostRuntimeSourceUses]).toEqual([])
  386. expect([...expectedPackageDependencies(subject)].map(([name, rule]) => [name, rule.section]).sort()).toEqual([
  387. ['@browser/kit', 'devDependencies'],
  388. [CORDIS, 'peer-dev'],
  389. ['augmented', 'devDependencies'],
  390. ['hidden-value', 'devDependencies'],
  391. ['host-types', 'devDependencies'],
  392. ['mixed-types', 'devDependencies'],
  393. ['react', 'devDependencies'],
  394. ['react-dom', 'devDependencies'],
  395. ])
  396. })
  397. it.each(['client-host', 'configured-host'] as const)('retains Host and shared third-party values in dependencies for %s', (role) => {
  398. const subject = sourceFacts({
  399. 'src/index.ts': "import 'host-only'; export { shared } from './nested.ts'",
  400. 'src/nested.ts': "export { shared } from 'shared-runtime'",
  401. 'src/client/index.ts': "import 'browser-only'; import 'shared-runtime'",
  402. }, {}, role)
  403. const expected = expectedPackageDependencies(subject)
  404. expect(expected.get('browser-only')?.section).toBe('devDependencies')
  405. expect(expected.get('host-only')?.section).toBe('dependencies')
  406. expect(expected.get('shared-runtime')?.section).toBe('dependencies')
  407. })
  408. it('uses declared DefinitelyTyped providers only for erased source references', () => {
  409. const subject = sourceFacts({
  410. 'src/index.ts': "import type { ReactNode } from 'react'",
  411. 'src/client/index.ts': "import type { Root } from 'mdast'; import type { Kind } from '@scope/types'",
  412. }, {
  413. dependencies: { '@types/mdast': '^4.0.0' },
  414. devDependencies: { '@types/react': '^18.0.0', '@types/scope__types': '^1.0.0' },
  415. })
  416. expect([...subject.allSourceUses.keys()].sort()).toEqual(['@types/mdast', '@types/react', '@types/scope__types'])
  417. expect([...subject.hostRuntimeSourceUses]).toEqual([])
  418. repairPackageDependencyManifest(subject)
  419. expect(subject.manifest.devDependencies?.['@types/mdast']).toBe('^4.0.0')
  420. expect(subject.manifest.dependencies?.['@types/mdast']).toBeUndefined()
  421. expect(subject.manifest.devDependencies?.mdast).toBeUndefined()
  422. })
  423. it.each(["import 'runtime-library'", 'export const view = <div />'])('does not let type providers satisfy runtime imports or JSX: %s', (source) => {
  424. const name = source.includes('<div') ? 'react' : 'runtime-library'
  425. const subject = sourceFacts({
  426. 'src/index.ts': 'export function apply() {}',
  427. 'src/client/index.tsx': source,
  428. }, { devDependencies: { [`@types/${name}`]: '^1.0.0' } })
  429. expect(subject.allSourceUses.has(name)).toBe(true)
  430. expect(() => { repairPackageDependencyManifest(subject) }).toThrow(`undeclared third-party dependency ${name}`)
  431. })
  432. it('does not treat static browser library entries as Host modules', () => {
  433. const subject = sourceFacts({
  434. 'src/index.tsx': "import 'static-input'; export const view = <div />",
  435. 'src/invariant.ts': "import 'browser-companion'",
  436. }, {
  437. exports: {
  438. '.': { types: './lib/types/index.d.ts', default: './lib/index.js' },
  439. './invariant': { types: './lib/types/invariant.d.ts', default: './lib/invariant.js' },
  440. },
  441. }, 'client-only')
  442. expect([...subject.hostRuntimeSourceUses]).toEqual([])
  443. for (const name of ['static-input', 'react', 'browser-companion']) {
  444. expect(expectedPackageDependencies(subject).get(name)?.section).toBe('devDependencies')
  445. }
  446. })
  447. it('scans published Node companions, conditional entries, and emitted-tree subpaths from source', () => {
  448. const subject = sourceFacts({
  449. 'src/index.ts': 'export function apply() {}',
  450. 'src/invariant.ts': "import 'invariant-runtime'; import type { Kind } from 'invariant-types'",
  451. 'src/node/helper.ts': "export { helper } from 'node-helper'",
  452. 'src/node.mts': "import 'node-import'",
  453. 'src/node.cts': "require('node-require')",
  454. 'src/emitted.tsx': 'export const view = <div />',
  455. 'src/worker/one.ts': "import 'worker-one'",
  456. 'src/worker/two.ts': "import('worker-two')",
  457. 'src/client/index.ts': "import 'browser-only'",
  458. 'src/types-only.ts': "import 'type-export-only'",
  459. }, {
  460. exports: {
  461. '.': { types: './lib/types/index.d.ts', default: './lib/index.js' },
  462. './invariant': { types: './lib/types/invariant.d.ts', default: './lib/invariant.js' },
  463. './renamed': { types: './lib/types/node/helper.d.ts', default: './lib/node-bundle.js' },
  464. './conditional': { browser: './lib/browser.js', node: { import: './lib/node.mjs', require: './lib/node.cjs' } },
  465. './emitted': { types: './lib/types/emitted.d.ts', default: './lib/types/emitted.js' },
  466. './worker/*': './lib/worker/*.js',
  467. './client': { types: './lib/types/client/index.d.ts', default: './lib/client.js' },
  468. './client/extra': './lib/missing-browser.js',
  469. './types-only': { types: './lib/types/types-only.d.ts' },
  470. './src/*': './src/*',
  471. './package.json': './package.json',
  472. './disabled': null,
  473. },
  474. })
  475. expect([...subject.hostRuntimeSourceUses.keys()].sort()).toEqual([
  476. 'invariant-runtime', 'node-helper', 'node-import', 'node-require', 'react', 'worker-one', 'worker-two',
  477. ])
  478. const expected = expectedPackageDependencies(subject)
  479. for (const name of subject.hostRuntimeSourceUses.keys()) expect(expected.get(name)?.section).toBe('dependencies')
  480. for (const name of ['browser-only', 'invariant-types', 'type-export-only']) {
  481. expect(expected.get(name)?.section).toBe('devDependencies')
  482. }
  483. })
  484. it.each([
  485. './lib/missing.js',
  486. { types: './lib/types/missing.d.ts', default: './lib/renamed.js' },
  487. ['./lib/missing.cjs'],
  488. './lib/missing/*.js',
  489. ])('rejects a published Node entry with no matching source: %j', (target) => {
  490. expect(() => sourceFacts({ 'src/index.ts': 'export function apply() {}' }, {
  491. exports: { './node': target },
  492. })).toThrow('Host export ./node has no source entry')
  493. })
  494. it('rejects a Node export outside the source mapping', () => {
  495. expect(() => sourceFacts({ 'src/index.ts': 'export function apply() {}' }, {
  496. exports: { './node': './other/node.js' },
  497. })).toThrow('Host export ./node cannot map ./other/node.js to a source entry')
  498. })
  499. it('fails when a managed Host package has no Host entry', () => {
  500. const root = mkdtempSync(join(tmpdir(), 'dsh-package-missing-host-'))
  501. roots.push(root)
  502. const subject = pkg('@f/host', 'packages/g/host/package.json')
  503. expect(() => readPackageDependencyFacts(root, subject, 'configured-host', new Set([subject.name])))
  504. .toThrow('packages/g/host/package.json: Host runtime entry packages/g/host/src/index.ts does not exist')
  505. })
  506. it('counts Host values as dependencies and Client values as development inputs', () => {
  507. const root = mkdtempSync(join(tmpdir(), 'dsh-package-faces-'))
  508. roots.push(root)
  509. const subject = pkg('@f/dual', 'packages/g/dual/package.json', {
  510. dsh: { client: { inject: ['@f/injected'] } },
  511. })
  512. const files = {
  513. 'packages/g/dual/src/index.ts': [
  514. "import { value } from '@f/runtime'",
  515. "import type { Shared } from '@f/types'",
  516. "import type { Hidden } from './types.ts'",
  517. "export { nested } from './nested.ts'",
  518. ].join('\n'),
  519. 'packages/g/dual/src/nested.ts': "export { nested } from '@f/nested'",
  520. 'packages/g/dual/src/types.ts': "import { hidden } from '@f/hidden'; export type Hidden = typeof hidden",
  521. 'packages/g/dual/src/client/index.ts': "import { browser } from '@f/browser'",
  522. }
  523. for (const [path, source] of Object.entries(files)) {
  524. mkdirSync(dirname(join(root, path)), { recursive: true })
  525. writeFileSync(join(root, path), source)
  526. }
  527. const found = readPackageDependencyFacts(root, subject, 'client-host', new Set([
  528. CORDIS, '@f/runtime', '@f/types', '@f/nested', '@f/hidden', '@f/browser', '@f/injected',
  529. ]), policy({
  530. configurationOnlyDevDependencies: { '@f/dual': ['@f/injected'] },
  531. }))
  532. expect([...found.hostRuntimeSourceUses.keys()].sort()).toEqual(['@f/nested', '@f/runtime'])
  533. expect([...found.configurationOnlyDevDependencies]).toEqual(['@f/injected'])
  534. expect(found.hostRuntimeExportUses).toEqual([
  535. {
  536. packageName: '@f/nested',
  537. specifier: '@f/nested',
  538. exportName: 'nested',
  539. sourcePath: 'packages/g/dual/src/nested.ts',
  540. line: 1,
  541. column: 10,
  542. sourceLine: "export { nested } from '@f/nested'",
  543. },
  544. {
  545. packageName: '@f/runtime',
  546. specifier: '@f/runtime',
  547. exportName: 'value',
  548. sourcePath: 'packages/g/dual/src/index.ts',
  549. line: 1,
  550. column: 10,
  551. sourceLine: "import { value } from '@f/runtime'",
  552. },
  553. ])
  554. expect([...found.allSourceUses.keys()].sort()).toEqual([
  555. '@f/browser', '@f/hidden', '@f/nested', '@f/runtime', '@f/types',
  556. ])
  557. })
  558. it('identifies exact runtime exports without treating type imports as values', () => {
  559. const source = [
  560. "import defaultValue, { value as local, type Kind } from '@f/root'",
  561. "import * as namespace from '@f/namespace'",
  562. "import '@f/effect'",
  563. "import type { TypeOnly } from '@f/types'",
  564. "export { source as renamed, type SourceType } from '@f/reexport'",
  565. "export * from '@f/star'",
  566. "void import('@f/dynamic')",
  567. "void require('@f/required')",
  568. 'void defaultValue; void local; void namespace',
  569. ].join('\n')
  570. const uses = collectRuntimeSourceExportUses('probe.ts', source)
  571. expect(uses.map(({ specifier, exportName }) => ({ specifier, exportName }))).toEqual([
  572. { specifier: '@f/dynamic', exportName: '*' },
  573. { specifier: '@f/effect', exportName: '(side effect)' },
  574. { specifier: '@f/namespace', exportName: '*' },
  575. { specifier: '@f/reexport', exportName: 'source' },
  576. { specifier: '@f/required', exportName: '*' },
  577. { specifier: '@f/root', exportName: 'default' },
  578. { specifier: '@f/root', exportName: 'value' },
  579. { specifier: '@f/star', exportName: '*' },
  580. ])
  581. expect(uses.find(use => use.specifier === '@f/root' && use.exportName === 'value')).toMatchObject({
  582. line: 1,
  583. column: 24,
  584. sourceLine: "import defaultValue, { value as local, type Kind } from '@f/root'",
  585. })
  586. })
  587. })
  588. describe('dependency sections', () => {
  589. it.each(['client-only', 'client-host'] as const)('moves unused third-party and CSS inputs to development dependencies for %s', (role) => {
  590. const subject = sourceFacts({
  591. 'src/index.ts': "import 'host-runtime'",
  592. }, {
  593. dependencies: { 'unused-browser-dep': '^1.2.3', '@fontsource/test-font': '~2.0.0', 'host-runtime': '^3.0.0' },
  594. optionalDependencies: { 'unused-optional': '^4.0.0' },
  595. }, role)
  596. repairPackageDependencyManifest(subject)
  597. expect(subject.manifest.devDependencies).toMatchObject({
  598. 'unused-browser-dep': '^1.2.3',
  599. '@fontsource/test-font': '~2.0.0',
  600. 'unused-optional': '^4.0.0',
  601. })
  602. expect(subject.manifest.dependencies).toEqual(role === 'client-host' ? { 'host-runtime': '^3.0.0' } : undefined)
  603. expect(subject.manifest.optionalDependencies).toBeUndefined()
  604. const repaired = structuredClone(subject.manifest)
  605. repairPackageDependencyManifest(subject)
  606. expect(subject.manifest).toEqual(repaired)
  607. })
  608. it('preserves unreferenced third-party declarations in configured Host packages', () => {
  609. const subject = sourceFacts({ 'src/index.ts': 'export function apply() {}' }, {
  610. dependencies: { 'unused-host-dep': '^1.0.0' },
  611. optionalDependencies: { 'unused-host-optional': '^2.0.0' },
  612. }, 'configured-host')
  613. repairPackageDependencyManifest(subject)
  614. expect(subject.manifest.dependencies).toEqual({ 'unused-host-dep': '^1.0.0' })
  615. expect(subject.manifest.optionalDependencies).toEqual({ 'unused-host-optional': '^2.0.0' })
  616. })
  617. it.each(['peerDependencies', 'optionalDependencies'] as const)('rejects browser-only imports declared in %s', (section) => {
  618. const subject = sourceFacts({
  619. 'src/index.ts': 'export function apply() {}',
  620. 'src/client/index.ts': "import 'external'",
  621. }, {
  622. devDependencies: { [CORDIS]: 'workspace:^' },
  623. peerDependencies: { [CORDIS]: 'workspace:^' },
  624. [section]: { [CORDIS]: 'workspace:^', external: '~1.2.3' },
  625. peerDependenciesMeta: { external: { optional: true } },
  626. })
  627. if (section === 'optionalDependencies') delete subject.manifest.optionalDependencies?.[CORDIS]
  628. const state = { facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames }
  629. expect(collectPackageDependencyViolations(state)).toContainEqual(
  630. expect.stringContaining(`must be devDependencies-only; found ${section}`),
  631. )
  632. repairPackageDependencyManifest(subject)
  633. expect(subject.manifest.devDependencies?.external).toBe('~1.2.3')
  634. expect(subject.manifest[section]?.external).toBeUndefined()
  635. expect(subject.manifest.peerDependenciesMeta).toBeUndefined()
  636. expect(collectPackageDependencyViolations(state)).toEqual([])
  637. })
  638. it('rejects a missing third-party declaration and leaves the in-memory manifest unchanged', () => {
  639. const subject = sourceFacts({
  640. 'src/index.ts': 'export function apply() {}',
  641. 'src/client/index.ts': "import 'undeclared'",
  642. })
  643. const before = structuredClone(subject.manifest)
  644. const state = { facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames }
  645. expect(collectPackageDependencyViolations(state)).toContain(
  646. 'packages/g/probe/package.json: undeclared (packages/g/probe/src/client/index.ts) '
  647. + 'must be devDependencies-only; found no dependency section',
  648. )
  649. expect(() => { repairPackageDependencyManifest(subject) }).toThrow(
  650. 'packages/g/probe/package.json: cannot repair undeclared third-party dependency undeclared; declare its version range first',
  651. )
  652. expect(subject.manifest).toEqual(before)
  653. })
  654. it('validates every third-party range before writing any manifest in a repair batch', () => {
  655. const root = mkdtempSync(join(tmpdir(), 'dsh-dependency-batch-'))
  656. roots.push(root)
  657. const valid = { ...facts({ name: '@deepseek-ai/dsh-first' }), manifestPath: 'first.json' }
  658. const base = facts({ name: '@deepseek-ai/dsh-second' })
  659. const invalid: PackageDependencyFacts = {
  660. ...base,
  661. manifestPath: 'second.json',
  662. allSourceUses: new Map([...base.allSourceUses, ['undeclared', ['src/client/index.ts']]]),
  663. }
  664. const subjects = [valid, invalid]
  665. const originals = subjects.map(subject => ({ subject, content: `${JSON.stringify(subject.manifest)}\n` }))
  666. for (const { subject, content } of originals) writeFileSync(join(root, subject.manifestPath), content)
  667. const state = { facts: subjects, packages: [], policyViolations: [], workspaceNames: valid.workspaceNames }
  668. expect(fixPackageDependencies(root, { ...state, policyViolations: ['unclassified Host export'] })).toEqual([])
  669. expect(() => fixPackageDependencies(root, state)).toThrow(
  670. 'second.json: cannot repair undeclared third-party dependency undeclared; declare its version range first',
  671. )
  672. for (const { subject, content } of originals) {
  673. expect(readFileSync(join(root, subject.manifestPath), 'utf8')).toBe(content)
  674. expect(`${JSON.stringify(subject.manifest)}\n`).toBe(content)
  675. }
  676. })
  677. it('moves browser-only third-party imports to development dependencies without changing their ranges', () => {
  678. const manifest: PackageDependencyManifest = {
  679. name: '@deepseek-ai/dsh-probe',
  680. dependencies: { '@deepseek-ai/dsh-runtime': 'workspace:^', external: '^1.2.3' },
  681. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-types': 'workspace:^' },
  682. peerDependencies: { [CORDIS]: 'workspace:^' },
  683. }
  684. const base = facts(manifest)
  685. const subject: PackageDependencyFacts = {
  686. ...base,
  687. allSourceUses: new Map([...base.allSourceUses, ['external', ['packages/core/probe/src/client/index.ts']]]),
  688. }
  689. const state = { facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames }
  690. expect(collectPackageDependencyViolations(state)).toEqual([
  691. 'packages/core/probe/package.json: external (packages/core/probe/src/client/index.ts) '
  692. + 'must be devDependencies-only; found dependencies',
  693. ])
  694. repairPackageDependencyManifest(subject)
  695. expect(manifest.dependencies?.external).toBeUndefined()
  696. expect(manifest.devDependencies?.external).toBe('^1.2.3')
  697. expect(collectPackageDependencyViolations(state)).toEqual([])
  698. const repaired = structuredClone(manifest)
  699. repairPackageDependencyManifest(subject)
  700. expect(manifest).toEqual(repaired)
  701. })
  702. it('does not leak repository configuration into captured dependency facts', () => {
  703. const manifest: PackageDependencyManifest = {
  704. name: '@deepseek-ai/dsh-client-locale',
  705. dependencies: { '@deepseek-ai/dsh-runtime': 'workspace:^' },
  706. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-types': 'workspace:^' },
  707. peerDependencies: { [CORDIS]: 'workspace:^' },
  708. }
  709. const base = facts(manifest)
  710. const subject: PackageDependencyFacts = {
  711. ...base,
  712. workspaceNames: new Set([...base.workspaceNames, '@deepseek-ai/dsh-api-remotes']),
  713. }
  714. expect(collectPackageDependencyViolations({
  715. facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames,
  716. })).toEqual([])
  717. })
  718. it('requires non-workspace Host runtime imports in dependencies', () => {
  719. const manifest: PackageDependencyManifest = {
  720. name: '@deepseek-ai/dsh-probe',
  721. dependencies: { '@deepseek-ai/dsh-runtime': 'workspace:^' },
  722. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-types': 'workspace:^', external: '^1.0.0' },
  723. peerDependencies: { [CORDIS]: 'workspace:^' },
  724. }
  725. const subject: PackageDependencyFacts = {
  726. ...facts(manifest),
  727. hostRuntimeSourceUses: new Map([
  728. ['@deepseek-ai/dsh-runtime', ['packages/core/probe/src/index.ts']],
  729. ['external', ['packages/core/probe/src/index.ts']],
  730. ]),
  731. allSourceUses: new Map([
  732. ...facts(manifest).allSourceUses,
  733. ['external', ['packages/core/probe/src/client/index.ts']],
  734. ]),
  735. }
  736. const state = {
  737. facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames,
  738. }
  739. expect(collectPackageDependencyViolations(state)).toContain(
  740. 'packages/core/probe/package.json: external (packages/core/probe/src/client/index.ts, packages/core/probe/src/index.ts) '
  741. + 'must be dependencies-only; found devDependencies',
  742. )
  743. repairPackageDependencyManifest(subject)
  744. expect(manifest.dependencies?.external).toBe('^1.0.0')
  745. expect(manifest.devDependencies?.external).toBeUndefined()
  746. const repaired = structuredClone(manifest)
  747. repairPackageDependencyManifest(subject)
  748. expect(manifest).toEqual(repaired)
  749. delete manifest.dependencies?.external
  750. expect(collectPackageDependencyViolations(state)).toContain(
  751. 'packages/core/probe/package.json: external (packages/core/probe/src/client/index.ts, packages/core/probe/src/index.ts) '
  752. + 'must be dependencies-only; found no dependency section',
  753. )
  754. })
  755. it('accepts Host dependencies, development-only inputs, and shared Cordis', () => {
  756. const manifest: PackageDependencyManifest = {
  757. name: '@deepseek-ai/dsh-probe',
  758. dependencies: {
  759. '@deepseek-ai/dsh-runtime': 'workspace:^',
  760. '@deepseek-ai/schemastery': 'workspace:^',
  761. external: '^1.0.0',
  762. },
  763. devDependencies: {
  764. '@deepseek-ai/dsh-types': 'workspace:^',
  765. [CORDIS]: 'workspace:^',
  766. },
  767. peerDependencies: { [CORDIS]: 'workspace:^' },
  768. }
  769. expect(collectPackageDependencyViolations({
  770. facts: [facts(manifest)], packages: [], policyViolations: [], workspaceNames: facts(manifest).workspaceNames,
  771. })).toEqual([])
  772. })
  773. it('lists managed Host runtime dependencies for fix review', () => {
  774. const subject = facts({ name: '@deepseek-ai/dsh-probe' })
  775. expect(formatManagedRuntimeDependencies({
  776. facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames,
  777. })).toEqual([
  778. 'verify-package-dependencies: 1 managed Host runtime edge(s) remain in dependencies across 1 package(s):',
  779. ' @deepseek-ai/dsh-probe -> @deepseek-ai/dsh-runtime: @deepseek-ai/dsh-runtime#runtimeValue',
  780. ])
  781. })
  782. it('reports an unapproved Host runtime export without rewriting its dependency section', () => {
  783. const manifest: PackageDependencyManifest = {
  784. name: '@deepseek-ai/dsh-probe',
  785. dependencies: { '@deepseek-ai/dsh-runtime': 'workspace:^' },
  786. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-types': 'workspace:^' },
  787. peerDependencies: { [CORDIS]: 'workspace:^' },
  788. }
  789. const subject = facts(manifest)
  790. const safetyViolations = collectHostDependencyExportPolicyViolations(
  791. [subject],
  792. subject.workspaceNames,
  793. { safeHostDependencyExports: {}, peerRequiredHostExports: {} },
  794. )
  795. const state = {
  796. facts: [subject], packages: [], policyViolations: safetyViolations, workspaceNames: subject.workspaceNames,
  797. }
  798. expect(safetyViolations).toEqual([
  799. 'packages/core/probe/src/index.ts:1:10: @deepseek-ai/dsh-runtime#runtimeValue is not classified as '
  800. + 'safe or peer-required — import { runtimeValue } from \'@deepseek-ai/dsh-runtime\'',
  801. ])
  802. expect(fixPackageDependencies('/unused', state)).toEqual([])
  803. expect(manifest.dependencies).toEqual({ '@deepseek-ai/dsh-runtime': 'workspace:^' })
  804. })
  805. it('keeps an edge as a peer when one imported export requires shared identity', () => {
  806. const manifest: PackageDependencyManifest = {
  807. name: '@deepseek-ai/dsh-probe',
  808. dependencies: { '@deepseek-ai/dsh-runtime': 'workspace:^' },
  809. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-types': 'workspace:^' },
  810. peerDependencies: { [CORDIS]: 'workspace:^' },
  811. }
  812. const subject: PackageDependencyFacts = {
  813. ...facts(manifest),
  814. peerRequiredHostDependencies: new Set(['@deepseek-ai/dsh-runtime']),
  815. }
  816. expect(collectHostDependencyExportPolicyViolations(
  817. [subject],
  818. subject.workspaceNames,
  819. {
  820. safeHostDependencyExports: {},
  821. peerRequiredHostExports: {
  822. '@deepseek-ai/dsh-runtime': ['runtimeValue'],
  823. },
  824. },
  825. )).toEqual([])
  826. repairPackageDependencyManifest(subject)
  827. expect(manifest.dependencies).toBeUndefined()
  828. expect(manifest.peerDependencies).toMatchObject({
  829. [CORDIS]: 'workspace:^',
  830. '@deepseek-ai/dsh-runtime': 'workspace:^',
  831. })
  832. expect(manifest.devDependencies).toMatchObject({
  833. [CORDIS]: 'workspace:^',
  834. '@deepseek-ai/dsh-runtime': 'workspace:^',
  835. })
  836. expect(formatPeerRequiredRuntimeDependencies({
  837. facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames,
  838. })).toEqual([
  839. 'verify-package-dependencies: 1 Host runtime edge(s) remain in peerDependencies because their exports require shared identity across 1 package(s):',
  840. ' @deepseek-ai/dsh-probe -> @deepseek-ai/dsh-runtime: @deepseek-ai/dsh-runtime#runtimeValue',
  841. ])
  842. })
  843. it('reports wrong sections, workspace ranges, and stale peer metadata', () => {
  844. const manifest: PackageDependencyManifest = {
  845. name: '@deepseek-ai/dsh-probe',
  846. dependencies: { '@deepseek-ai/dsh-types': 'workspace:*' },
  847. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-runtime': 'workspace:^' },
  848. peerDependencies: { [CORDIS]: 'workspace:*', '@deepseek-ai/dsh-runtime': 'workspace:^' },
  849. peerDependenciesMeta: { '@deepseek-ai/dsh-missing': { optional: true } },
  850. }
  851. const state = {
  852. facts: [facts(manifest)], packages: [], policyViolations: [], workspaceNames: facts(manifest).workspaceNames,
  853. }
  854. const violations = collectPackageDependencyViolations(state)
  855. expect(violations).toEqual(expect.arrayContaining([
  856. expect.stringContaining('@deepseek-ai/dsh-runtime'),
  857. expect.stringContaining('@deepseek-ai/dsh-types'),
  858. expect.stringContaining(`${CORDIS} must be matching peerDependencies + devDependencies`),
  859. expect.stringContaining('dependencies.@deepseek-ai/dsh-types must use workspace:^'),
  860. expect.stringContaining('peerDependenciesMeta.@deepseek-ai/dsh-missing has no matching'),
  861. ]))
  862. })
  863. it('repairs owned relationships without changing unrelated dependencies', () => {
  864. const root = mkdtempSync(join(tmpdir(), 'dsh-package-dependencies-'))
  865. roots.push(root)
  866. const manifestPath = 'package.json'
  867. const manifest: PackageDependencyManifest = {
  868. name: '@deepseek-ai/dsh-probe',
  869. dependencies: { '@deepseek-ai/schemastery': 'workspace:*', external: '^1.0.0' },
  870. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-runtime': 'workspace:^' },
  871. peerDependencies: {
  872. [CORDIS]: 'workspace:^',
  873. '@deepseek-ai/dsh-runtime': 'workspace:^',
  874. '@deepseek-ai/dsh-stale': 'workspace:^',
  875. },
  876. peerDependenciesMeta: { '@deepseek-ai/dsh-stale': { optional: true } },
  877. }
  878. writeFileSync(join(root, manifestPath), `${JSON.stringify(manifest, null, 2)}\n`)
  879. const subject = { ...facts(manifest), manifestPath }
  880. const state = { facts: [subject], packages: [], policyViolations: [], workspaceNames: subject.workspaceNames }
  881. expect(fixPackageDependencies(root, state)).toEqual([manifestPath])
  882. const fixed = JSON.parse(readFileSync(join(root, manifestPath), 'utf8')) as PackageDependencyManifest
  883. expect(fixed.dependencies).toEqual({
  884. '@deepseek-ai/schemastery': 'workspace:^',
  885. external: '^1.0.0',
  886. '@deepseek-ai/dsh-runtime': 'workspace:^',
  887. })
  888. expect(fixed.devDependencies).toEqual({
  889. [CORDIS]: 'workspace:^',
  890. '@deepseek-ai/dsh-types': 'workspace:^',
  891. '@deepseek-ai/dsh-stale': 'workspace:^',
  892. })
  893. expect(fixed.peerDependencies).toEqual({ [CORDIS]: 'workspace:^' })
  894. expect(fixed.peerDependenciesMeta).toBeUndefined()
  895. })
  896. it('repairs an in-memory manifest for benchmark simulation', () => {
  897. const manifest: PackageDependencyManifest = {
  898. name: '@deepseek-ai/dsh-probe',
  899. peerDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-runtime': 'workspace:^' },
  900. devDependencies: { [CORDIS]: 'workspace:^', '@deepseek-ai/dsh-runtime': 'workspace:^' },
  901. }
  902. repairPackageDependencyManifest(facts(manifest))
  903. expect(manifest.dependencies).toEqual({ '@deepseek-ai/dsh-runtime': 'workspace:^' })
  904. expect(manifest.peerDependencies).toEqual({ [CORDIS]: 'workspace:^' })
  905. })
  906. })