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

projectScripts.test.ts

173 lines5.0 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 setupProjectScript,
8} from "@t3tools/shared/projectScripts";
9​
10import {
11 buildProjectScript,
12 commandForProjectScript,
13 nextProjectScriptId,
14 primaryProjectScript,
15 projectScriptIdFromCommand,
16} from "./projectScripts";
17​
18describe("projectScripts helpers", () => {
19 it("builds scripts with preview settings", () => {
20 expect(
21 buildProjectScript("dev", {
22 name: "Dev server",
23 command: "pnpm dev",
24 icon: "debug",
25 runOnWorktreeCreate: false,
26 waitForSetup: false,
27 previewUrl: "http://localhost:5733",
28 autoOpenPreview: true,
29 }),
30 ).toEqual({
31 id: "dev",
32 name: "Dev server",
33 command: "pnpm dev",
34 icon: "debug",
35 runOnWorktreeCreate: false,
36 previewUrl: "http://localhost:5733",
37 autoOpenPreview: true,
38 });
39 });
40​
41 it("omits preview settings when no preview URL is configured", () => {
42 expect(
43 buildProjectScript("test", {
44 name: "Test",
45 command: "pnpm test",
46 icon: "test",
47 runOnWorktreeCreate: false,
48 waitForSetup: false,
49 previewUrl: null,
50 autoOpenPreview: false,
51 }),
52 ).toEqual({
53 id: "test",
54 name: "Test",
55 command: "pnpm test",
56 icon: "test",
57 runOnWorktreeCreate: false,
58 });
59 });
60​
61 it("only records async: false for setup scripts that should block the agent", () => {
62 const input = {
63 name: "Setup",
64 command: "pnpm i",
65 icon: "configure",
66 previewUrl: null,
67 autoOpenPreview: false,
68 } as const;
69 expect(
70 buildProjectScript("setup", { ...input, runOnWorktreeCreate: true, waitForSetup: true }),
71 ).toMatchObject({ runOnWorktreeCreate: true, async: false });
72 expect(
73 buildProjectScript("setup", { ...input, runOnWorktreeCreate: true, waitForSetup: false }),
74 ).not.toHaveProperty("async");
75 expect(
76 buildProjectScript("setup", { ...input, runOnWorktreeCreate: false, waitForSetup: true }),
77 ).not.toHaveProperty("async");
78 });
79​
80 it("builds and parses script run commands", () => {
81 const command = commandForProjectScript("lint");
82 expect(command).toBe("script.lint.run");
83 expect(projectScriptIdFromCommand(command ?? "")).toBe("lint");
84 expect(projectScriptIdFromCommand("terminal.toggle")).toBeNull();
85 });
86​
87 it.each(["install-javascript-dependencies", "A", "a.b", "a b", "-a", "", "a".repeat(25)])(
88 "omits the shortcut for legacy script ID %j without crashing script menus",
89 (id) => {
90 const commands = ["lint", id, "test"].map(commandForProjectScript);
91 expect(commands).toEqual(["script.lint.run", null, "script.test.run"]);
92 expect(commands.map((command) => shortcutLabelForCommand([], command))).toEqual([
93 null,
94 null,
95 null,
96 ]);
97 },
98 );
99​
100 it("preserves the exact ID at the shortcut length limit", () => {
101 const id = "a".repeat(MAX_SCRIPT_ID_LENGTH);
102 expect(projectScriptIdFromCommand(commandForProjectScript(id) ?? "")).toBe(id);
103 });
104​
105 it("slugifies and dedupes project script ids", () => {
106 expect(nextProjectScriptId("Run Tests", [])).toBe("run-tests");
107 expect(nextProjectScriptId("Run Tests", ["run-tests"])).toBe("run-tests-2");
108 expect(nextProjectScriptId("!!!", [])).toBe("script");
109 });
110​
111 it("resolves primary and setup scripts", () => {
112 const scripts = [
113 {
114 id: "setup",
115 name: "Setup",
116 command: "bun install",
117 icon: "configure" as const,
118 runOnWorktreeCreate: true,
119 },
120 {
121 id: "test",
122 name: "Test",
123 command: "bun test",
124 icon: "test" as const,
125 runOnWorktreeCreate: false,
126 },
127 ];
128​
129 expect(primaryProjectScript(scripts)?.id).toBe("test");
130 expect(setupProjectScript(scripts)?.id).toBe("setup");
131 });
132​
133 it("builds default runtime env for scripts", () => {
134 const env = projectScriptRuntimeEnv({
135 project: { cwd: "/repo" },
136 worktreePath: "/repo/worktree-a",
137 });
138​
139 expect(env).toMatchObject({
140 T3CODE_PROJECT_ROOT: "/repo",
141 T3CODE_WORKTREE_PATH: "/repo/worktree-a",
142 });
143 });
144​
145 it("allows overriding runtime env values", () => {
146 const env = projectScriptRuntimeEnv({
147 project: { cwd: "/repo" },
148 extraEnv: {
149 T3CODE_PROJECT_ROOT: "/custom-root",
150 CUSTOM_FLAG: "1",
151 },
152 });
153​
154 expect(env.T3CODE_PROJECT_ROOT).toBe("/custom-root");
155 expect(env.CUSTOM_FLAG).toBe("1");
156 expect(env.T3CODE_WORKTREE_PATH).toBeUndefined();
157 });
158​
159 it("prefers the worktree path for script cwd resolution", () => {
160 expect(
161 projectScriptCwd({
162 project: { cwd: "/repo" },
163 worktreePath: "/repo/worktree-a",
164 }),
165 ).toBe("/repo/worktree-a");
166 expect(
167 projectScriptCwd({
168 project: { cwd: "/repo" },
169 worktreePath: null,
170 }),
171 ).toBe("/repo");
172 });
173});