Open History to see the last commit
1import { act, useLayoutEffect, type ReactNode } from "react";
2import { createRoot } from "react-dom/client";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4import {
5 EnvironmentId,
6 RunId,
7 ThreadId,
8 type OrchestrationV2SearchThreadInput,
9 type OrchestrationV2SearchThreadResult,
10} from "@t3tools/contracts";
11import { useThreadFind } from "./useThreadFind";
12import { orchestrationEnvironment } from "~/state/orchestration";
13import { requestThreadFindOpen } from "./threadFindActionBus";
14
15const queries = vi.hoisted(() => ({
16 results: new Map<string, OrchestrationV2SearchThreadResult>(),
17 pending: false,
18 refresh: vi.fn(),
19}));
20vi.mock("~/state/orchestration", () => ({
21 orchestrationEnvironment: {
22 threadFindProgressive: vi.fn(
23 (input: { environmentId: EnvironmentId; input: OrchestrationV2SearchThreadInput }) => input,
24 ),
25 threadFind: vi.fn(
26 (input: { environmentId: EnvironmentId; input: OrchestrationV2SearchThreadInput }) => input,
27 ),
28 },
29}));
30vi.mock("~/state/queries", () => ({ useDebouncedValue: <T,>(value: T) => value }));
31vi.mock("~/state/query", () => ({
32 useEnvironmentQuery: (atom: { environmentId: EnvironmentId } | null) => ({
33 data: atom === null ? null : (queries.results.get(atom.environmentId) ?? null),
34 isPending: queries.pending,
35 error: null,
36 refresh: queries.refresh,
37 }),
38}));
39
40vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
41
42type Rendered = { update: (node: ReactNode) => void; unmount: () => void };
43function render(node: ReactNode): Rendered {
44 const root = createRoot(document.createElement("div"));
45 root.render(node);
46 return { update: (next) => root.render(next), unmount: () => root.unmount() };
47}
48
49let renderer: Rendered | undefined;
50let find: ReturnType<typeof useThreadFind>;
51const a = EnvironmentId.make("environment:a");
52const b = EnvironmentId.make("environment:b");
53const threadId = ThreadId.make("shared-thread");
54const runId = RunId.make("run:plan");
55function Probe({
56 environmentId,
57 enabled = true,
58 progressive = false,
59 content,
60}: {
61 environmentId: EnvironmentId;
62 enabled?: boolean;
63 progressive?: boolean;
64 content?: Parameters<typeof useThreadFind>[0]["content"];
65}) {
66 const state = useThreadFind({
67 thread: { environmentId, threadId },
68 enabled,
69 progressive,
70 content,
71 });
72 useLayoutEffect(() => {
73 find = state;
74 });
75 return null;
76}
77beforeEach(() => {
78 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
79});
80afterEach(async () => {
81 if (renderer) await act(async () => renderer?.unmount());
82 renderer = undefined;
83 queries.results.clear();
84 queries.pending = false;
85 vi.clearAllMocks();
86 vi.useRealTimers();
87 vi.unstubAllGlobals();
88});
89
90describe("V2 find state", () => {
91 function messageResult(index: number, snapshotSequence = 9): OrchestrationV2SearchThreadResult {
92 return {
93 snapshotSequence,
94 totalMatches: 10,
95 activeIndex: index,
96 match: { entryId: `message:${index + 2}`, runId, occurrence: 0 },
97 };
98 }
99
100 it("reveals an early match and updates its count without requesting another jump", async () => {
101 await act(async () => {
102 renderer = render(<Probe environmentId={a} progressive />);
103 });
104 await act(async () => find.open());
105 await act(async () => find.barProps.onQueryChange("needle"));
106 const partial: OrchestrationV2SearchThreadResult = {
107 complete: false,
108 snapshotSequence: 9,
109 totalMatches: 1,
110 activeIndex: 0,
111 match: { entryId: "early", runId, occurrence: 2 },
112 };
113 queries.pending = true;
114 queries.results.set(a, partial);
115 await act(async () => renderer?.update(<Probe environmentId={a} progressive />));
116 expect(find.barProps.counting).toBe(true);
117 expect(find.barProps.status).toBeNull();
118 expect(find.timelineProps.activeFindMatch).toEqual(partial.match);
119 const match = find.timelineProps.activeFindMatch;
120 const navigationId = find.timelineProps.findNavigationId;
121 const requests = vi.mocked(orchestrationEnvironment.threadFindProgressive).mock.calls.length;
122 await act(async () => {
123 find.barProps.onNext();
124 find.barProps.onPrevious();
125 });
126 expect(orchestrationEnvironment.threadFindProgressive).toHaveBeenCalledTimes(requests);
127 queries.results.set(a, { ...partial, complete: true, totalMatches: 123, activeIndex: 42 });
128 await act(async () => renderer?.update(<Probe environmentId={a} progressive />));
129 expect(find.barProps.counting).toBe(false);
130 expect(find.barProps.matchCount).toBe(123);
131 expect(find.barProps.activeIndex).toBe(42);
132 expect(find.timelineProps.activeFindMatch).toBe(match);
133 expect(find.timelineProps.findNavigationId).toBe(navigationId);
134 expect(orchestrationEnvironment.threadFind).not.toHaveBeenCalled();
135 queries.pending = false;
136 await act(async () => renderer?.update(<Probe environmentId={a} progressive />));
137 await act(async () => find.barProps.onNext());
138 expect(orchestrationEnvironment.threadFindProgressive).toHaveBeenCalledTimes(requests + 1);
139 });
140
141 it("coalesces incoming pages until the progressive scan finishes", async () => {
142 vi.useFakeTimers();
143 queries.pending = true;
144 await act(async () => {
145 renderer = render(<Probe environmentId={a} progressive />);
146 });
147 await act(async () => find.open());
148 await act(async () => find.barProps.onQueryChange("needle"));
149 queries.results.set(a, { ...messageResult(0), complete: false, totalMatches: 1 });
150 const content = { visibleTurnItems: [], runs: [] };
151 await act(async () =>
152 renderer?.update(<Probe environmentId={a} progressive content={content} />),
153 );
154 const requests = vi.mocked(orchestrationEnvironment.threadFindProgressive).mock.calls.length;
155 await act(async () => vi.advanceTimersByTime(300));
156 expect(orchestrationEnvironment.threadFindProgressive).toHaveBeenCalledTimes(requests);
157 expect(queries.refresh).not.toHaveBeenCalled();
158 queries.results.set(a, messageResult(0));
159 queries.pending = false;
160 await act(async () =>
161 renderer?.update(<Probe environmentId={a} progressive content={content} />),
162 );
163 expect(orchestrationEnvironment.threadFindProgressive).toHaveBeenCalledTimes(requests + 1);
164 expect(orchestrationEnvironment.threadFindProgressive).toHaveBeenLastCalledWith({
165 environmentId: a,
166 input: {
167 threadId,
168 query: "needle",
169 skills: [],
170 start: { entryId: "message:2", occurrence: 0 },
171 },
172 });
173 expect(find.timelineProps.activeFindMatch?.entryId).toBe("message:2");
174 });
175
176 it("requests a fresh jump when the same search wraps to the same match from a new position", async () => {
177 queries.results.set(a, messageResult(0));
178 await act(async () => {
179 renderer = render(<Probe environmentId={a} />);
180 });
181 await act(async () => find.open());
182 find.timelineProps.findPositionReaderRef.current = () => ({
183 entryId: "reading",
184 occurrence: 0,
185 });
186 await act(async () => find.barProps.onQueryChange("COD4"));
187 const firstNavigationId = find.timelineProps.findNavigationId;
188 const firstMatch = find.timelineProps.activeFindMatch;
189 find.timelineProps.findPositionReaderRef.current = () => ({ entryId: "bottom", occurrence: 0 });
190 await act(async () => find.barProps.onQueryChange("COD4"));
191 expect(find.timelineProps.findNavigationId).toBeGreaterThan(firstNavigationId);
192 expect(find.timelineProps.activeFindMatch).toEqual(firstMatch);
193 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
194 environmentId: a,
195 input: { threadId, query: "COD4", skills: [], start: { entryId: "bottom", occurrence: 0 } },
196 });
197 const repeatedNavigationId = find.timelineProps.findNavigationId;
198 find.timelineProps.findPositionReaderRef.current = () => ({
199 entryId: "new-bottom",
200 occurrence: 0,
201 });
202 await act(async () => requestThreadFindOpen());
203 expect(find.timelineProps.findNavigationId).toBeGreaterThan(repeatedNavigationId);
204 expect(find.timelineProps.activeFindMatch).toEqual(firstMatch);
205 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
206 environmentId: a,
207 input: {
208 threadId,
209 query: "COD4",
210 skills: [],
211 start: { entryId: "new-bottom", occurrence: 0 },
212 },
213 });
214 });
215
216 it("cycles cached entries immediately, wraps, and accumulates clicks in one event", async () => {
217 const initial = {
218 ...messageResult(0),
219 totalMatches: 5,
220 match: { entryId: "first", runId, occurrence: 0 },
221 navigation: [
222 { entryId: "first", runId, startIndex: 0, count: 3 },
223 { entryId: "second", runId, startIndex: 3, count: 2 },
224 ],
225 };
226 queries.results.set(a, initial);
227 await act(async () => {
228 renderer = render(<Probe environmentId={a} />);
229 });
230 await act(async () => find.open());
231 await act(async () => find.barProps.onQueryChange("COD4"));
232 const requests = vi.mocked(orchestrationEnvironment.threadFind).mock.calls.length;
233 await act(async () => {
234 find.barProps.onNext();
235 find.barProps.onNext();
236 });
237 expect(find.barProps.activeIndex).toBe(2);
238 expect(find.timelineProps.activeFindMatch).toEqual({ entryId: "first", runId, occurrence: 2 });
239 expect(find.timelineProps.findNavigationId).toBe(3);
240 await act(async () => find.barProps.onNext());
241 expect(find.timelineProps.activeFindMatch).toEqual({ entryId: "second", runId, occurrence: 0 });
242 await act(async () => {
243 find.barProps.onNext();
244 find.barProps.onNext();
245 });
246 expect(find.barProps.activeIndex).toBe(0);
247 await act(async () => find.barProps.onPrevious());
248 expect(find.barProps.activeIndex).toBe(4);
249 expect(find.timelineProps.activeFindMatch).toEqual({ entryId: "second", runId, occurrence: 1 });
250 expect(orchestrationEnvironment.threadFind).toHaveBeenCalledTimes(requests);
251 expect(queries.refresh).not.toHaveBeenCalled();
252 });
253
254 it("does not let an older background response undo a cached step", async () => {
255 const initial = {
256 ...messageResult(0),
257 match: { entryId: "first", runId, occurrence: 0 },
258 navigation: [{ entryId: "first", runId, startIndex: 0, count: 3 }],
259 };
260 queries.results.set(a, initial);
261 await act(async () => {
262 renderer = render(<Probe environmentId={a} />);
263 });
264 await act(async () => find.open());
265 await act(async () => find.barProps.onQueryChange("COD4"));
266 const requests = vi.mocked(orchestrationEnvironment.threadFind).mock.calls.length;
267 await act(async () => find.barProps.onNext());
268 queries.results.set(a, {
269 ...initial,
270 snapshotSequence: 10,
271 totalMatches: 11,
272 activeIndex: 1,
273 navigation: [{ entryId: "first", runId, startIndex: 1, count: 3 }],
274 });
275 await act(async () => renderer?.update(<Probe environmentId={a} />));
276 expect(find.timelineProps.activeFindMatch).toEqual({ entryId: "first", runId, occurrence: 1 });
277 expect(find.barProps.activeIndex).toBe(2);
278 expect(find.barProps.matchCount).toBe(11);
279 expect(find.timelineProps.findNavigationId).toBe(2);
280 await act(async () => find.barProps.onNext());
281 expect(find.timelineProps.activeFindMatch?.occurrence).toBe(2);
282 expect(find.barProps.activeIndex).toBe(3);
283 expect(orchestrationEnvironment.threadFind).toHaveBeenCalledTimes(requests);
284 });
285
286 it("fetches the next window from the locally selected match and retains it while pending", async () => {
287 const initial = {
288 ...messageResult(0),
289 match: { entryId: "first", runId, occurrence: 0 },
290 navigation: [{ entryId: "first", runId, startIndex: 0, count: 2 }],
291 };
292 queries.results.set(a, initial);
293 await act(async () => {
294 renderer = render(<Probe environmentId={a} />);
295 });
296 await act(async () => find.open());
297 await act(async () => find.barProps.onQueryChange("COD4"));
298 await act(async () => find.barProps.onNext());
299 expect(find.timelineProps.activeFindMatch?.occurrence).toBe(1);
300 queries.pending = true;
301 queries.results.delete(a);
302 await act(async () => {
303 find.barProps.onNext();
304 find.barProps.onNext();
305 });
306 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
307 environmentId: a,
308 input: {
309 threadId,
310 query: "COD4",
311 skills: [],
312 start: { entryId: "first", occurrence: 1 },
313 offset: 2,
314 },
315 });
316 expect(find.barProps.activeIndex).toBe(1);
317 expect(find.timelineProps.findNavigationId).toBe(2);
318 queries.pending = false;
319 queries.results.set(a, messageResult(3));
320 await act(async () => renderer?.update(<Probe environmentId={a} />));
321 expect(find.barProps.activeIndex).toBe(3);
322 expect(find.timelineProps.findNavigationId).toBe(4);
323 });
324
325 it("refreshes around the cached selection when live matches are inserted before it", async () => {
326 vi.useFakeTimers();
327 const initial = {
328 ...messageResult(0),
329 match: { entryId: "first", runId, occurrence: 0 },
330 navigation: [
331 { entryId: "first", runId, startIndex: 0, count: 1 },
332 { entryId: "second", runId, startIndex: 1, count: 1 },
333 ],
334 };
335 queries.results.set(a, initial);
336 await act(async () => {
337 renderer = render(<Probe environmentId={a} />);
338 });
339 await act(async () => find.open());
340 await act(async () => find.barProps.onQueryChange("COD4"));
341 await act(async () => find.barProps.onNext());
342 expect(find.timelineProps.activeFindMatch?.entryId).toBe("second");
343 const content = { runs: [], visibleTurnItems: [] };
344 await act(async () => renderer?.update(<Probe environmentId={a} content={content} />));
345 await act(async () => vi.advanceTimersByTime(300));
346 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
347 environmentId: a,
348 input: { threadId, query: "COD4", skills: [], start: { entryId: "second", occurrence: 0 } },
349 });
350 queries.results.set(a, {
351 ...initial,
352 snapshotSequence: 10,
353 totalMatches: 11,
354 activeIndex: 2,
355 match: { entryId: "second", runId, occurrence: 0 },
356 navigation: [
357 { entryId: "inserted", runId, startIndex: 1, count: 1 },
358 { entryId: "second", runId, startIndex: 2, count: 1 },
359 ],
360 });
361 await act(async () => renderer?.update(<Probe environmentId={a} content={content} />));
362 expect(find.barProps.matchCount).toBe(11);
363 expect(find.barProps.activeIndex).toBe(2);
364 expect(find.timelineProps.activeFindMatch?.entryId).toBe("second");
365 expect(find.timelineProps.findNavigationId).toBe(2);
366 await act(async () => find.barProps.onPrevious());
367 expect(find.timelineProps.activeFindMatch?.entryId).toBe("inserted");
368 });
369
370 it("asks the server rather than using stale navigation counts during live updates", async () => {
371 const initial = {
372 ...messageResult(0),
373 navigation: [{ entryId: "message:2", runId, startIndex: 0, count: 10 }],
374 };
375 queries.results.set(a, initial);
376 await act(async () => {
377 renderer = render(<Probe environmentId={a} />);
378 });
379 await act(async () => find.open());
380 await act(async () => find.barProps.onQueryChange("COD4"));
381 await act(async () =>
382 renderer?.update(<Probe environmentId={a} content={{ runs: [], visibleTurnItems: [] }} />),
383 );
384 await act(async () => find.barProps.onNext());
385 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
386 environmentId: a,
387 input: {
388 threadId,
389 query: "COD4",
390 skills: [],
391 start: { entryId: "message:2", occurrence: 0 },
392 offset: 1,
393 },
394 });
395 });
396
397 it("starts from the viewport and steps from the server-selected match", async () => {
398 queries.results.set(a, messageResult(4));
399 await act(async () => {
400 renderer = render(<Probe environmentId={a} />);
401 });
402 await act(async () => find.open());
403 find.timelineProps.findPositionReaderRef.current = () => ({
404 entryId: "message:6",
405 occurrence: 1,
406 });
407 await act(async () => find.barProps.onQueryChange("COD4"));
408 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
409 environmentId: a,
410 input: {
411 threadId,
412 query: "COD4",
413 skills: [],
414 start: { entryId: "message:6", occurrence: 1 },
415 },
416 });
417 expect(find.barProps.activeIndex).toBe(4);
418 queries.results.set(a, messageResult(5));
419 await act(async () => find.barProps.onNext());
420 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
421 environmentId: a,
422 input: {
423 threadId,
424 query: "COD4",
425 skills: [],
426 start: { entryId: "message:6", occurrence: 0 },
427 offset: 1,
428 },
429 });
430 expect(find.timelineProps.activeFindMatch?.entryId).toBe("message:7");
431 queries.results.set(a, messageResult(4));
432 await act(async () => find.barProps.onPrevious());
433 expect(find.barProps.activeIndex).toBe(4);
434 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
435 environmentId: a,
436 input: {
437 threadId,
438 query: "COD4",
439 skills: [],
440 start: { entryId: "message:7", occurrence: 0 },
441 offset: -1,
442 },
443 });
444 });
445
446 it("accumulates rapid navigation against the same match until the response arrives", async () => {
447 queries.results.set(a, messageResult(4));
448 await act(async () => {
449 renderer = render(<Probe environmentId={a} />);
450 });
451 await act(async () => find.open());
452 await act(async () => find.barProps.onQueryChange("COD4"));
453 queries.pending = true;
454 queries.results.delete(a);
455 await act(async () => find.barProps.onNext());
456 await act(async () => find.barProps.onNext());
457 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
458 environmentId: a,
459 input: {
460 threadId,
461 query: "COD4",
462 skills: [],
463 start: { entryId: "message:6", occurrence: 0 },
464 offset: 2,
465 },
466 });
467 expect(find.timelineProps.findNavigationId).toBe(1);
468 queries.pending = false;
469 queries.results.set(a, messageResult(6));
470 await act(async () => renderer?.update(<Probe environmentId={a} />));
471 expect(find.timelineProps.findNavigationId).toBe(3);
472 expect(find.barProps.activeIndex).toBe(6);
473 });
474
475 it("refreshes continuous updates without shifting the current match or showing Searching", async () => {
476 vi.useFakeTimers();
477 queries.results.set(a, messageResult(4));
478 await act(async () => {
479 renderer = render(<Probe environmentId={a} />);
480 });
481 await act(async () => find.open());
482 await act(async () => find.barProps.onQueryChange("COD4"));
483 for (let i = 0; i < 3; i++) {
484 await act(async () =>
485 renderer?.update(<Probe environmentId={a} content={{ visibleTurnItems: [], runs: [] }} />),
486 );
487 await act(async () => vi.advanceTimersByTime(100));
488 }
489 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
490 environmentId: a,
491 input: {
492 threadId,
493 query: "COD4",
494 skills: [],
495 start: { entryId: "message:6", occurrence: 0 },
496 },
497 });
498 const updated = { ...messageResult(5, 10), totalMatches: 11, match: messageResult(4).match };
499 queries.results.set(a, updated);
500 await act(async () => renderer?.update(<Probe environmentId={a} />));
501 expect(find.barProps.matchCount).toBe(11);
502 expect(find.barProps.activeIndex).toBe(5);
503 expect(find.timelineProps.activeFindMatch?.entryId).toBe("message:6");
504 expect(find.timelineProps.findNavigationId).toBe(1);
505 expect(find.barProps.status).toBeNull();
506 await act(async () => find.barProps.onNext());
507 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
508 environmentId: a,
509 input: {
510 threadId,
511 query: "COD4",
512 skills: [],
513 start: { entryId: "message:6", occurrence: 0 },
514 offset: 1,
515 },
516 });
517 });
518
519 it("keeps pending navigation when incoming content triggers a refresh", async () => {
520 vi.useFakeTimers();
521 queries.results.set(a, messageResult(4));
522 await act(async () => {
523 renderer = render(<Probe environmentId={a} />);
524 });
525 await act(async () => find.open());
526 await act(async () => find.barProps.onQueryChange("COD4"));
527 queries.pending = true;
528 queries.results.delete(a);
529 await act(async () => find.barProps.onNext());
530 await act(async () =>
531 renderer?.update(<Probe environmentId={a} content={{ visibleTurnItems: [], runs: [] }} />),
532 );
533 await act(async () => vi.advanceTimersByTime(300));
534 expect(queries.refresh).toHaveBeenCalledTimes(1);
535 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
536 environmentId: a,
537 input: {
538 threadId,
539 query: "COD4",
540 skills: [],
541 start: { entryId: "message:6", occurrence: 0 },
542 offset: 1,
543 },
544 });
545 queries.pending = false;
546 queries.results.set(a, { ...messageResult(6, 10), totalMatches: 11 });
547 await act(async () => renderer?.update(<Probe environmentId={a} />));
548 expect(find.timelineProps.activeFindMatch?.entryId).toBe("message:8");
549 expect(find.timelineProps.findNavigationId).toBe(2);
550 expect(find.barProps.matchCount).toBe(11);
551 });
552
553 it("does not navigate the previous result while a new match is loading", async () => {
554 queries.results.set(a, messageResult(0));
555 await act(async () => {
556 renderer = render(<Probe environmentId={a} />);
557 });
558 await act(async () => find.open());
559 await act(async () => find.barProps.onQueryChange("COD4"));
560 queries.pending = true;
561 queries.results.delete(a);
562 await act(async () => find.barProps.onNext());
563 expect(find.timelineProps.activeFindMatch?.entryId).toBe("message:2");
564 expect(find.timelineProps.findNavigationId).toBe(1);
565 expect(find.barProps.status).toBeNull();
566 expect(find.barProps.activeIndex).toBe(0);
567 expect(find.barProps.matchCount).toBe(10);
568 queries.pending = false;
569 queries.results.set(a, messageResult(1));
570 await act(async () => renderer?.update(<Probe environmentId={a} />));
571 expect(find.timelineProps.activeFindMatch?.entryId).toBe("message:3");
572 expect(find.timelineProps.findNavigationId).toBe(2);
573 });
574
575 it("keeps the reading-position start when Enter is pressed before results arrive", async () => {
576 queries.pending = true;
577 await act(async () => {
578 renderer = render(<Probe environmentId={a} />);
579 });
580 await act(async () => find.open());
581 find.timelineProps.findPositionReaderRef.current = () => ({
582 entryId: "message:6",
583 occurrence: 1,
584 });
585 await act(async () => find.barProps.onQueryChange("COD4"));
586 await act(async () => find.barProps.onNext());
587 expect(orchestrationEnvironment.threadFind).toHaveBeenLastCalledWith({
588 environmentId: a,
589 input: {
590 threadId,
591 query: "COD4",
592 skills: [],
593 start: { entryId: "message:6", occurrence: 1 },
594 },
595 });
596 });
597
598 it("shows Searching only when the query has no result yet", async () => {
599 queries.pending = true;
600 await act(async () => {
601 renderer = render(<Probe environmentId={a} />);
602 });
603 await act(async () => find.open());
604 await act(async () => find.barProps.onQueryChange("COD4"));
605 expect(find.barProps.status).toBe("loading");
606 queries.pending = false;
607 queries.results.set(a, messageResult(0));
608 await act(async () => renderer?.update(<Probe environmentId={a} />));
609 expect(find.barProps.status).toBeNull();
610 queries.pending = true;
611 queries.results.delete(a);
612 await act(async () => find.barProps.onQueryChange("different query"));
613 expect(find.barProps.status).toBe("loading");
614 expect(find.barProps.matchCount).toBe(0);
615
616 expect(find.timelineProps.findQuery).toBe("");
617 expect(find.timelineProps.findExpanded).toBe(true);
618 await act(async () => find.barProps.onQueryChange(""));
619 expect(find.timelineProps.findExpanded).toBe(false);
620 });
621
622 it("keeps search unavailable when the server does not support it", async () => {
623 await act(async () => {
624 renderer = render(<Probe environmentId={a} enabled={false} />);
625 });
626 await act(async () => {
627 find.open();
628 requestThreadFindOpen();
629 });
630 expect(find.isOpen).toBe(false);
631 expect(find.timelineProps.activeFindMatch).toBeNull();
632 expect(orchestrationEnvironment.threadFind).not.toHaveBeenCalled();
633 });
634
635 it("closes search when support disappears without restoring stale queries", async () => {
636 await act(async () => {
637 renderer = render(<Probe environmentId={a} />);
638 });
639 await act(async () => find.open());
640 await act(async () => find.barProps.onQueryChange("needle"));
641 expect(find.isOpen).toBe(true);
642 vi.mocked(orchestrationEnvironment.threadFind).mockClear();
643 await act(async () => {
644 renderer?.update(<Probe environmentId={a} enabled={false} />);
645 });
646 expect(find.isOpen).toBe(false);
647 expect(find.timelineProps.activeFindMatch).toBeNull();
648 expect(find.timelineProps.findQuery).toBe("");
649 expect(orchestrationEnvironment.threadFind).not.toHaveBeenCalled();
650 await act(async () => {
651 renderer?.update(<Probe environmentId={a} />);
652 });
653 expect(find.isOpen).toBe(false);
654 await act(async () => find.open());
655 expect(find.barProps.query).toBe("");
656 });
657
658 it("preserves plan item ID and run ownership for navigation", async () => {
659 queries.results.set(a, {
660 snapshotSequence: 9,
661 totalMatches: 1,
662 activeIndex: 0,
663 match: { entryId: "plan-item", runId, occurrence: 0 },
664 });
665 await act(async () => {
666 renderer = render(<Probe environmentId={a} />);
667 });
668 await act(async () => {
669 find.open();
670 });
671 await act(async () => {
672 find.barProps.onQueryChange("needle");
673 });
674 expect(find.timelineProps.activeFindMatch).toEqual({
675 entryId: "plan-item",
676 runId,
677 occurrence: 0,
678 });
679 await act(async () => {
680 find.close();
681 });
682 expect(find.timelineProps.activeFindMatch).toBeNull();
683 expect(find.timelineProps.findQuery).toBe("");
684 });
685
686 it("drops results on environment changes even when the thread IDs are identical", async () => {
687 queries.results.set(a, {
688 snapshotSequence: 1,
689 totalMatches: 3,
690 activeIndex: 0,
691 match: null,
692 });
693 queries.results.set(b, {
694 snapshotSequence: 2,
695 totalMatches: 0,
696 activeIndex: 0,
697 match: null,
698 });
699 await act(async () => {
700 renderer = render(<Probe environmentId={a} />);
701 });
702 await act(async () => {
703 find.open();
704 });
705 await act(async () => {
706 find.barProps.onQueryChange("needle");
707 });
708 expect(find.barProps.matchCount).toBe(3);
709 await act(async () => {
710 renderer?.update(<Probe environmentId={b} />);
711 });
712 expect(find.isOpen).toBe(false);
713 expect(find.timelineProps.activeFindMatch).toBeNull();
714 await act(async () => {
715 find.open();
716 });
717 await act(async () => {
718 find.barProps.onQueryChange("needle");
719 });
720 expect(find.barProps.matchCount).toBe(0);
721 await act(async () => {
722 renderer?.update(<Probe environmentId={a} />);
723 });
724 expect(find.isOpen).toBe(false);
725 });
726});