Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 ThreadId,
5 type OrchestrationSearchThreadsResult,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
9import { expect, it } from "vite-plus/test";
10
11import {
12 createThreadSearchResultsAtomFamily,
13 makeThreadSearchKey,
14 threadSearchMatchKey,
15} from "./threadSearch.ts";
16
17const envA = EnvironmentId.make("env-a");
18const envB = EnvironmentId.make("env-b");
19
20it("creates stable keys regardless of environment order", () => {
21 expect(makeThreadSearchKey([envB, envA], "needle")).toBe(
22 makeThreadSearchKey([envA, envB], "needle"),
23 );
24});
25
26it("creates keys without array methods unavailable in Hermes", () => {
27 const descriptor = Object.getOwnPropertyDescriptor(Array.prototype, "toSorted");
28 Reflect.deleteProperty(Array.prototype, "toSorted");
29
30 try {
31 expect(makeThreadSearchKey([envB, envA], "needle")).toBe('[["env-a","env-b"],"needle"]');
32 } finally {
33 if (descriptor !== undefined) {
34 Reflect.defineProperty(Array.prototype, "toSorted", descriptor);
35 }
36 }
37});
38
39it("encodes scoped thread keys without delimiter collisions", () => {
40 const first = threadSearchMatchKey({
41 environmentId: EnvironmentId.make("env\u0000thread"),
42 threadId: ThreadId.make("id"),
43 });
44 const second = threadSearchMatchKey({
45 environmentId: EnvironmentId.make("env"),
46 threadId: ThreadId.make("thread\u0000id"),
47 });
48
49 expect(first).not.toBe(second);
50});
51
52it("merges successful environments and silently ignores failures", () => {
53 const result: OrchestrationSearchThreadsResult = {
54 matches: [
55 {
56 threadId: ThreadId.make("thread-a"),
57 projectId: ProjectId.make("project-a"),
58 source: "user",
59 snippet: "needle",
60 messageCreatedAt: "2026-07-30T00:00:00.000Z",
61 },
62 ],
63 };
64 const searchAtom = createThreadSearchResultsAtomFamily<Error>({
65 getSearchAtom: (environmentId) =>
66 environmentId === envA
67 ? Atom.make(AsyncResult.success(result))
68 : Atom.make(
69 AsyncResult.failure<OrchestrationSearchThreadsResult, Error>(
70 Cause.fail(new Error("unsupported rpc")),
71 ),
72 ),
73 labelPrefix: "test:thread-search",
74 });
75 const registry = AtomRegistry.make();
76
77 const state = registry.get(searchAtom(makeThreadSearchKey([envB, envA], "needle")));
78 expect(state).toEqual({
79 matches: [{ ...result.matches[0], environmentId: envA }],
80 isLoading: false,
81 });
82 expect(threadSearchMatchKey(state.matches[0]!)).toBe('["env-a","thread-a"]');
83
84 registry.dispose();
85});