Open History to see the last commit
1import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
4import { describe, expect, it } from "vite-plus/test";
5
6import { v2Projection } from "./orchestrationV2TestFixtures.ts";
7import { EMPTY_THREAD_HISTORY_META } from "./threadHistoryMerge.ts";
8import { createEnvironmentThreadDetailAtoms } from "./threadDetail.ts";
9import type { EnvironmentThreadState } from "./threads.ts";
10
11const ref: ScopedThreadRef = {
12 environmentId: EnvironmentId.make("environment-detail"),
13 threadId: ThreadId.make(v2Projection.thread.id),
14};
15
16function threadState(
17 partial: Pick<EnvironmentThreadState, "data" | "status" | "error"> &
18 Partial<Pick<EnvironmentThreadState, "history">>,
19): EnvironmentThreadState {
20 return { ...partial, history: partial.history ?? EMPTY_THREAD_HISTORY_META };
21}
22
23describe("createEnvironmentThreadDetailAtoms", () => {
24 it("adds environment scope while preserving the pristine projection", () => {
25 const initial: AsyncResult.AsyncResult<EnvironmentThreadState, never> = AsyncResult.success(
26 threadState({
27 data: Option.some(v2Projection),
28 status: "cached",
29 error: Option.none(),
30 }),
31 );
32 const sourceAtom = Atom.make(initial);
33 const details = createEnvironmentThreadDetailAtoms(() => sourceAtom);
34 const registry = AtomRegistry.make();
35
36 const thread = registry.get(details.threadAtom(ref));
37 expect(thread).toEqual({ environmentId: ref.environmentId, projection: v2Projection });
38 expect(thread?.projection).toBe(v2Projection);
39 expect(registry.get(details.visibleTurnItemsAtom(ref))).toBe(v2Projection.visibleTurnItems);
40 expect(registry.get(details.statusAtom(ref))).toBe("cached");
41 expect(registry.get(details.historyAtom(ref))).toBe(EMPTY_THREAD_HISTORY_META);
42
43 registry.set(
44 sourceAtom,
45 AsyncResult.success(
46 threadState({
47 data: Option.some(v2Projection),
48 status: "synchronizing",
49 error: Option.none(),
50 }),
51 ),
52 );
53
54 expect(registry.get(details.threadAtom(ref))).toBe(thread);
55 expect(registry.get(details.statusAtom(ref))).toBe("synchronizing");
56
57 const progressiveHistory = {
58 ...EMPTY_THREAD_HISTORY_META,
59 historyCursor: "cursor-1",
60 hasMoreHistory: true,
61 };
62 registry.set(
63 sourceAtom,
64 AsyncResult.success(
65 threadState({
66 data: Option.some(v2Projection),
67 status: "live",
68 error: Option.some("Stream interrupted."),
69 history: progressiveHistory,
70 }),
71 ),
72 );
73
74 expect(registry.get(details.threadAtom(ref))).toBe(thread);
75 expect(registry.get(details.visibleTurnItemsAtom(ref))).toBe(v2Projection.visibleTurnItems);
76 expect(registry.get(details.statusAtom(ref))).toBe("live");
77 expect(registry.get(details.errorAtom(ref))).toBe("Stream interrupted.");
78 expect(registry.get(details.historyAtom(ref))).toBe(progressiveHistory);
79
80 registry.set(
81 sourceAtom,
82 AsyncResult.success(
83 threadState({
84 data: Option.none(),
85 status: "deleted",
86 error: Option.none(),
87 }),
88 ),
89 );
90
91 expect(registry.get(details.threadAtom(ref))).toBeNull();
92 expect(registry.get(details.visibleTurnItemsAtom(ref))).toEqual([]);
93 expect(registry.get(details.statusAtom(ref))).toBe("deleted");
94 expect(registry.get(details.errorAtom(ref))).toBeNull();
95 expect(registry.get(details.historyAtom(ref))).toBe(EMPTY_THREAD_HISTORY_META);
96 registry.dispose();
97 });
98});