Files
ttstd-infinite-canvas/web/test/floating-dock-pointer-guard.test.tsx
ddCat 8b8ea29148 fix(runtime): 运行稳定性 - 修复技能种子、任务续租与回归检查
保留社区技能作者与时间戳修复、Agent 参数约束、资源删除 worker 生命周期、PostgreSQL/Redis 测试门禁及相关回归。

Original-Commit: 0d05ff88bbe6c875d01964041788023a662ff7e8
2026-09-21 09:48:43 +08:00

35 lines
2.0 KiB
TypeScript
Raw Permalink 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.
import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { renderToStaticMarkup } from "react-dom/server";
import { FloatingDock } from "../src/components/ui/aceternity/floating-dock";
// 指针能力探测是纯增强:`window` 存在不代表 `matchMedia` 存在(SSR 垫片、嵌入式 webview、
// 部分 DOM polyfill 下它是 undefined),而 FloatingDock 在**渲染期**就取
// `window.matchMedia("(pointer: coarse)")`,取不到会直接抛 TypeError 让整个 dock 渲染失败。
// 仓库里同一模式已有先例:canvas-workspace-tool-panel.tsx 用 `typeof window.matchMedia === "function"` 守卫。
describe("floating dock pointer capability probe", () => {
const source = readFileSync(new URL("../src/components/ui/aceternity/floating-dock.tsx", import.meta.url), "utf8");
test("probes matchMedia through a guarded helper", () => {
expect(source).toContain('typeof window.matchMedia !== "function"');
expect(source).toContain("function coarsePointerQuery()");
// 不允许再出现"直接取用 matchMedia 结果"的写法(初始化与 useEffect 都不得裸取)
expect(source).not.toMatch(/window\.matchMedia\("\(pointer: coarse\)"\)\.matches/);
expect(source).not.toMatch(/const media = window\.matchMedia/);
});
test("renders the real dock with a window shim lacking matchMedia", () => {
const original = Object.getOwnPropertyDescriptor(globalThis, "window");
try {
Object.defineProperty(globalThis, "window", { configurable: true, value: new EventTarget() });
const markup = renderToStaticMarkup(<FloatingDock items={[{ id: "probe", label: "操作", icon: <span>+</span> }]} />);
expect(markup).toContain("操作");
expect(markup).toContain("button");
} finally {
if (original) Object.defineProperty(globalThis, "window", original);
else Reflect.deleteProperty(globalThis, "window");
}
});
});