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("encodes scoped thread keys without delimiter collisions", () => {
27 const first = threadSearchMatchKey({
28 environmentId: EnvironmentId.make("env\u0000thread"),
29 threadId: ThreadId.make("id"),
30 });
31 const second = threadSearchMatchKey({
32 environmentId: EnvironmentId.make("env"),
33 threadId: ThreadId.make("thread\u0000id"),
34 });
35
36 expect(first).not.toBe(second);
37});
38
39it("merges successful environments and silently ignores failures", () => {
40 const result: OrchestrationSearchThreadsResult = {
41 matches: [
42 {
43 threadId: ThreadId.make("thread-a"),
44 projectId: ProjectId.make("project-a"),
45 source: "user",
46 snippet: "needle",
47 messageCreatedAt: "2026-07-30T00:00:00.000Z",
48 },
49 ],
50 };
51 const searchAtom = createThreadSearchResultsAtomFamily<Error>({
52 getSearchAtom: (environmentId) =>
53 environmentId === envA
54 ? Atom.make(AsyncResult.success(result))
55 : Atom.make(
56 AsyncResult.failure<OrchestrationSearchThreadsResult, Error>(
57 Cause.fail(new Error("unsupported rpc")),
58 ),
59 ),
60 labelPrefix: "test:thread-search",
61 });
62 const registry = AtomRegistry.make();
63
64 const state = registry.get(searchAtom(makeThreadSearchKey([envB, envA], "needle")));
65 expect(state).toEqual({
66 matches: [{ ...result.matches[0], environmentId: envA }],
67 isLoading: false,
68 });
69 expect(threadSearchMatchKey(state.matches[0]!)).toBe('["env-a","thread-a"]');
70
71 registry.dispose();
72});