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

threadDetail.test.ts

175 lines6.2 KB

1import {
2 EnvironmentId,
3 MessageId,
4 RunId,
5 TurnItemId,
6 type OrchestrationV2ThreadProjection,
7 type OrchestrationV2TurnItem,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "@effect/vitest";
10import * as DateTime from "effect/DateTime";
11import * as Option from "effect/Option";
12import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
13​
14import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
15import { createEnvironmentThreadDetailAtoms, sameThreadStructure } from "./threadDetail.ts";
16import { EMPTY_ENVIRONMENT_THREAD_STATE } from "./threadState.ts";
17​
18const later = DateTime.makeUnsafe("2026-06-20T00:00:01.000Z");
19​
20const assistant: OrchestrationV2TurnItem = {
21 id: TurnItemId.make("item-assistant"),
22 threadId: v2ThreadId,
23 runId: RunId.make("run-1"),
24 nodeId: null,
25 providerThreadId: null,
26 providerTurnId: null,
27 nativeItemRef: null,
28 parentItemId: null,
29 ordinal: 1,
30 status: "running",
31 title: null,
32 startedAt: v2Now,
33 completedAt: null,
34 updatedAt: v2Now,
35 type: "assistant_message",
36 messageId: MessageId.make("message-assistant"),
37 text: "Hel",
38 streaming: true,
39};
40​
41function withItem(item: OrchestrationV2TurnItem, updatedAt = v2Now) {
42 return {
43 ...v2Projection,
44 turnItems: [item],
45 visibleTurnItems: [
46 {
47 position: 0,
48 visibility: "local" as const,
49 sourceThreadId: v2ThreadId,
50 sourceItemId: item.id,
51 item,
52 },
53 ],
54 updatedAt,
55 };
57​
58describe("sameThreadStructure", () => {
59 const before = withItem(assistant);
60​
61 it("holds through streamed text", () => {
62 const next = withItem({ ...assistant, text: "Hello", updatedAt: later }, later);
63 expect(sameThreadStructure(before, next)).toBe(true);
64 });
65​
66 it("sees the item finish streaming", () => {
67 const next = withItem({ ...assistant, text: "Hello", streaming: false, status: "completed" });
68 expect(sameThreadStructure(before, next)).toBe(false);
69 });
70​
71 it("sees a new item", () => {
72 const next = { ...before, turnItems: [assistant, { ...assistant, id: TurnItemId.make("b") }] };
73 expect(sameThreadStructure(before, next)).toBe(false);
74 });
75​
76 it("sees other fields change on a streaming item", () => {
77 const next = withItem({ ...assistant, text: "Hello", title: "Answer" });
78 expect(sameThreadStructure(before, next)).toBe(false);
79 });
80​
81 it("holds through a visit mark", () => {
82 const thread = { ...before.thread, lastVisitedAt: later, updatedAt: later };
83 expect(sameThreadStructure(before, { ...before, thread })).toBe(true);
84 });
85​
86 it("sees other thread changes", () => {
87 const thread = { ...before.thread, title: "Renamed" };
88 expect(sameThreadStructure(before, { ...before, thread })).toBe(false);
89 });
90​
91 it("sees changes outside the turn items", () => {
92 expect(sameThreadStructure(before, { ...before, runs: [] })).toBe(false);
93 });
94});
95​
96describe("lastVisitedAtAtom", () => {
97 it("follows a visit mark the quiet thread holds through", () => {
98 const projection = withItem(assistant);
99 const state = Atom.make(
100 AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }),
101 );
102 const details = createEnvironmentThreadDetailAtoms(() => state);
103 const ref = { environmentId: EnvironmentId.make("env"), threadId: v2ThreadId };
104 const registry = AtomRegistry.make();
105 const unmountQuiet = registry.mount(details.quietThreadAtom(ref));
106 const unmountVisit = registry.mount(details.lastVisitedAtAtom(ref));
107 const quietBefore = registry.get(details.quietThreadAtom(ref));
108​
109 const visited = { ...projection, thread: { ...projection.thread, lastVisitedAt: later } };
110 registry.set(
111 state,
112 AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(visited) }),
113 );
114​
115 expect(registry.get(details.quietThreadAtom(ref))).toBe(quietBefore);
116 expect(registry.get(details.lastVisitedAtAtom(ref))).toBe(later);
117 unmountQuiet();
118 unmountVisit();
119 registry.dispose();
120 });
121});
122​
123describe("quietThreadAtom", () => {
124 const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
125​
126 function setup(initial: OrchestrationV2ThreadProjection) {
127 const stateAtom = Atom.make(
128 AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(initial) }),
129 );
130 const quietAtom = createEnvironmentThreadDetailAtoms(() => stateAtom).quietThreadAtom(ref);
131 const registry = AtomRegistry.make();
132 registry.mount(quietAtom);
133 return {
134 read: () => registry.get(quietAtom),
135 push: (projection: OrchestrationV2ThreadProjection) =>
136 registry.set(
137 stateAtom,
138 AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }),
139 ),
140 };
141 }
142​
143 it("keeps the same value through streamed text", () => {
144 const thread = setup(withItem(assistant));
145 const first = thread.read();
146 thread.push(withItem({ ...assistant, text: "Hello", updatedAt: later }, later));
147 thread.push(withItem({ ...assistant, text: "Hello there", updatedAt: later }, later));
148 expect(thread.read()).toBe(first);
149 });
150​
151 it("passes a structure change on", () => {
152 const thread = setup(withItem(assistant));
153 const first = thread.read();
154 thread.push(withItem({ ...assistant, text: "Hello", updatedAt: later }, later));
155 thread.push(withItem({ ...assistant, text: "Hello", status: "completed", streaming: false }));
156 const next = thread.read();
157 expect(next).not.toBe(first);
158 expect(next?.projection.turnItems[0]).toMatchObject({ status: "completed", streaming: false });
159 });
160​
161 it("leaves out the fields that streaming moves", () => {
162 const thread = setup(withItem(assistant));
163 const projection = thread.read()?.projection;
164 expect(projection).not.toHaveProperty("updatedAt");
165 expect(projection?.thread).not.toHaveProperty("updatedAt");
166 expect(projection?.thread).not.toHaveProperty("lastVisitedAt");
167 expect(projection?.turnItems[0]).not.toHaveProperty("text");
168 expect(projection?.turnItems[0]).not.toHaveProperty("updatedAt");
169 expect(projection?.visibleTurnItems[0]?.item).not.toHaveProperty("text");
170 expect(projection?.visibleTurnItems[0]?.item).toMatchObject({
171 messageId: assistant.messageId,
172 streaming: true,
173 });
174 });
175});