package-graph.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, describe, expect, it } from 'vitest'
  5. import { collectPackageGraph } from './package-graph.ts'
  6. const roots: string[] = []
  7. afterEach(() => {
  8. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  9. })
  10. function fixture(packages: Readonly<Record<string, readonly string[]>>): string {
  11. const root = mkdtempSync(join(tmpdir(), 'dsh-package-graph-'))
  12. roots.push(root)
  13. for (const [name, dependencies] of Object.entries(packages)) {
  14. const directory = join(root, 'packages', 'client', name)
  15. mkdirSync(directory, { recursive: true })
  16. writeFileSync(join(directory, 'package.json'), `${JSON.stringify({
  17. name: `@deepseek-ai/dsh-${name}`,
  18. peerDependencies: Object.fromEntries(dependencies.map(dependency => [
  19. `@deepseek-ai/dsh-${dependency}`,
  20. 'workspace:^',
  21. ])),
  22. }, null, 2)}\n`)
  23. }
  24. return root
  25. }
  26. describe('collectPackageGraph', () => {
  27. it('orders packages after their dependencies', () => {
  28. const root = fixture({ application: ['feature'], feature: ['foundation'], foundation: [] })
  29. expect(collectPackageGraph(root, ['client'], 'fixture').map(pkg => pkg.short))
  30. .toEqual(['foundation', 'feature', 'application'])
  31. })
  32. it('keeps a dependency cycle together and before its consumers', () => {
  33. const root = fixture({ consumer: ['left'], left: ['right'], right: ['left'], foundation: [] })
  34. expect(collectPackageGraph(root, ['client'], 'fixture').map(pkg => pkg.short))
  35. .toEqual(['foundation', 'left', 'right', 'consumer'])
  36. })
  37. it('rejects a missing in-repo peer', () => {
  38. const root = fixture({ consumer: ['missing'] })
  39. expect(() => collectPackageGraph(root, ['client'], 'fixture'))
  40. .toThrow('fixture: @deepseek-ai/dsh-consumer references missing in-repo peer @deepseek-ai/dsh-missing')
  41. })
  42. })