fix(gui): contain selector failures and key the chain boundary by entry

Two hardenings on the chain outlet branch:

A throwing chain selector runs before its entry's SlotErrorBoundary
exists, so uncontained it blacked out the whole owner region and skipped
the remaining chain. It now degrades to a decline: reported via
console.error with the registrant identity, later entries still tried,
all-null/all-throw passes land on the owner fallback.

The elected entry's boundary is now keyed by entry identity: an unkeyed
boundary that failed on entry A survived a re-election and kept a
healthy entry B blacked out until the outlet unmounted. The key remounts
the boundary fresh whenever the election changes.
This commit is contained in:
imccyu
2026-07-23 19:53:54 +08:00
parent abe3da3656
commit a8c6dcb180
2 changed files with 85 additions and 3 deletions

View File

@@ -312,6 +312,55 @@ describe('chain outlets and the renderSlotChain binding', () => {
expect(declinerBody).not.toHaveBeenCalled()
})
it('contains a throwing selector to its entry: reported, treated as declined, chain and fallback intact', () => {
const h = makeHost()
h.declare('k.chain', CHAIN_ROOT)
h.add('k.chain', chainEntryOf({
component: () => <span>never</span>,
select: () => { throw new Error('selector boom') },
}))
h.add('k.chain', chainEntryOf({
component: ({ matched }: { matched?: string }) => <b>{matched}</b>,
select: (owner) => (owner as { pick?: string }).pick ?? null,
}))
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
const { view } = mountChainRoot(h, { 'k.chain': CHAIN_ROOT }, (renderSlotChain) => <>
<main>{renderSlotChain('k.chain', { pick: 'OK' })}</main>
<aside>{renderSlotChain('k.chain', {}, { fallback: <i>fb</i> })}</aside>
</>)
// The breach never escapes to the owner region: later entries still get
// tried, and an all-throw/all-null pass still lands on the fallback.
expect(view.container.querySelector('main')!.textContent).toBe('OK')
expect(view.container.querySelector('aside')!.textContent).toBe('fb')
expect(spy.mock.calls.some(([msg]) => String(msg).includes('chain selector crashed'))).toBe(true)
spy.mockRestore()
})
it('remounts the boundary on re-election: a failed entry does not black out its replacement', () => {
const h = makeHost()
h.declare('k.chain', CHAIN_ROOT)
h.add('k.chain', chainEntryOf({
component: () => { throw new Error('entry A boom') },
select: (owner) => (owner as { pick?: string }).pick === 'A' ? {} : null,
}))
h.add('k.chain', chainEntryOf({
component: () => <b>B-ok</b>,
select: (owner) => (owner as { pick?: string }).pick === 'B' ? {} : null,
}))
let pick = 'A'
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
const { view } = mountChainRoot(h, { 'k.chain': CHAIN_ROOT },
(renderSlotChain) => renderSlotChain('k.chain', { pick }))
spy.mockRestore()
expect(view.container.querySelector('[data-slot-error]')).not.toBeNull()
// Re-elect entry B: the entry-keyed boundary remounts fresh instead of
// holding A's failed state over the healthy replacement.
pick = 'B'
act(() => { h.add('root', { component: () => null }) }) // root bump re-renders the dispatch site
expect(view.container.textContent).toBe('B-ok')
expect(view.container.querySelector('[data-slot-error]')).toBeNull()
})
it('falls to the owner fallback when every selector declines, and re-routes live', () => {
const h = makeHost()
h.declare('k.chain', CHAIN_ROOT)