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

threadSort.test.ts

243 lines6.7 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 DEFAULT_RUNTIME_MODE,
4 EnvironmentId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8} from "@t3tools/contracts";
9import type { Thread } from "../types";
10import { getLatestThreadForProject, sortThreads } from "./threadSort";
11​
12const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
13const PROJECT_ID = ProjectId.make("project-1");
14​
15function makeThread(overrides: Partial<Thread> = {}): Thread {
16 return {
17 id: ThreadId.make("thread-1"),
18 environmentId: LOCAL_ENVIRONMENT_ID,
19 projectId: PROJECT_ID,
20 title: "Thread",
21 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
22 runtimeMode: DEFAULT_RUNTIME_MODE,
23 interactionMode: "default",
24 session: null,
25 messages: [],
26 proposedPlans: [],
27 createdAt: "2026-03-09T10:00:00.000Z",
28 archivedAt: null,
29 deletedAt: null,
30 updatedAt: "2026-03-09T10:00:00.000Z",
31 latestTurn: null,
32 branch: null,
33 worktreePath: null,
34 checkpoints: [],
35 activities: [],
36 ...overrides,
37 };
39​
40describe("sortThreads", () => {
41 it("sorts threads by the latest user message in recency mode", () => {
42 const sorted = sortThreads(
43 [
44 makeThread({
45 id: ThreadId.make("thread-1"),
46 updatedAt: "2026-03-09T10:10:00.000Z",
47 messages: [
48 {
49 id: "message-1" as never,
50 role: "user",
51 text: "older",
52 turnId: null,
53 createdAt: "2026-03-09T10:01:00.000Z",
54 updatedAt: "2026-03-09T10:01:00.000Z",
55 streaming: false,
56 },
57 ],
58 }),
59 makeThread({
60 id: ThreadId.make("thread-2"),
61 createdAt: "2026-03-09T10:05:00.000Z",
62 updatedAt: "2026-03-09T10:05:00.000Z",
63 messages: [
64 {
65 id: "message-2" as never,
66 role: "user",
67 text: "newer",
68 turnId: null,
69 createdAt: "2026-03-09T10:06:00.000Z",
70 updatedAt: "2026-03-09T10:06:00.000Z",
71 streaming: false,
72 },
73 ],
74 }),
75 ],
76 "updated_at",
77 );
78​
79 expect(sorted.map((thread) => thread.id)).toEqual([
80 ThreadId.make("thread-2"),
81 ThreadId.make("thread-1"),
82 ]);
83 });
84​
85 it("falls back to thread timestamps when there is no user message", () => {
86 const sorted = sortThreads(
87 [
88 makeThread({
89 id: ThreadId.make("thread-1"),
90 updatedAt: "2026-03-09T10:01:00.000Z",
91 messages: [
92 {
93 id: "message-1" as never,
94 role: "assistant",
95 text: "assistant only",
96 turnId: null,
97 createdAt: "2026-03-09T10:02:00.000Z",
98 updatedAt: "2026-03-09T10:02:00.000Z",
99 streaming: false,
100 },
101 ],
102 }),
103 makeThread({
104 id: ThreadId.make("thread-2"),
105 createdAt: "2026-03-09T10:05:00.000Z",
106 updatedAt: "2026-03-09T10:05:00.000Z",
107 messages: [],
108 }),
109 ],
110 "updated_at",
111 );
112​
113 expect(sorted.map((thread) => thread.id)).toEqual([
114 ThreadId.make("thread-2"),
115 ThreadId.make("thread-1"),
116 ]);
117 });
118​
119 it("falls back to createdAt when updatedAt is invalid", () => {
120 const sorted = sortThreads(
121 [
122 makeThread({
123 id: ThreadId.make("thread-1"),
124 createdAt: "2026-03-09T10:00:00.000Z",
125 updatedAt: "invalid-date" as never,
126 messages: [],
127 }),
128 makeThread({
129 id: ThreadId.make("thread-2"),
130 createdAt: "2026-03-09T09:00:00.000Z",
131 updatedAt: "2026-03-09T09:30:00.000Z",
132 messages: [],
133 }),
134 ],
135 "updated_at",
136 );
137​
138 expect(sorted.map((thread) => thread.id)).toEqual([
139 ThreadId.make("thread-1"),
140 ThreadId.make("thread-2"),
141 ]);
142 });
143​
144 it("falls back to id ordering when threads have no sortable timestamps", () => {
145 const sorted = sortThreads(
146 [
147 makeThread({
148 id: ThreadId.make("thread-1"),
149 createdAt: "invalid-created-at" as never,
150 updatedAt: "invalid-updated-at" as never,
151 messages: [],
152 }),
153 makeThread({
154 id: ThreadId.make("thread-2"),
155 createdAt: "invalid-created-at" as never,
156 updatedAt: "invalid-updated-at" as never,
157 messages: [],
158 }),
159 ],
160 "updated_at",
161 );
162​
163 expect(sorted.map((thread) => thread.id)).toEqual([
164 ThreadId.make("thread-2"),
165 ThreadId.make("thread-1"),
166 ]);
167 });
168​
169 it("can sort threads by createdAt when configured", () => {
170 const sorted = sortThreads(
171 [
172 makeThread({
173 id: ThreadId.make("thread-1"),
174 createdAt: "2026-03-09T10:05:00.000Z",
175 updatedAt: "2026-03-09T10:05:00.000Z",
176 }),
177 makeThread({
178 id: ThreadId.make("thread-2"),
179 createdAt: "2026-03-09T10:00:00.000Z",
180 updatedAt: "2026-03-09T10:10:00.000Z",
181 }),
182 ],
183 "created_at",
184 );
185​
186 expect(sorted.map((thread) => thread.id)).toEqual([
187 ThreadId.make("thread-1"),
188 ThreadId.make("thread-2"),
189 ]);
190 });
191​
192 it("uses updatedAt as a fallback for created_at sorting when createdAt is invalid", () => {
193 const sorted = sortThreads(
194 [
195 makeThread({
196 id: ThreadId.make("thread-1"),
197 createdAt: "invalid-date" as never,
198 updatedAt: "2026-03-09T10:05:00.000Z",
199 }),
200 makeThread({
201 id: ThreadId.make("thread-2"),
202 createdAt: "2026-03-09T10:00:00.000Z",
203 updatedAt: "2026-03-09T10:10:00.000Z",
204 }),
205 ],
206 "created_at",
207 );
208​
209 expect(sorted.map((thread) => thread.id)).toEqual([
210 ThreadId.make("thread-1"),
211 ThreadId.make("thread-2"),
212 ]);
213 });
214​
215 it("returns the latest active thread for a project", () => {
216 const latestThread = getLatestThreadForProject(
217 [
218 makeThread({
219 id: ThreadId.make("thread-1"),
220 createdAt: "2026-03-09T10:00:00.000Z",
221 updatedAt: "2026-03-09T10:01:00.000Z",
222 archivedAt: null,
223 }),
224 makeThread({
225 id: ThreadId.make("thread-2"),
226 createdAt: "2026-03-09T10:05:00.000Z",
227 updatedAt: "2026-03-09T10:10:00.000Z",
228 archivedAt: "2026-03-10T00:00:00.000Z",
229 }),
230 makeThread({
231 id: ThreadId.make("thread-3"),
232 createdAt: "2026-03-09T10:06:00.000Z",
233 updatedAt: "2026-03-09T10:06:00.000Z",
234 archivedAt: null,
235 }),
236 ],
237 PROJECT_ID,
238 "updated_at",
239 );
240​
241 expect(latestThread?.id).toBe(ThreadId.make("thread-3"));
242 });
243});