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 { makeThreadFixture, type ThreadFixtureOverrides } from "../test-fixtures";
11import { 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 });
39}
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});