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

project.test.ts

270 lines10.5 KB

1import { assert, it as effectIt } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4import { describe, expect, it } from "vite-plus/test";
5​
6import { CommandId, ProjectId, ThreadId } from "./baseSchemas.ts";
7import { VcsStatusInput } from "./git.ts";
8import { OrchestrationProjectShell } from "./orchestrationProject.ts";
9import {
10 TaskFolderRepository,
11 WorkspaceAddTaskFolderRepositoryInput,
12 WorkspaceListFoldersResult,
13} from "./workspaceFolders.ts";
14​
15import {
16 ProjectFaviconPath,
17 ReceivedProjectIcon,
18 StoredProjectIcon,
19 ProjectReadFileError,
20 ProjectReadFileInput,
21 ProjectReadFileResult,
22 ProjectCreatePayload,
23 ProjectUpdatePayload,
24 ProjectMutation,
25 ProjectTrackedFolders,
26 ProjectSearchContentsError,
27 ProjectSearchEntriesError,
28 ProjectWriteFileError,
29 ProjectWriteFileInput,
30 ProjectWriteFileResult,
31} from "./project.ts";
32​
33const decodeProjectCreatePayload = Schema.decodeUnknownSync(ProjectCreatePayload);
34const decodeProjectUpdatePayload = Schema.decodeUnknownSync(ProjectUpdatePayload);
35const decodeProjectMutation = Schema.decodeUnknownSync(ProjectMutation);
36const decodeProjectReadFileInput = Schema.decodeUnknownSync(ProjectReadFileInput);
37const decodeProjectWriteFileInput = Schema.decodeUnknownSync(ProjectWriteFileInput);
38​
39describe("project RPC errors", () => {
40 it("accepts deeply nested file paths longer than 512 characters", () => {
41 const relativePath = ["a".repeat(200), "b".repeat(200), "c".repeat(110), "demo.txt"].join("/");
42 expect(relativePath).toHaveLength(521);
43 const cwd = "/workspace";
44 expect(decodeProjectReadFileInput({ cwd, relativePath })).toEqual({
45 cwd,
46 relativePath,
47 });
48 expect(decodeProjectWriteFileInput({ cwd, relativePath, contents: "edited" })).toEqual({
49 cwd,
50 relativePath,
51 contents: "edited",
52 });
53 });
54​
55 it("keeps whitespace in file paths across the wire", () => {
56 const roundTrip = <S extends Schema.Codec<unknown, unknown>>(schema: S, value: S["Type"]) =>
57 Schema.decodeUnknownSync(schema)(Schema.encodeSync(schema)(value));
58 const storedRepositories = Schema.fromJsonString(Schema.Array(TaskFolderRepository));
59​
60 for (const relativePath of [" ", " report.txt", "report.txt\t"]) {
61 const cwd = "/workspace";
62 const resolvedPath = `${cwd}/${relativePath}`;
63 expect(roundTrip(ProjectReadFileInput, { cwd, relativePath }).relativePath).toBe(
64 relativePath,
65 );
66 expect(
67 roundTrip(ProjectWriteFileInput, { cwd, relativePath, contents: "" }).relativePath,
68 ).toBe(relativePath);
69 expect(
70 roundTrip(ProjectReadFileResult, {
71 relativePath,
72 contents: "",
73 byteLength: 0,
74 truncated: false,
75 }).relativePath,
76 ).toBe(relativePath);
77 expect(roundTrip(ProjectWriteFileResult, { relativePath }).relativePath).toBe(relativePath);
78 const folderPath = `${cwd}/${relativePath}`;
79 const folder = { path: folderPath, name: "deps" };
80 expect(roundTrip(ProjectTrackedFolders, [folder])).toEqual([folder]);
81 const listing = { folders: [{ ...folder, state: "plain" as const }] };
82 expect(roundTrip(WorkspaceListFoldersResult, listing)).toEqual(listing);
83 const addRepository = { threadId: ThreadId.make("thread-1"), repositoryPath: folderPath };
84 expect(roundTrip(WorkspaceAddTaskFolderRepositoryInput, addRepository)).toEqual(
85 addRepository,
86 );
87 const repositories = [
88 { repositoryPath: folderPath, worktreePath: `/task/${relativePath}`, name: folder.name },
89 ];
90 expect(roundTrip(storedRepositories, repositories)).toEqual(repositories);
91 const status = { cwd: repositories[0]!.worktreePath };
92 expect(roundTrip(VcsStatusInput, status)).toEqual(status);
93 const untrack = {
94 type: "project.update" as const,
95 commandId: CommandId.make("untrack-folder"),
96 projectId: ProjectId.make("project-1"),
97 untrackFolders: [folderPath],
98 };
99 expect(roundTrip(ProjectMutation, untrack)).toEqual(untrack);
100 for (const FileError of [ProjectReadFileError, ProjectWriteFileError]) {
101 const error = new FileError({
102 cwd,
103 relativePath,
104 failure: "operation_failed",
105 resolvedPath,
106 operationPath: resolvedPath,
107 });
108 expect(roundTrip(FileError, error)).toMatchObject({
109 relativePath,
110 resolvedPath,
111 operationPath: resolvedPath,
112 });
113 }
114 }
115 });
116​
117 it("derives stable messages from structured request context while retaining causes", () => {
118 const cause = new Error("sensitive platform detail");
119 const searchError = new ProjectSearchEntriesError({
120 cwd: "/workspace",
121 queryLength: "authorization: Bearer secret-token".length,
122 limit: 20,
123 failure: "search_index_search_failed",
124 normalizedCwd: "/workspace",
125 detail: "index unavailable",
126 cause,
127 });
128 const readError = new ProjectReadFileError({
129 cwd: "/workspace",
130 relativePath: "src/index.ts",
131 failure: "operation_failed",
132 operation: "read",
133 operationPath: "/workspace/src/index.ts",
134 resolvedPath: "/workspace/src/index.ts",
135 cause,
136 });
137​
138 expect(searchError.message).toBe("Failed to search workspace entries in '/workspace'.");
139 expect(searchError.message).not.toContain(cause.message);
140 expect(searchError.normalizedCwd).toBe("/workspace");
141 expect(searchError.queryLength).toBe("authorization: Bearer secret-token".length);
142 expect(searchError).not.toHaveProperty("query");
143 expect(searchError.message).not.toMatch(/Bearer|secret-token/);
144 expect(searchError.cause).toBe(cause);
145 expect(readError.message).toBe("Failed to read workspace file 'src/index.ts' in '/workspace'.");
146 expect(readError.message).not.toContain(cause.message);
147 expect(readError.cause).toBe(cause);
148​
149 const contentSearchError = new ProjectSearchContentsError({
150 cwd: "/workspace",
151 queryLength: "authorization: Bearer secret-token".length,
152 limit: 100,
153 failure: "search_index_search_failed",
154 cause,
155 });
156 expect(contentSearchError.message).toBe("Failed to search workspace contents in '/workspace'.");
157 expect(contentSearchError.message).not.toContain(cause.message);
158 expect(contentSearchError).not.toHaveProperty("query");
159 expect(contentSearchError.cause).toBe(cause);
160 });
161​
162 it("decodes legacy message-only errors during rolling upgrades", () => {
163 const decodeSearchError = Schema.decodeUnknownSync(ProjectSearchEntriesError);
164 const decodeWriteError = Schema.decodeUnknownSync(ProjectWriteFileError);
165​
166 const searchError = decodeSearchError({
167 _tag: "ProjectSearchEntriesError",
168 message: "Legacy project search failure.",
169 query: "legacy sensitive query",
170 });
171 const writeError = decodeWriteError({
172 _tag: "ProjectWriteFileError",
173 message: "Legacy project write failure.",
174 });
175​
176 expect(searchError.message).toBe("Legacy project search failure.");
177 expect(searchError.cwd).toBeUndefined();
178 expect(searchError.queryLength).toBeUndefined();
179 expect(searchError).not.toHaveProperty("query");
180 expect(searchError.failure).toBeUndefined();
181 expect(writeError.message).toBe("Legacy project write failure.");
182 expect(writeError.relativePath).toBeUndefined();
183 expect(writeError.failure).toBeUndefined();
184 });
185});
186​
187describe("shared project payloads", () => {
188 it("preserves omitted, false, and null values through RPC envelopes", () => {
189 const create = decodeProjectCreatePayload({
190 title: " Example ",
191 workspaceRoot: "/workspace",
192 createWorkspaceRootIfMissing: false,
193 });
194 const update = decodeProjectUpdatePayload({
195 autoPull: false,
196 defaultModelSelection: null,
197 faviconPath: null,
198 });
199 const envelope = { commandId: "command", projectId: "project" };
200 expect(decodeProjectMutation({ type: "project.create", ...envelope, ...create })).toEqual({
201 type: "project.create",
202 ...envelope,
203 title: "Example",
204 workspaceRoot: "/workspace",
205 createWorkspaceRootIfMissing: false,
206 });
207 expect(decodeProjectMutation({ type: "project.update", ...envelope, ...update })).toEqual({
208 type: "project.update",
209 ...envelope,
210 autoPull: false,
211 defaultModelSelection: null,
212 faviconPath: null,
213 });
214 expect(Object.hasOwn(create, "scripts")).toBe(false);
215 expect(Object.hasOwn(update, "title")).toBe(false);
216 expect(
217 decodeProjectMutation({ type: "project.update", ...envelope, title: undefined }),
218 ).toHaveProperty("title", undefined);
219 });
220});
221​
222const decodeFaviconPath = Schema.decodeUnknownEffect(ProjectFaviconPath);
223​
224effectIt.effect("project favicon paths accept only supported image files", () =>
225 Effect.gen(function* () {
226 assert.strictEqual(yield* decodeFaviconPath("brand/icon.svg"), "brand/icon.svg");
227 assert.strictEqual(
228 yield* decodeFaviconPath("Resources/AppIcon.icns"),
229 "Resources/AppIcon.icns",
230 );
231 assert.strictEqual((yield* Effect.exit(decodeFaviconPath(".env")))._tag, "Failure");
232 }),
233);
234​
235const decodeStoredIcon = Schema.decodeUnknownEffect(StoredProjectIcon);
236const decodeReceivedIcon = Schema.decodeUnknownEffect(ReceivedProjectIcon);
237const decodeProjectShell = Schema.decodeUnknownEffect(OrchestrationProjectShell);
238​
239effectIt.effect("reads monograms stored in the pre-v2 fallback shape", () =>
240 Effect.gen(function* () {
241 const monogram = { kind: "monogram", text: "T3", color: "violet" } as const;
242 const fallback = { kind: "lucide", name: "folder-code", color: "violet" } as const;
243 for (const legacy of [
244 { ...fallback, monogramText: "T3" },
245 { ...fallback, monogram: "T3" },
246 ]) {
247 assert.deepEqual(yield* decodeStoredIcon(legacy), monogram);
248 assert.deepEqual(yield* decodeReceivedIcon(legacy), monogram);
249 }
250 }),
251);
252​
253effectIt.effect("an icon kind from a newer server shows the default icon", () =>
254 Effect.gen(function* () {
255 assert.isNull(yield* decodeReceivedIcon({ kind: "image", url: "https://example.com/a.png" }));
256 const shell = yield* decodeProjectShell({
257 id: "project-1",
258 title: "Project",
259 workspaceRoot: "/tmp/project",
260 defaultModelSelection: null,
261 scripts: [],
262 projectIcon: { kind: "image", url: "https://example.com/a.png" },
263 createdAt: "2026-01-01T00:00:00.000Z",
264 updatedAt: "2026-01-01T00:00:00.000Z",
265 });
266 assert.isNull(shell.projectIcon);
267 const broken = yield* Effect.exit(decodeReceivedIcon({ kind: "emoji" }));
268 assert.strictEqual(broken._tag, "Failure");
269 }),
270);