|
| 1 | +// @vitest-environment jsdom |
| 2 | +/** |
| 3 | + * FeedFragmentList.test.tsx — 收件箱状态机组件测试(v0.12.2 验收标准 2)。 |
| 4 | + * |
| 5 | + * @ai-context: 状态机覆盖 捕获→升笔记→移除(升卡幂等语义 + 删除二次确认 + |
| 6 | + * 空态引导)——invoke 全 mock(不触碰真实后端),断言命令参数 |
| 7 | + * 契约(fragmentId/title/groupId)与回调时序。 |
| 8 | + */ |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 10 | +import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; |
| 11 | +import type { Fragment, Note, NoteGroup } from "../types"; |
| 12 | + |
| 13 | +const { invokeMock } = vi.hoisted(() => ({ invokeMock: vi.fn() })); |
| 14 | +vi.mock("@tauri-apps/api/core", () => ({ |
| 15 | + invoke: invokeMock, |
| 16 | + convertFileSrc: (p: string) => `asset://${p}`, |
| 17 | +})); |
| 18 | + |
| 19 | +import FeedFragmentList from "./FeedFragmentList"; |
| 20 | + |
| 21 | +/** 最小 Fragment 桩 */ |
| 22 | +function frag(id: number, text: string, groupId: number | null = null): Fragment { |
| 23 | + return { |
| 24 | + id, text, imagePath: null, domainTag: null, groupId, |
| 25 | + source: "manual", status: "active", createdAt: 1000, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +const containerGroup: NoteGroup = { |
| 30 | + id: 9, name: "化妆美妆", terrain: "container", kind: "topic", domainTag: "beauty", |
| 31 | + source: "route", seriesKey: null, routeReason: null, routeOverridden: 0, |
| 32 | + noteCount: 0, createdAt: 0, updatedAt: 0, |
| 33 | +}; |
| 34 | + |
| 35 | +const noteStub: Note = { |
| 36 | + id: 42, title: "晕染笔记", content: "眼影要晕染。第二步定妆。", source: "manual", |
| 37 | + tags: "[]", pin: 0, group_id: 9, created_at: 2000, updated_at: 2000, |
| 38 | +}; |
| 39 | + |
| 40 | +let dbFragments: Fragment[]; |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + dbFragments = [frag(1, "眼影要晕染。第二步定妆。", 9), frag(2, "单句灵感", null)]; |
| 44 | + invokeMock.mockReset(); |
| 45 | + invokeMock.mockImplementation(async (cmd: string, args: Record<string, unknown> = {}) => { |
| 46 | + switch (cmd) { |
| 47 | + case "list_fragments": |
| 48 | + return dbFragments; |
| 49 | + case "list_note_groups": |
| 50 | + return [containerGroup]; |
| 51 | + case "promote_fragment_to_note": |
| 52 | + dbFragments = dbFragments.filter((f) => f.id !== args.fragmentId); |
| 53 | + return noteStub; |
| 54 | + case "promote_fragment_to_card": |
| 55 | + return 1; |
| 56 | + case "delete_fragment": |
| 57 | + dbFragments = dbFragments.filter((f) => f.id !== args.fragmentId); |
| 58 | + return true; |
| 59 | + case "resolve_fragment_image": |
| 60 | + return null; |
| 61 | + default: |
| 62 | + throw new Error(`unexpected command: ${cmd}`); |
| 63 | + } |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +afterEach(() => cleanup()); |
| 68 | + |
| 69 | +describe("FeedFragmentList 收件箱状态机", () => { |
| 70 | + it("捕获展示:碎片卡渲染 + 空态引导三种归宿", async () => { |
| 71 | + const { unmount } = render(<FeedFragmentList onChanged={vi.fn()} onPromoted={vi.fn()} />); |
| 72 | + // Arrange/Act:两条 fragments |
| 73 | + expect(await screen.findByTestId("fragment-card-1")).toBeTruthy(); |
| 74 | + expect(screen.getByTestId("fragment-card-2")).toBeTruthy(); |
| 75 | + unmount(); |
| 76 | + // Arrange:空收件箱 |
| 77 | + dbFragments = []; |
| 78 | + render(<FeedFragmentList onChanged={vi.fn()} onPromoted={vi.fn()} />); |
| 79 | + // Assert:空态文案(三种归宿引导) |
| 80 | + expect((await screen.findByTestId("inbox-empty")).textContent).toContain("升为笔记沉淀它"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("升笔记:表单预填首句 → 确认 → 碎片移除 + 父层打开新笔记", async () => { |
| 84 | + const onPromoted = vi.fn(); |
| 85 | + const onChanged = vi.fn(); |
| 86 | + render(<FeedFragmentList onChanged={onChanged} onPromoted={onPromoted} />); |
| 87 | + await screen.findByTestId("fragment-card-1"); |
| 88 | + |
| 89 | + // 打开轻确认表单:标题预填首句(可改),归组默认未归组 |
| 90 | + fireEvent.click(screen.getByTestId("promote-note-1")); |
| 91 | + const titleInput = (await screen.findByTestId("promote-title")) as HTMLInputElement; |
| 92 | + expect(titleInput.value).toBe("眼影要晕染"); |
| 93 | + const groupSelect = screen.getByTestId("promote-group") as HTMLSelectElement; |
| 94 | + expect(groupSelect.value).toBe(""); |
| 95 | + |
| 96 | + // 改标题 + 选组 → 确认 |
| 97 | + fireEvent.change(titleInput, { target: { value: "晕染笔记" } }); |
| 98 | + fireEvent.change(groupSelect, { target: { value: "9" } }); |
| 99 | + fireEvent.click(screen.getByTestId("promote-confirm")); |
| 100 | + |
| 101 | + // 命令契约:事务建笔记+删碎片(fragmentId/title/groupId) |
| 102 | + await waitFor(() => { |
| 103 | + expect(invokeMock).toHaveBeenCalledWith("promote_fragment_to_note", { |
| 104 | + fragmentId: 1, title: "晕染笔记", groupId: 9, |
| 105 | + }); |
| 106 | + }); |
| 107 | + // 碎片从收件箱移除(列表已重载) |
| 108 | + await waitFor(() => { |
| 109 | + expect(screen.queryByTestId("fragment-card-1")).toBeNull(); |
| 110 | + }); |
| 111 | + // 回调:父层右侧自动打开新笔记 + 刷新侧栏计数 |
| 112 | + expect(onPromoted).toHaveBeenCalledWith(noteStub); |
| 113 | + expect(onChanged).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("升闪卡:成功后刷新;已升级(返回 0)不打扰——幂等可重复触发", async () => { |
| 117 | + const onChanged = vi.fn(); |
| 118 | + render(<FeedFragmentList onChanged={onChanged} onPromoted={vi.fn()} />); |
| 119 | + await screen.findByTestId("fragment-card-2"); |
| 120 | + |
| 121 | + // 首次:升卡成功(新卡 1 张)→ 刷新 |
| 122 | + fireEvent.click(screen.getByTestId("promote-card-2")); |
| 123 | + await waitFor(() => { |
| 124 | + expect(invokeMock).toHaveBeenCalledWith("promote_fragment_to_card", { fragmentId: 2 }); |
| 125 | + }); |
| 126 | + await waitFor(() => expect(onChanged).toHaveBeenCalled()); |
| 127 | + |
| 128 | + // 幂等:再触发(backend 返回 0——已升级过)→ 不报错不刷新 |
| 129 | + onChanged.mockClear(); |
| 130 | + invokeMock.mockImplementation(async (cmd: string) => { |
| 131 | + if (cmd === "promote_fragment_to_card") return 0; |
| 132 | + if (cmd === "list_fragments") return dbFragments; |
| 133 | + if (cmd === "list_note_groups") return [containerGroup]; |
| 134 | + if (cmd === "resolve_fragment_image") return null; |
| 135 | + throw new Error(`unexpected: ${cmd}`); |
| 136 | + }); |
| 137 | + fireEvent.click(screen.getByTestId("promote-card-2")); |
| 138 | + await new Promise((r) => setTimeout(r, 0)); |
| 139 | + expect(onChanged).not.toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it("删除:二次确认后移除(取消则保留)", async () => { |
| 143 | + const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(true); |
| 144 | + render(<FeedFragmentList onChanged={vi.fn()} onPromoted={vi.fn()} />); |
| 145 | + await screen.findByTestId("fragment-card-2"); |
| 146 | + |
| 147 | + const card2 = screen.getByTestId("fragment-card-2"); |
| 148 | + fireEvent.click(within(card2).getByText("🗑 删除")); |
| 149 | + await waitFor(() => { |
| 150 | + expect(invokeMock).toHaveBeenCalledWith("delete_fragment", { fragmentId: 2 }); |
| 151 | + }); |
| 152 | + await waitFor(() => expect(screen.queryByTestId("fragment-card-2")).toBeNull()); |
| 153 | + |
| 154 | + // 取消:确认框拒绝 → 不触发删除 |
| 155 | + confirmSpy.mockReturnValue(false); |
| 156 | + invokeMock.mockClear(); |
| 157 | + const card1 = screen.getByTestId("fragment-card-1"); |
| 158 | + fireEvent.click(within(card1).getByText("🗑 删除")); |
| 159 | + await new Promise((r) => setTimeout(r, 0)); |
| 160 | + expect(invokeMock).not.toHaveBeenCalledWith("delete_fragment", { fragmentId: 1 }); |
| 161 | + confirmSpy.mockRestore(); |
| 162 | + }); |
| 163 | +}); |
0 commit comments