|
| 1 | +// @vitest-environment jsdom |
| 2 | +/** |
| 3 | + * @ai-context 大纲列接线守卫(批 3 T8 / 规格 §6.2「大纲列 180/140/260 · 1280 · **接线** |
| 4 | + * 拖拽+记忆或删钩子(现为「假可调」)」+ 审计 J1-6 假可调 / J1-3 折叠死局)。 |
| 5 | + * |
| 6 | + * Why 用**真执行器 + 真注册表规格**(不是给组件塞假 props):本文件要证的正是「页面那句 |
| 7 | + * `useColumnLayout("notes-outline", columnSpec("notes-outline"))` 的结果真的走到了 DOM」—— |
| 8 | + * 用假 props 只能证明组件认这个 prop,证不了接线。故 Harness 逐字复刻页面形态。 |
| 9 | + * |
| 10 | + * 判据(每条都配了能红的变异体,见 task-8-report §变异体实测): |
| 11 | + * ① localStorage 里记的列宽 → 渲染宽度(旧实现写死 180 ⇒ 记忆无效 ⇒ 红); |
| 12 | + * ② 未记忆时 = 注册表默认 180(零视觉变化); |
| 13 | + * ③ 窄窗自动折叠下点窄条能展开(J1-3 死局:旧实现只翻 manualFolded ⇒ 红); |
| 14 | + * ④ 展开态的 ✕「收起大纲」仍能折叠(若把 onToggleOutline 一刀切成 expand() ⇒ 红); |
| 15 | + * ⑤ 拖拽手柄在大纲列上(条件之外,折叠态也在)。 |
| 16 | + * |
| 17 | + * 边界:jsdom 不排版——本文件只判 `style.width` 与元素有无,**不判像素**(像素归 T14 探针)。 |
| 18 | + */ |
| 19 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 20 | +import { cleanup, fireEvent, render, screen } from "@testing-library/react"; |
| 21 | +import { useRef } from "react"; |
| 22 | +import type { Note } from "../../types"; |
| 23 | +import type { NoteEditHandle } from "../NoteEditView"; |
| 24 | +import { useColumnLayout } from "../../hooks/useColumnLayout"; |
| 25 | +import { columnSpec } from "../../shell/columnRegistry"; |
| 26 | +import NoteReadingView from "../NoteReadingView"; |
| 27 | +import NotesReadingColumn from "./NotesReadingColumn"; |
| 28 | + |
| 29 | +const { invokeMock } = vi.hoisted(() => ({ invokeMock: vi.fn() })); |
| 30 | +vi.mock("@tauri-apps/api/core", () => ({ invoke: invokeMock })); |
| 31 | + |
| 32 | +const note: Note = { |
| 33 | + id: 1, |
| 34 | + title: "标题", |
| 35 | + content: "# 正文\n内容段落", |
| 36 | + source: "session", |
| 37 | + session_id: 42, |
| 38 | + rule_version: null, |
| 39 | + purify_stats: null, |
| 40 | + tags: "[]", |
| 41 | + properties: null, |
| 42 | + pin: 0, |
| 43 | + group_id: null, |
| 44 | + created_at: 1, |
| 45 | + updated_at: 2, |
| 46 | +}; |
| 47 | + |
| 48 | +const noop = vi.fn(); |
| 49 | + |
| 50 | +/** 页面形态的宿主:真 hook + 真注册表规格(= NotesPage.tsx 的那一行) */ |
| 51 | +function Harness() { |
| 52 | + const outlineCol = useColumnLayout("notes-outline", columnSpec("notes-outline")); |
| 53 | + const editorRef = useRef<NoteEditHandle | null>(null); |
| 54 | + return ( |
| 55 | + <NotesReadingColumn |
| 56 | + selected={note} |
| 57 | + editing={false} |
| 58 | + setEditing={noop} |
| 59 | + readerSearch={null} |
| 60 | + noteColors={{}} |
| 61 | + groups={[]} |
| 62 | + editorRef={editorRef} |
| 63 | + outlineCol={outlineCol} |
| 64 | + onChanged={noop} |
| 65 | + onError={noop} |
| 66 | + onOpenAi={noop} |
| 67 | + onOpenModelCard={noop} |
| 68 | + onSelectionAction={noop} |
| 69 | + onPinToggle={noop} |
| 70 | + onDelete={noop} |
| 71 | + onTaskToggle={noop} |
| 72 | + onTagClick={noop} |
| 73 | + onImageOpen={noop} |
| 74 | + onCleanNotice={noop} |
| 75 | + /> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +/** 大纲列渲染宽度(jsdom 不排版,读 inline style) */ |
| 80 | +const outlineWidth = () => (screen.getByTestId("outline-panel") as HTMLElement).style.width; |
| 81 | + |
| 82 | +beforeEach(() => { |
| 83 | + invokeMock.mockReset(); |
| 84 | + invokeMock.mockResolvedValue([]); |
| 85 | + window.localStorage.clear(); |
| 86 | + // 规格 §1 决策 17 默认窗宽 1280;大纲列阈值 = 1280 ⇒ `1280 < 1280` 为假 ⇒ 不自动折叠。 |
| 87 | + // 显式声明视口(jsdom 默认 1024 会让大纲列静默折叠——本仓既有先例 SelectionActionMenu.test.tsx) |
| 88 | + window.innerWidth = 1280; |
| 89 | +}); |
| 90 | + |
| 91 | +afterEach(() => { |
| 92 | + cleanup(); |
| 93 | + window.localStorage.clear(); |
| 94 | + window.innerWidth = 1024; |
| 95 | +}); |
| 96 | + |
| 97 | +describe("大纲列接线(批 3 T8)", () => { |
| 98 | + it("① 宽度来自执行器的记忆值(此前组件写死 180 ⇒ 记忆永不生效)", async () => { |
| 99 | + window.localStorage.setItem("layout:col-width:notes-outline", "240"); |
| 100 | + render(<Harness />); |
| 101 | + expect(await screen.findByTestId("outline-panel")).toBeTruthy(); |
| 102 | + expect(outlineWidth()).toBe("240px"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("② 未记忆时 = 注册表默认 180(规格默认值经 hook 走到 DOM)", async () => { |
| 106 | + render(<Harness />); |
| 107 | + expect(await screen.findByTestId("outline-panel")).toBeTruthy(); |
| 108 | + expect(outlineWidth()).toBe("180px"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("②b 组件自身缺省也来自注册表(不传 outlineWidth 时 = 180px)", async () => { |
| 112 | + // 期望值刻意写**字面** "180px"(不写成 `${columnSpec(...).default}px` —— |
| 113 | + // 那样等于拿注册表和它自己比,判据会退化成同义反复、永远绿) |
| 114 | + render( |
| 115 | + <NoteReadingView |
| 116 | + note={note} |
| 117 | + editing={false} |
| 118 | + onEdit={noop} onPinToggle={noop} onDelete={noop} onTagClick={noop} |
| 119 | + onOpenSession={noop} onTaskToggle={noop} onImageOpen={noop} |
| 120 | + />, |
| 121 | + ); |
| 122 | + expect(await screen.findByTestId("outline-panel")).toBeTruthy(); |
| 123 | + expect(outlineWidth()).toBe("180px"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("③ J1-3:窄窗自动折叠下点窄条可展开(旧实现只翻手动折叠 ⇒ 死局)", async () => { |
| 127 | + window.innerWidth = 1024; // < 1280 ⇒ 自动折叠 |
| 128 | + render(<Harness />); |
| 129 | + const bar = await screen.findByTestId("column-bar"); |
| 130 | + expect(screen.queryByTestId("outline-panel")).toBeNull(); |
| 131 | + fireEvent.click(bar); |
| 132 | + // expand() 同时清自动/手动折叠态 ⇒ 窄窗下也真的展开 |
| 133 | + expect(await screen.findByTestId("outline-panel")).toBeTruthy(); |
| 134 | + expect(screen.queryByTestId("column-bar")).toBeNull(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("④ 展开态的 ✕「收起大纲」仍折叠(收起语义不许被 expand() 取代)", async () => { |
| 138 | + render(<Harness />); |
| 139 | + expect(await screen.findByTestId("outline-panel")).toBeTruthy(); |
| 140 | + fireEvent.click(screen.getByTitle("收起大纲")); |
| 141 | + expect(await screen.findByTestId("column-bar")).toBeTruthy(); |
| 142 | + expect(screen.queryByTestId("outline-panel")).toBeNull(); |
| 143 | + }); |
| 144 | + |
| 145 | + it("⑤ 拖拽手柄接在大纲列上(与其它列同款:在折叠三元之外,折叠态也在)", async () => { |
| 146 | + render(<Harness />); |
| 147 | + expect(await screen.findByTestId("outline-panel")).toBeTruthy(); |
| 148 | + expect(screen.getByTestId("column-resizer")).toBeTruthy(); |
| 149 | + fireEvent.click(screen.getByTitle("收起大纲")); |
| 150 | + expect(await screen.findByTestId("column-bar")).toBeTruthy(); |
| 151 | + expect(screen.getByTestId("column-resizer")).toBeTruthy(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments