Official
git
Built-in git UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/git@0.3.0
Permissions in 0.3.0
Files
pulls.test.ts9.9 KB
// The branch's pull request against a fake host and gh: offered only for a GitHub upstream with gh installed and off
// the default branch, written from the branch's commits, edited, opened with gh, and shown as a link.
import assert from "node:assert/strict";
import { test } from "node:test";
import type { GitOutput, ModelAgent } from "divergence";
import activate from "./main.ts";
import { answer, fakeApi, held, untilAborted } from "./testing.ts";
import type { FakeOptions } from "./testing.ts";
const agents: ModelAgent[] = [
{
id: "agent",
name: "Agent",
icon: null,
model: null,
effort: null,
models: [{ value: "mini", name: "Mini", group: null, efforts: [] }],
},
];
const link = "https://github.com/me/repo/pull/12";
type Gh = NonNullable<FakeOptions["gh"]>;
const gh =
(overrides: Record<string, Partial<GitOutput>> = {}): Gh =>
(args) => {
const command = args.slice(0, 2).join(" ");
if (overrides[command]) return overrides[command];
if (command === "repo view") return { stdout: "main\n" };
if (command === "pr list") return { stdout: "[]\n" };
if (command === "pr create") return { stdout: `${link}\n` };
throw new Error(`Unexpected gh ${command}`);
};
const repository = (args: readonly string[]): Partial<GitOutput> | undefined => {
if (args[0] === "remote")
return { stdout: "origin\tgit@github.com:me/repo.git (fetch)\norigin\tgit@github.com:me/repo.git (push)\n" };
if (args[0] === "rev-parse") return { stdout: "/repo\n" };
if (args[0] === "log") return { stdout: "* feat: write commit messages\n\nThe agent writes them.\n" };
if (args[0] === "diff") return { stdout: " git/actions.ts | 20 +++++\n" };
return undefined;
};
const feature = { branch: "feature", upstream: "origin/feature", entries: [] };
async function withPanel(options: FakeOptions, body: (fake: ReturnType<typeof fakeApi>) => Promise<void>) {
const fake = fakeApi({ agents, status: feature, run: repository, gh: gh(), ...options });
const dispose = activate(fake.api);
try {
await fake.settle();
await body(fake);
} finally {
await dispose();
}
}
test("a branch with an upstream on GitHub is offered a pull request, and the default branch is not", async () => {
await withPanel({}, async (fake) => {
assert.ok(fake.node("git-pr-create"));
assert.deepEqual(fake.ghCalls, [
["repo", "view", "--json", "defaultBranchRef", "--jq", ".defaultBranchRef.name"],
["pr", "list", "--head", "feature", "--state", "open", "--json", "url,number", "--limit", "1"],
]);
});
await withPanel({ status: { ...feature, branch: "main", upstream: "origin/main" } }, async (fake) => {
assert.equal(fake.node("git-pr-create"), undefined);
});
});
test("without gh, or with an upstream that is not on GitHub, nothing is offered and gh never runs", async () => {
await withPanel({ gh: undefined }, async (fake) => {
assert.equal(fake.node("git-pr-create"), undefined);
});
const gitlab = (args: readonly string[]) =>
args[0] === "remote" ? { stdout: "origin\thttps://gitlab.com/me/repo.git (fetch)\n" } : repository(args);
await withPanel({ run: gitlab }, async (fake) => {
assert.equal(fake.node("git-pr-create"), undefined);
assert.deepEqual(fake.ghCalls, []);
});
});
test("an open pull request shows as a link that opens in the browser", async () => {
await withPanel({ gh: gh({ "pr list": { stdout: JSON.stringify([{ url: link, number: 12 }]) } }) }, async (fake) => {
assert.equal(fake.node("git-pr-create"), undefined);
assert.ok(fake.rendered().includes("Pull request #12"));
fake.invoke("git-pr-link");
await fake.settle();
assert.deepEqual(fake.opened, [link]);
});
});
test("gh's own words show when it cannot look the branch up", async () => {
const words = "To get started with GitHub CLI, please run: gh auth login";
await withPanel({ gh: gh({ "repo view": { code: 4, stderr: `${words}\n` } }) }, async (fake) => {
assert.ok(fake.rendered().includes(words));
assert.equal(fake.node("git-pr-create"), undefined);
});
});
test("Create Pull Request has an agent write it from the commits, and Open Pull Request pushes and opens what was edited", async () => {
const prompt = async () => answer("Write commit messages\n\n- The agent writes them.");
await withPanel({ prompt, status: { ...feature, ahead: 1 } }, async (fake) => {
fake.invoke("git-pr-create");
await fake.settle();
assert.match(
fake.prompts[0]?.prompt ?? "",
/Branch feature into main\.[\s\S]*feat: write commit messages[\s\S]*git\/actions\.ts/,
);
assert.ok(
fake.gitRuns().some((args) => args.join(" ") === "log --no-merges --format=* %s%n%n%b origin/main..HEAD"),
);
assert.equal(fake.node("git-pr-title")?.["value"], "Write commit messages");
assert.equal(fake.node("git-pr-body")?.["value"], "- The agent writes them.");
fake.invoke("git-pr-body", "onChange", { value: "- Edited." });
await fake.settle();
fake.invoke("git-pr-open");
await fake.settle();
assert.deepEqual(fake.gitRuns().at(-1), ["push"]);
assert.deepEqual(fake.ghCalls.at(-1), [
"pr",
"create",
"--base",
"main",
"--title",
"Write commit messages",
"--body",
"- Edited.",
]);
assert.ok(fake.rendered().includes("Pull request #12"));
fake.invoke("git-pr-link");
await fake.settle();
assert.deepEqual(fake.opened, [link]);
});
});
test("typing in the form stops the agent, and gh's error shows as it is while the form stays", async () => {
const aborted = { value: false };
const prompt = untilAborted(aborted);
const refused = "pull request create failed: GraphQL: No commits between main and feature";
await withPanel({ prompt, gh: gh({ "pr create": { code: 1, stderr: `${refused}\n` } }) }, async (fake) => {
fake.invoke("git-pr-create");
await fake.settle();
assert.equal(fake.node("git-pr-title")?.["placeholder"], "Writing the title…");
assert.equal(fake.node("git-pr-open")?.["disabled"], true);
fake.invoke("git-pr-title", "onChange", { value: "My title" });
await fake.settle();
assert.equal(aborted.value, true);
assert.equal(fake.node("git-pr-title")?.["placeholder"], "Title");
fake.invoke("git-pr-open");
await fake.settle();
assert.deepEqual(fake.notices, [refused]);
assert.equal(fake.node("git-pr-title")?.["value"], "My title");
fake.invoke("git-pr-cancel");
await fake.settle();
assert.ok(fake.node("git-pr-create"));
});
});
test("a branch with no commits over the default branch is told so and nothing is written", async () => {
const empty = (args: readonly string[]) => (args[0] === "log" ? { stdout: "" } : repository(args));
await withPanel({ run: empty }, async (fake) => {
fake.invoke("git-pr-create");
await fake.settle();
assert.deepEqual(fake.notices, ['The branch "feature" has no commits that are not on "main".']);
assert.deepEqual(fake.prompts, []);
assert.ok(fake.node("git-pr-create"));
});
});
test("switching branch drops the form and stops its writing, and looks the new branch up", async () => {
const aborted = { value: false };
const prompt = untilAborted(aborted);
await withPanel({ prompt }, async (fake) => {
fake.invoke("git-pr-create");
await fake.settle();
assert.ok(fake.node("git-pr-title"));
fake.setStatus({ branch: "other", upstream: "origin/other" });
fake.emit({ event: "git_changed", workspaceId: "w1" });
await fake.settle();
assert.equal(aborted.value, true);
assert.equal(fake.node("git-pr-title"), undefined);
assert.ok(fake.node("git-pr-create"));
assert.deepEqual(fake.ghCalls.at(-1), [
"pr",
"list",
"--head",
"other",
"--state",
"open",
"--json",
"url,number",
"--limit",
"1",
]);
});
});
test("a pull request that opens after another branch came on screen does not show as that branch's link", async () => {
const hold = held();
const answers = gh();
const waiting: Gh = async (args) => {
if (args[1] === "create") await hold.promise;
return answers(args);
};
await withPanel({ prompt: async () => answer("Add a thing"), gh: waiting }, async (fake) => {
fake.invoke("git-pr-create");
await fake.settle();
fake.invoke("git-pr-open");
await fake.settle();
fake.setStatus({ branch: "other", upstream: "origin/other" });
fake.emit({ event: "git_changed", workspaceId: "w1" });
await fake.settle();
hold.release();
await fake.settle();
assert.ok(!fake.rendered().includes("Pull request #12"));
assert.ok(fake.node("git-pr-create"));
});
});
test("a title cleared while the branch is pushed keeps the pull request from opening", async () => {
const hold = held();
const run = async (args: readonly string[]) => {
if (args[0] === "push") await hold.promise;
return repository(args) ?? {};
};
await withPanel(
{ prompt: async () => answer("Add a thing"), run, status: { ...feature, ahead: 1 } },
async (fake) => {
fake.invoke("git-pr-create");
await fake.settle();
fake.invoke("git-pr-open");
await fake.settle();
fake.invoke("git-pr-title", "onChange", { value: " " });
hold.release();
await fake.settle();
assert.ok(!fake.ghCalls.some((args) => args[1] === "create"));
assert.equal(fake.node("git-pr-open")?.["disabled"], true);
},
);
});
test("Create Pull Request while Refresh looks the branch up keeps the form", async () => {
const hold = held();
const answers = gh();
let lists = 0;
const waiting: Gh = async (args) => {
if (args[1] === "list" && ++lists === 2) await hold.promise;
return answers(args);
};
await withPanel({ prompt: async () => answer("Add a thing"), gh: waiting }, async (fake) => {
fake.invoke("git-refresh");
await fake.settle();
fake.invoke("git-pr-create");
await fake.settle();
hold.release();
await fake.settle();
assert.equal(fake.node("git-pr-title")?.["value"], "Add a thing");
});
});
No comments yet.