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

feature-commit-keeps-its-starting-base.e2e.ts

79 lines3.6 KB

1import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
2import { expect } from "e2e";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5​
6import { T3_HOME } from "../e2e.config.ts";
7import { run } from "./shell.ts";
8import { runPromise, test, WEB } from "./t3.ts";
9​
10test(
11 "Committing on a new branch keeps the starting branch as its base",
12 WEB,
13 async ({ t3, app, screen }) => {
14 const project = await t3.project("demo");
15 const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
16 await git("config", "user.name", "Scenario");
17 await git("config", "user.email", "scenario@example.invalid");
18 await git("switch", "-c", "terminal-work");
19 await git("commit", "--allow-empty", "-m", "Unrelated branch commit");
20 await git("switch", "main");
21​
22 const input = { project, title: "Commit work", messages: ["Start"] };
23 const thread = await t3.thread({ ...input, model: "default" }).catch((error: unknown) => {
24 if (!String(error).includes("Model default is not advertised by provider devReplay.")) {
25 throw error;
26 }
27 return t3.thread(input);
28 });
29 await runPromise(
30 Effect.flatMap(FileSystem.FileSystem, (fs) =>
31 fs.writeFileString(`${project}/README.md`, "A local edit\n"),
32 ),
33 );
34 await t3.open(thread);
35 await screen.getByRole("button", "Git action options").tap();
36 await screen.getByRole("menuitem", "Commit", { exact: true }).tap();
37 await screen.getByPlaceholder("Leave empty to auto-generate").fill("Add local notes");
38​
39 const shim = writeFakeCli({
40 directory: `${T3_HOME}/bin`,
41 name: "git",
42 source: `
43 import { spawnSync } from "node:child_process";
44 import { dirname, delimiter } from "node:path";
45 import { fileURLToPath } from "node:url";
46 process.env.PATH = process.env.PATH.split(delimiter)
47 .filter((entry) => entry !== dirname(fileURLToPath(import.meta.url))).join(delimiter);
48 const args = process.argv.slice(2);
49 // Switch in another Git process after the action starts and before it creates the branch.
50 if (process.cwd() === ${JSON.stringify(project)} &&
51 args.join(" ") === "branch --list --no-column --format=%(refname:lstrip=2)") {
52 const switched = spawnSync("git", ["switch", "--quiet", "terminal-work"], { stdio: "inherit" });
53 if (switched.status !== 0) process.exit(switched.status ?? 1);
54 }
55 const result = spawnSync("git", args, { stdio: "inherit" });
56 process.exit(result.status ?? 1);
57 `,
58 });
59 try {
60 await screen.getByRole("button", "Commit on new branch", { exact: true }).tap();
61 const success = screen.getByText(/^Committed [0-9a-f]+$/);
62 const failure = screen.getByText("Action failed", { exact: true });
63 await success.or(failure).first().waitFor();
64​
65 const unexpectedBase =
66 (await success.isVisible()) &&
67 (await git("log", "-1", "--format=%s", "HEAD^")).stdout.trim() ===
68 "Unrelated branch commit";
69 await app.screenshot("feature-commit-after-switch");
70 expect(unexpectedBase).toBe(false);
71 // The injected switch must have landed and stopped the action before any branch was made.
72 await screen.getByText(/main is no longer checked out/).waitFor();
73 expect((await git("branch", "--show-current")).stdout.trim()).toBe("terminal-work");
74 expect((await git("branch", "--list", "feature/*")).stdout.trim()).toBe("");
75 } finally {
76 await runPromise(Effect.flatMap(FileSystem.FileSystem, (fs) => fs.remove(shim)));
77 }
78 },
79);