1
0

test-invariants.spec.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, load] of Object.entries(testInvariantCompanions)) {
  59. const companion = await load()
  60. const path = rawPath.replace(/^\.\.\//, '')
  61. expect(companion.default, path).toBeUndefined()
  62. const unwrapped = loader.unwrapExports(companion) as typeof companion
  63. expect(unwrapped, path).toBe(companion)
  64. expect(typeof unwrapped.name, path).toBe('string')
  65. expect(unwrapped.inject, path).toContain('invariants')
  66. expect(typeof unwrapped.apply, path).toBe('function')
  67. await unwrapped.apply(fakeContext)
  68. const call = register.mock.calls.at(-1)
  69. if (call === undefined) throw new Error(`${path}: companion did not register`)
  70. registrations.set(path, call[0])
  71. }
  72. expect(registrations).toEqual(owners)
  73. })
  74. it('recognizes focused invariant suites without a package inventory', () => {
  75. expect(usesManualInvariantTree('/repo/packages/core/session/tests/invariant.spec.ts')).toBe(true)
  76. expect(usesManualInvariantTree('/repo/packages/core/session/tests/request-invariant-hmr.spec.ts')).toBe(true)
  77. expect(usesManualInvariantTree('C:\\repo\\packages\\support\\invariants\\tests\\service.spec.ts')).toBe(true)
  78. expect(usesManualInvariantTree('/repo/packages/examples/agent-spine-demo/tests/agent-core.spec.ts')).toBe(true)
  79. expect(usesManualInvariantTree('/repo/packages/core/session/tests/session.spec.ts')).toBe(false)
  80. })
  81. })