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

projectScripts.test.ts

132 lines3.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 projectScriptCwd,
4 projectScriptRuntimeEnv,
5 setupProjectScript,
6} from "@t3tools/shared/projectScripts";
7​
8import {
9 buildProjectScript,
10 commandForProjectScript,
11 nextProjectScriptId,
12 primaryProjectScript,
13 projectScriptIdFromCommand,
14} from "./projectScripts";
15​
16describe("projectScripts helpers", () => {
17 it("builds scripts with preview settings", () => {
18 expect(
19 buildProjectScript("dev", {
20 name: "Dev server",
21 command: "pnpm dev",
22 icon: "debug",
23 runOnWorktreeCreate: false,
24 previewUrl: "http://localhost:5733",
25 autoOpenPreview: true,
26 }),
27 ).toEqual({
28 id: "dev",
29 name: "Dev server",
30 command: "pnpm dev",
31 icon: "debug",
32 runOnWorktreeCreate: false,
33 previewUrl: "http://localhost:5733",
34 autoOpenPreview: true,
35 });
36 });
37​
38 it("omits preview settings when no preview URL is configured", () => {
39 expect(
40 buildProjectScript("test", {
41 name: "Test",
42 command: "pnpm test",
43 icon: "test",
44 runOnWorktreeCreate: false,
45 previewUrl: null,
46 autoOpenPreview: false,
47 }),
48 ).toEqual({
49 id: "test",
50 name: "Test",
51 command: "pnpm test",
52 icon: "test",
53 runOnWorktreeCreate: false,
54 });
55 });
56​
57 it("builds and parses script run commands", () => {
58 const command = commandForProjectScript("lint");
59 expect(command).toBe("script.lint.run");
60 expect(projectScriptIdFromCommand(command)).toBe("lint");
61 expect(projectScriptIdFromCommand("terminal.toggle")).toBeNull();
62 });
63​
64 it("slugifies and dedupes project script ids", () => {
65 expect(nextProjectScriptId("Run Tests", [])).toBe("run-tests");
66 expect(nextProjectScriptId("Run Tests", ["run-tests"])).toBe("run-tests-2");
67 expect(nextProjectScriptId("!!!", [])).toBe("script");
68 });
69​
70 it("resolves primary and setup scripts", () => {
71 const scripts = [
72 {
73 id: "setup",
74 name: "Setup",
75 command: "bun install",
76 icon: "configure" as const,
77 runOnWorktreeCreate: true,
78 },
79 {
80 id: "test",
81 name: "Test",
82 command: "bun test",
83 icon: "test" as const,
84 runOnWorktreeCreate: false,
85 },
86 ];
87​
88 expect(primaryProjectScript(scripts)?.id).toBe("test");
89 expect(setupProjectScript(scripts)?.id).toBe("setup");
90 });
91​
92 it("builds default runtime env for scripts", () => {
93 const env = projectScriptRuntimeEnv({
94 project: { cwd: "/repo" },
95 worktreePath: "/repo/worktree-a",
96 });
97​
98 expect(env).toMatchObject({
99 T3CODE_PROJECT_ROOT: "/repo",
100 T3CODE_WORKTREE_PATH: "/repo/worktree-a",
101 });
102 });
103​
104 it("allows overriding runtime env values", () => {
105 const env = projectScriptRuntimeEnv({
106 project: { cwd: "/repo" },
107 extraEnv: {
108 T3CODE_PROJECT_ROOT: "/custom-root",
109 CUSTOM_FLAG: "1",
110 },
111 });
112​
113 expect(env.T3CODE_PROJECT_ROOT).toBe("/custom-root");
114 expect(env.CUSTOM_FLAG).toBe("1");
115 expect(env.T3CODE_WORKTREE_PATH).toBeUndefined();
116 });
117​
118 it("prefers the worktree path for script cwd resolution", () => {
119 expect(
120 projectScriptCwd({
121 project: { cwd: "/repo" },
122 worktreePath: "/repo/worktree-a",
123 }),
124 ).toBe("/repo/worktree-a");
125 expect(
126 projectScriptCwd({
127 project: { cwd: "/repo" },
128 worktreePath: null,
129 }),
130 ).toBe("/repo");
131 });
132});