Fix invariant config validation readiness

This commit is contained in:
Hypatia May
2026-07-31 01:04:21 +08:00
parent 096fe8b6d7
commit ed5a82f930
2 changed files with 50 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { Context, FiberState, Service } from 'cordis'
import { Context, FiberState, Service, ValidationError } from 'cordis'
import Loader from '@cordisjs/plugin-loader'
import z from 'schemastery'
import InvariantService from '@deepseek-ai/dsh-invariants'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { packageInvariantOwners } from './package-invariants.ts'
@@ -136,6 +137,35 @@ describe('global test invariant host', () => {
expect(usesManualInvariantTree('/repo/packages/core/session/tests/session.spec.ts')).toBe(false)
})
it('preserves config validation failures without starting the rejected plugin', async () => {
const ctx = new Context()
const apply = vi.fn(function invalidConfigApply() {
throw new Error('invalid plugin apply executed')
})
const plugin = {
apply,
Config: z.object({
requiredValue: z.string().required(),
}),
}
const fiber = ctx.plugin(plugin, {})
const firstError: unknown = await fiber.then(
() => undefined,
(error: unknown) => error,
)
expect(firstError).toBeInstanceOf(ValidationError)
expect(firstError).toHaveProperty('message', expect.stringMatching(/requiredValue/))
await ctx.plugin(TestInvariantProbe)
const secondError: unknown = await fiber.then(
() => undefined,
(error: unknown) => error,
)
expect(secondError).toBe(firstError)
expect(fiber.state).toBe(FiberState.DISPOSED)
expect(apply).not.toHaveBeenCalled()
})
it('holds a root plugin until every lazy companion is active, then permits nested startup', async () => {
const delayedStarted = deferred()
const releaseDelayed = deferred()