| 1 | // Run: tsx src/__tests__/workspace-reference-reader.test.tsx |
| 2 | // |
| 3 | // Selecting an answer reference must re-read the file through the reference |
| 4 | // reader even when that path is already open, because only the reference read |
| 5 | // re-resolves the path on the host. A ref-backed reader flag would leave the |
| 6 | // stale workspace read on screen. |
| 7 | import { act } from "react"; |
| 8 | import { flushPromises, renderFilesWorkspace, waitFor } from "./workspace-panel-test-harness"; |
| 9 | import { fileNavigationOwner } from "../lib/fileNavigationCommands"; |
| 10 | |
| 11 | const reads: string[] = []; |
| 12 | const preview = (path: string, body: string) => { |
| 13 | reads.push(body); |
| 14 | return { path, body, size: body.length, truncated: false, binary: false }; |
| 15 | }; |
| 16 | |
| 17 | const { dom, root, dockTabId } = await renderFilesWorkspace({ |
| 18 | ReadFileForTab: async (_tabId, path) => preview(path, "WORKSPACE READ"), |
| 19 | ReadReferenceFileForTab: async (_tabId, path) => preview(path, "REFERENCE READ"), |
| 20 | ReadReferenceFileSourceForTab: async (_tabId, path) => preview(path, "REFERENCE SOURCE"), |
| 21 | ResolveReferencePathForTab: async (_tabId, path) => `/repo/${path}`, |
| 22 | }); |
| 23 | |
| 24 | const scope = { sessionTabId: "tab-a", dockTabId }; |
| 25 | await act(async () => { |
| 26 | await fileNavigationOwner().openIn(scope, { |
| 27 | ref: { source: "workspace", hostId: "local", tabId: "tab-a", path: "docs/note.md" }, |
| 28 | params: { action: "preview", view: "files" }, |
| 29 | }); |
| 30 | await flushPromises(); |
| 31 | }); |
| 32 | |
| 33 | await waitFor("workspace read", () => document.body.textContent?.includes("WORKSPACE READ") === true); |
| 34 | |
| 35 | // Same path, now owned by a verified answer reference. |
| 36 | await act(async () => { |
| 37 | await fileNavigationOwner().openIn(scope, { |
| 38 | ref: { source: "reference", hostId: "local", tabId: "tab-a", path: "docs/note.md" }, |
| 39 | params: { action: "preview", view: "files" }, |
| 40 | }); |
| 41 | await flushPromises(); |
| 42 | }); |
| 43 | await waitFor("reference read", () => document.body.textContent?.includes("REFERENCE READ") === true); |
| 44 | if (reads.filter(entry => entry === "REFERENCE READ").length !== 1) { |
| 45 | throw new Error(`the reference reader was not used exactly once: ${JSON.stringify(reads)}`); |
| 46 | } |
| 47 | |
| 48 | // The in-dock source toggle keeps using the reference reader. |
| 49 | const toggle = Array.from(document.querySelectorAll<HTMLButtonElement>("button.workspace-iconbtn")) |
| 50 | .find(button => /^(source|源码|原始碼)$/i.test(button.getAttribute("aria-label") ?? "")); |
| 51 | if (!toggle) throw new Error("a text reference should expose the source toggle"); |
| 52 | await act(async () => { |
| 53 | toggle.dispatchEvent(new window.MouseEvent("click", { bubbles: true })); |
| 54 | await flushPromises(); |
| 55 | }); |
| 56 | await waitFor("reference source", () => document.body.textContent?.includes("REFERENCE SOURCE") === true); |
| 57 | |
| 58 | await act(async () => root.unmount()); |
| 59 | dom.window.close(); |
| 60 | console.log("workspace reference reader passed"); |
| 61 |