feat(invariants): require package-owned companions
This commit is contained in:
70
scripts/test-invariants.spec.ts
Normal file
70
scripts/test-invariants.spec.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { Context, Service } from 'cordis'
|
||||
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
|
||||
import { packageInvariantOwners } from './package-invariants.ts'
|
||||
import { MANUAL_INVARIANT_TESTS, testInvariantCompanions } from './test-invariants.ts'
|
||||
|
||||
declare module 'cordis' {
|
||||
interface Context {
|
||||
testInvariantProbe: TestInvariantProbe
|
||||
}
|
||||
}
|
||||
|
||||
class TestInvariantProbe extends Service {
|
||||
constructor(ctx: Context) {
|
||||
super(ctx, 'testInvariantProbe')
|
||||
}
|
||||
}
|
||||
|
||||
describe('global test invariant host', () => {
|
||||
it('loads every companion and reserves every package name with enabled checks', async () => {
|
||||
const ctx = new Context()
|
||||
await ctx.plugin(TestInvariantProbe)
|
||||
|
||||
const owners = packageInvariantOwners(process.cwd())
|
||||
expect(Object.keys(testInvariantCompanions)).toHaveLength(owners.length)
|
||||
const unreserved: string[] = []
|
||||
for (const owner of owners) {
|
||||
try {
|
||||
const dispose = ctx.invariants.register(owner.packageName, () => {})
|
||||
unreserved.push(owner.packageName)
|
||||
dispose()
|
||||
} catch (error) {
|
||||
expect(error).toHaveProperty(
|
||||
'message',
|
||||
`invariants: package "${owner.packageName}" is already registered`,
|
||||
)
|
||||
}
|
||||
}
|
||||
expect(unreserved).toEqual([])
|
||||
})
|
||||
|
||||
it('executes each companion registration with its owning package name', async () => {
|
||||
const owners = new Map(packageInvariantOwners(process.cwd()).map(owner => [owner.sourcePath, owner.packageName]))
|
||||
const registrations = new Map<string, string>()
|
||||
const register = vi.fn((_packageName: string, installer: InvariantInstaller) => {
|
||||
expect(typeof installer).toBe('function')
|
||||
return () => {}
|
||||
})
|
||||
const fakeContext = { invariants: { register } } as unknown as Context
|
||||
for (const [rawPath, companion] of Object.entries(testInvariantCompanions)) {
|
||||
const path = rawPath.replace(/^\.\.\//, '')
|
||||
await companion.apply(fakeContext)
|
||||
const call = register.mock.calls.at(-1)
|
||||
if (call === undefined) throw new Error(`${path}: companion did not register`)
|
||||
registrations.set(path, call[0])
|
||||
}
|
||||
expect(registrations).toEqual(owners)
|
||||
})
|
||||
|
||||
it('limits manual composition to focused invariant topology tests', () => {
|
||||
expect(MANUAL_INVARIANT_TESTS).toEqual([
|
||||
'/packages/support/invariants/tests/service.spec.ts',
|
||||
'/packages/core/session/tests/invariant.spec.ts',
|
||||
'/packages/core/agent/tests/invariant.spec.ts',
|
||||
'/packages/core/scope/tests/invariant.spec.ts',
|
||||
'/packages/core/agent-loop/tests/invariant.spec.ts',
|
||||
'/packages/examples/agent-spine-demo/tests/agent-core.spec.ts',
|
||||
])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user