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

threadSort.test.ts

142 lines4.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 planPinnedMove,
5 sortPinnedThreadsByOrderKey,
6 sortThreads,
7 type ThreadSortInput,
8} from "./threadSort.ts";
9​
10type TestThread = { readonly id: string } & ThreadSortInput;
11​
12function makeThread(overrides: Partial<TestThread> = {}): TestThread {
13 return {
14 id: "thread-1",
15 createdAt: "2026-03-09T10:00:00.000Z",
16 updatedAt: "2026-03-09T10:00:00.000Z",
17 messages: [],
18 latestUserMessageAt: null,
19 ...overrides,
20 };
22​
23describe("sortThreads", () => {
24 it("falls back to updatedAt and createdAt when latestUserMessageAt is invalid and there are no messages", () => {
25 const sorted = sortThreads(
26 [
27 makeThread({
28 id: "thread-1",
29 latestUserMessageAt: "not-a-date",
30 createdAt: "2026-03-09T10:00:00.000Z",
31 updatedAt: "2026-03-09T10:05:00.000Z",
32 }),
33 makeThread({
34 id: "thread-2",
35 latestUserMessageAt: "still-not-a-date",
36 createdAt: "invalid-created-at",
37 updatedAt: "invalid-updated-at",
38 }),
39 makeThread({
40 id: "thread-3",
41 latestUserMessageAt: "invalid-latest-user-message-at",
42 createdAt: "2026-03-09T10:06:00.000Z",
43 updatedAt: "invalid-updated-at",
44 }),
45 ],
46 "updated_at",
47 );
48​
49 expect(sorted.map((thread) => thread.id)).toEqual(["thread-3", "thread-1", "thread-2"]);
50 });
51​
52 it("falls back to the latest valid user message when latestUserMessageAt is invalid", () => {
53 const sorted = sortThreads(
54 [
55 makeThread({
56 id: "thread-1",
57 latestUserMessageAt: "invalid-latest-user-message-at",
58 updatedAt: "2026-03-09T10:00:00.000Z",
59 messages: [
60 { role: "user", createdAt: "2026-03-09T10:05:00.000Z" },
61 { role: "assistant", createdAt: "2026-03-09T10:30:00.000Z" },
62 { role: "user", createdAt: "2026-03-09T10:20:00.000Z" },
63 ],
64 }),
65 makeThread({
66 id: "thread-2",
67 createdAt: "2026-03-09T10:15:00.000Z",
68 updatedAt: "2026-03-09T10:15:00.000Z",
69 }),
70 ],
71 "updated_at",
72 );
73​
74 expect(sorted.map((thread) => thread.id)).toEqual(["thread-1", "thread-2"]);
75 });
76});
77​
78describe("planPinnedMove", () => {
79 it("moves a thread up with a single key write", () => {
80 const assignments = planPinnedMove({
81 orderedIds: ["a", "b", "c"],
82 keysById: new Map([
83 ["a", "f"],
84 ["b", "m"],
85 ["c", "t"],
86 ]),
87 movedId: "c",
88 direction: "up",
89 });
90 expect(assignments).toHaveLength(1);
91 expect(assignments![0]!.id).toBe("c");
92 expect(assignments![0]!.orderKey > "f" && assignments![0]!.orderKey < "m").toBe(true);
93 });
94​
95 it("returns null when the move falls off the end of the list", () => {
96 const input = {
97 orderedIds: ["a", "b"],
98 keysById: new Map([
99 ["a", "f"],
100 ["b", "m"],
101 ]),
102 };
103 expect(planPinnedMove({ ...input, movedId: "a", direction: "up" })).toBeNull();
104 expect(planPinnedMove({ ...input, movedId: "b", direction: "down" })).toBeNull();
105 });
106​
107 it("materializes keys for the whole section when a neighbor is keyless", () => {
108 const assignments = planPinnedMove({
109 orderedIds: ["a", "b", "c"],
110 keysById: new Map([
111 ["a", null],
112 ["b", "m"],
113 ["c", null],
114 ]),
115 movedId: "b",
116 direction: "up",
117 });
118 expect(assignments).not.toBeNull();
119 const keys = assignments!.map((entry) => entry.orderKey);
120 expect([...keys].sort()).toEqual(keys);
121 });
122});
123​
124describe("sortPinnedThreadsByOrderKey", () => {
125 it("breaks equal keys by id THEN environment so merged lists are stable everywhere", () => {
126 const sorted = sortPinnedThreadsByOrderKey([
127 {
128 id: "thread-1",
129 createdAt: "2026-03-09T10:00:00.000Z",
130 pinOrderKey: "m",
131 environmentId: "env-b",
132 },
133 {
134 id: "thread-1",
135 createdAt: "2026-03-09T11:00:00.000Z",
136 pinOrderKey: "m",
137 environmentId: "env-a",
138 },
139 ]);
140 expect(sorted.map((thread) => thread.environmentId)).toEqual(["env-a", "env-b"]);
141 });
142});