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

threadSort.test.ts

187 lines5.2 KB

1import { describe, expect, it } from "vitest";
2import { DEFAULT_RUNTIME_MODE, EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
3import type { Thread } from "../types";
4import { getLatestThreadForProject, sortThreads } from "./threadSort";
5​
6const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
7const PROJECT_ID = ProjectId.make("project-1");
8​
9function makeThread(overrides: Partial<Thread> = {}): Thread {
10 return {
11 id: ThreadId.make("thread-1"),
12 environmentId: LOCAL_ENVIRONMENT_ID,
13 codexThreadId: null,
14 projectId: PROJECT_ID,
15 title: "Thread",
16 modelSelection: { provider: "codex", model: "gpt-5.4" },
17 runtimeMode: DEFAULT_RUNTIME_MODE,
18 interactionMode: "default",
19 session: null,
20 messages: [],
21 proposedPlans: [],
22 error: null,
23 createdAt: "2026-03-09T10:00:00.000Z",
24 archivedAt: null,
25 updatedAt: "2026-03-09T10:00:00.000Z",
26 latestTurn: null,
27 branch: null,
28 worktreePath: null,
29 turnDiffSummaries: [],
30 activities: [],
31 ...overrides,
32 };
34​
35describe("sortThreads", () => {
36 it("sorts threads by the latest user message in recency mode", () => {
37 const sorted = sortThreads(
38 [
39 makeThread({
40 id: ThreadId.make("thread-1"),
41 updatedAt: "2026-03-09T10:10:00.000Z",
42 messages: [
43 {
44 id: "message-1" as never,
45 role: "user",
46 text: "older",
47 createdAt: "2026-03-09T10:01:00.000Z",
48 streaming: false,
49 completedAt: "2026-03-09T10:01:00.000Z",
50 },
51 ],
52 }),
53 makeThread({
54 id: ThreadId.make("thread-2"),
55 createdAt: "2026-03-09T10:05:00.000Z",
56 updatedAt: "2026-03-09T10:05:00.000Z",
57 messages: [
58 {
59 id: "message-2" as never,
60 role: "user",
61 text: "newer",
62 createdAt: "2026-03-09T10:06:00.000Z",
63 streaming: false,
64 completedAt: "2026-03-09T10:06:00.000Z",
65 },
66 ],
67 }),
68 ],
69 "updated_at",
70 );
71​
72 expect(sorted.map((thread) => thread.id)).toEqual([
73 ThreadId.make("thread-2"),
74 ThreadId.make("thread-1"),
75 ]);
76 });
77​
78 it("falls back to thread timestamps when there is no user message", () => {
79 const sorted = sortThreads(
80 [
81 makeThread({
82 id: ThreadId.make("thread-1"),
83 updatedAt: "2026-03-09T10:01:00.000Z",
84 messages: [
85 {
86 id: "message-1" as never,
87 role: "assistant",
88 text: "assistant only",
89 createdAt: "2026-03-09T10:02:00.000Z",
90 streaming: false,
91 completedAt: "2026-03-09T10:02:00.000Z",
92 },
93 ],
94 }),
95 makeThread({
96 id: ThreadId.make("thread-2"),
97 createdAt: "2026-03-09T10:05:00.000Z",
98 updatedAt: "2026-03-09T10:05:00.000Z",
99 messages: [],
100 }),
101 ],
102 "updated_at",
103 );
104​
105 expect(sorted.map((thread) => thread.id)).toEqual([
106 ThreadId.make("thread-2"),
107 ThreadId.make("thread-1"),
108 ]);
109 });
110​
111 it("falls back to id ordering when threads have no sortable timestamps", () => {
112 const sorted = sortThreads(
113 [
114 makeThread({
115 id: ThreadId.make("thread-1"),
116 createdAt: "" as never,
117 updatedAt: undefined,
118 messages: [],
119 }),
120 makeThread({
121 id: ThreadId.make("thread-2"),
122 createdAt: "" as never,
123 updatedAt: undefined,
124 messages: [],
125 }),
126 ],
127 "updated_at",
128 );
129​
130 expect(sorted.map((thread) => thread.id)).toEqual([
131 ThreadId.make("thread-2"),
132 ThreadId.make("thread-1"),
133 ]);
134 });
135​
136 it("can sort threads by createdAt when configured", () => {
137 const sorted = sortThreads(
138 [
139 makeThread({
140 id: ThreadId.make("thread-1"),
141 createdAt: "2026-03-09T10:05:00.000Z",
142 updatedAt: "2026-03-09T10:05:00.000Z",
143 }),
144 makeThread({
145 id: ThreadId.make("thread-2"),
146 createdAt: "2026-03-09T10:00:00.000Z",
147 updatedAt: "2026-03-09T10:10:00.000Z",
148 }),
149 ],
150 "created_at",
151 );
152​
153 expect(sorted.map((thread) => thread.id)).toEqual([
154 ThreadId.make("thread-1"),
155 ThreadId.make("thread-2"),
156 ]);
157 });
158​
159 it("returns the latest active thread for a project", () => {
160 const latestThread = getLatestThreadForProject(
161 [
162 makeThread({
163 id: ThreadId.make("thread-1"),
164 createdAt: "2026-03-09T10:00:00.000Z",
165 updatedAt: "2026-03-09T10:01:00.000Z",
166 archivedAt: null,
167 }),
168 makeThread({
169 id: ThreadId.make("thread-2"),
170 createdAt: "2026-03-09T10:05:00.000Z",
171 updatedAt: "2026-03-09T10:10:00.000Z",
172 archivedAt: "2026-03-10T00:00:00.000Z",
173 }),
174 makeThread({
175 id: ThreadId.make("thread-3"),
176 createdAt: "2026-03-09T10:06:00.000Z",
177 updatedAt: "2026-03-09T10:06:00.000Z",
178 archivedAt: null,
179 }),
180 ],
181 PROJECT_ID,
182 "updated_at",
183 );
184​
185 expect(latestThread?.id).toBe(ThreadId.make("thread-3"));
186 });
187});