Files
apes-sidebar/tests/native-tab-unread.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

201 lines
7.3 KiB
TypeScript

// @vitest-environment jsdom
/**
* The unread dot on DSH's OWN tab chip (`NativeTabTitle`) — the second of the
* two surfaces the mark is drawn on, and the only one the plugin does not
* render a strip for: a chip's content IS the plugin's registration in the
* host's `sidebar.right.pane.tab.title` slot, so this component is the whole
* carrier.
*
* The two halves pinned here are the ones the bottom workbench cannot answer:
*
* - a marked tab type draws the dot on its chip (and an unmarked one does not);
* - the chip RETIRES the mark by itself. Clicking a chip is a host interaction
* this plugin never sees — `ISidebarRight` exposes a plain `active()` reader
* and no selection feed — so the host's own `tab.visible` flag is the only
* signal that the reader is looking at this page now. That is also why the
* clearing must not fire for an INACTIVE chip: the host draws the title of
* every expanded tab, so a `!visible` chip that cleared on render would
* erase the dot the moment it was raised.
*/
import { afterEach, describe, expect, it } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
import {
NativeTabTitle,
createNativeTabRecords,
type NativeTabInfo,
} from '../src/client/native/tab-adapter.tsx'
import { createBetterSidebarService, type BetterSidebarService } from '../src/client/service.ts'
import { createSidebarStore, type SidebarStore } from '../src/client/state.ts'
import { t } from '../src/client/locales.ts'
const SESSION = 's1'
/** One native tab record, as the host hands it to a title registration. */
function nativeInfo(visible: boolean): NativeTabInfo {
return {
tab: {
id: 't1',
kind: 'subagent',
title: 'Tasks',
contentId: 'sidebar://subagent',
visible,
navigation: { address: 'sidebar://subagent', params: undefined, revision: 0 },
signal: new AbortController().signal,
},
}
}
function createService(store: SidebarStore): BetterSidebarService {
const service = createBetterSidebarService(store)
service.registerTab({
id: 'subagent',
title: () => 'Tasks',
// The chip's glyph is the descriptor's, like every other plugin tab type.
icon: (size: number) => createElement('i', { 'data-type-icon': size }),
component: () => createElement('div'),
})
return service
}
/** The unread dots currently drawn in a chip's host element. */
function dots(host: HTMLElement): Element[] {
return [...host.querySelectorAll(`[aria-label="${t('tabUnread')}"]`)]
}
const mounted: Array<{ host: HTMLDivElement; root: Root }> = []
/**
* Render one chip and hand back its host. `info` is re-read on every render
* (the returned closure keeps whatever object the caller last stored), which
* is how a visibility change is delivered — exactly like the host's own
* `useTabInfo` re-delivering its snapshot.
*/
function renderChip(options: {
store: SidebarStore
service: BetterSidebarService
initialVisible: boolean
}): { host: HTMLDivElement; rerender: (visible: boolean) => void } {
const { store, service } = options
const records = createNativeTabRecords()
records.attachStore(store)
let info = nativeInfo(options.initialVisible)
const host = document.createElement('div')
document.body.append(host)
const root = createRoot(host)
const draw = (): void => {
act(() => {
root.render(createElement(NativeTabTitle, {
records,
service,
descriptorId: 'subagent',
sessionId: SESSION,
useTabInfo: () => info,
} as never))
})
}
draw()
mounted.push({ host, root })
return {
host,
rerender: (visible: boolean) => {
info = nativeInfo(visible)
draw()
},
}
}
function makeStore(): SidebarStore {
const store = createSidebarStore()
store.setSession(SESSION)
return store
}
afterEach(() => {
while (mounted.length > 0) {
const entry = mounted.pop()!
act(() => { entry.root.unmount() })
entry.host.remove()
}
document.body.innerHTML = ''
})
describe('native tab chip: the unread dot', () => {
it('draws the dot on a marked tab type and nothing on an unmarked one', () => {
const store = makeStore()
const service = createService(store)
const marked = renderChip({ store, service, initialVisible: false })
// The chip is drawn BEFORE the mark arrives — the host draws the titles of
// every expanded tab, and the page this one announces may not even be open
// yet.
expect(dots(marked.host)).toHaveLength(0)
act(() => { store.markUnread('subagent') })
const drawn = dots(marked.host)
expect(drawn).toHaveLength(1)
// The dot is labelled, not decorative: the glyph beside it is the part
// that is `aria-hidden`.
expect(drawn[0]!.getAttribute('title')).toBe(t('tabUnread'))
})
it('retires the mark once the host says this tab is the visible one', () => {
const store = makeStore()
const service = createService(store)
act(() => { store.markUnread('subagent') })
const chip = renderChip({ store, service, initialVisible: false })
// Still marked while the reader is elsewhere: an inactive chip is drawn by
// the host too, so it must not clear what it did not earn.
expect(dots(chip.host)).toHaveLength(1)
expect(store.getSnapshot().state!.unread).toEqual(['subagent'])
chip.rerender(true)
expect(store.getSnapshot().state!.unread).toEqual([])
expect(dots(chip.host)).toHaveLength(0)
})
it('a chip that is never visible never clears the mark', () => {
const store = makeStore()
const service = createService(store)
act(() => { store.markUnread('subagent') })
const chip = renderChip({ store, service, initialVisible: false })
chip.rerender(false)
chip.rerender(false)
expect(store.getSnapshot().state!.unread).toEqual(['subagent'])
expect(dots(chip.host)).toHaveLength(1)
})
it('an unread type of ANOTHER tab does not mark this chip', () => {
// The mark is per tab TYPE: the chip must read its own, not "any mark".
const store = makeStore()
const service = createService(store)
act(() => { store.markUnread('editor') })
const chip = renderChip({ store, service, initialVisible: false })
expect(dots(chip.host)).toHaveLength(0)
})
it('adds the dot WITHOUT touching the glyph or the title', () => {
// A guard on the shape this change could have broken: the glyph stays
// decorative (its wrapper is `aria-hidden`, so the chip's accessible name
// is still the title) and the dot is a sibling added after the title
// rather than something that replaces either.
const store = makeStore()
const service = createService(store)
act(() => { store.markUnread('subagent') })
const chip = renderChip({ store, service, initialVisible: false })
const glyphIcon = chip.host.querySelector('[data-type-icon]')
expect(glyphIcon).not.toBeNull()
expect(glyphIcon!.closest('[aria-hidden="true"]')).not.toBeNull()
expect(chip.host.textContent).toBe('Tasks')
const drawn = dots(chip.host)
expect(drawn).toHaveLength(1)
// The dot is empty by design: its meaning is the label, and an empty
// `role="img"` without one would be announced as nothing at all.
expect(drawn[0]!.textContent).toBe('')
})
})