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

projectScripts.test.ts

91 lines2.5 KB

1import { describe, expect, it } from "vitest";
2import {
3 projectScriptCwd,
4 projectScriptRuntimeEnv,
5 setupProjectScript,
6} from "@workbench/shared/projectScripts";
7​
8import {
9 commandForProjectScript,
10 nextProjectScriptId,
11 primaryProjectScript,
12 projectScriptIdFromCommand,
13} from "./projectScripts";
14​
15describe("projectScripts helpers", () => {
16 it("builds and parses script run commands", () => {
17 const command = commandForProjectScript("lint");
18 expect(command).toBe("script.lint.run");
19 expect(projectScriptIdFromCommand(command)).toBe("lint");
20 expect(projectScriptIdFromCommand("terminal.toggle")).toBeNull();
21 });
22​
23 it("slugifies and dedupes project script ids", () => {
24 expect(nextProjectScriptId("Run Tests", [])).toBe("run-tests");
25 expect(nextProjectScriptId("Run Tests", ["run-tests"])).toBe("run-tests-2");
26 expect(nextProjectScriptId("!!!", [])).toBe("script");
27 });
28​
29 it("resolves primary and setup scripts", () => {
30 const scripts = [
31 {
32 id: "setup",
33 name: "Setup",
34 command: "bun install",
35 icon: "configure" as const,
36 runOnWorktreeCreate: true,
37 },
38 {
39 id: "test",
40 name: "Test",
41 command: "bun test",
42 icon: "test" as const,
43 runOnWorktreeCreate: false,
44 },
45 ];
46​
47 expect(primaryProjectScript(scripts)?.id).toBe("test");
48 expect(setupProjectScript(scripts)?.id).toBe("setup");
49 });
50​
51 it("builds default runtime env for scripts", () => {
52 const env = projectScriptRuntimeEnv({
53 project: { cwd: "/repo" },
54 worktreePath: "/repo/worktree-a",
55 });
56​
57 expect(env).toMatchObject({
58 T3CODE_PROJECT_ROOT: "/repo",
59 T3CODE_WORKTREE_PATH: "/repo/worktree-a",
60 });
61 });
62​
63 it("allows overriding runtime env values", () => {
64 const env = projectScriptRuntimeEnv({
65 project: { cwd: "/repo" },
66 extraEnv: {
67 T3CODE_PROJECT_ROOT: "/custom-root",
68 CUSTOM_FLAG: "1",
69 },
70 });
71​
72 expect(env.T3CODE_PROJECT_ROOT).toBe("/custom-root");
73 expect(env.CUSTOM_FLAG).toBe("1");
74 expect(env.T3CODE_WORKTREE_PATH).toBeUndefined();
75 });
76​
77 it("prefers the worktree path for script cwd resolution", () => {
78 expect(
79 projectScriptCwd({
80 project: { cwd: "/repo" },
81 worktreePath: "/repo/worktree-a",
82 }),
83 ).toBe("/repo/worktree-a");
84 expect(
85 projectScriptCwd({
86 project: { cwd: "/repo" },
87 worktreePath: null,
88 }),
89 ).toBe("/repo");
90 });
91});