Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 ProjectReadFileError,
6 ProjectCreatePayload,
7 ProjectUpdatePayload,
8 ProjectMutation,
9 ProjectSearchContentsError,
10 ProjectSearchContentsInput,
11 ProjectSearchEntriesError,
12 ProjectSearchEntriesInput,
13 ProjectWriteFileError,
14} from "./project.ts";
15
16const decodeProjectCreatePayload = Schema.decodeUnknownSync(ProjectCreatePayload);
17const decodeProjectUpdatePayload = Schema.decodeUnknownSync(ProjectUpdatePayload);
18const decodeProjectMutation = Schema.decodeUnknownSync(ProjectMutation);
19const decodeSearchEntriesInput = Schema.decodeUnknownSync(ProjectSearchEntriesInput);
20const decodeSearchContentsInput = Schema.decodeUnknownSync(ProjectSearchContentsInput);
21
22describe("project search inputs", () => {
23 it("allows an empty entries query for bounded frecency browsing", () => {
24 const decoded = decodeSearchEntriesInput({
25 cwd: "/workspace",
26 query: " ",
27 limit: 10,
28 kind: "file",
29 });
30 expect(decoded.query).toBe("");
31 });
32
33 it("preserves whitespace in content search queries", () => {
34 const decoded = decodeSearchContentsInput({
35 cwd: "/workspace",
36 query: " foo ",
37 limit: 10,
38 caseSensitive: false,
39 wholeWord: false,
40 useRegex: false,
41 });
42 expect(decoded.query).toBe(" foo ");
43 });
44});
45
46describe("project RPC errors", () => {
47 it("derives stable messages from structured request context while retaining causes", () => {
48 const cause = new Error("sensitive platform detail");
49 const searchError = new ProjectSearchEntriesError({
50 cwd: "/workspace",
51 queryLength: "authorization: Bearer secret-token".length,
52 limit: 20,
53 failure: "search_index_search_failed",
54 normalizedCwd: "/workspace",
55 detail: "index unavailable",
56 cause,
57 });
58 const readError = new ProjectReadFileError({
59 cwd: "/workspace",
60 relativePath: "src/index.ts",
61 failure: "operation_failed",
62 operation: "read",
63 operationPath: "/workspace/src/index.ts",
64 resolvedPath: "/workspace/src/index.ts",
65 cause,
66 });
67
68 expect(searchError.message).toBe("Failed to search workspace entries in '/workspace'.");
69 expect(searchError.message).not.toContain(cause.message);
70 expect(searchError.normalizedCwd).toBe("/workspace");
71 expect(searchError.queryLength).toBe("authorization: Bearer secret-token".length);
72 expect(searchError).not.toHaveProperty("query");
73 expect(searchError.message).not.toMatch(/Bearer|secret-token/);
74 expect(searchError.cause).toBe(cause);
75 expect(readError.message).toBe("Failed to read workspace file 'src/index.ts' in '/workspace'.");
76 expect(readError.message).not.toContain(cause.message);
77 expect(readError.cause).toBe(cause);
78
79 const contentSearchError = new ProjectSearchContentsError({
80 cwd: "/workspace",
81 queryLength: "authorization: Bearer secret-token".length,
82 limit: 100,
83 failure: "search_index_search_failed",
84 cause,
85 });
86 expect(contentSearchError.message).toBe("Failed to search workspace contents in '/workspace'.");
87 expect(contentSearchError.message).not.toContain(cause.message);
88 expect(contentSearchError).not.toHaveProperty("query");
89 expect(contentSearchError.cause).toBe(cause);
90 });
91
92 it("decodes legacy message-only errors during rolling upgrades", () => {
93 const decodeSearchError = Schema.decodeUnknownSync(ProjectSearchEntriesError);
94 const decodeWriteError = Schema.decodeUnknownSync(ProjectWriteFileError);
95
96 const searchError = decodeSearchError({
97 _tag: "ProjectSearchEntriesError",
98 message: "Legacy project search failure.",
99 query: "legacy sensitive query",
100 });
101 const writeError = decodeWriteError({
102 _tag: "ProjectWriteFileError",
103 message: "Legacy project write failure.",
104 });
105
106 expect(searchError.message).toBe("Legacy project search failure.");
107 expect(searchError.cwd).toBeUndefined();
108 expect(searchError.queryLength).toBeUndefined();
109 expect(searchError).not.toHaveProperty("query");
110 expect(searchError.failure).toBeUndefined();
111 expect(writeError.message).toBe("Legacy project write failure.");
112 expect(writeError.relativePath).toBeUndefined();
113 expect(writeError.failure).toBeUndefined();
114 });
115});
116
117describe("shared project payloads", () => {
118 it("preserves omitted, false, and null values through RPC envelopes", () => {
119 const create = decodeProjectCreatePayload({
120 title: " Example ",
121 workspaceRoot: "/workspace",
122 createWorkspaceRootIfMissing: false,
123 });
124 const update = decodeProjectUpdatePayload({
125 autoPull: false,
126 defaultModelSelection: null,
127 faviconPath: null,
128 });
129 const envelope = { commandId: "command", projectId: "project" };
130 expect(decodeProjectMutation({ type: "project.create", ...envelope, ...create })).toEqual({
131 type: "project.create",
132 ...envelope,
133 title: "Example",
134 workspaceRoot: "/workspace",
135 createWorkspaceRootIfMissing: false,
136 });
137 expect(decodeProjectMutation({ type: "project.update", ...envelope, ...update })).toEqual({
138 type: "project.update",
139 ...envelope,
140 autoPull: false,
141 defaultModelSelection: null,
142 faviconPath: null,
143 });
144 expect(Object.hasOwn(create, "scripts")).toBe(false);
145 expect(Object.hasOwn(update, "title")).toBe(false);
146 // Internal RPC callers may explicitly supply undefined, as before the extraction.
147 expect(
148 decodeProjectMutation({ type: "project.update", ...envelope, title: undefined }),
149 ).toHaveProperty("title", undefined);
150 });
151});