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