Files
apes-sidebar/tests/desktop-env.spec.ts
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

146 lines
6.0 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.
/**
* Desktop-shell detection tests: URL stamps (dsh-desktop-mode/platform)
* from the official Electron shell, the preload marker
* (__DSH_DESKTOP_FILE_PATH__), the `dsh-desktop-titlebar-inset` contract
* parameter, and the shell's own window service probe
* (`ctx.desktopWindow` — see titlebar-strip.spec.ts for how the chain
* consumes it). Geometry ADAPTATION is deliberately NOT here — the strip
* resolution chain lives in titlebar-strip.ts (standard WCO first, see
* wco.spec.ts), keeping this module a pure environment reporter.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import './browser-globals.ts'
import { parseDesktopEnv, probeDesktopWindow, resetDesktopEnvForTests } from '../src/client/desktop-env.ts'
import type { Context, SidebarDesktopWindowService } from '../src/context-types.ts'
function setSearch(search: string): void {
;(window.location as { search: string }).search = search
}
beforeEach(() => {
resetDesktopEnvForTests()
delete (window as unknown as Record<string, unknown>).__DSH_DESKTOP_FILE_PATH__
setSearch('/')
})
describe('parseDesktopEnv', () => {
it('reports a plain browser page as non-desktop with no inset', () => {
expect(parseDesktopEnv()).toEqual({ desktop: false, mode: null, platform: null, titlebarInset: 0 })
})
it('parses win32 advanced stamps (no overlay guess — geometry comes from WCO/preset)', () => {
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-platform=win32')
const env = parseDesktopEnv()
expect(env.desktop).toBe(true)
expect(env.mode).toBe('advanced')
expect(env.platform).toBe('win32')
expect(env.titlebarInset).toBe(0)
})
it('parses darwin advanced stamps', () => {
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-platform=darwin')
const env = parseDesktopEnv()
expect(env.desktop).toBe(true)
expect(env.platform).toBe('darwin')
expect(env.titlebarInset).toBe(0)
})
it('parses compatibility mode as desktop with the native frame (no adaptation)', () => {
setSearch('?dsh-desktop-mode=compatibility&dsh-desktop-platform=win32')
const env = parseDesktopEnv()
expect(env.desktop).toBe(true)
expect(env.mode).toBe('compatibility')
expect(env.titlebarInset).toBe(0)
})
it('parses extended mode — the shell-owned 36px frame with compatibility geometry', () => {
// Extended window (DSH Desktop 2.x): the shell hosts the upstream
// surfaces below its OWN frame, exactly like compatibility — the mode is
// not an unknown value, and must not be read as `advanced` (whose preset
// strip would reserve the caption row a second time).
setSearch('?dsh-desktop-mode=extended&dsh-desktop-platform=darwin&dsh-desktop-titlebar-inset=36')
const env = parseDesktopEnv()
expect(env.desktop).toBe(true)
expect(env.mode).toBe('extended')
expect(env.platform).toBe('darwin')
// The stamp is still REPORTED verbatim: it is the shell's own reserve
// (the strip chain ignores it only because the window service exists).
expect(env.titlebarInset).toBe(36)
})
it('detects the desktop preload marker even without URL stamps', () => {
;(window as unknown as Record<string, unknown>).__DSH_DESKTOP_FILE_PATH__ = { getPathForFile: () => '' }
const env = parseDesktopEnv()
expect(env.desktop).toBe(true)
expect(env.mode).toBeNull()
expect(env.titlebarInset).toBe(0)
})
it('ignores unknown mode values (exotic shells keep plain-browser semantics)', () => {
setSearch('?dsh-desktop-mode=weird&dsh-desktop-platform=win32')
expect(parseDesktopEnv().mode).toBeNull()
expect(parseDesktopEnv().desktop).toBe(false)
})
it('reads the documented titlebar-inset contract parameter (clamped 0–120)', () => {
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-platform=win32&dsh-desktop-titlebar-inset=36')
expect(parseDesktopEnv().titlebarInset).toBe(36)
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-titlebar-inset=200')
resetDesktopEnvForTests()
expect(parseDesktopEnv().titlebarInset).toBe(120)
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-titlebar-inset=-5')
resetDesktopEnvForTests()
expect(parseDesktopEnv().titlebarInset).toBe(0)
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-titlebar-inset=abc')
resetDesktopEnvForTests()
expect(parseDesktopEnv().titlebarInset).toBe(0)
})
it('memoizes across calls until the test hook resets', () => {
setSearch('?dsh-desktop-mode=advanced&dsh-desktop-platform=win32')
const first = parseDesktopEnv()
setSearch('/')
expect(parseDesktopEnv()).toBe(first)
resetDesktopEnvForTests()
expect(parseDesktopEnv().desktop).toBe(false)
})
})
describe('probeDesktopWindow', () => {
const service: SidebarDesktopWindowService = {
mode: 'extended',
platform: 'win32',
material: 'off',
safeAreaInsets: { top: 0, right: 0, bottom: 0, left: 0 },
dragRegion: { height: 0, leftInset: 0, rightInset: 0 },
}
/** A client context whose `get` answers one service name. */
const ctxWith = (name: string, value: unknown): Context => ({
get: (asked: string): unknown => asked === name ? value : undefined,
}) as unknown as Context
it('returns the shell service the client context publishes', () => {
expect(probeDesktopWindow(ctxWith('desktopWindow', service))).toBe(service)
})
it('returns undefined on a plain browser page (the service is never injected)', () => {
expect(probeDesktopWindow(ctxWith('desktopWindow', undefined))).toBeUndefined()
expect(probeDesktopWindow({} as Context)).toBeUndefined()
})
it('stays best-effort when the context getter throws', () => {
const ctx: Context = {
get: (): never => { throw new Error('cannot get property without inject') },
} as unknown as Context
const logged = vi.spyOn(console, 'error').mockImplementation(() => {})
try {
expect(probeDesktopWindow(ctx)).toBeUndefined()
// Assert inside the spy's lifetime: mockRestore also clears the calls.
expect(logged).toHaveBeenCalledTimes(1)
} finally {
logged.mockRestore()
}
})
})