Fix invariant config validation readiness
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { describe, expect, it, vi } from 'vitest'
|
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 Loader from '@cordisjs/plugin-loader'
|
||||||
|
import z from 'schemastery'
|
||||||
import InvariantService from '@deepseek-ai/dsh-invariants'
|
import InvariantService from '@deepseek-ai/dsh-invariants'
|
||||||
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
|
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
|
||||||
import { packageInvariantOwners } from './package-invariants.ts'
|
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)
|
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 () => {
|
it('holds a root plugin until every lazy companion is active, then permits nested startup', async () => {
|
||||||
const delayedStarted = deferred()
|
const delayedStarted = deferred()
|
||||||
const releaseDelayed = deferred()
|
const releaseDelayed = deferred()
|
||||||
|
|||||||
@@ -75,7 +75,9 @@ RegistryService.prototype.plugin = function(plugin: Plugin, config?: unknown, ge
|
|||||||
if (hasBarrierOwner(host, this.ctx)) {
|
if (hasBarrierOwner(host, this.ctx)) {
|
||||||
return originalPlugin.call(this, plugin, config, getOuterStack)
|
return originalPlugin.call(this, plugin, config, getOuterStack)
|
||||||
}
|
}
|
||||||
if (callback === undefined) return originalPlugin.call(this, plugin, config, getOuterStack)
|
if (callback === undefined) {
|
||||||
|
return originalPlugin.call(this, plugin, config, getOuterStack)
|
||||||
|
}
|
||||||
|
|
||||||
const fiber = originalPlugin.call(
|
const fiber = originalPlugin.call(
|
||||||
this,
|
this,
|
||||||
@@ -84,7 +86,7 @@ RegistryService.prototype.plugin = function(plugin: Plugin, config?: unknown, ge
|
|||||||
getOuterStack,
|
getOuterStack,
|
||||||
)
|
)
|
||||||
host.barrierOwners.add(fiber.ctx.fiber)
|
host.barrierOwners.add(fiber.ctx.fiber)
|
||||||
return joinInvariantStartup(fiber, host.ready)
|
return joinInvariantStartup(fiber, host.ready, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,8 +206,21 @@ function withInvariantReadiness(plugin: Plugin, callback: PluginCallback): Plugi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function joinInvariantStartup(fiber: PluginFiber, invariantReady: Promise<void>): PluginFiber {
|
function joinInvariantStartup(
|
||||||
const readiness = invariantReady.then(() => fiber.await())
|
fiber: PluginFiber,
|
||||||
|
invariantReady: Promise<void>,
|
||||||
|
disposePendingFailure = false,
|
||||||
|
): PluginFiber {
|
||||||
|
const initialized = disposePendingFailure
|
||||||
|
? fiber.await().catch(async (error: unknown) => {
|
||||||
|
// Config validation is the only failure recorded while a gated fiber
|
||||||
|
// is still PENDING. Dispose it before readiness publication can
|
||||||
|
// refresh the rejected fiber with its uninitialized config.
|
||||||
|
if (fiber.state === FiberState.PENDING) await fiber.dispose()
|
||||||
|
throw error
|
||||||
|
})
|
||||||
|
: Promise.resolve()
|
||||||
|
const readiness = initialized.then(() => invariantReady).then(() => fiber.await())
|
||||||
const joined = Object.create(fiber) as PluginFiber
|
const joined = Object.create(fiber) as PluginFiber
|
||||||
joined.then = readiness.then.bind(readiness)
|
joined.then = readiness.then.bind(readiness)
|
||||||
return joined
|
return joined
|
||||||
|
|||||||
Reference in New Issue
Block a user