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

projectScripts.test.ts

233 lines6.9 KB

1import { MAX_SCRIPT_ID_LENGTH } from "@t3tools/contracts";
2import { shortcutLabelForCommand } from "./keybindings";
3import { describe, expect, it } from "vite-plus/test";
4import {
5 projectScriptCwd,
6 projectScriptRuntimeEnv,
7 projectScriptMenuLabel,
8 settleProjectScript,
9 setupProjectScript,
10} from "@t3tools/shared/projectScripts";
11​
12import {
13 buildProjectScript,
14 commandForProjectScript,
15 nextProjectScriptId,
16 primaryProjectScript,
17 projectScriptIdFromCommand,
18 releaseClaimedRoles,
19} from "./projectScripts";
20​
21describe("projectScripts helpers", () => {
22 it("builds scripts with preview settings", () => {
23 expect(
24 buildProjectScript("dev", {
25 name: "Dev server",
26 command: "pnpm dev",
27 icon: "debug",
28 runOnWorktreeCreate: false,
29 waitForSetup: false,
30 runOnSettle: false,
31 previewUrl: "http://localhost:5733",
32 autoOpenPreview: true,
33 }),
34 ).toEqual({
35 id: "dev",
36 name: "Dev server",
37 command: "pnpm dev",
38 icon: "debug",
39 runOnWorktreeCreate: false,
40 previewUrl: "http://localhost:5733",
41 autoOpenPreview: true,
42 });
43 });
44​
45 it("omits preview settings when no preview URL is configured", () => {
46 expect(
47 buildProjectScript("test", {
48 name: "Test",
49 command: "pnpm test",
50 icon: "test",
51 runOnWorktreeCreate: false,
52 waitForSetup: false,
53 runOnSettle: false,
54 previewUrl: null,
55 autoOpenPreview: false,
56 }),
57 ).toEqual({
58 id: "test",
59 name: "Test",
60 command: "pnpm test",
61 icon: "test",
62 runOnWorktreeCreate: false,
63 });
64 });
65​
66 it("only records async: false for setup scripts that should block the agent", () => {
67 const input = {
68 name: "Setup",
69 command: "pnpm i",
70 icon: "configure",
71 previewUrl: null,
72 autoOpenPreview: false,
73 runOnSettle: false,
74 } as const;
75 expect(
76 buildProjectScript("setup", { ...input, runOnWorktreeCreate: true, waitForSetup: true }),
77 ).toMatchObject({ runOnWorktreeCreate: true, async: false });
78 expect(
79 buildProjectScript("setup", { ...input, runOnWorktreeCreate: true, waitForSetup: false }),
80 ).not.toHaveProperty("async");
81 expect(
82 buildProjectScript("setup", { ...input, runOnWorktreeCreate: false, waitForSetup: true }),
83 ).not.toHaveProperty("async");
84 });
85​
86 it("builds and parses script run commands", () => {
87 const command = commandForProjectScript("lint");
88 expect(command).toBe("script.lint.run");
89 expect(projectScriptIdFromCommand(command ?? "")).toBe("lint");
90 expect(projectScriptIdFromCommand("terminal.toggle")).toBeNull();
91 });
92​
93 it.each(["install-javascript-dependencies", "A", "a.b", "a b", "-a", "", "a".repeat(25)])(
94 "omits the shortcut for legacy script ID %j without crashing script menus",
95 (id) => {
96 const commands = ["lint", id, "test"].map(commandForProjectScript);
97 expect(commands).toEqual(["script.lint.run", null, "script.test.run"]);
98 expect(commands.map((command) => shortcutLabelForCommand([], command))).toEqual([
99 null,
100 null,
101 null,
102 ]);
103 },
104 );
105​
106 it("preserves the exact ID at the shortcut length limit", () => {
107 const id = "a".repeat(MAX_SCRIPT_ID_LENGTH);
108 expect(projectScriptIdFromCommand(commandForProjectScript(id) ?? "")).toBe(id);
109 });
110​
111 it("slugifies and dedupes project script ids", () => {
112 expect(nextProjectScriptId("Run Tests", [])).toBe("run-tests");
113 expect(nextProjectScriptId("Run Tests", ["run-tests"])).toBe("run-tests-2");
114 expect(nextProjectScriptId("!!!", [])).toBe("script");
115 });
116​
117 it("resolves primary, setup, and settle scripts", () => {
118 const scripts = [
119 {
120 id: "setup",
121 name: "Setup",
122 command: "bun install",
123 icon: "configure" as const,
124 runOnWorktreeCreate: true,
125 },
126 {
127 id: "clean",
128 name: "Clean",
129 command: "cargo clean",
130 icon: "build" as const,
131 runOnWorktreeCreate: false,
132 runOnSettle: true,
133 },
134 {
135 id: "test",
136 name: "Test",
137 command: "bun test",
138 icon: "test" as const,
139 runOnWorktreeCreate: false,
140 },
141 ];
142​
143 expect(primaryProjectScript(scripts)?.id).toBe("test");
144 expect(setupProjectScript(scripts)?.id).toBe("setup");
145 expect(settleProjectScript(scripts)?.id).toBe("clean");
146 expect(primaryProjectScript(scripts.filter((script) => script.id === "clean"))).toBeNull();
147 });
148​
149 it("labels every lifecycle role a script runs in", () => {
150 const script = {
151 id: "both",
152 name: "Both",
153 command: "true",
154 icon: "play" as const,
155 runOnWorktreeCreate: true,
156 };
157 expect(projectScriptMenuLabel(script)).toBe("Both (setup)");
158 expect(projectScriptMenuLabel({ ...script, runOnSettle: true })).toBe(
159 "Both (setup, on settle)",
160 );
161 expect(projectScriptMenuLabel({ ...script, runOnWorktreeCreate: false })).toBe("Both");
162 });
163​
164 it("moves the setup and settle roles to the script that claims them", () => {
165 const input = {
166 name: "Next",
167 command: "true",
168 icon: "play",
169 waitForSetup: false,
170 previewUrl: null,
171 autoOpenPreview: false,
172 } as const;
173 const holder = {
174 id: "holder",
175 name: "Holder",
176 command: "true",
177 icon: "play" as const,
178 runOnWorktreeCreate: true,
179 runOnSettle: true,
180 };
181​
182 expect(
183 releaseClaimedRoles(holder, { ...input, runOnWorktreeCreate: false, runOnSettle: true }),
184 ).toMatchObject({ runOnWorktreeCreate: true, runOnSettle: false });
185 expect(
186 releaseClaimedRoles(holder, { ...input, runOnWorktreeCreate: true, runOnSettle: false }),
187 ).toMatchObject({ runOnWorktreeCreate: false, runOnSettle: true });
188 expect(
189 releaseClaimedRoles(holder, { ...input, runOnWorktreeCreate: false, runOnSettle: false }),
190 ).toBe(holder);
191 });
192​
193 it("builds default runtime env for scripts", () => {
194 const env = projectScriptRuntimeEnv({
195 project: { cwd: "/repo" },
196 worktreePath: "/repo/worktree-a",
197 });
198​
199 expect(env).toMatchObject({
200 T3CODE_PROJECT_ROOT: "/repo",
201 T3CODE_WORKTREE_PATH: "/repo/worktree-a",
202 });
203 });
204​
205 it("allows overriding runtime env values", () => {
206 const env = projectScriptRuntimeEnv({
207 project: { cwd: "/repo" },
208 extraEnv: {
209 T3CODE_PROJECT_ROOT: "/custom-root",
210 CUSTOM_FLAG: "1",
211 },
212 });
213​
214 expect(env.T3CODE_PROJECT_ROOT).toBe("/custom-root");
215 expect(env.CUSTOM_FLAG).toBe("1");
216 expect(env.T3CODE_WORKTREE_PATH).toBeUndefined();
217 });
218​
219 it("prefers the worktree path for script cwd resolution", () => {
220 expect(
221 projectScriptCwd({
222 project: { cwd: "/repo" },
223 worktreePath: "/repo/worktree-a",
224 }),
225 ).toBe("/repo/worktree-a");
226 expect(
227 projectScriptCwd({
228 project: { cwd: "/repo" },
229 worktreePath: null,
230 }),
231 ).toBe("/repo");
232 });
233});