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