|
| 1 | +// @vitest-environment node |
| 2 | +/** |
| 3 | + * useViewMemory.test.ts — 视图记忆的判据 M1–M5(C5 / T6)。 |
| 4 | + * |
| 5 | + * @ai-context 环境:**node 环境**(与全局 `environment:"node"` 一致;仍显式写指令,防全局配置漂移)。 |
| 6 | + * M1–M5 只依赖**注入的内存 `Storage` 桩**;hook 的渲染路径用 `react-dom/server` 的 |
| 7 | + * `renderToStaticMarkup` 观测(真 node 无 DOM,`@testing-library/react` 用不了 —— 先例 |
| 8 | + * `ui/primitives/usePresence.node.test.ts`)。SSR 跑完**渲染阶段**(含 `useState` 的惰性初始化, |
| 9 | + * 那正是读记忆的点)⇒「初始值」与「写后重挂载恢复」两条都在判据里。 |
| 10 | + * @ai-context **诚实边界**:SSR 的 `useState` setter 是 no-op ⇒「setter 触发的重渲染」不可观测; |
| 11 | + * 本文件用「写入已落盘 + **新挂载**读回同一值」覆盖同一语义(写入那半段 = `writeViewMemory`, |
| 12 | + * 与 hook 的 setter 调的是**同一个**函数)。 |
| 13 | + * 副作用:无(纯内存;绝不碰 `window.localStorage`)。 |
| 14 | + */ |
| 15 | +import { createElement } from "react"; |
| 16 | +import type { ReactElement } from "react"; |
| 17 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 18 | +import { describe, expect, it } from "vitest"; |
| 19 | +import type { ObjectType } from "./registry"; |
| 20 | +import { VIEW_MEMORY_PREFIX, readViewMemory, useViewMemory, viewMemoryKey, writeViewMemory } from "./useViewMemory"; |
| 21 | + |
| 22 | +const SESSION_KEYS = ["raw", "tritrack", "proof", "cardflow", "preview"] as const; |
| 23 | +const NOTE_KEYS = ["raw", "cardflow"] as const; |
| 24 | + |
| 25 | +/** 记录键集合的内存 `Storage` 桩(M5 要「全程键集合」这个观测面)。 */ |
| 26 | +function memoryStorage(seed: Record<string, string> = {}): { storage: Storage; keys: () => string[] } { |
| 27 | + const map = new Map<string, string>(Object.entries(seed)); |
| 28 | + const storage = { |
| 29 | + get length(): number { |
| 30 | + return map.size; |
| 31 | + }, |
| 32 | + clear: (): void => void map.clear(), |
| 33 | + getItem: (k: string): string | null => (map.has(k) ? String(map.get(k)) : null), |
| 34 | + key: (i: number): string | null => [...map.keys()][i] ?? null, |
| 35 | + removeItem: (k: string): void => void map.delete(k), |
| 36 | + setItem: (k: string, v: string): void => void map.set(k, v), |
| 37 | + } as unknown as Storage; |
| 38 | + return { storage, keys: () => [...map.keys()].sort() }; |
| 39 | +} |
| 40 | + |
| 41 | +/** 任何访问都抛的 `Storage` 桩(隐私模式 / 配额拒绝的形状)。 */ |
| 42 | +function throwingStorage(): Storage { |
| 43 | + const boom = (): never => { |
| 44 | + throw new Error("SecurityError: storage 被拒绝"); |
| 45 | + }; |
| 46 | + return { |
| 47 | + get length(): number { |
| 48 | + return boom(); |
| 49 | + }, |
| 50 | + clear: boom, |
| 51 | + getItem: boom, |
| 52 | + key: boom, |
| 53 | + removeItem: boom, |
| 54 | + setItem: boom, |
| 55 | + } as unknown as Storage; |
| 56 | +} |
| 57 | + |
| 58 | +interface ProbeProps { |
| 59 | + objectType: ObjectType; |
| 60 | + defaultKey: string; |
| 61 | + validKeys: readonly string[]; |
| 62 | + /** 省略 = 走 hook 的默认值(`globalThis.localStorage`)—— 真 node 下那是 `undefined`。 */ |
| 63 | + storage?: Storage; |
| 64 | +} |
| 65 | + |
| 66 | +/** 把 hook 的返回值摊到 DOM 属性上(`renderToStaticMarkup` 不允许外部捕获 ⇒ 属性即观测面)。 */ |
| 67 | +function Probe(props: ProbeProps): ReactElement { |
| 68 | + const [key, select] = useViewMemory(props.objectType, props.defaultKey, props.validKeys, props.storage); |
| 69 | + return createElement("i", { "data-key": key, "data-select": typeof select }); |
| 70 | +} |
| 71 | + |
| 72 | +/** 一次「挂载」(独立 hook 实例 ⇒ 再调一次 = **重挂载**)。 */ |
| 73 | +function mount(props: ProbeProps): string { |
| 74 | + return renderToStaticMarkup(createElement(Probe, props)); |
| 75 | +} |
| 76 | + |
| 77 | +/** 从渲染产物里读回当前视图键(读不到即判据失败,而不是静默返回空)。 */ |
| 78 | +function shownKey(props: ProbeProps): string { |
| 79 | + const html = mount(props); |
| 80 | + const m = /data-key="([^"]*)"/.exec(html); |
| 81 | + if (m === null || m[1] === undefined) throw new Error(`渲染产物里没有 data-key:${html}`); |
| 82 | + return m[1]; |
| 83 | +} |
| 84 | + |
| 85 | +const sessionProbe = (storage: Storage, defaultKey = "raw"): ProbeProps => ({ |
| 86 | + objectType: "session", |
| 87 | + defaultKey, |
| 88 | + validKeys: SESSION_KEYS, |
| 89 | + storage, |
| 90 | +}); |
| 91 | +const noteProbe = (storage: Storage, defaultKey = "raw"): ProbeProps => ({ |
| 92 | + objectType: "note", |
| 93 | + defaultKey, |
| 94 | + validKeys: NOTE_KEYS, |
| 95 | + storage, |
| 96 | +}); |
| 97 | + |
| 98 | +describe("M1 初始值取记忆(有则取之、无则默认);写后重挂载恢复同一值", () => { |
| 99 | + it("无记忆 ⇒ 默认视图;第二个返回值是函数(选择器在位)", () => { |
| 100 | + const s = memoryStorage(); |
| 101 | + expect(shownKey(sessionProbe(s.storage))).toBe("raw"); |
| 102 | + expect(mount(sessionProbe(s.storage))).toContain('data-select="function"'); |
| 103 | + }); |
| 104 | + |
| 105 | + it("有合法记忆 ⇒ 取记忆值(不是默认值)", () => { |
| 106 | + const s = memoryStorage({ "view:default:session": "proof" }); |
| 107 | + expect(readViewMemory("session", SESSION_KEYS, s.storage)).toBe("proof"); |
| 108 | + expect(shownKey(sessionProbe(s.storage))).toBe("proof"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("默认值由调用方给(不是硬编码 raw)", () => { |
| 112 | + expect(shownKey(sessionProbe(memoryStorage().storage, "cardflow"))).toBe("cardflow"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("写入后**重挂载**读回同一值(记忆真的落到了 storage 上)", () => { |
| 116 | + const s = memoryStorage(); |
| 117 | + expect(shownKey(sessionProbe(s.storage))).toBe("raw"); |
| 118 | + // 等于 hook 的 setter 调用的那半段(见文件头「诚实边界」) |
| 119 | + writeViewMemory("session", "tritrack", s.storage); |
| 120 | + expect(shownKey(sessionProbe(s.storage))).toBe("tritrack"); |
| 121 | + expect(s.storage.getItem("view:default:session")).toBe("tritrack"); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe("M2 两类互不串(键带 objectType)", () => { |
| 126 | + it("写 session 不影响 note,反之亦然;两个键各自独立落盘", () => { |
| 127 | + const s = memoryStorage(); |
| 128 | + writeViewMemory("session", "proof", s.storage); |
| 129 | + expect(shownKey(sessionProbe(s.storage))).toBe("proof"); |
| 130 | + expect(shownKey(noteProbe(s.storage))).toBe("raw"); |
| 131 | + |
| 132 | + writeViewMemory("note", "cardflow", s.storage); |
| 133 | + expect(shownKey(sessionProbe(s.storage))).toBe("proof"); |
| 134 | + expect(shownKey(noteProbe(s.storage))).toBe("cardflow"); |
| 135 | + expect(s.keys()).toEqual(["view:default:note", "view:default:session"]); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe("M3 Storage 抛异常 ⇒ 回退默认、不崩、写也不抛", () => { |
| 140 | + it("读抛 ⇒ null;hook 挂载不抛且给出默认值", () => { |
| 141 | + const s = throwingStorage(); |
| 142 | + expect(readViewMemory("session", SESSION_KEYS, s)).toBeNull(); |
| 143 | + expect(() => shownKey(sessionProbe(s.storage))).not.toThrow(); |
| 144 | + expect(shownKey(sessionProbe(s.storage))).toBe("raw"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("写抛 ⇒ 静默(不抛错)", () => { |
| 148 | + expect(() => writeViewMemory("session", "proof", throwingStorage())).not.toThrow(); |
| 149 | + expect(() => mount(noteProbe(throwingStorage()))).not.toThrow(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("真 node 环境没有 localStorage:**省略注入**也不崩(默认值降级,不是 ReferenceError)", () => { |
| 153 | + expect(typeof window).toBe("undefined"); |
| 154 | + expect(typeof globalThis.localStorage).toBe("undefined"); |
| 155 | + expect(shownKey({ objectType: "session", defaultKey: "raw", validKeys: SESSION_KEYS })).toBe("raw"); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +describe("M4 存值不在 validKeys(被手改成垃圾)⇒ 回退默认", () => { |
| 160 | + it("未知视图键 / 空串 / 多余空白 都不算记忆", () => { |
| 161 | + const cases: Array<[string, string]> = [ |
| 162 | + ["view:default:session", "nope"], |
| 163 | + ["view:default:session", ""], |
| 164 | + ["view:default:session", "proof "], |
| 165 | + ]; |
| 166 | + for (const [k, v] of cases) { |
| 167 | + const s = memoryStorage({ [k]: v }); |
| 168 | + expect(readViewMemory("session", SESSION_KEYS, s.storage), `值 ${JSON.stringify(v)} 应视为无记忆`).toBeNull(); |
| 169 | + expect(shownKey(sessionProbe(s.storage))).toBe("raw"); |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + it("反向对照:合法值**不**被误判为垃圾(否则 M4 会退化成恒返回默认)", () => { |
| 174 | + for (const key of SESSION_KEYS) { |
| 175 | + const s = memoryStorage({ "view:default:session": key }); |
| 176 | + expect(readViewMemory("session", SESSION_KEYS, s.storage)).toBe(key); |
| 177 | + expect(shownKey(sessionProbe(s.storage))).toBe(key); |
| 178 | + } |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe("M5 不写 layout: 前缀的任何键(键集合恰为记忆键)", () => { |
| 183 | + it("键形状逐字:view:default:{objectType}", () => { |
| 184 | + expect(VIEW_MEMORY_PREFIX).toBe("view:default:"); |
| 185 | + expect(viewMemoryKey("session")).toBe("view:default:session"); |
| 186 | + expect(viewMemoryKey("note")).toBe("view:default:note"); |
| 187 | + }); |
| 188 | + |
| 189 | + it("一次完整往返(读 + 挂载 + 写)后 storage 的键集合恰为该类型的记忆键,0 个 layout: 键", () => { |
| 190 | + const s = memoryStorage(); |
| 191 | + readViewMemory("session", SESSION_KEYS, s.storage); |
| 192 | + mount(sessionProbe(s.storage)); |
| 193 | + writeViewMemory("session", "cardflow", s.storage); |
| 194 | + expect(s.keys()).toEqual(["view:default:session"]); |
| 195 | + expect(s.keys().filter((k) => k.startsWith("layout:"))).toEqual([]); |
| 196 | + // 与 `shell/columnKeys.freeze.test.ts` 的冻结前缀正面互斥(复用它就会撞红那条判据) |
| 197 | + expect(s.keys().filter((k) => k.startsWith("layout:col-width:") || k.startsWith("layout:col-fold:"))).toEqual([]); |
| 198 | + }); |
| 199 | + |
| 200 | + it("`objectType` 不含 kind:任何 kind 都归一到同一个键(代价已登记)", () => { |
| 201 | + const s = memoryStorage(); |
| 202 | + writeViewMemory("session", "proof", s.storage); |
| 203 | + expect(s.keys()).toEqual(["view:default:session"]); |
| 204 | + expect(viewMemoryKey("session")).not.toContain("kind"); |
| 205 | + }); |
| 206 | +}); |
0 commit comments