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

project.test.ts

288 lines11.0 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 { ProjectId } from "./baseSchemas.ts";
7import { OrchestrationProjectShell } from "./orchestrationProject.ts";
8​
9import {
10 ProjectFaviconPath,
11 ProjectIconOverride,
12 ProjectReadFileError,
13 ProjectCreatePayload,
14 ProjectUpdatePayload,
15 ProjectMutation,
16 ProjectSearchContentsError,
17 ProjectSearchContentsInput,
18 ProjectSearchEntriesError,
19 ProjectSearchEntriesInput,
20 ProjectWriteFileError,
21} from "./project.ts";
22​
23const decodeProjectCreatePayload = Schema.decodeUnknownSync(ProjectCreatePayload);
24const decodeProjectUpdatePayload = Schema.decodeUnknownSync(ProjectUpdatePayload);
25const decodeProjectMutation = Schema.decodeUnknownSync(ProjectMutation);
26const decodeSearchEntriesInput = Schema.decodeUnknownSync(ProjectSearchEntriesInput);
27const decodeSearchContentsInput = Schema.decodeUnknownSync(ProjectSearchContentsInput);
28​
29describe("project search inputs", () => {
30 it("allows an empty entries query for bounded frecency browsing", () => {
31 const decoded = decodeSearchEntriesInput({
32 cwd: "/workspace",
33 query: " ",
34 limit: 10,
35 kind: "file",
36 });
37 expect(decoded.query).toBe("");
38 });
39​
40 it("preserves whitespace in content search queries", () => {
41 const decoded = decodeSearchContentsInput({
42 cwd: "/workspace",
43 query: " foo ",
44 limit: 10,
45 caseSensitive: false,
46 wholeWord: false,
47 useRegex: false,
48 });
49 expect(decoded.query).toBe(" foo ");
50 });
51});
52​
53describe("project RPC errors", () => {
54 it("derives stable messages from structured request context while retaining causes", () => {
55 const cause = new Error("sensitive platform detail");
56 const searchError = new ProjectSearchEntriesError({
57 cwd: "/workspace",
58 queryLength: "authorization: Bearer secret-token".length,
59 limit: 20,
60 failure: "search_index_search_failed",
61 normalizedCwd: "/workspace",
62 detail: "index unavailable",
63 cause,
64 });
65 const readError = new ProjectReadFileError({
66 cwd: "/workspace",
67 relativePath: "src/index.ts",
68 failure: "operation_failed",
69 operation: "read",
70 operationPath: "/workspace/src/index.ts",
71 resolvedPath: "/workspace/src/index.ts",
72 cause,
73 });
74​
75 expect(searchError.message).toBe("Failed to search workspace entries in '/workspace'.");
76 expect(searchError.message).not.toContain(cause.message);
77 expect(searchError.normalizedCwd).toBe("/workspace");
78 expect(searchError.queryLength).toBe("authorization: Bearer secret-token".length);
79 expect(searchError).not.toHaveProperty("query");
80 expect(searchError.message).not.toMatch(/Bearer|secret-token/);
81 expect(searchError.cause).toBe(cause);
82 expect(readError.message).toBe("Failed to read workspace file 'src/index.ts' in '/workspace'.");
83 expect(readError.message).not.toContain(cause.message);
84 expect(readError.cause).toBe(cause);
85​
86 const contentSearchError = new ProjectSearchContentsError({
87 cwd: "/workspace",
88 queryLength: "authorization: Bearer secret-token".length,
89 limit: 100,
90 failure: "search_index_search_failed",
91 cause,
92 });
93 expect(contentSearchError.message).toBe("Failed to search workspace contents in '/workspace'.");
94 expect(contentSearchError.message).not.toContain(cause.message);
95 expect(contentSearchError).not.toHaveProperty("query");
96 expect(contentSearchError.cause).toBe(cause);
97 });
98​
99 it("decodes legacy message-only errors during rolling upgrades", () => {
100 const decodeSearchError = Schema.decodeUnknownSync(ProjectSearchEntriesError);
101 const decodeWriteError = Schema.decodeUnknownSync(ProjectWriteFileError);
102​
103 const searchError = decodeSearchError({
104 _tag: "ProjectSearchEntriesError",
105 message: "Legacy project search failure.",
106 query: "legacy sensitive query",
107 });
108 const writeError = decodeWriteError({
109 _tag: "ProjectWriteFileError",
110 message: "Legacy project write failure.",
111 });
112​
113 expect(searchError.message).toBe("Legacy project search failure.");
114 expect(searchError.cwd).toBeUndefined();
115 expect(searchError.queryLength).toBeUndefined();
116 expect(searchError).not.toHaveProperty("query");
117 expect(searchError.failure).toBeUndefined();
118 expect(writeError.message).toBe("Legacy project write failure.");
119 expect(writeError.relativePath).toBeUndefined();
120 expect(writeError.failure).toBeUndefined();
121 });
122});
123​
124describe("shared project payloads", () => {
125 it("preserves omitted, false, and null values through RPC envelopes", () => {
126 const create = decodeProjectCreatePayload({
127 title: " Example ",
128 workspaceRoot: "/workspace",
129 createWorkspaceRootIfMissing: false,
130 });
131 const update = decodeProjectUpdatePayload({
132 autoPull: false,
133 defaultModelSelection: null,
134 faviconPath: null,
135 });
136 const envelope = { commandId: "command", projectId: "project" };
137 expect(decodeProjectMutation({ type: "project.create", ...envelope, ...create })).toEqual({
138 type: "project.create",
139 ...envelope,
140 title: "Example",
141 workspaceRoot: "/workspace",
142 createWorkspaceRootIfMissing: false,
143 });
144 expect(decodeProjectMutation({ type: "project.update", ...envelope, ...update })).toEqual({
145 type: "project.update",
146 ...envelope,
147 autoPull: false,
148 defaultModelSelection: null,
149 faviconPath: null,
150 });
151 expect(Object.hasOwn(create, "scripts")).toBe(false);
152 expect(Object.hasOwn(update, "title")).toBe(false);
153 // Internal RPC callers may explicitly supply undefined, as before the extraction.
154 expect(
155 decodeProjectMutation({ type: "project.update", ...envelope, title: undefined }),
156 ).toHaveProperty("title", undefined);
157 });
158});
159​
160const decodeFaviconPath = Schema.decodeUnknownEffect(ProjectFaviconPath);
161​
162effectIt.effect("project favicon paths accept only supported image files", () =>
163 Effect.gen(function* () {
164 assert.strictEqual(yield* decodeFaviconPath("brand/icon.svg"), "brand/icon.svg");
165 assert.strictEqual((yield* Effect.exit(decodeFaviconPath(".env")))._tag, "Failure");
166 }),
167);
168​
169const decodeProjectUpdateEffect = Schema.decodeUnknownEffect(ProjectUpdatePayload);
170const decodeUpdateIcon = (projectIcon: unknown) =>
171 Effect.map(decodeProjectUpdateEffect({ projectIcon }), (update) => update.projectIcon);
172​
173effectIt.effect("project icon overrides accept Lucide icons, colors, and emoji", () =>
174 Effect.gen(function* () {
175 const lucide = { kind: "lucide", name: "alarm-clock", color: "violet" } as const;
176 assert.deepEqual(yield* decodeUpdateIcon(lucide), lucide);
177 const emoji = { kind: "emoji", emoji: "👩🏽‍💻" } as const;
178 assert.deepEqual(yield* decodeUpdateIcon(emoji), emoji);
179 const invalid = yield* Effect.exit(
180 decodeUpdateIcon({ kind: "lucide", name: "Alarm Clock", color: "ultraviolet" }),
181 );
182 assert.strictEqual(invalid._tag, "Failure");
183 }),
184);
185​
186effectIt.effect("project monograms validate text and palette colors", () =>
187 Effect.gen(function* () {
188 for (const text of ["A", "T3", "É", "文書", "कि", "किखि", "e\u0301"]) {
189 assert.deepEqual(yield* decodeUpdateIcon({ kind: "monogram", color: "violet", text }), {
190 kind: "monogram",
191 text,
192 color: "violet",
193 });
194 }
195 for (const projectIcon of [
196 { kind: "monogram", text: "", color: "blue" },
197 { kind: "monogram", text: "\u0301", color: "blue" },
198 { kind: "monogram", text: "A B", color: "blue" },
199 { kind: "monogram", text: "🚀", color: "blue" },
200 { kind: "monogram", text: "T3", color: "ultraviolet" },
201 ]) {
202 assert.strictEqual((yield* Effect.exit(decodeUpdateIcon(projectIcon)))._tag, "Failure");
203 }
204 }),
205);
206​
207const decodeProjectIcon = Schema.decodeUnknownEffect(ProjectIconOverride);
208const encodeProjectIcon = Schema.encodeEffect(ProjectIconOverride);
209​
210// Pre-monogram clients reject unknown variants; nightly clients additionally validate monogram.
211const decodeOldIcon = Schema.decodeUnknownEffect(
212 Schema.Union([
213 Schema.Struct({ kind: Schema.Literal("lucide"), name: Schema.String, color: Schema.String }),
214 Schema.Struct({ kind: Schema.Literal("emoji"), emoji: Schema.String }),
215 ]),
216);
217const decodeNightlyIcon = Schema.decodeUnknownEffect(
218 Schema.Union([
219 Schema.Struct({
220 kind: Schema.Literal("lucide"),
221 name: Schema.String,
222 color: Schema.String,
223 // Fail if this field is ever sent; old validators must never see the new text.
224 monogram: Schema.optional(Schema.Never),
225 }),
226 Schema.Struct({ kind: Schema.Literal("emoji"), emoji: Schema.String }),
227 ]),
228);
229​
230effectIt.effect("sends monograms as fallback icons that old and nightly clients can decode", () =>
231 Effect.gen(function* () {
232 const fallback = { kind: "lucide", name: "folder-code", color: "violet" } as const;
233 for (const text of ["T3", "क्ष्म", "e\u0301"]) {
234 const monogram = { kind: "monogram", text, color: "violet" } as const;
235 const wire = yield* encodeProjectIcon(monogram);
236 assert.deepEqual(wire, { ...fallback, monogramText: text });
237 assert.deepEqual(yield* decodeOldIcon(wire), fallback);
238 assert.deepEqual(yield* decodeNightlyIcon(wire), fallback);
239 assert.deepEqual(yield* decodeProjectIcon(wire), monogram);
240 assert.deepEqual(yield* decodeProjectIcon(monogram), monogram);
241 assert.deepEqual(yield* decodeProjectIcon({ ...fallback, monogram: text }), monogram);
242 }
243 for (const icon of [
244 { kind: "lucide", name: "alarm-clock", color: "blue" },
245 { kind: "emoji", emoji: "🚀" },
246 ] as const) {
247 assert.deepEqual(yield* decodeProjectIcon(icon), icon);
248 assert.deepEqual(yield* encodeProjectIcon(icon), icon);
249 }
250 }),
251);
252​
253const encodeProjectShell = Schema.encodeEffect(OrchestrationProjectShell);
254const encodeProjectUpdate = Schema.encodeEffect(ProjectUpdatePayload);
255const decodeLegacyShell = Schema.decodeUnknownEffect(
256 Schema.Struct({
257 ...OrchestrationProjectShell.fields,
258 projectIcon: Schema.optional(
259 Schema.NullOr(
260 Schema.Struct({
261 kind: Schema.Literal("lucide"),
262 name: Schema.String,
263 color: Schema.String,
264 }),
265 ),
266 ),
267 }),
268);
269​
270effectIt.effect("encodes compatible icons inside snapshots and project updates", () =>
271 Effect.gen(function* () {
272 const projectIcon = { kind: "monogram", text: "क्ष्म", color: "violet" } as const;
273 const shell = yield* encodeProjectShell({
274 id: ProjectId.make("monogram"),
275 title: "Monogram",
276 workspaceRoot: "/tmp/monogram",
277 defaultModelSelection: null,
278 scripts: [],
279 projectIcon,
280 createdAt: "2026-01-01T00:00:00.000Z",
281 updatedAt: "2026-01-01T00:00:00.000Z",
282 });
283 const fallback = { kind: "lucide", name: "folder-code", color: "violet" } as const;
284 assert.deepEqual((yield* decodeLegacyShell(shell)).projectIcon, fallback);
285 const update = yield* encodeProjectUpdate({ projectIcon });
286 assert.deepEqual(yield* decodeNightlyIcon(update.projectIcon), fallback);
287 }),
288);