Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import {
3 CommandId,
4 EventId,
5 ProjectId,
6 ProviderInstanceId,
7 type ModelSelection,
8 type ProjectScript,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11import * as Result from "effect/Result";
12
13import {
14 planProjectCommand,
15 type ProjectCommand,
16 type ProjectCommandState,
17} from "./ProjectCommands.ts";
18import type { ProjectRow } from "./ProjectStore.ts";
19
20const now = DateTime.makeUnsafe("2026-01-01T00:00:00.000Z");
21const projectId = ProjectId.make("project-scripts");
22
23const row = (overrides: Partial<ProjectRow> = {}): ProjectRow => ({
24 projectId,
25 title: "Scripts",
26 workspaceRoot: "/tmp/scripts",
27 defaultModelSelection: null,
28 defaultThreadEnvMode: null,
29 autoPull: false,
30 faviconPath: null,
31 projectIcon: null,
32 scripts: [],
33 createdAt: "2026-01-01T00:00:00.000Z",
34 updatedAt: "2026-01-01T00:00:00.000Z",
35 deletedAt: null,
36 ...overrides,
37});
38
39const script = (id: string): ProjectScript => ({
40 id,
41 name: "Install dependencies",
42 command: "vp i",
43 icon: "configure",
44 runOnWorktreeCreate: false,
45});
46
47const plan = (command: ProjectCommand, state: Partial<ProjectCommandState> = {}) =>
48 planProjectCommand({
49 command,
50 state: { project: undefined, workspaceOwner: undefined, ...state },
51 eventId: EventId.make("event:planned"),
52 now,
53 });
54
55const update = (
56 fields: Omit<
57 Extract<ProjectCommand, { type: "project.meta.update" }>,
58 "type" | "commandId" | "projectId"
59 >,
60) =>
61 plan(
62 {
63 type: "project.meta.update",
64 commandId: CommandId.make("cmd-update"),
65 projectId,
66 ...fields,
67 },
68 { project: row() },
69 );
70
71const payloadOf = (result: ReturnType<typeof plan>) => {
72 assert.isTrue(Result.isSuccess(result));
73 return Result.getOrThrow(result).payload as Record<string, unknown>;
74};
75
76const failureOf = (result: ReturnType<typeof plan>) => {
77 assert.isTrue(Result.isFailure(result));
78 return Result.isFailure(result) ? result.failure : assert.fail("expected a rejection");
79};
80
81describe("planProjectCommand", () => {
82 it("creates projects with empty scripts and no model default", () => {
83 const result = plan({
84 type: "project.create",
85 commandId: CommandId.make("cmd-create"),
86 projectId,
87 title: "Scripts",
88 workspaceRoot: "/tmp/scripts",
89 });
90 const event = Result.getOrThrow(result);
91 assert.equal(event.type, "project.created");
92 assert.equal(event.occurredAt, "2026-01-01T00:00:00.000Z");
93 assert.deepInclude(event.payload, { scripts: [], defaultModelSelection: null });
94 });
95
96 it("only treats metadata updates as explicit model defaults", () => {
97 const selection: ModelSelection = {
98 instanceId: ProviderInstanceId.make("codex"),
99 model: "gpt-5.6-sol",
100 options: [{ id: "reasoningEffort", value: "high" }],
101 };
102 assert.deepEqual(
103 payloadOf(update({ defaultModelSelection: selection })).defaultModelSelection,
104 selection,
105 );
106 });
107
108 it("carries every edited field and omits the rest", () => {
109 const scripts = [script("lint")];
110 const payload = payloadOf(
111 update({
112 scripts,
113 defaultThreadEnvMode: "worktree",
114 autoPull: true,
115 faviconPath: "brand/icon.svg",
116 projectIcon: { kind: "lucide", name: "alarm-clock", color: "violet" },
117 }),
118 );
119 assert.deepInclude(payload, {
120 scripts,
121 defaultThreadEnvMode: "worktree",
122 autoPull: true,
123 faviconPath: "brand/icon.svg",
124 projectIcon: { kind: "lucide", name: "alarm-clock", color: "violet" },
125 });
126 const renamed = payloadOf(update({ title: "Renamed" }));
127 assert.isFalse("defaultThreadEnvMode" in renamed);
128 assert.isNull(payloadOf(update({ defaultThreadEnvMode: null })).defaultThreadEnvMode);
129 });
130
131 it.each(["install-javascript-dependencies", "A", "a.b", "a b", "-a", "a".repeat(25)])(
132 "rejects a new script ID that cannot have a shortcut: %s",
133 (id) => {
134 const failure = failureOf(update({ scripts: [script("lint"), script(id)] }));
135 assert.equal(failure._tag, "ProjectCommandInvariantError");
136 assert.include(failure.message, "Script ID");
137 assert.include(failure.message, "24");
138 assert.notInclude(failure.message, `'${id}'`);
139 },
140 );
141
142 it("accepts a script ID at the shortcut length limit", () => {
143 const scripts = [script("a".repeat(24))];
144 assert.deepEqual(payloadOf(update({ scripts })).scripts, scripts);
145 });
146
147 it("keeps legacy scripts editable and removable while rejecting new invalid ones", () => {
148 const legacy = script("install-javascript-dependencies");
149 const withLegacy = (scripts: ReadonlyArray<ProjectScript>) =>
150 plan(
151 {
152 type: "project.meta.update",
153 commandId: CommandId.make("cmd-repair-script"),
154 projectId,
155 scripts,
156 },
157 { project: row({ scripts: [legacy] }) },
158 );
159 for (const scripts of [[{ ...legacy, command: "vp install" }, script("lint")], []]) {
160 assert.deepEqual(payloadOf(withLegacy(scripts)).scripts, scripts);
161 }
162 assert.equal(
163 failureOf(withLegacy([legacy, script("another.invalid.id")]))._tag,
164 "ProjectCommandInvariantError",
165 );
166 });
167
168 it("limits monograms to two graphemes", () => {
169 for (const text of ["T3", "é", "किखि", "क्ष्म", "각"]) {
170 const monogram = { kind: "monogram", text, color: "violet" } as const;
171 assert.deepEqual(payloadOf(update({ projectIcon: monogram })).projectIcon, monogram);
172 }
173 for (const text of ["ABC", "किखिगि"]) {
174 assert.equal(
175 failureOf(update({ projectIcon: { kind: "monogram", text, color: "violet" } }))._tag,
176 "ProjectCommandInvariantError",
177 );
178 }
179 });
180
181 it("rejects a workspace root held by another active project", () => {
182 const owner = row({
183 projectId: ProjectId.make("project-existing"),
184 workspaceRoot: "/tmp/project",
185 });
186 const create = failureOf(
187 plan(
188 {
189 type: "project.create",
190 commandId: CommandId.make("cmd-duplicate-root"),
191 projectId: ProjectId.make("project-duplicate-root"),
192 title: "Duplicate",
193 workspaceRoot: "/tmp/project",
194 },
195 { workspaceOwner: owner },
196 ),
197 );
198 assert.equal(create._tag, "ProjectWorkspaceConflictError");
199 assert.equal(
200 create.message,
201 "Active project 'project-existing' already exists for workspace root '/tmp/project'.",
202 );
203 const move = failureOf(
204 plan(
205 {
206 type: "project.meta.update",
207 commandId: CommandId.make("cmd-move-root"),
208 projectId,
209 workspaceRoot: "/tmp/project",
210 },
211 { project: row(), workspaceOwner: owner },
212 ),
213 );
214 assert.equal(move._tag, "ProjectWorkspaceConflictError");
215 });
216
217 it("requires the project to exist, and to be absent on create", () => {
218 const create: ProjectCommand = {
219 type: "project.create",
220 commandId: CommandId.make("cmd-create-twice"),
221 projectId,
222 title: "Twice",
223 workspaceRoot: "/tmp/twice",
224 };
225 assert.include(failureOf(plan(create, { project: row() })).message, "cannot be created twice");
226 const deleted = row({ deletedAt: "2026-01-01T00:00:00.000Z" });
227 assert.include(
228 failureOf(plan(create, { project: deleted })).message,
229 "cannot be created twice",
230 );
231 for (const command of [
232 { type: "project.meta.update", commandId: CommandId.make("cmd-missing"), projectId },
233 { type: "project.delete", commandId: CommandId.make("cmd-missing"), projectId },
234 ] as const) {
235 for (const project of [undefined, deleted]) {
236 assert.equal(
237 failureOf(plan(command, { project }))._tag,
238 "ProjectCommandMissingProjectError",
239 );
240 }
241 }
242 });
243
244 it("deletes with a single project.deleted event", () => {
245 const event = Result.getOrThrow(
246 plan(
247 { type: "project.delete", commandId: CommandId.make("cmd-delete"), projectId },
248 { project: row() },
249 ),
250 );
251 assert.equal(event.type, "project.deleted");
252 assert.deepEqual(event.payload, { projectId, deletedAt: "2026-01-01T00:00:00.000Z" });
253 });
254});