Open History to see the last commit
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 { OrchestrationProjectShell } from "./orchestrationProject.ts";
7
8import {
9 ProjectFaviconPath,
10 ReceivedProjectIcon,
11 StoredProjectIcon,
12 ProjectReadFileError,
13 ProjectCreatePayload,
14 ProjectUpdatePayload,
15 ProjectMutation,
16 ProjectSearchContentsError,
17 ProjectSearchEntriesError,
18 ProjectWriteFileError,
19} from "./project.ts";
20
21const decodeProjectCreatePayload = Schema.decodeUnknownSync(ProjectCreatePayload);
22const decodeProjectUpdatePayload = Schema.decodeUnknownSync(ProjectUpdatePayload);
23const decodeProjectMutation = Schema.decodeUnknownSync(ProjectMutation);
24
25describe("project RPC errors", () => {
26 it("derives stable messages from structured request context while retaining causes", () => {
27 const cause = new Error("sensitive platform detail");
28 const searchError = new ProjectSearchEntriesError({
29 cwd: "/workspace",
30 queryLength: "authorization: Bearer secret-token".length,
31 limit: 20,
32 failure: "search_index_search_failed",
33 normalizedCwd: "/workspace",
34 detail: "index unavailable",
35 cause,
36 });
37 const readError = new ProjectReadFileError({
38 cwd: "/workspace",
39 relativePath: "src/index.ts",
40 failure: "operation_failed",
41 operation: "read",
42 operationPath: "/workspace/src/index.ts",
43 resolvedPath: "/workspace/src/index.ts",
44 cause,
45 });
46
47 expect(searchError.message).toBe("Failed to search workspace entries in '/workspace'.");
48 expect(searchError.message).not.toContain(cause.message);
49 expect(searchError.normalizedCwd).toBe("/workspace");
50 expect(searchError.queryLength).toBe("authorization: Bearer secret-token".length);
51 expect(searchError).not.toHaveProperty("query");
52 expect(searchError.message).not.toMatch(/Bearer|secret-token/);
53 expect(searchError.cause).toBe(cause);
54 expect(readError.message).toBe("Failed to read workspace file 'src/index.ts' in '/workspace'.");
55 expect(readError.message).not.toContain(cause.message);
56 expect(readError.cause).toBe(cause);
57
58 const contentSearchError = new ProjectSearchContentsError({
59 cwd: "/workspace",
60 queryLength: "authorization: Bearer secret-token".length,
61 limit: 100,
62 failure: "search_index_search_failed",
63 cause,
64 });
65 expect(contentSearchError.message).toBe("Failed to search workspace contents in '/workspace'.");
66 expect(contentSearchError.message).not.toContain(cause.message);
67 expect(contentSearchError).not.toHaveProperty("query");
68 expect(contentSearchError.cause).toBe(cause);
69 });
70
71 it("decodes legacy message-only errors during rolling upgrades", () => {
72 const decodeSearchError = Schema.decodeUnknownSync(ProjectSearchEntriesError);
73 const decodeWriteError = Schema.decodeUnknownSync(ProjectWriteFileError);
74
75 const searchError = decodeSearchError({
76 _tag: "ProjectSearchEntriesError",
77 message: "Legacy project search failure.",
78 query: "legacy sensitive query",
79 });
80 const writeError = decodeWriteError({
81 _tag: "ProjectWriteFileError",
82 message: "Legacy project write failure.",
83 });
84
85 expect(searchError.message).toBe("Legacy project search failure.");
86 expect(searchError.cwd).toBeUndefined();
87 expect(searchError.queryLength).toBeUndefined();
88 expect(searchError).not.toHaveProperty("query");
89 expect(searchError.failure).toBeUndefined();
90 expect(writeError.message).toBe("Legacy project write failure.");
91 expect(writeError.relativePath).toBeUndefined();
92 expect(writeError.failure).toBeUndefined();
93 });
94});
95
96describe("shared project payloads", () => {
97 it("preserves omitted, false, and null values through RPC envelopes", () => {
98 const create = decodeProjectCreatePayload({
99 title: " Example ",
100 workspaceRoot: "/workspace",
101 createWorkspaceRootIfMissing: false,
102 });
103 const update = decodeProjectUpdatePayload({
104 autoPull: false,
105 defaultModelSelection: null,
106 faviconPath: null,
107 });
108 const envelope = { commandId: "command", projectId: "project" };
109 expect(decodeProjectMutation({ type: "project.create", ...envelope, ...create })).toEqual({
110 type: "project.create",
111 ...envelope,
112 title: "Example",
113 workspaceRoot: "/workspace",
114 createWorkspaceRootIfMissing: false,
115 });
116 expect(decodeProjectMutation({ type: "project.update", ...envelope, ...update })).toEqual({
117 type: "project.update",
118 ...envelope,
119 autoPull: false,
120 defaultModelSelection: null,
121 faviconPath: null,
122 });
123 expect(Object.hasOwn(create, "scripts")).toBe(false);
124 expect(Object.hasOwn(update, "title")).toBe(false);
125 expect(
126 decodeProjectMutation({ type: "project.update", ...envelope, title: undefined }),
127 ).toHaveProperty("title", undefined);
128 });
129});
130
131const decodeFaviconPath = Schema.decodeUnknownEffect(ProjectFaviconPath);
132
133effectIt.effect("project favicon paths accept only supported image files", () =>
134 Effect.gen(function* () {
135 assert.strictEqual(yield* decodeFaviconPath("brand/icon.svg"), "brand/icon.svg");
136 assert.strictEqual(
137 yield* decodeFaviconPath("Resources/AppIcon.icns"),
138 "Resources/AppIcon.icns",
139 );
140 assert.strictEqual((yield* Effect.exit(decodeFaviconPath(".env")))._tag, "Failure");
141 }),
142);
143
144const decodeStoredIcon = Schema.decodeUnknownEffect(StoredProjectIcon);
145const decodeReceivedIcon = Schema.decodeUnknownEffect(ReceivedProjectIcon);
146const decodeProjectShell = Schema.decodeUnknownEffect(OrchestrationProjectShell);
147
148effectIt.effect("reads monograms stored in the pre-v2 fallback shape", () =>
149 Effect.gen(function* () {
150 const monogram = { kind: "monogram", text: "T3", color: "violet" } as const;
151 const fallback = { kind: "lucide", name: "folder-code", color: "violet" } as const;
152 for (const legacy of [
153 { ...fallback, monogramText: "T3" },
154 { ...fallback, monogram: "T3" },
155 ]) {
156 assert.deepEqual(yield* decodeStoredIcon(legacy), monogram);
157 assert.deepEqual(yield* decodeReceivedIcon(legacy), monogram);
158 }
159 }),
160);
161
162effectIt.effect("an icon kind from a newer server shows the default icon", () =>
163 Effect.gen(function* () {
164 assert.isNull(yield* decodeReceivedIcon({ kind: "image", url: "https://example.com/a.png" }));
165 const shell = yield* decodeProjectShell({
166 id: "project-1",
167 title: "Project",
168 workspaceRoot: "/tmp/project",
169 defaultModelSelection: null,
170 scripts: [],
171 projectIcon: { kind: "image", url: "https://example.com/a.png" },
172 createdAt: "2026-01-01T00:00:00.000Z",
173 updatedAt: "2026-01-01T00:00:00.000Z",
174 });
175 assert.isNull(shell.projectIcon);
176 const broken = yield* Effect.exit(decodeReceivedIcon({ kind: "emoji" }));
177 assert.strictEqual(broken._tag, "Failure");
178 }),
179);