Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import { CommandId, EventId, ProjectId, type ProjectScript } from "@t3tools/contracts";
3import * as DateTime from "effect/DateTime";
4import * as Result from "effect/Result";
5
6import {
7 planProjectCommand,
8 type ProjectCommand,
9 type ProjectCommandState,
10} from "./ProjectCommands.ts";
11import type { ProjectRow } from "./ProjectStore.ts";
12
13const now = DateTime.makeUnsafe("2026-01-01T00:00:00.000Z");
14const projectId = ProjectId.make("project-scripts");
15
16const row = (overrides: Partial<ProjectRow> = {}): ProjectRow => ({
17 projectId,
18 title: "Scripts",
19 workspaceRoot: "/tmp/scripts",
20 defaultModelSelection: null,
21 defaultThreadEnvMode: null,
22 autoPull: false,
23 faviconPath: null,
24 projectIcon: null,
25 scripts: [],
26 createdAt: "2026-01-01T00:00:00.000Z",
27 updatedAt: "2026-01-01T00:00:00.000Z",
28 deletedAt: null,
29 ...overrides,
30});
31
32const script = (id: string): ProjectScript => ({
33 id,
34 name: "Install dependencies",
35 command: "vp i",
36 icon: "configure",
37 runOnWorktreeCreate: false,
38});
39
40const plan = (command: ProjectCommand, state: Partial<ProjectCommandState> = {}) =>
41 planProjectCommand({
42 command,
43 state: { project: undefined, workspaceOwner: undefined, ...state },
44 eventId: EventId.make("event:planned"),
45 now,
46 });
47
48const payloadOf = (result: ReturnType<typeof plan>) => {
49 assert.isTrue(Result.isSuccess(result));
50 return Result.getOrThrow(result).payload as Record<string, unknown>;
51};
52
53const failureOf = (result: ReturnType<typeof plan>) => {
54 assert.isTrue(Result.isFailure(result));
55 return Result.isFailure(result) ? result.failure : assert.fail("expected a rejection");
56};
57
58describe("planProjectCommand", () => {
59 it("keeps legacy scripts editable and removable while rejecting new invalid ones", () => {
60 const legacy = script("install-javascript-dependencies");
61 const withLegacy = (scripts: ReadonlyArray<ProjectScript>) =>
62 plan(
63 {
64 type: "project.meta.update",
65 commandId: CommandId.make("cmd-repair-script"),
66 projectId,
67 scripts,
68 },
69 { project: row({ scripts: [legacy] }) },
70 );
71 for (const scripts of [[{ ...legacy, command: "vp install" }, script("lint")], []]) {
72 assert.deepEqual(payloadOf(withLegacy(scripts)).scripts, scripts);
73 }
74 assert.equal(
75 failureOf(withLegacy([legacy, script("another.invalid.id")]))._tag,
76 "ProjectCommandInvariantError",
77 );
78 });
79
80 it("rejects a workspace root held by another active project", () => {
81 const owner = row({
82 projectId: ProjectId.make("project-existing"),
83 workspaceRoot: "/tmp/project",
84 });
85 const create = failureOf(
86 plan(
87 {
88 type: "project.create",
89 commandId: CommandId.make("cmd-duplicate-root"),
90 projectId: ProjectId.make("project-duplicate-root"),
91 title: "Duplicate",
92 workspaceRoot: "/tmp/project",
93 },
94 { workspaceOwner: owner },
95 ),
96 );
97 assert.equal(create._tag, "ProjectWorkspaceConflictError");
98 assert.equal(
99 create.message,
100 "Active project 'project-existing' already exists for workspace root '/tmp/project'.",
101 );
102 const move = failureOf(
103 plan(
104 {
105 type: "project.meta.update",
106 commandId: CommandId.make("cmd-move-root"),
107 projectId,
108 workspaceRoot: "/tmp/project",
109 },
110 { project: row(), workspaceOwner: owner },
111 ),
112 );
113 assert.equal(move._tag, "ProjectWorkspaceConflictError");
114 });
115
116 it("requires the project to exist, and to be absent on create", () => {
117 const create: ProjectCommand = {
118 type: "project.create",
119 commandId: CommandId.make("cmd-create-twice"),
120 projectId,
121 title: "Twice",
122 workspaceRoot: "/tmp/twice",
123 };
124 assert.include(failureOf(plan(create, { project: row() })).message, "cannot be created twice");
125 const deleted = row({ deletedAt: "2026-01-01T00:00:00.000Z" });
126 assert.include(
127 failureOf(plan(create, { project: deleted })).message,
128 "cannot be created twice",
129 );
130 for (const command of [
131 { type: "project.meta.update", commandId: CommandId.make("cmd-missing"), projectId },
132 { type: "project.delete", commandId: CommandId.make("cmd-missing"), projectId },
133 ] as const) {
134 for (const project of [undefined, deleted]) {
135 assert.equal(
136 failureOf(plan(command, { project }))._tag,
137 "ProjectCommandMissingProjectError",
138 );
139 }
140 }
141 });
142});