Skip to content
Source
Sign inSign up

projectScripts.test.ts

92 lines2.9 KB

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