The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts"; |
| 3 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | |
| 5 | import { |
| 6 | attachThreadFindTimeline, |
| 7 | closeThreadFind, |
| 8 | findForMessageMatch, |
| 9 | openThreadFind, |
| 10 | openThreadFindOnQuery, |
| 11 | retainThreadFindForRoute, |
| 12 | setThreadFindQuery, |
| 13 | useThreadFindStore, |
| 14 | } from "./threadFindStore"; |
| 15 | |
| 16 | const threadRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1")); |
| 17 | const otherRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-2")); |
| 18 | const threadKey = "env-1:thread-1"; |
| 19 | |
| 20 | function setup() { |
| 21 | closeThreadFind(); |
| 22 | const navigate = vi.fn(); |
| 23 | const readPosition = vi.fn(() => ({ entryId: "entry-9", occurrence: 1 })); |
| 24 | return { navigate, readPosition, timeline: { threadKey, navigate, readPosition } }; |
| 25 | } |
| 26 | |
| 27 | describe("thread find store", () => { |
| 28 | it("restarts an open query from the viewport of the timeline showing the thread", () => { |
| 29 | const { navigate, readPosition, timeline } = setup(); |
| 30 | const detach = attachThreadFindTimeline(timeline); |
| 31 | openThreadFind(threadKey); |
| 32 | // Opening find is manual navigation; reopening it is not. |
| 33 | expect(navigate).toHaveBeenCalledTimes(1); |
| 34 | expect(useThreadFindStore.getState()).toMatchObject({ threadKey, focusRequestId: 1 }); |
| 35 | |
| 36 | setThreadFindQuery(threadKey, " needle "); |
| 37 | expect(readPosition).toHaveBeenLastCalledWith("needle"); |
| 38 | // Opening again refocuses and restarts the search from where the viewport is. |
| 39 | openThreadFind(threadKey); |
| 40 | expect(navigate).toHaveBeenCalledTimes(1); |
| 41 | expect(useThreadFindStore.getState()).toMatchObject({ |
| 42 | query: " needle ", |
| 43 | focusRequestId: 2, |
| 44 | start: { entryId: "entry-9", occurrence: 1 }, |
| 45 | navigationId: 2, |
| 46 | requestId: 2, |
| 47 | }); |
| 48 | detach(); |
| 49 | }); |
| 50 | |
| 51 | it("opens a search result's find for a thread before its timeline shows it", () => { |
| 52 | const { navigate, readPosition, timeline } = setup(); |
| 53 | const detach = attachThreadFindTimeline({ ...timeline, threadKey: "env-1:thread-2" }); |
| 54 | openThreadFindOnQuery(threadKey, { query: "Café", start: { entryId: "m", occurrence: 0 } }); |
| 55 | expect(useThreadFindStore.getState()).toMatchObject({ |
| 56 | threadKey, |
| 57 | query: "Café", |
| 58 | start: { entryId: "m", occurrence: 0 }, |
| 59 | }); |
| 60 | // Another thread's timeline does not answer for this one. |
| 61 | setThreadFindQuery(threadKey, "Caf"); |
| 62 | expect(readPosition).not.toHaveBeenCalled(); |
| 63 | expect(navigate).not.toHaveBeenCalled(); |
| 64 | detach(); |
| 65 | }); |
| 66 | |
| 67 | it("keeps find over Settings and Usage, and closes it on any other route", () => { |
| 68 | setup(); |
| 69 | openThreadFindOnQuery(threadKey, { query: "x" }); |
| 70 | retainThreadFindForRoute({ modalPage: false, threadRef }); |
| 71 | retainThreadFindForRoute({ modalPage: true, threadRef: null }); |
| 72 | expect(useThreadFindStore.getState()).toMatchObject({ threadKey, query: "x" }); |
| 73 | |
| 74 | retainThreadFindForRoute({ modalPage: false, threadRef: otherRef }); |
| 75 | expect(useThreadFindStore.getState()).toMatchObject({ threadKey: null, query: "" }); |
| 76 | |
| 77 | openThreadFindOnQuery(threadKey, { query: "x" }); |
| 78 | retainThreadFindForRoute({ modalPage: false, threadRef: null }); |
| 79 | expect(useThreadFindStore.getState().threadKey).toBeNull(); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | describe("findForMessageMatch", () => { |
| 84 | it("opens find on the longest word as the snippet writes it, at the matched message", () => { |
| 85 | expect( |
| 86 | findForMessageMatch( |
| 87 | { snippet: "The cafeteria, then the Café", messageId: MessageId.make("message-1") }, |
| 88 | "cafe", |
| 89 | ), |
| 90 | ).toEqual({ query: "Café", start: { entryId: "message-1", occurrence: 0 } }); |
| 91 | expect(findForMessageMatch({ snippet: "fix the sidebar lag" }, "fix sidebar")).toEqual({ |
| 92 | query: "sidebar", |
| 93 | }); |
| 94 | // The whole query, when the snippet holds it as typed. |
| 95 | expect(findForMessageMatch({ snippet: "then Vp run dev again" }, "vp run dev")).toEqual({ |
| 96 | query: "Vp run dev", |
| 97 | }); |
| 98 | }); |
| 99 | }); |