Open History to see the last commit
1import { shortcutLabelForCommand } from "./keybindings";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 projectScriptCwd,
5 projectScriptRuntimeEnv,
6 settleProjectScript,
7 setupProjectScript,
8} from "@t3tools/shared/projectScripts";
9
10import {
11 commandForProjectScript,
12 nextProjectScriptId,
13 primaryProjectScript,
14 releaseClaimedRoles,
15} from "./projectScripts";
16
17describe("projectScripts helpers", () => {
18 it.each(["install-javascript-dependencies", "A", "a.b", "a b", "-a", "", "a".repeat(25)])(
19 "omits the shortcut for legacy script ID %j without crashing script menus",
20 (id) => {
21 const commands = ["lint", id, "test"].map(commandForProjectScript);
22 expect(commands).toEqual(["script.lint.run", null, "script.test.run"]);
23 expect(commands.map((command) => shortcutLabelForCommand([], command))).toEqual([
24 null,
25 null,
26 null,
27 ]);
28 },
29 );
30
31 it("slugifies and dedupes project script ids", () => {
32 expect(nextProjectScriptId("Run Tests", [])).toBe("run-tests");
33 expect(nextProjectScriptId("Run Tests", ["run-tests"])).toBe("run-tests-2");
34 expect(nextProjectScriptId("!!!", [])).toBe("script");
35 });
36
37 it("resolves primary, setup, and settle scripts", () => {
38 const scripts = [
39 {
40 id: "setup",
41 name: "Setup",
42 command: "bun install",
43 icon: "configure" as const,
44 runOnWorktreeCreate: true,
45 },
46 {
47 id: "clean",
48 name: "Clean",
49 command: "cargo clean",
50 icon: "build" as const,
51 runOnWorktreeCreate: false,
52 runOnSettle: true,
53 },
54 {
55 id: "test",
56 name: "Test",
57 command: "bun test",
58 icon: "test" as const,
59 runOnWorktreeCreate: false,
60 },
61 ];
62
63 expect(primaryProjectScript(scripts)?.id).toBe("test");
64 expect(setupProjectScript(scripts)?.id).toBe("setup");
65 expect(settleProjectScript(scripts)?.id).toBe("clean");
66 expect(primaryProjectScript(scripts.filter((script) => script.id === "clean"))).toBeNull();
67 });
68
69 it("moves the setup and settle roles to the script that claims them", () => {
70 const input = {
71 name: "Next",
72 command: "true",
73 icon: "play",
74 waitForSetup: false,
75 previewUrl: null,
76 autoOpenPreview: false,
77 } as const;
78 const holder = {
79 id: "holder",
80 name: "Holder",
81 command: "true",
82 icon: "play" as const,
83 runOnWorktreeCreate: true,
84 runOnSettle: true,
85 };
86
87 expect(
88 releaseClaimedRoles(holder, { ...input, runOnWorktreeCreate: false, runOnSettle: true }),
89 ).toMatchObject({ runOnWorktreeCreate: true, runOnSettle: false });
90 expect(
91 releaseClaimedRoles(holder, { ...input, runOnWorktreeCreate: true, runOnSettle: false }),
92 ).toMatchObject({ runOnWorktreeCreate: false, runOnSettle: true });
93 expect(
94 releaseClaimedRoles(holder, { ...input, runOnWorktreeCreate: false, runOnSettle: false }),
95 ).toBe(holder);
96 });
97
98 it("allows overriding runtime env values", () => {
99 const env = projectScriptRuntimeEnv({
100 project: { cwd: "/repo" },
101 extraEnv: {
102 T3CODE_PROJECT_ROOT: "/custom-root",
103 CUSTOM_FLAG: "1",
104 },
105 });
106
107 expect(env.T3CODE_PROJECT_ROOT).toBe("/custom-root");
108 expect(env.CUSTOM_FLAG).toBe("1");
109 expect(env.T3CODE_WORKTREE_PATH).toBeUndefined();
110 });
111
112 it("prefers the worktree path for script cwd resolution", () => {
113 expect(
114 projectScriptCwd({
115 project: { cwd: "/repo" },
116 worktreePath: "/repo/worktree-a",
117 }),
118 ).toBe("/repo/worktree-a");
119 expect(
120 projectScriptCwd({
121 project: { cwd: "/repo" },
122 worktreePath: null,
123 }),
124 ).toBe("/repo");
125 });
126});