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

threadSort.test.ts

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