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