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

persistenceSchema.test.ts

168 lines4.5 KB

1import { describe, expect, it } from "vitest";
2​
3import { DEFAULT_MODEL } from "./model-logic";
4import { hydratePersistedState, toPersistedState } from "./persistenceSchema";
5import type { Thread } from "./types";
6​
7describe("hydratePersistedState", () => {
8 it("returns null for invalid payloads", () => {
9 expect(hydratePersistedState('{"projects":"bad"}', false)).toBeNull();
10 expect(hydratePersistedState("not-json", false)).toBeNull();
11 });
12​
13 it("migrates the legacy default model to the current default", () => {
14 const payload = JSON.stringify({
15 projects: [
16 {
17 id: "p-1",
18 name: "Project",
19 cwd: "/tmp/project",
20 model: "gpt-5.2-codex",
21 expanded: true,
22 },
23 ],
24 threads: [
25 {
26 id: "t-1",
27 projectId: "p-1",
28 title: "Thread",
29 model: "gpt-5.2-codex",
30 messages: [
31 {
32 id: "m-1",
33 role: "assistant",
34 text: "Hello",
35 createdAt: "2026-02-08T10:00:00.000Z",
36 streaming: true,
37 },
38 ],
39 createdAt: "2026-02-08T10:00:00.000Z",
40 },
41 ],
42 activeThreadId: "t-1",
43 });
44​
45 const hydrated = hydratePersistedState(payload, true);
46 expect(hydrated).not.toBeNull();
47 expect(hydrated?.projects[0]?.model).toBe(DEFAULT_MODEL);
48 expect(hydrated?.threads[0]?.model).toBe(DEFAULT_MODEL);
49 expect(hydrated?.threads[0]?.messages[0]?.streaming).toBe(false);
50 expect(hydrated?.runtimeMode).toBe("full-access");
51 });
52​
53 it("filters unknown project references and repairs active thread", () => {
54 const payload = JSON.stringify({
55 projects: [
56 {
57 id: "p-1",
58 name: "Project",
59 cwd: "/tmp/project",
60 model: "gpt-5.3-codex",
61 expanded: false,
62 },
63 ],
64 threads: [
65 {
66 id: "t-1",
67 projectId: "p-1",
68 title: "Valid thread",
69 model: "gpt-5.3-codex",
70 messages: [],
71 createdAt: "2026-02-08T10:00:00.000Z",
72 },
73 {
74 id: "t-2",
75 projectId: "p-missing",
76 title: "Dangling thread",
77 model: "gpt-5.3-codex",
78 messages: [],
79 createdAt: "2026-02-08T10:00:00.000Z",
80 },
81 ],
82 activeThreadId: "t-2",
83 });
84​
85 const hydrated = hydratePersistedState(payload, false);
86 expect(hydrated).not.toBeNull();
87 expect(hydrated?.threads.map((thread) => thread.id)).toEqual(["t-1"]);
88 expect(hydrated?.activeThreadId).toBe("t-1");
89 expect(hydrated?.runtimeMode).toBe("full-access");
90 });
91​
92 it("hydrates runtime mode from v3 payload", () => {
93 const payload = JSON.stringify({
94 version: 3,
95 runtimeMode: "approval-required",
96 projects: [
97 {
98 id: "p-1",
99 name: "Project",
100 cwd: "/tmp/project",
101 model: "gpt-5.3-codex",
102 expanded: false,
103 },
104 ],
105 threads: [],
106 activeThreadId: null,
107 });
108​
109 const hydrated = hydratePersistedState(payload, false);
110 expect(hydrated?.runtimeMode).toBe("approval-required");
111 });
112});
113​
114describe("toPersistedState", () => {
115 it("writes v3 payload and strips non-persisted thread fields", () => {
116 const thread: Thread = {
117 id: "t-1",
118 projectId: "p-1",
119 title: "Thread",
120 model: "gpt-5.3-codex",
121 session: null,
122 messages: [
123 {
124 id: "m-1",
125 role: "user",
126 text: "Hi",
127 createdAt: "2026-02-08T10:00:00.000Z",
128 streaming: false,
129 },
130 ],
131 events: [],
132 error: "boom",
133 createdAt: "2026-02-08T10:00:00.000Z",
134 };
135​
136 const persisted = toPersistedState({
137 projects: [
138 {
139 id: "p-1",
140 name: "Project",
141 cwd: "/tmp/project",
142 model: "gpt-5.3-codex",
143 expanded: true,
144 },
145 ],
146 threads: [thread],
147 activeThreadId: "t-1",
148 runtimeMode: "full-access",
149 });
150​
151 expect(persisted.version).toBe(3);
152 expect(persisted.runtimeMode).toBe("full-access");
153 expect(persisted.threads[0]).toEqual({
154 id: "t-1",
155 projectId: "p-1",
156 title: "Thread",
157 model: "gpt-5.3-codex",
158 messages: thread.messages,
159 createdAt: thread.createdAt,
160 });
161 const persistedThread = persisted.threads[0];
162 expect(persistedThread).toBeDefined();
163 if (!persistedThread) return;
164​
165 expect("error" in persistedThread).toBe(false);
166 expect("session" in persistedThread).toBe(false);
167 });
168});