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

store.test.ts

84 lines2.3 KB

1import { ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3​
4import { markThreadUnread, type AppState } from "./store";
5import type { Thread } from "./types";
6​
7function makeThread(overrides: Partial<Thread> = {}): Thread {
8 return {
9 id: ThreadId.makeUnsafe("thread-1"),
10 codexThreadId: null,
11 projectId: ProjectId.makeUnsafe("project-1"),
12 title: "Thread",
13 model: "gpt-5-codex",
14 session: null,
15 messages: [],
16 turnDiffSummaries: [],
17 activities: [],
18 error: null,
19 createdAt: "2026-02-13T00:00:00.000Z",
20 latestTurn: null,
21 branch: null,
22 worktreePath: null,
23 ...overrides,
24 };
26​
27function makeState(thread: Thread): AppState {
28 return {
29 projects: [
30 {
31 id: ProjectId.makeUnsafe("project-1"),
32 name: "Project",
33 cwd: "/tmp/project",
34 model: "gpt-5-codex",
35 expanded: true,
36 scripts: [],
37 },
38 ],
39 threads: [thread],
40 threadsHydrated: true,
41 runtimeMode: "full-access",
42 };
44​
45describe("store pure functions", () => {
46 it("markThreadUnread moves lastVisitedAt before completion for a completed thread", () => {
47 const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
48 const initialState = makeState(
49 makeThread({
50 latestTurn: {
51 turnId: TurnId.makeUnsafe("turn-1"),
52 state: "completed",
53 requestedAt: "2026-02-25T12:28:00.000Z",
54 startedAt: "2026-02-25T12:28:30.000Z",
55 completedAt: latestTurnCompletedAt,
56 assistantMessageId: null,
57 },
58 lastVisitedAt: "2026-02-25T12:35:00.000Z",
59 }),
60 );
61​
62 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
63​
64 const updatedThread = next.threads[0];
65 expect(updatedThread).toBeDefined();
66 expect(updatedThread?.lastVisitedAt).toBe("2026-02-25T12:29:59.999Z");
67 expect(Date.parse(updatedThread?.lastVisitedAt ?? "")).toBeLessThan(
68 Date.parse(latestTurnCompletedAt),
69 );
70 });
71​
72 it("markThreadUnread does not change a thread without a completed turn", () => {
73 const initialState = makeState(
74 makeThread({
75 latestTurn: null,
76 lastVisitedAt: "2026-02-25T12:35:00.000Z",
77 }),
78 );
79​
80 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
81​
82 expect(next).toEqual(initialState);
83 });
84});