Files
apes-sidebar/tests/tab-bar-right-actions.spec.tsx
T
Menghuan1918 de1e76e2f5 feat(sidebar): 后台激活的页面不再顶掉读者,改为标记未读红点
维护者在 #817 上批准了「后台自动激活不接管右侧栏」的行为,并要求同批补上
「后台新开了页面」的提示:事件不能丢,但也不能把读者正在看的页面顶走。

行为:
- 被门控跳过的那次激活仍然创建任务管理页,只是不再抢焦点
  (`OpenTabSeed.reveal: false` → 宿主 `revealIfOpened: false`,即
  「放置但不聚焦」;页面类型本来就没有自己的聚焦路径,已经存在的页面
  再开一次也只是一次聚焦,正是门控要拒绝的那件事)。
- 该页在原生右栏芯片与插件底部工作台对应页签上显示一个静态小红点
  (`aria-label` = 新词条 `tabUnread`,颜色取既有 `--dsw-alias-danger`)。
- 读者切到该页即清除标记:底部工作台走 service.activateTab;原生栏的
  点击插件看不见(`ISidebarRight` 只有 `active()` 读取器,没有选中事件流),
  所以由宿主自己的芯片按 `tab.visible` 清除——非可见芯片不清除,
  否则宿主为每个展开页签都画标题,红点刚亮就会被抹掉。
- 标记存进会话状态的 `unread: TabType[]`(随布局持久化,跨挂载/跨会话/
  刷新都在),两个承载面读同一份,不会各自为政。

文案:20 份词典同批更新两条设置项描述(门控行为变了)+ 新增 `tabUnread`
词条(ja 真日文)。

测试:`tests/sidebar-auto-activation.spec.tsx` 的门控两条改为断言
「页面仍创建 + revealIfOpened:false + 标记出现」,并加红点生命周期与
「已在任务页/正常激活不标红」三条;新增 `tests/native-tab-unread.spec.tsx`
钉住原生芯片这一承载面(画点、按 visible 清除、非可见不清除、别的类型
不串味、不破坏可访问名)。判别性用 6 处镜像还原逐条证伪(见报告)。
2026-10-05 23:03:40 +08:00

227 lines
7.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* TabBar / Workbench right-actions tests (TabDescriptor.rightActions, the
* 'rightActions' capability):
*
* - The resolver's node renders inside the strip's right-actions area, which
* is the LAST child of the tab bar (after the + button; the panel's close
* control is reserved by the strip's padding, not a DOM sibling).
* - The resolver receives the ACTIVE tab and the strip's paneId — with
* `single: false` and split panes, several instances of one type are open
* at once and every pane renders its own strip, so the toolbar must be
* able to target the right instance.
* - A resolver that returns null/undefined (or an absent resolver) must not
* mount the wrapper AT ALL: the wrapper is a flex item, and an empty one
* would still change every existing tab strip's layout.
* - The split tree forwards the resolver through NESTED splits: a split
* inside a split must still render each pane's own actions (regression:
* the recursive NodeView call once dropped getTabRightActions, so any
* pane below the first split level silently lost its toolbar).
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
// The act() environment flag (React 18.2 reads it before flushing effects).
import { setupReactAct } from './test-utils.ts'
setupReactAct()
import { TabBar } from '../src/client/TabBar.tsx'
import { Workbench, type WorkbenchActions } from '../src/client/split-pane.tsx'
import type { SidebarState, SidebarTab, SplitNode } from '../src/client/state.ts'
/** The strip's right-actions wrappers (empty when nothing renders). */
function rightActionsEls(root: ParentNode): HTMLElement[] {
return [...root.querySelectorAll<HTMLElement>('[class*="tabBarRightActions"]')]
}
function twoTabs(): SidebarTab[] {
return [
{ id: 't1', type: 'demo:tool', title: 'Tool 1' },
{ id: 't2', type: 'demo:tool', title: 'Tool 2' },
]
}
function mountBar(opts: {
tabs: SidebarTab[]
active: string | null
getTabRightActions?: (tab: SidebarTab, paneId: string) => React.ReactNode
}): { container: HTMLDivElement; rerender: (active: string | null) => void; unmount: () => void } {
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
const render = (active: string | null): void => {
act(() => {
root.render(createElement(TabBar, {
paneId: 'pane:1',
tabs: opts.tabs,
active,
onActivate: () => {},
onClose: () => {},
onNewTab: () => {},
newTabOptions: [],
onDropTab: () => {},
...(opts.getTabRightActions !== undefined ? { getTabRightActions: opts.getTabRightActions } : {}),
}))
})
}
render(opts.active)
return {
container,
rerender: render,
unmount: () => {
act(() => { root.unmount() })
container.remove()
},
}
}
afterEach(() => {
document.body.innerHTML = ''
vi.restoreAllMocks()
})
describe('TabBar right-actions area', () => {
it('renders the resolved node as the tab bar’s last child and passes the active tab + paneId', () => {
const resolver = vi.fn((tab: SidebarTab, _paneId: string) =>
createElement('button', { type: 'button', 'data-testid': 'restart' }, `restart ${tab.id}`))
const { container, unmount } = mountBar({ tabs: twoTabs(), active: 't2', getTabRightActions: resolver })
try {
// The resolver saw the ACTIVE tab and this strip's pane id.
expect(resolver).toHaveBeenCalledWith(expect.objectContaining({ id: 't2' }), 'pane:1')
const areas = rightActionsEls(container)
expect(areas).toHaveLength(1)
expect(areas[0]!.textContent).toBe('restart t2')
// The area sits at the strip's right end: the last child of the bar.
const bar = container.querySelector<HTMLElement>('[class*="tabBar"]')!
expect(bar.lastElementChild).toBe(areas[0])
} finally {
unmount()
}
})
it('follows the active tab across re-renders', () => {
const resolver = vi.fn((tab: SidebarTab) => createElement('span', null, `actions:${tab.id}`))
const { container, rerender, unmount } = mountBar({ tabs: twoTabs(), active: 't1', getTabRightActions: resolver })
try {
expect(rightActionsEls(container)[0]!.textContent).toBe('actions:t1')
rerender('t2')
expect(rightActionsEls(container)[0]!.textContent).toBe('actions:t2')
// One call per render, always for the then-active tab.
expect(resolver.mock.calls.map(call => call[0].id)).toEqual(['t1', 't2'])
} finally {
unmount()
}
})
it('mounts no wrapper when the resolver returns null/undefined', () => {
const { container, unmount } = mountBar({
tabs: twoTabs(),
active: 't1',
getTabRightActions: () => null,
})
try {
expect(rightActionsEls(container)).toHaveLength(0)
} finally {
unmount()
}
const undef = mountBar({ tabs: twoTabs(), active: 't1', getTabRightActions: () => undefined })
try {
expect(rightActionsEls(undef.container)).toHaveLength(0)
} finally {
undef.unmount()
}
})
it('mounts nothing when no resolver is supplied (every pre-rightActions strip)', () => {
const { container, unmount } = mountBar({ tabs: twoTabs(), active: 't1' })
try {
expect(rightActionsEls(container)).toHaveLength(0)
} finally {
unmount()
}
})
})
/** No-op workbench actions: these tests never interact with the panes. */
function stubActions(): WorkbenchActions {
return {
closeTab: () => {},
activateTab: () => {},
focusPane: () => {},
moveTabToEdge: () => {},
moveTabBefore: () => {},
resizeSplit: () => {},
}
}
/** A split-inside-a-split tree: p1 | (p2 / p3) — the nested split is the
* regression path (the recursive NodeView call once dropped the resolver). */
function nestedSplitTree(): SplitNode {
return {
kind: 'split',
id: 's1',
dir: 'row',
sizes: [0.5, 0.5],
children: [
{ kind: 'leaf', id: 'p1', tabs: [{ id: 'a1', type: 'demo:tool', title: 'A' }], active: 'a1' },
{
kind: 'split',
id: 's2',
dir: 'col',
sizes: [0.5, 0.5],
children: [
{ kind: 'leaf', id: 'p2', tabs: [{ id: 'b1', type: 'demo:tool', title: 'B' }], active: 'b1' },
{ kind: 'leaf', id: 'p3', tabs: [{ id: 'c1', type: 'demo:tool', title: 'C' }], active: 'c1' },
],
},
],
}
}
describe('Workbench right-actions forwarding', () => {
it('renders each pane’s own actions with its own active tab and paneId, through nested splits', () => {
const state: SidebarState = {
activePane: 'p1',
nextBrowser: 1,
expanded: [],
revealed: [],
unread: [],
bottomOpen: true,
bottomHeight: 220,
bottomSplits: nestedSplitTree(),
}
const resolver = vi.fn((tab: SidebarTab, paneId: string) =>
createElement('span', { 'data-pane': paneId }, `${paneId}:${tab.id}`))
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
act(() => {
root.render(createElement(Workbench, {
state,
newTabOptions: [],
actions: stubActions(),
onNewTab: () => {},
renderTab: (tab: SidebarTab) => createElement('div', null, tab.title),
getTabRightActions: resolver,
}))
})
try {
// One strip per pane — p2/p3 sit BELOW a nested split, so this fails
// if the recursive NodeView call drops the resolver.
const areas = rightActionsEls(container)
expect(areas.map(area => area.textContent)).toEqual(['p1:a1', 'p2:b1', 'p3:c1'])
expect(resolver.mock.calls.map(([tab, paneId]) => [paneId, tab.id])).toEqual([
['p1', 'a1'],
['p2', 'b1'],
['p3', 'c1'],
])
} finally {
act(() => { root.unmount() })
container.remove()
}
})
})