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

projectScripts.test.ts

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