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

threadSort.test.ts

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