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

threadRequests.test.ts

153 lines4.6 KB

1import {
2 NodeId,
3 ProviderSessionId,
4 RuntimeRequestId,
5 TurnItemId,
6 type OrchestrationV2ThreadProjection,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9import { v2Now, v2Projection } from "./orchestrationV2TestFixtures.ts";
10import { createQuestionHistoryProjector, derivePendingThreadRequests } from "./threadRequests.ts";
11​
12const requestId = RuntimeRequestId.make("async-question");
13const nodeId = NodeId.make("async-question-node");
14const projection: OrchestrationV2ThreadProjection = {
15 ...v2Projection,
16 runtimeRequests: [
17 {
18 id: requestId,
19 nodeId,
20 providerTurnId: null,
21 nativeRequestRef: null,
22 kind: "user_input",
23 status: "pending",
24 responseCapability: { type: "message" },
25 createdAt: v2Now,
26 resolvedAt: null,
27 },
28 ],
29 turnItems: [
30 {
31 id: TurnItemId.make("async-question-item"),
32 threadId: v2Projection.thread.id,
33 runId: null,
34 nodeId,
35 providerThreadId: null,
36 providerTurnId: null,
37 nativeItemRef: null,
38 parentItemId: null,
39 ordinal: 0,
40 status: "completed",
41 title: null,
42 startedAt: v2Now,
43 completedAt: v2Now,
44 updatedAt: v2Now,
45 type: "user_input_request",
46 requestId,
47 responseMode: "message",
48 questions: [
49 {
50 id: "next",
51 header: "Next",
52 question: "What should happen next?",
53 required: true,
54 allowCustomAnswer: false,
55 options: [{ label: "Continue", value: " continue ", description: "Resume work" }],
56 },
57 ],
58 },
59 ],
60};
61​
62describe("pending v2 questions", () => {
63 it("keeps message responses available after the originating runtime exits", () => {
64 expect(projection.providerSessions).toEqual([]);
65 expect(derivePendingThreadRequests(projection).userInputs).toEqual([
66 {
67 requestId,
68 createdAt: "2026-06-20T00:00:00.000Z",
69 responseCapability: "message",
70 responseMode: "message",
71 dismissible: true,
72 questions: [
73 {
74 id: "next",
75 header: "Next",
76 question: "What should happen next?",
77 required: true,
78 allowCustomAnswer: false,
79 multiSelect: false,
80 options: [{ label: "Continue", value: " continue ", description: "Resume work" }],
81 },
82 ],
83 },
84 ]);
85 });
86​
87 it("only marks asynchronous questions as dismissible", () => {
88 const live = {
89 ...projection,
90 runtimeRequests: projection.runtimeRequests.map((request) => ({
91 ...request,
92 responseCapability: {
93 type: "live" as const,
94 providerSessionId: ProviderSessionId.make("live-session"),
95 },
96 })),
97 turnItems: projection.turnItems.map((item) =>
98 item.type === "user_input_request" ? { ...item, responseMode: undefined } : item,
99 ),
100 };
101 expect(derivePendingThreadRequests(live).userInputs[0]?.dismissible).toBe(false);
102 });
103​
104 it("removes answered requests from the composer while retaining their answers in projection data", () => {
105 const answered = {
106 ...projection,
107 runtimeRequests: projection.runtimeRequests.map((request) => ({
108 ...request,
109 status: "resolved" as const,
110 resolvedAt: v2Now,
111 answers: { next: " continue " },
112 })),
113 };
114 expect(derivePendingThreadRequests(answered).userInputs).toEqual([]);
115 expect(answered.runtimeRequests[0]?.answers).toEqual({ next: " continue " });
116 });
117});
118​
119it("restores old text answers without mutating history or replacing unchanged rows", () => {
120 const project = createQuestionHistoryProjector();
121 const item = projection.turnItems[0]!;
122 const row = {
123 item,
124 position: 0,
125 sourceItemId: item.id,
126 sourceThreadId: item.threadId,
127 visibility: "local" as const,
128 };
129 const rows = [row];
130 const pending = { visibleTurnItems: rows, runtimeRequests: projection.runtimeRequests };
131 expect(project(pending)).toBe(rows);
132 const answered = {
133 ...pending,
134 runtimeRequests: [
135 {
136 ...projection.runtimeRequests[0]!,
137 status: "resolved" as const,
138 answers: { next: "Continue" },
139 },
140 ],
141 };
142 const result = project(answered);
143 expect(result[0]?.item).toMatchObject({
144 questionAnswer: {
145 answers: { next: "Continue" },
146 attachmentsByQuestionId: {},
147 questionTextById: { next: "What should happen next?" },
148 },
149 });
150 expect(row.item).not.toHaveProperty("questionAnswer");
151 expect(project(answered)).toBe(result);
152 expect(project({ ...answered, visibleTurnItems: [...rows] })[0]).toBe(result[0]);
153});