Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadSearch.test.ts

102 lines3.1 KB

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/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 keys without array methods unavailable in Hermes", () => {
21 const descriptor = Object.getOwnPropertyDescriptor(Array.prototype, "toSorted");
22 Reflect.deleteProperty(Array.prototype, "toSorted");
23​
24 try {
25 expect(makeThreadSearchKey([envB, envA], "needle")).toBe('[["env-a","env-b"],"needle"]');
26 } finally {
27 if (descriptor !== undefined) {
28 Reflect.defineProperty(Array.prototype, "toSorted", descriptor);
29 }
30 }
31});
32​
33it("encodes scoped thread keys without delimiter collisions", () => {
34 const first = threadSearchMatchKey({
35 environmentId: EnvironmentId.make("env\u0000thread"),
36 threadId: ThreadId.make("id"),
37 });
38 const second = threadSearchMatchKey({
39 environmentId: EnvironmentId.make("env"),
40 threadId: ThreadId.make("thread\u0000id"),
41 });
42​
43 expect(first).not.toBe(second);
44});
45​
46it("ignores invalid search keys", () => {
47 let searchCount = 0;
48 const searchAtom = createThreadSearchResultsAtomFamily<Error>({
49 getSearchAtom: () => {
50 searchCount += 1;
51 return Atom.make(AsyncResult.success({ matches: [] }));
52 },
53 labelPrefix: "test:thread-search",
54 });
55 const registry = AtomRegistry.make();
56​
57 try {
58 const state = registry.get(searchAtom(makeThreadSearchKey([envA], "a".repeat(201))));
59 const malformedState = registry.get(searchAtom("not json{{"));
60​
61 expect(state).toEqual({ matches: [], isLoading: false });
62 expect(malformedState).toEqual({ matches: [], isLoading: false });
63 expect(searchCount).toBe(0);
64 } finally {
65 registry.dispose();
66 }
67});
68​
69it("merges successful environments and silently ignores failures", () => {
70 const result: OrchestrationSearchThreadsResult = {
71 matches: [
72 {
73 threadId: ThreadId.make("thread-a"),
74 projectId: ProjectId.make("project-a"),
75 source: "user",
76 snippet: "needle",
77 messageCreatedAt: "2026-07-30T00:00:00.000Z",
78 },
79 ],
80 };
81 const searchAtom = createThreadSearchResultsAtomFamily<Error>({
82 getSearchAtom: (environmentId) =>
83 environmentId === envA
84 ? Atom.make(AsyncResult.success(result))
85 : Atom.make(
86 AsyncResult.failure<OrchestrationSearchThreadsResult, Error>(
87 Cause.fail(new Error("unsupported rpc")),
88 ),
89 ),
90 labelPrefix: "test:thread-search",
91 });
92 const registry = AtomRegistry.make();
93​
94 const state = registry.get(searchAtom(makeThreadSearchKey([envB, envA], "needle")));
95 expect(state).toEqual({
96 matches: [{ ...result.matches[0], environmentId: envA }],
97 isLoading: false,
98 });
99 expect(threadSearchMatchKey(state.matches[0]!)).toBe('["env-a","thread-a"]');
100​
101 registry.dispose();
102});