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