test-invariants.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context, Service } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
  5. import { packageInvariantOwners } from './package-invariants.ts'
  6. import {
  7. testInvariantCompanionPaths,
  8. testInvariantCompanions,
  9. usesManualInvariantTree,
  10. } from './test-invariants.ts'
  11. declare module 'cordis' {
  12. interface Context {
  13. testInvariantProbe: TestInvariantProbe
  14. }
  15. }
  16. class TestInvariantProbe extends Service {
  17. constructor(ctx: Context) {
  18. super(ctx, 'testInvariantProbe')
  19. }
  20. }
  21. describe('global test invariant host', () => {
  22. it('uses one exhaustive topology to reserve every package name with enabled checks', async () => {
  23. const ctx = new Context()
  24. await ctx.plugin(TestInvariantProbe)
  25. const owners = packageInvariantOwners(process.cwd())
  26. expect(Object.keys(testInvariantCompanions)).toHaveLength(owners.length)
  27. const unreserved: string[] = []
  28. for (const owner of owners) {
  29. try {
  30. const dispose = ctx.invariants.register(owner.packageName, () => {})
  31. unreserved.push(owner.packageName)
  32. dispose()
  33. } catch (error) {
  34. expect(error).toHaveProperty(
  35. 'message',
  36. `invariants: package "${owner.packageName}" is already registered`,
  37. )
  38. }
  39. }
  40. expect(unreserved).toEqual([])
  41. })
  42. it('mounts the owning package companion while leaving non-package roots service-only', () => {
  43. expect(testInvariantCompanionPaths('/repo/packages/core/tools/tests/tools.spec.ts'))
  44. .toEqual(['../packages/core/tools/src/invariant.ts'])
  45. expect(testInvariantCompanionPaths('/repo/examples/echo-agent/tests/echo.spec.ts')).toEqual([])
  46. expect(testInvariantCompanionPaths('/repo/scripts/test-invariants.spec.ts'))
  47. .toEqual(Object.keys(testInvariantCompanions).sort())
  48. })
  49. it('loads and executes every source companion through the real Loader shape', async () => {
  50. const owners = new Map(packageInvariantOwners(process.cwd()).map(owner => [owner.sourcePath, owner.packageName]))
  51. const registrations = new Map<string, string>()
  52. const loader = Object.create(Loader.prototype) as Loader
  53. const register = vi.fn((_packageName: string, installer: InvariantInstaller) => {
  54. expect(typeof installer).toBe('function')
  55. return () => {}
  56. })
  57. const fakeContext = { invariants: { register } } as unknown as Context
  58. for (const [rawPath, companion] of Object.entries(testInvariantCompanions)) {
  59. const path = rawPath.replace(/^\.\.\//, '')
  60. expect(companion.default, path).toBeUndefined()
  61. const unwrapped = loader.unwrapExports(companion) as typeof companion
  62. expect(unwrapped, path).toBe(companion)
  63. expect(typeof unwrapped.name, path).toBe('string')
  64. expect(unwrapped.inject, path).toContain('invariants')
  65. expect(typeof unwrapped.apply, path).toBe('function')
  66. await unwrapped.apply(fakeContext)
  67. const call = register.mock.calls.at(-1)
  68. if (call === undefined) throw new Error(`${path}: companion did not register`)
  69. registrations.set(path, call[0])
  70. }
  71. expect(registrations).toEqual(owners)
  72. })
  73. it('recognizes focused invariant suites without a package inventory', () => {
  74. expect(usesManualInvariantTree('/repo/packages/core/session/tests/invariant.spec.ts')).toBe(true)
  75. expect(usesManualInvariantTree('/repo/packages/core/session/tests/request-invariant-hmr.spec.ts')).toBe(true)
  76. expect(usesManualInvariantTree('C:\\repo\\packages\\support\\invariants\\tests\\service.spec.ts')).toBe(true)
  77. expect(usesManualInvariantTree('/repo/packages/examples/agent-spine-demo/tests/agent-core.spec.ts')).toBe(true)
  78. expect(usesManualInvariantTree('/repo/packages/core/session/tests/session.spec.ts')).toBe(false)
  79. })
  80. })