Files
apes-sidebar/tests/sidechat-composition-priority.spec.tsx
T
NolanHo bed56758c7 feat(sidechat): align the side conversation with the main pane
The side chat inherited none of the main pane's reading behaviour and could
not say where it forked off. This change brings it in line and keeps the
upstream v0.30.1 release intact.

Work details follow the main conversation's setting (transcriptView +
collapseTiming): settled turns fold behind the host's own "Completed in Ns"
line, running turns stay open, verbose never folds.

The transcript now names its origin: the events route reports the fork point,
and the block above the first row renders the inherited question as an
ordinary user bubble and the inherited answer as ordinary assistant prose,
bracketed by two divider lines and carried verbatim.

Reading the thread got cheaper and calmer: only the tail is mounted with a
step-wise reveal, scroll-follow answers an explicit intent, and the composer
guards IME composition, key repeat and the submit-gesture choice while send
and stop stay independent controls.

Everything visual rides the host's own surface: content-size and code font
axes, the host's markdown surface for reasoning, host cards for search/web/
terminal/diff results, compaction and retry rows, and host-defined design
tokens (six borders and eleven colours had referenced tokens the host never
defines, so they never painted).

Thread creation became reliable: a stored parent is resumed through the
session controller, the create budget is a deployment setting instead of a
hard-coded 15 seconds, and the child attaches at the parent's workspace cwd
while its tools keep running in the reader's current directory.

Upstream v0.30.1 brings side-chat images, clickable quote chips, editor draft
backup, the settings icon slot and the 0.2.2 model seam; its per-thread draft
owner and image projection replace the hand-written paths this branch used to
carry, so only one implementation of each survives.
2026-11-09 11:59:12 +08:00

180 lines
7.7 KiB
TypeScript

// @vitest-environment jsdom
/**
* Composer priority — a LIVE IME composition outranks the modifier / repeat
* guards. Design basis: `/tmp/bsb-parity/spec.md` §D7's keymap parity work plus
* the host keymap (`/root/dsh-web/app/packages/client/ui-conversation/src/
* client/input/editor/keymap.ts:145-149`: the composition check answers
* "neither submit nor break the line. No preventDefault: the browser owns the
* gesture").
*
* Why this spec exists: `SideChatView` now consumes every shortcut/layout and
* repeated Enter (`isGuardedKeydown(gesture) || gesture.repeat === true` →
* `preventDefault()`) BEFORE it asks whether a composition owns the keyboard.
* So while an IME is composing, `Alt+Enter` (and a repeated Enter) is
* swallowed: the key never reaches the candidate picker / layout switcher the
* reader is actually using. The guards must not outrank the IME.
*
* ============================ CONTRACT ============================
* 1. LIVE COMPOSITION = `compositionPhase(...) === 'active'` — an event with
* `isComposing === true`, an event with the legacy `keyCode === 229`, or a
* composition tracked live from `compositionstart`. Such a keydown is the
* input method's:
* - it never submits (`sidechat.prompt` stays at zero), AND
* - it is NEVER consumed (`event.defaultPrevented === false`), even when
* it also carries `altKey` or `repeat === true`. Consuming it fights the
* candidate picker / layout switcher.
* 2. NON-COMPOSING keydowns keep the guards from
* tests/sidechat-composer-guards.spec.tsx: `Alt+Enter` and a repeated Enter
* are consumed (`defaultPrevented === true`) and never submit.
* 3. The draft is never damaged: none of these keydowns may submit, so the
* composer keeps its text.
*
* The composition-CLOSING grace window (Safari's late keydown after
* `compositionend`) is NOT part of this contract: it is owned by
* tests/sidechat-ime-consuming-enter.spec.tsx, which pins "consumed, not
* submitted" for it.
* ==================================================================
*
* jsdom does not implement textarea default actions, so "consumed" is read as
* `defaultPrevented`; `isComposing` / `keyCode` / `repeat` are not constructor
* options there and are defined on the dispatched event.
*/
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { act } from 'react-dom/test-utils'
import { installSidechatHarness, type SidechatHarness } from './helpers/sidechat-view-harness.tsx'
import type { RenderedRoot } from './test-utils.ts'
/** What one synthesized Enter carries. */
interface PressInit {
alt?: boolean
repeat?: boolean
/** `KeyboardEvent#isComposing` — the engine's composition flag. */
isComposing?: boolean
/** The legacy IME key code (229 while an engine hides its composition). */
keyCode?: number
}
/** The composer's textarea in a mounted view. */
function composerField(view: RenderedRoot): HTMLTextAreaElement {
const field = view.container.querySelector('textarea')
if (field === null) throw new Error('the composer textarea is missing')
return field
}
/** Dispatch one real Enter keydown at the composer. */
function pressEnter(field: HTMLTextAreaElement, init: PressInit = {}): KeyboardEvent {
const event = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
bubbles: true,
cancelable: true,
altKey: init.alt === true,
})
if (init.repeat !== undefined) Object.defineProperty(event, 'repeat', { value: init.repeat })
if (init.isComposing !== undefined) Object.defineProperty(event, 'isComposing', { value: init.isComposing })
if (init.keyCode !== undefined) Object.defineProperty(event, 'keyCode', { value: init.keyCode })
act(() => { field.dispatchEvent(event) })
return event
}
/** Mark the composer as being inside a live composition (the IME's own event). */
function startComposition(field: HTMLTextAreaElement): void {
act(() => { field.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true })) })
}
let harness: SidechatHarness
beforeEach(() => {
harness = installSidechatHarness({
events: [
{ type: 'session/end-seed', seq: 0, time: 0, data: {} },
{ type: 'user/message', seq: 1, time: 1_000, data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } } },
],
})
})
afterEach(() => {
harness.cleanup()
})
/** Mount with a draft in the composer. */
async function mountWithDraft(draft = 'nihao'): Promise<{ view: RenderedRoot, field: HTMLTextAreaElement }> {
const view = harness.mount()
await harness.flush()
harness.typeComposer(draft)
await harness.flush(2)
return { view, field: composerField(view) }
}
describe('a live IME composition outranks the composer guards', () => {
it('leaves an Alt+Enter inside a composition to the input method (no submit, no consume)', async () => {
const cases: Array<[string, PressInit, boolean]> = [
['isComposing + Alt', { isComposing: true, alt: true }, false],
['keyCode 229 + Alt', { keyCode: 229, alt: true }, false],
['live composition (compositionstart) + Alt', { alt: true }, true],
]
for (const [label, init, viaCompositionStart] of cases) {
const { view, field } = await mountWithDraft()
try {
if (viaCompositionStart) startComposition(field)
const before = harness.promptCalls.length
const event = pressEnter(field, init)
await harness.flush(2)
expect.soft(harness.promptCalls.length, `${label}: a composition key never submits`).toBe(before)
expect.soft(
event.defaultPrevented,
`${label}: the IME owns the gesture — consuming it would fight the candidate picker`,
).toBe(false)
expect.soft(harness.composerValue(), `${label}: the draft survives`).toBe('nihao')
} finally {
view.unmount()
}
}
})
it('leaves a repeated Enter inside a composition to the input method as well', async () => {
const cases: Array<[string, PressInit, boolean]> = [
['isComposing + repeat', { isComposing: true, repeat: true }, false],
['keyCode 229 + repeat', { keyCode: 229, repeat: true }, false],
['live composition (compositionstart) + repeat', { repeat: true }, true],
]
for (const [label, init, viaCompositionStart] of cases) {
const { view, field } = await mountWithDraft()
try {
if (viaCompositionStart) startComposition(field)
const before = harness.promptCalls.length
const event = pressEnter(field, init)
await harness.flush(2)
expect.soft(harness.promptCalls.length, `${label}: a composition key never submits`).toBe(before)
expect.soft(
event.defaultPrevented,
`${label}: a repeated key inside a composition belongs to the IME`,
).toBe(false)
expect.soft(harness.composerValue(), `${label}: the draft survives`).toBe('nihao')
} finally {
view.unmount()
}
}
})
it('still consumes the same keydowns when no composition is live', async () => {
const cases: Array<[string, PressInit]> = [
['Alt+Enter', { alt: true }],
['repeated Enter', { repeat: true }],
]
for (const [label, init] of cases) {
const { view, field } = await mountWithDraft()
try {
const before = harness.promptCalls.length
const event = pressEnter(field, init)
await harness.flush(2)
expect.soft(harness.promptCalls.length, `${label}: never submits`).toBe(before)
expect.soft(event.defaultPrevented, `${label}: outside a composition the guard still consumes it`).toBe(true)
expect.soft(harness.composerValue(), `${label}: the draft survives`).toBe('nihao')
} finally {
view.unmount()
}
}
})
})