Files
apes-sidebar/tests/desktop-window-strip.spec.tsx
T
Menghuan1918 a67779acdc fix(desktop): 顶部保留区优先读壳的 desktopWindow 契约(#864)
现象:dsh-desktop 的 compatibility / extended 模式下,壳已经把整个页面下移 36px(页面在独立
WebContentsView / 壳自有 root 里从 y=36 开始),却仍按契约在 URL 上盖
`dsh-desktop-titlebar-inset=36`;插件把这枚戳当成「页面还要再让位 36px」,于是面板顶部多一条空白。

修法:
① `desktop-env.ts`:mode 接受第三种取值 `extended`(与 compatibility 同几何的独立 36px
   frame),并新增 `probeDesktopWindow(ctx)` —— 结构化探测客户端服务 `ctx.desktopWindow`
   (镜像类型 `SidebarDesktopWindowService` 落在 `context-types.ts`,照 sidebar-file.ts 的
   probe 约定)。**不写进 `dsh.client.inject`**:普通浏览器没有该服务,注入它会让整个插件
   pending;探测按调用读(服务由壳自己的 client fiber 提供,可能后到),`get` 抛错按缺失处理。
② `computeTitleBarStrip`:取值链在 ⓪ `web` 强制 0 之后、① WCO 之前插入壳契约 —— 服务存在时
   以 `safeAreaInsets.top` 为权威(compatibility / extended 报 0 → 不再预留;advanced 报它画进
   内容里的 caption 行,darwin 与 win32 同为 32,以壳上报值为准)。URL 戳因此在有服务时被忽略;服务不存在时旧链路
   (WCO → URL → preset/custom)逐字不变。
③ `shell-presets.ts`:复核 `env.mode !== 'advanced'` —— extended 不套用 advanced 的 preset
   顶部值(guard 原样成立,补注释与用例把它钉住)。

文档:指南 §12 的壳信号与 strip 取值链、`docs/plans/2026-08-20-desktop-scheme-settings-design.md`
的实施偏差记录同步。

判别性(`git stash push -- src` 临时还原源码、保留测试后重跑):
- titlebar-strip.spec.ts:(a)/(d) 由 0 变 **36**、(b) 由 32 变 0 → 3 例红((c)「无服务 + 戳 36
  → 36」在旧代码上照旧绿,正是本次刻意不动的那段);
- desktop-window-strip.spec.tsx(挂真 Sidebar、断言页面实际状态):期望
  `{compat:false, px:''}` 实得 `{compat:true, px:'36px'}` —— issue 的可见症状本身;advanced 实得
  无 strip → 3 例红;
- desktop-env.spec.ts:extended 解析成 null、`probeDesktopWindow is not a function` → 4 例红
  (typecheck 同时报 12 条:链只收 5 参 / `'extended'` 不在联合里);
- 还原后 34/34 绿,src diff 与还原前逐字节一致(`git diff -- src | diff - /tmp/430-src.patch`)。

全量:174 files / 1977 passed | 13 skipped(新增 14 例),typecheck 0,eslint 0。

本改动**不**解决 #430(那是「preset / custom 档位不改变位置」的取值链优先级问题),两者不要互相关闭。
2026-10-05 21:59:14 +08:00

140 lines
5.2 KiB
TypeScript

/**
* The desktop shell's window contract must reach the DOM, not just the pure
* chain (issue #430).
*
* `computeTitleBarStrip` is covered field by field in titlebar-strip.spec.ts;
* what this file adds is the WIRING, which is where the regression actually
* lived: the shell places the whole page below its own 36px frame and
* publishes `safeAreaInsets.top = 0` through `ctx.desktopWindow`, yet still
* stamps the render URL with `dsh-desktop-titlebar-inset=36`. If the Sidebar
* never hands the service to the chain, that stamp wins and every panel gets
* a second 36px blank strip at the top (`body[data-dsh-title-bar-compat]` +
* `--dsh-title-bar-strip: 36px`).
*
* Rendered with the REAL Sidebar + real store/service against a minimal fake
* context (the repo's jsdom mount pattern), so the assertion is on the
* document state the CSS actually consumes.
*/
// @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'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
import { Sidebar } from '../src/client/Sidebar.tsx'
import { createSidebarStore } from '../src/client/state.ts'
import { createBetterSidebarService } from '../src/client/service.ts'
import { resetDesktopEnvForTests } from '../src/client/desktop-env.ts'
import type { Context, SidebarDesktopWindowService } from '../src/context-types.ts'
const shell = (mode: SidebarDesktopWindowService['mode'], top: number): SidebarDesktopWindowService => ({
mode,
platform: 'win32',
material: 'off',
safeAreaInsets: { top, right: 0, bottom: 0, left: 0 },
dragRegion: { height: top, leftInset: 0, rightInset: 0 },
})
/**
* Mount the real sidebar on a desktop-shell URL.
* @param search - the render URL's query (the shell's own stamps).
* @param desktopWindow - the service `ctx.get('desktopWindow')` answers, or
* undefined for a shell that does not publish one.
*/
function mount(search: string, desktopWindow: SidebarDesktopWindowService | undefined): { unmount: () => void } {
window.history.replaceState({}, '', `/${search}`)
resetDesktopEnvForTests()
// jsdom has no WebSocket; the panel's host feeds construct one on mount.
vi.stubGlobal('WebSocket', class { close(): void {} })
const store = createSidebarStore()
store.setSession('s1')
const service = createBetterSidebarService(store)
// useSyncExternalStore requires STABLE snapshots across calls (the real DSH
// services return stable objects) — a fresh object per call loops forever.
const locale = { active: 'en' }
const sessions = { byId: { s1: { id: 's1', cwd: '/ws' } } }
const ctx = {
locale: { subscribe: () => () => {}, getSnapshot: () => locale },
sessions: { list: { subscribe: () => () => {}, getSnapshot: () => sessions } },
get: (name: string) => name === 'betterSidebar' ? service : name === 'desktopWindow' ? desktopWindow : undefined,
}
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
act(() => { root.render(createElement(Sidebar, { ctx: ctx as unknown as Context, store })) })
return {
unmount: () => {
act(() => { root.unmount() })
container.remove()
},
}
}
/** The strip the plugin applied to the page, as the CSS reads it. */
function appliedStrip(): { compat: boolean; px: string } {
return {
compat: document.body.hasAttribute('data-dsh-title-bar-compat'),
px: document.documentElement.style.getPropertyValue('--dsh-title-bar-strip'),
}
}
afterEach(() => {
document.body.innerHTML = ''
document.body.removeAttribute('data-dsh-title-bar-compat')
document.documentElement.style.removeProperty('--dsh-title-bar-strip')
vi.unstubAllGlobals()
resetDesktopEnvForTests()
})
describe('the shell window contract drives the applied strip', () => {
it('compatibility + URL inset 36 + contract top 0 → no strip at all', () => {
const view = mount(
'?dsh-desktop-mode=compatibility&dsh-desktop-platform=win32&dsh-desktop-titlebar-inset=36',
shell('compatibility', 0),
)
try {
expect(appliedStrip()).toEqual({ compat: false, px: '' })
} finally {
view.unmount()
}
})
it('extended + URL inset 36 + contract top 0 → no strip at all', () => {
const view = mount(
'?dsh-desktop-mode=extended&dsh-desktop-platform=darwin&dsh-desktop-titlebar-inset=36',
shell('extended', 0),
)
try {
expect(appliedStrip()).toEqual({ compat: false, px: '' })
} finally {
view.unmount()
}
})
it('advanced + contract top 32 → the caption row is yielded', () => {
const view = mount(
'?dsh-desktop-mode=advanced&dsh-desktop-platform=win32',
shell('advanced', 32),
)
try {
expect(appliedStrip()).toEqual({ compat: true, px: '32px' })
} finally {
view.unmount()
}
})
it('no service (plain browser / older shell) → the URL stamp chain is unchanged', () => {
const view = mount(
'?dsh-desktop-mode=compatibility&dsh-desktop-platform=win32&dsh-desktop-titlebar-inset=36',
undefined,
)
try {
expect(appliedStrip()).toEqual({ compat: true, px: '36px' })
} finally {
view.unmount()
}
})
})