Files
apes-sidebar/tests/turn-tail-intercept.spec.ts
T
figmar 5a7e2139a0 chore(sync): re-base the fork onto upstream dsh-better-sidebar v0.19.1
The fork was pinned at upstream v0.18.0. That is the entire reason the window
ran TWO sidebars and TWO near-identical panel toggles: DSH 2.0.10 ships its own
right sidebar (@deepseek-ai/dsh-client-ui-sidebar-right), and upstream only
adopted it in v0.19.0-alpha.1 — which arrived after our fork point. v0.19.1 is
also the one upstream line whose support range actually matches our host
(engines.dsh >=0.1.5-rc.1, 14 DSH peers ^0.1.5-rc.1, host = 0.1.5-rc.2), so it
is the only tag the host's startup precheck accepts.

What upstream v0.18.0 → v0.19.1 brings
- src/client/native/{index,surface,tab-adapter}.tsx: every tab the plugin can
  draw becomes an OFFICIAL native tab type + body, and an external plugin's
  descriptor registered later gets its native type too. This is what retires
  the fork's own right panel — and with it the duplicate toggle cluster
  (`toggleCluster` is gone from v0.19.1).
- native tab-chip icons via the `sidebar.right.pane.tab.title` seat, coloured
  built-in tab icons, file/folder glyphs from DSH's own `FileTypeIcon`.
- guide descriptions (DSH renders them while the guide lists <= 4 entries),
  tab bodies that fill the panel, and the 0.1.5-rc.2 adaptation work we were
  carrying by hand (peer/devDep pinning, primitives' transitive devDeps).

How the sync was done
Rebuilt the tree from the v0.19.1 tag rather than resolving 53 conflicted
files: our v0.18.0 delta was measured first and is only (a) one mechanical
rename `dsh-better-sidebar` -> `dsh-apes-sidebar` and (b) ten real edits. So:
tag tree + rename (scoped: code/config/scripts/tests/README, while the 46
historical docs/plans records and switch.sh's upstream package name stay
untouched) + re-applied deltas + our apes module.

Fork deltas re-applied on the new base
- src/client/apes/ (icons / locales / tabs): the three apes:* information-layer
  tabs (agents / apps / profile), registered through the same BetterSidebarService
  as the builtins — which is exactly why they now ride upstream's native adapter
  into the official sidebar for free.
- identity: package.json (name/version 1.2.0/description/forkedFrom), dsh.plugin.json
  (id/version), SIDEBAR_SERVICE_VERSION 1.2.0, README fork notice,
  docs/external-plugin-guide.md.
- scripts/switch.sh + the pnpm switch* scripts (apes <-> upstream two-line switch).
- tests/apes-tabs.spec.ts (unescaped: it starts from the v0.19.1 base).

Dropped
- the Phase A native-registration spike (`apes/native-tabs.tsx` + its spec): it
  re-derived by hand exactly what upstream's native layer already does — the
  hand-written bridge is dead weight now, and v0.19.1's own e2e/jsdom lanes
  cover the same seat contract far better.
- the unused apesProfileGuideDesc key and the two package.json.bak.* backups.

Verified on the new base: typecheck clean; build clean; 1351 passed / 9 skipped
(128 files), up from 1255 on the old base.
2026-10-03 22:51:09 +08:00

201 lines
7.5 KiB
TypeScript

/**
* Turn-tail interception registration spec (issue #15): `registerTurnTailInterception`
* must go through `ctx.slots.inject` — the slot is a CHILD slot the host's
* ui-conversation declares in its `conversation.chat.node` children table, so a
* direct `slots.register` races the declaration and the ui-slots core throws
* "not declared (a parent entry's children table must declare it)".
*
* The fake `slots` mirrors SlotRegistry.inject's semantics: run the callback
* synchronously when the slot is already declared; otherwise wait and run it
* when the declaration commits; the returned disposer cancels a pending wait
* and disposes any active registration; the register disposer is idempotent.
*/
import { describe, expect, it, vi } from 'vitest'
import { createSidebarStore } from '../src/client/state.ts'
import { registerTurnTailInterception } from '../src/client/intercept.tsx'
import type { Context } from '../src/context-types.ts'
interface RegisteredSlot {
options: Record<string, unknown>
component: unknown
}
/**
* A structural fake of the client slots service. `declared` selects the
* timing: already-on-ledger (callback runs synchronously) vs. pending
* (callback runs on `declare()`, unless the controller was disposed first).
*/
const fakeSlots = (declared: boolean) => {
const registered: RegisteredSlot[] = []
const disposals: number[] = []
const pendings: Array<() => void> = []
return {
registered,
disposals,
pendings,
slots: {
register: (options: Record<string, unknown>, component: unknown) => {
registered.push({ options, component })
return () => { disposals.push(1) }
},
inject: (key: string, callback: () => () => void) => {
if (key !== 'conversation.chat.turnTail') {
throw new Error(`unexpected injected key "${key}"`)
}
let active: (() => void) | undefined
let stopped = false
const run = (): void => {
if (stopped || active !== undefined) return
active = callback()
}
if (declared) run()
else pendings.push(run)
return () => {
stopped = true
active?.()
active = undefined
}
},
},
}
}
/** A produced-files owner currency: one closing assistant seq + its nodes. */
const producedOwner = (paths: string[]): unknown => ({
nodes: [
{ kind: 'assistant', seq: 1, turn: 1 },
...paths.map(path => ({
kind: 'tool-result', isError: false, callView: { card: 'diff', locations: [{ path }] },
})),
{ kind: 'assistant', seq: 2, turn: 1 },
],
seq: 2,
})
const emptyOwner = (): unknown => ({ nodes: [{ kind: 'assistant', seq: 1, turn: 1 }], seq: 1 })
/** The minimal client-context fake the registration (and its seats) touch. */
const clientCtx = (slots: unknown): Context => {
const betterSidebar = { openTab: vi.fn() }
return {
slots,
sessions: {
list: { getSnapshot: () => ({ current: 's1', byId: { s1: { id: 's1', cwd: '/w', displayTitle: 's1' } } }) },
},
betterSidebar,
get: (name: string) => name === 'betterSidebar' ? betterSidebar : undefined,
} as unknown as Context
}
describe('turn-tail interception registration (issue #15)', () => {
it('registers through slots.inject and lands once the slot is already declared', () => {
const fake = fakeSlots(true)
const store = createSidebarStore()
const restore = registerTurnTailInterception(clientCtx(fake.slots), store)
// Exactly one registration, with the takeover descriptor.
expect(fake.registered).toHaveLength(1)
const { options, component } = fake.registered[0]!
expect(options.name).toBe('conversation.chat.turnTail')
expect(options.priority).toBe(-1)
expect(options.registrant).toBe('dsh-apes-sidebar')
expect(options.select).toBeTypeOf('function')
expect(options.inject).toBeTypeOf('function')
expect(component).toBeTypeOf('function')
// Disposal removes the registration; the disposer is idempotent.
restore()
expect(fake.disposals).toHaveLength(1)
restore()
expect(fake.disposals).toHaveLength(1)
})
it('waits for the host declaration instead of throwing (the pre-fix race)', () => {
const fake = fakeSlots(false)
const store = createSidebarStore()
const restore = registerTurnTailInterception(clientCtx(fake.slots), store)
// The slot is undeclared: nothing registered yet, no error.
expect(fake.registered).toHaveLength(0)
// The host's ui-conversation commits the declaration → the entry lands.
expect(fake.pendings).toHaveLength(1)
fake.pendings[0]!()
expect(fake.registered).toHaveLength(1)
// A later re-declaration does not double-register while the entry is live.
fake.pendings[0]!()
expect(fake.registered).toHaveLength(1)
restore()
expect(fake.disposals).toHaveLength(1)
})
it('a disposal before the declaration cancels the wait permanently', () => {
const fake = fakeSlots(false)
const store = createSidebarStore()
const restore = registerTurnTailInterception(clientCtx(fake.slots), store)
restore()
// The declaration arrives after the controller was disposed: ignored.
fake.pendings[0]!()
expect(fake.registered).toHaveLength(0)
})
it('declines the takeover while the editor tab is disabled in the settings', () => {
const fake = fakeSlots(true)
const store = createSidebarStore()
const restore = registerTurnTailInterception(clientCtx(fake.slots), store)
const select = fake.registered[0]!.options.select as (owner: unknown) => unknown
// Enabled (default): a produced turn claims the chain; an empty one declines.
expect(select(producedOwner(['a.ts', 'b.ts']))).toEqual(['a.ts', 'b.ts'])
expect(select(emptyOwner())).toBeNull()
// The engine Turn data path (the real owner currency: { turn, seq,
// openFile }) claims through the deliverables record too.
expect(select({
turn: { data: { get: (key: string) => key === 'deliverables' ? { produced: [{ seq: 1, path: 'a.ts' }] } : undefined } },
seq: 1,
})).toEqual(['a.ts'])
// Editor tab disabled: even a produced turn falls back to the default
// deliverables row (chips that cannot open must not be offered).
store.setPrefs({ ...store.getPrefs(), tabsEnabled: { editor: false } })
expect(select(producedOwner(['a.ts']))).toBeNull()
restore()
})
it('wires the openInSidebar and onShowInFolder seats', () => {
const fake = fakeSlots(true)
const ctx = clientCtx(fake.slots)
const store = createSidebarStore()
const restore = registerTurnTailInterception(ctx, store)
const inject = fake.registered[0]!.options.inject as (sessionId: string) => {
openInSidebar: (path: string) => void
onShowInFolder: (files: readonly string[]) => void
}
// The seat hands the session-scoped opener to the chips row.
const seat = inject('s1')
expect(seat.openInSidebar).toBeTypeOf('function')
seat.openInSidebar('/w/src/a.ts')
expect(ctx.betterSidebar.openTab).toHaveBeenCalledWith({
type: 'editor',
title: 'a.ts',
path: '/w/src/a.ts',
id: 'editor:/w/src/a.ts',
})
// The show-in-folder seat reveals the produced files in the files window
// (the editor home tab) — the panel expands and the rows highlight.
expect(seat.onShowInFolder).toBeTypeOf('function')
seat.onShowInFolder(['/w/src/a.ts'])
expect(ctx.betterSidebar.openTab).toHaveBeenLastCalledWith(expect.objectContaining({
type: 'editor',
}))
restore()
})
})