dsh-better-chat/tests/inline-interaction.test.ts

377 lines
15 KiB
TypeScript

import assert from 'node:assert/strict';
import { test } from 'vitest';
import { SlotCore } from '@deepseek-ai/dsh-client-ui-slots';
import type { ComposerChainProps } from '@deepseek-ai/dsh-client-ui-conversation/client';
import type { SessionId } from '@deepseek-ai/dsh-session/types';
import {
INLINE_COMPOSER_INTERCEPTOR_PRIORITY,
INLINE_COMPOSER_INTERCEPTOR_REGISTRANT,
OFFICIAL_SEATS,
OFFICIAL_SLOTS,
acceptMirroredComposerEntry,
type CompositionRegistry,
} from '../src/client/official-slots.js';
import {
INLINE_INTERACTIONS_DEFAULT,
InlineComposerInterceptor,
createInlineInteractionController,
inlineInteractionsOf,
isInlineQuestionPending,
} from '../src/client/inline-interaction.js';
const tick = () => Promise.resolve();
const twoTicks = async () => {
await tick();
await tick();
};
function createTestRegistry(options: { declareReaderSeat?: boolean } = {}) {
const core = new SlotCore();
const register = (opts: object, component: unknown) =>
(core.register as Function)(opts, component) as () => void;
const children: Record<string, { kind: 'chain'; scope: 'session' }> = {
[OFFICIAL_SLOTS.composer]: { kind: 'chain', scope: 'session' },
};
if (options.declareReaderSeat !== false) {
children[OFFICIAL_SEATS.composer] = { kind: 'chain', scope: 'session' };
}
const disposeRoot = register({ name: 'root', children }, () => null);
const slots: CompositionRegistry = {
register,
spec: key => core.spec(key as never),
entriesOfSlot: key => core.entriesOfSlot(key),
subscribe: (key, fn) => core.subscribe(key, fn),
inject: (key, effect) => {
let active: (() => void) | undefined;
let activeEpoch: number | undefined;
let stopped = false;
const reconcile = () => {
if (stopped) return;
const spec = core.specDynamic(key);
const epoch = core.declarationEpoch(key);
if (active !== undefined && activeEpoch === epoch) return;
const prev = active;
active = undefined;
activeEpoch = undefined;
prev?.();
if (spec === undefined) return;
active = effect();
activeEpoch = epoch;
};
const unsub = core.subscribeDeclaration(key, reconcile);
reconcile();
return () => {
if (stopped) return;
stopped = true;
unsub();
const prev = active;
active = undefined;
activeEpoch = undefined;
prev?.();
};
},
};
return { core, slots, register, disposeRoot };
}
function elect(core: SlotCore, key: string, owner: ComposerChainProps) {
for (const entry of core.entriesOfSlot(key)) {
const select = entry.select as ((value: ComposerChainProps) => unknown) | undefined;
if (typeof select !== 'function') continue;
const matched = select(owner);
if (matched !== null) return { entry, matched };
}
return null;
}
const makeOwner = (
sessionId: string,
kind?: string,
subagentMode?: 'one-shot' | 'unknown' | 'continuable',
): ComposerChainProps => ({
sessionId: sessionId as SessionId,
session: subagentMode
? ({ subagent: { address: { mode: subagentMode } } } as unknown as ComposerChainProps['session'])
: ({ subagent: null } as unknown as ComposerChainProps['session']),
pendingInteraction: kind
? ({ key: `${kind}:1`, kind, sessionId: sessionId as SessionId } as ComposerChainProps['pendingInteraction'])
: undefined,
});
test('inlineInteractionsOf defaults to true and honors explicit boolean overrides', () => {
assert.equal(INLINE_INTERACTIONS_DEFAULT, true);
assert.equal(inlineInteractionsOf(undefined), true);
assert.equal(inlineInteractionsOf({}), true);
assert.equal(inlineInteractionsOf({ inlineInteractions: true }), true);
assert.equal(inlineInteractionsOf({ inlineInteractions: false }), false);
});
test('acceptMirroredComposerEntry excludes interceptor and read-only subagent banners while accepting interaction cards', () => {
const { core, register } = createTestRegistry();
register({
name: OFFICIAL_SLOTS.composer,
priority: -10,
registrant: 'dsh-client-ui-subagent',
select: () => null,
}, () => null);
register({
name: OFFICIAL_SLOTS.composer,
priority: INLINE_COMPOSER_INTERCEPTOR_PRIORITY,
registrant: INLINE_COMPOSER_INTERCEPTOR_REGISTRANT,
select: () => null,
}, InlineComposerInterceptor);
register({
name: OFFICIAL_SLOTS.composer,
priority: -1,
registrant: 'dsh-cua',
select: () => null,
}, () => null);
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-user-questions',
select: () => null,
}, () => null);
register({
name: OFFICIAL_SLOTS.composer,
priority: 1,
registrant: 'dsh-client-ui-approval',
select: () => null,
}, () => null);
const accepted = core
.entriesOfSlot(OFFICIAL_SLOTS.composer)
.filter(acceptMirroredComposerEntry)
.map(entry => entry.registrant);
assert.deepEqual(accepted, ['dsh-cua', 'dsh-client-ui-user-questions', 'dsh-client-ui-approval']);
});
test('isInlineQuestionPending accepts only user-question option cards and keeps approvals/plan-review real-time', () => {
assert.equal(isInlineQuestionPending(undefined), false);
assert.equal(isInlineQuestionPending(makeOwner('s1', 'question').pendingInteraction), true);
assert.equal(isInlineQuestionPending(makeOwner('s1', 'approval').pendingInteraction), false);
assert.equal(isInlineQuestionPending(makeOwner('s1', 'plan-review').pendingInteraction), false);
});
test('steady-state interception claims official question cards while letting approvals/plan-review pop up natively in real time', async () => {
const { core, slots, register } = createTestRegistry();
const SubagentBanner = () => null;
const QuestionCard = () => null;
const ApprovalCard = () => null;
const PlanReviewCard = () => null;
register({
name: OFFICIAL_SLOTS.composer,
priority: -10,
registrant: 'dsh-client-ui-subagent',
select: (owner: ComposerChainProps) => {
const mode = (owner.session as { subagent?: { address?: { mode?: string } } | null } | undefined)?.subagent?.address?.mode;
return mode === 'one-shot' || mode === 'unknown' ? { reason: mode } : null;
},
}, SubagentBanner);
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-user-questions',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'question' ? owner.pendingInteraction : null,
}, QuestionCard);
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-plan-review',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'plan-review' ? owner.pendingInteraction : null,
}, PlanReviewCard);
register({
name: OFFICIAL_SLOTS.composer,
priority: 1,
registrant: 'dsh-client-ui-approval',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'approval' ? owner.pendingInteraction : null,
}, ApprovalCard);
const controller = createInlineInteractionController(slots);
const s1 = 'session-1' as SessionId;
const s2 = 'session-2' as SessionId;
const token = Symbol('reader-1');
const release = controller.acquire(s1, token);
// Single-mount timing: not claimed synchronously or after 1 microtask.
assert.equal(controller.isClaimed(s1, token), false);
await tick();
assert.equal(controller.isClaimed(s1, token), false);
await tick();
assert.equal(controller.isClaimed(s1, token), true);
// Question card for mounted Reader session elects our interceptor (renders in-bubble via OFFICIAL_SEATS.composer).
const questionWin = elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question'));
assert.equal(questionWin?.entry.component, InlineComposerInterceptor);
assert.equal(InlineComposerInterceptor(), null);
// Tool approval & plan confirmation MUST pass through real-time to native bottom composer so work is never blocked!
const approvalWin = elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'approval'));
assert.equal(approvalWin?.entry.component, ApprovalCard);
const planWin = elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'plan-review'));
assert.equal(planWin?.entry.component, PlanReviewCard);
// Idle session (no pendingInteraction) falls through to resident composerBar.
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, undefined)), null);
// Read-only subagent banner at priority -10 wins ahead of our interceptor.
const subagentWin = elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question', 'one-shot'));
assert.equal(subagentWin?.entry.component, SubagentBanner);
// Unhandled pendingInteraction kind is NOT swallowed.
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'unknown-kind')), null);
// Other session without mounted Reader keeps native bottom dock question cards.
const s2Win = elect(core, OFFICIAL_SLOTS.composer, makeOwner(s2, 'question'));
assert.equal(s2Win?.entry.component, QuestionCard);
release();
controller.dispose();
});
test('releasing a Reader mount (tab switch) clears inline claim synchronously and restores bottom dock immediately', async () => {
const { core, slots, register } = createTestRegistry();
const QuestionCard = () => null;
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-user-questions',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'question' ? owner.pendingInteraction : null,
}, QuestionCard);
const controller = createInlineInteractionController(slots);
const s1 = 'session-tab' as SessionId;
const token = Symbol('reader');
const release = controller.acquire(s1, token);
await twoTicks();
assert.equal(controller.isClaimed(s1, token), true);
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question'))?.entry.component, InlineComposerInterceptor);
let composerNotified = 0;
const unsubComposer = core.subscribe(OFFICIAL_SLOTS.composer, () => { composerNotified += 1; });
release();
// Inline claim clears synchronously and bottom composer elects official QuestionCard.
assert.equal(controller.isClaimed(s1, token), false);
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question'))?.entry.component, QuestionCard);
await tick();
assert.ok(composerNotified > 0, 'conversation.composer subscribers must be notified so bottom dock remounts');
unsubComposer();
controller.dispose();
});
test('setEnabled toggles cleanly with single-mount two-microtask handoff in both directions', async () => {
const { core, slots, register } = createTestRegistry();
const QuestionCard = () => null;
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-user-questions',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'question' ? owner.pendingInteraction : null,
}, QuestionCard);
const controller = createInlineInteractionController(slots);
const s1 = 'session-toggle' as SessionId;
const token = Symbol('reader');
const release = controller.acquire(s1, token);
await twoTicks();
assert.equal(controller.isClaimed(s1, token), true);
// Turning OFF: inline claim drops synchronously; interceptor stays removed after two microtasks.
controller.setEnabled(false);
assert.equal(controller.isEnabled(), false);
assert.equal(controller.isClaimed(s1, token), false);
// Interceptor entry still present until the 2-microtask handoff disposes it cleanly.
assert.equal(
core.entriesOfSlot(OFFICIAL_SLOTS.composer).some(e => e.registrant === INLINE_COMPOSER_INTERCEPTOR_REGISTRANT),
true,
);
await twoTicks();
assert.equal(
core.entriesOfSlot(OFFICIAL_SLOTS.composer).some(e => e.registrant === INLINE_COMPOSER_INTERCEPTOR_REGISTRANT),
false,
);
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question'))?.entry.component, QuestionCard);
// Turning ON: interceptor registers synchronously; inline claim waits 2 microtasks.
controller.setEnabled(true);
assert.equal(controller.isEnabled(), true);
assert.equal(
core.entriesOfSlot(OFFICIAL_SLOTS.composer).some(e => e.registrant === INLINE_COMPOSER_INTERCEPTOR_REGISTRANT),
true,
);
assert.equal(controller.isClaimed(s1, token), false);
await tick();
assert.equal(controller.isClaimed(s1, token), false);
await tick();
assert.equal(controller.isClaimed(s1, token), true);
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question'))?.entry.component, InlineComposerInterceptor);
release();
controller.dispose();
});
test('multiple Reader instances for the same session only claim the primary token and promote on release', async () => {
const { slots, register } = createTestRegistry();
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-user-questions',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'question' ? owner.pendingInteraction : null,
}, () => null);
const controller = createInlineInteractionController(slots);
const s1 = 'session-multi' as SessionId;
const primary = Symbol('primary');
const secondary = Symbol('secondary');
const releasePrimary = controller.acquire(s1, primary);
const releaseSecondary = controller.acquire(s1, secondary);
await twoTicks();
assert.equal(controller.isClaimed(s1, primary), true);
assert.equal(controller.isClaimed(s1, secondary), false);
releasePrimary();
assert.equal(controller.isClaimed(s1, primary), false);
assert.equal(controller.isClaimed(s1, secondary), false);
await twoTicks();
assert.equal(controller.isClaimed(s1, secondary), true);
releaseSecondary();
assert.equal(controller.isClaimed(s1, secondary), false);
controller.dispose();
});
test('interceptor declines when Reader mirrored composer seat is not declared and cleans up on dispose', async () => {
const { core, slots, register } = createTestRegistry({ declareReaderSeat: false });
const QuestionCard = () => null;
register({
name: OFFICIAL_SLOTS.composer,
priority: 0,
registrant: 'dsh-client-ui-user-questions',
select: (owner: ComposerChainProps) => owner.pendingInteraction?.kind === 'question' ? owner.pendingInteraction : null,
}, QuestionCard);
const controller = createInlineInteractionController(slots);
const s1 = 'session-no-seat' as SessionId;
const token = Symbol('reader');
controller.acquire(s1, token);
await twoTicks();
// Without OFFICIAL_SEATS.composer declared, interceptor refuses so bottom dock keeps handling it.
assert.equal(elect(core, OFFICIAL_SLOTS.composer, makeOwner(s1, 'question'))?.entry.component, QuestionCard);
controller.dispose();
assert.equal(controller.isClaimed(s1, token), false);
assert.equal(
core.entriesOfSlot(OFFICIAL_SLOTS.composer).some(e => e.registrant === INLINE_COMPOSER_INTERCEPTOR_REGISTRANT),
false,
);
});