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

ProjectCommands.test.ts

209 lines6.8 KB

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 hasCustomTitle: false,
23 autoPull: false,
24 faviconPath: null,
25 projectIcon: null,
26 scripts: [],
27 trackedFolders: [],
28 pinnedAt: null,
29 archivedAt: null,
30 createdAt: "2026-01-01T00:00:00.000Z",
31 updatedAt: "2026-01-01T00:00:00.000Z",
32 deletedAt: null,
33 ...overrides,
34});
35​
36const script = (id: string): ProjectScript => ({
37 id,
38 name: "Install dependencies",
39 command: "vp i",
40 icon: "configure",
41 runOnWorktreeCreate: false,
42});
43​
44const plan = (command: ProjectCommand, state: Partial<ProjectCommandState> = {}) =>
45 planProjectCommand({
46 command,
47 state: { project: undefined, workspaceOwner: undefined, ...state },
48 eventId: EventId.make("event:planned"),
49 now,
50 });
51​
52const payloadOf = (result: ReturnType<typeof plan>) => {
53 assert.isTrue(Result.isSuccess(result));
54 return Result.getOrThrow(result).payload as Record<string, unknown>;
55};
56​
57const failureOf = (result: ReturnType<typeof plan>) => {
58 assert.isTrue(Result.isFailure(result));
59 return Result.isFailure(result) ? result.failure : assert.fail("expected a rejection");
60};
61​
62describe("planProjectCommand", () => {
63 it("keeps legacy scripts editable and removable while rejecting new invalid ones", () => {
64 const legacy = script("install-javascript-dependencies");
65 const withLegacy = (scripts: ReadonlyArray<ProjectScript>) =>
66 plan(
67 {
68 type: "project.meta.update",
69 commandId: CommandId.make("cmd-repair-script"),
70 projectId,
71 scripts,
72 },
73 { project: row({ scripts: [legacy] }) },
74 );
75 for (const scripts of [[{ ...legacy, command: "vp install" }, script("lint")], []]) {
76 assert.deepEqual(payloadOf(withLegacy(scripts)).scripts, scripts);
77 }
78 assert.equal(
79 failureOf(withLegacy([legacy, script("another.invalid.id")]))._tag,
80 "ProjectCommandInvariantError",
81 );
82 });
83​
84 it("keeps the first pin and archive time, and clears both on undo", () => {
85 const earlier = "2025-12-01T00:00:00.000Z";
86 const update = (project: ProjectRow, change: { pinned?: boolean; archived?: boolean }) =>
87 payloadOf(
88 plan(
89 {
90 type: "project.meta.update",
91 commandId: CommandId.make("cmd-organize"),
92 projectId,
93 ...change,
94 },
95 { project },
96 ),
97 );
98 assert.deepInclude(update(row(), { pinned: true, archived: true }), {
99 pinnedAt: "2026-01-01T00:00:00.000Z",
100 archivedAt: "2026-01-01T00:00:00.000Z",
101 });
102 const organized = row({ pinnedAt: earlier, archivedAt: earlier });
103 assert.deepInclude(update(organized, { pinned: true, archived: true }), {
104 pinnedAt: earlier,
105 archivedAt: earlier,
106 });
107 assert.deepInclude(update(organized, { pinned: false, archived: false }), {
108 pinnedAt: null,
109 archivedAt: null,
110 });
111 const untouched = update(organized, {});
112 assert.notProperty(untouched, "pinnedAt");
113 assert.notProperty(untouched, "archivedAt");
114 });
115​
116 it("records tracked folders on create and replaces them on update", () => {
117 const trackedFolders = [{ path: "/tmp/shared", name: "Shared" }];
118 const create = (folders?: typeof trackedFolders) =>
119 payloadOf(
120 plan({
121 type: "project.create",
122 commandId: CommandId.make("cmd-create-tracked"),
123 projectId,
124 title: "Tracked",
125 workspaceRoot: "/tmp/scripts",
126 ...(folders === undefined ? {} : { trackedFolders: folders }),
127 }),
128 );
129 const update = (folders?: typeof trackedFolders) =>
130 payloadOf(
131 plan(
132 {
133 type: "project.meta.update",
134 commandId: CommandId.make("cmd-update-tracked"),
135 projectId,
136 ...(folders === undefined ? {} : { trackedFolders: folders }),
137 },
138 { project: row({ trackedFolders }) },
139 ),
140 );
141 assert.deepEqual(create(trackedFolders).trackedFolders, trackedFolders);
142 assert.notProperty(create(), "trackedFolders");
143 assert.deepEqual(update([]).trackedFolders, []);
144 assert.notProperty(update(), "trackedFolders");
145 });
146​
147 it("rejects a workspace root held by another active project", () => {
148 const owner = row({
149 projectId: ProjectId.make("project-existing"),
150 workspaceRoot: "/tmp/project",
151 });
152 const create = failureOf(
153 plan(
154 {
155 type: "project.create",
156 commandId: CommandId.make("cmd-duplicate-root"),
157 projectId: ProjectId.make("project-duplicate-root"),
158 title: "Duplicate",
159 workspaceRoot: "/tmp/project",
160 },
161 { workspaceOwner: owner },
162 ),
163 );
164 assert.equal(create._tag, "ProjectWorkspaceConflictError");
165 assert.equal(
166 create.message,
167 "Active project 'project-existing' already exists for workspace root '/tmp/project'.",
168 );
169 const move = failureOf(
170 plan(
171 {
172 type: "project.meta.update",
173 commandId: CommandId.make("cmd-move-root"),
174 projectId,
175 workspaceRoot: "/tmp/project",
176 },
177 { project: row(), workspaceOwner: owner },
178 ),
179 );
180 assert.equal(move._tag, "ProjectWorkspaceConflictError");
181 });
182​
183 it("requires the project to exist, and to be absent on create", () => {
184 const create: ProjectCommand = {
185 type: "project.create",
186 commandId: CommandId.make("cmd-create-twice"),
187 projectId,
188 title: "Twice",
189 workspaceRoot: "/tmp/twice",
190 };
191 assert.include(failureOf(plan(create, { project: row() })).message, "cannot be created twice");
192 const deleted = row({ deletedAt: "2026-01-01T00:00:00.000Z" });
193 assert.include(
194 failureOf(plan(create, { project: deleted })).message,
195 "cannot be created twice",
196 );
197 for (const command of [
198 { type: "project.meta.update", commandId: CommandId.make("cmd-missing"), projectId },
199 { type: "project.delete", commandId: CommandId.make("cmd-missing"), projectId },
200 ] as const) {
201 for (const project of [undefined, deleted]) {
202 assert.equal(
203 failureOf(plan(command, { project }))._tag,
204 "ProjectCommandMissingProjectError",
205 );
206 }
207 }
208 });
209});