Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
344 lines
13 KiB
TypeScript
344 lines
13 KiB
TypeScript
import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
|
|
import { describe, expect, it } from "vite-plus/test";
|
|
|
|
import {
|
|
applyGitStatusStreamEvent,
|
|
formatGeneratedBranchName,
|
|
buildTemporaryWorktreeBranchName,
|
|
flattenTemporaryWorktreeBranchName,
|
|
isTemporaryWorktreeBranch,
|
|
normalizeGitRemoteUrl,
|
|
parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
|
|
parseOriginUrlFromGitConfig,
|
|
WORKTREE_BRANCH_PREFIX,
|
|
} from "./git.ts";
|
|
|
|
describe("normalizeGitRemoteUrl", () => {
|
|
it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
|
|
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/T3Code.git")).toBe(
|
|
"github.com/t3tools/t3code",
|
|
);
|
|
expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
|
|
"github.com/t3tools/t3code",
|
|
);
|
|
expect(normalizeGitRemoteUrl("ssh://[email protected]/T3Tools/T3Code")).toBe(
|
|
"github.com/t3tools/t3code",
|
|
);
|
|
});
|
|
|
|
it("preserves nested group paths for providers like GitLab", () => {
|
|
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/platform/T3Code.git")).toBe(
|
|
"gitlab.com/t3tools/platform/t3code",
|
|
);
|
|
expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
|
|
"gitlab.com/t3tools/platform/t3code",
|
|
);
|
|
});
|
|
|
|
it("drops explicit ports from URL-shaped remotes", () => {
|
|
expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
|
|
"gitlab.company.com/team/project",
|
|
);
|
|
expect(normalizeGitRemoteUrl("ssh://[email protected]:2222/team/project.git")).toBe(
|
|
"gitlab.company.com/team/project",
|
|
);
|
|
});
|
|
|
|
it("normalizes SCP-like remotes with non-git SSH users", () => {
|
|
expect(normalizeGitRemoteUrl("[email protected]:group/project.git")).toBe(
|
|
"gitlab.example.com/group/project",
|
|
);
|
|
expect(normalizeGitRemoteUrl("[email protected]:workspace/repo.git")).toBe(
|
|
"bitbucket.org/workspace/repo",
|
|
);
|
|
});
|
|
|
|
it("gives an Azure DevOps repository the same key over SSH as over HTTPS", () => {
|
|
expect(normalizeGitRemoteUrl("[email protected]:v3/T3Tools/Platform/T3Code")).toBe(
|
|
"dev.azure.com/t3tools/platform/_git/t3code",
|
|
);
|
|
expect(normalizeGitRemoteUrl("ssh://[email protected]:22/v3/T3Tools/Platform/T3Code")).toBe(
|
|
"dev.azure.com/t3tools/platform/_git/t3code",
|
|
);
|
|
expect(
|
|
normalizeGitRemoteUrl("https://[email protected]/T3Tools/Platform/_git/T3Code"),
|
|
).toBe("dev.azure.com/t3tools/platform/_git/t3code");
|
|
});
|
|
|
|
it("puts the organization back in the host on the name dev.azure.com replaced", () => {
|
|
expect(
|
|
normalizeGitRemoteUrl("[email protected]:v3/T3Tools/Platform/T3Code"),
|
|
).toBe("t3tools.visualstudio.com/platform/_git/t3code");
|
|
expect(normalizeGitRemoteUrl("https://T3Tools.visualstudio.com/Platform/_git/T3Code")).toBe(
|
|
"t3tools.visualstudio.com/platform/_git/t3code",
|
|
);
|
|
});
|
|
|
|
it("leaves an Azure SSH host it cannot read as the path it was given", () => {
|
|
// Not `v3`, and not four segments: rewriting either would invent a repository that the web
|
|
// spelling has no name for, so the remote stands as it arrived.
|
|
expect(normalizeGitRemoteUrl("[email protected]:v4/T3Tools/Platform/T3Code")).toBe(
|
|
"ssh.dev.azure.com/v4/t3tools/platform/t3code",
|
|
);
|
|
expect(normalizeGitRemoteUrl("[email protected]:v3/T3Tools/T3Code")).toBe(
|
|
"ssh.dev.azure.com/v3/t3tools/t3code",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("parseOriginUrlFromGitConfig", () => {
|
|
it("reads the origin url and ignores other remotes", () => {
|
|
const config = [
|
|
"[core]",
|
|
"\trepositoryformatversion = 0",
|
|
'[remote "upstream"]',
|
|
"\turl = https://github.com/other/repo.git",
|
|
'[remote "origin"]',
|
|
"\turl = [email protected]:pingdotgg/t3code.git",
|
|
"\tfetch = +refs/heads/*:refs/remotes/origin/*",
|
|
'[branch "main"]',
|
|
"\tremote = origin",
|
|
].join("\n");
|
|
expect(parseOriginUrlFromGitConfig(config)).toBe("[email protected]:pingdotgg/t3code.git");
|
|
});
|
|
|
|
it("strips inline comments and quotes from the url value", () => {
|
|
expect(
|
|
parseOriginUrlFromGitConfig(
|
|
'[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
|
|
),
|
|
).toBe("https://github.com/acme/repo.git");
|
|
expect(
|
|
parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "[email protected]:acme/repo.git"\n'),
|
|
).toBe("[email protected]:acme/repo.git");
|
|
});
|
|
|
|
it("accepts legacy dotted headers, header comments, and line continuations", () => {
|
|
expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = [email protected]:a/b.git\n")).toBe(
|
|
"[email protected]:a/b.git",
|
|
);
|
|
// Git folds the dotted form to lowercase but keeps quoted names as written.
|
|
expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = [email protected]:a/b.git\n")).toBe(
|
|
"[email protected]:a/b.git",
|
|
);
|
|
expect(
|
|
parseOriginUrlFromGitConfig(
|
|
'[remote "Origin"]\n\turl = [email protected]:x/y.git\n[remote "origin"]\n\turl = [email protected]:a/b.git\n',
|
|
),
|
|
).toBe("[email protected]:a/b.git");
|
|
expect(
|
|
parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = [email protected]:a/b.git\n'),
|
|
).toBe("[email protected]:a/b.git");
|
|
expect(
|
|
parseOriginUrlFromGitConfig(
|
|
'[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
|
|
),
|
|
).toBe("https://github.com/acme/repo.git");
|
|
});
|
|
|
|
it("falls back to the first remote when there is no origin", () => {
|
|
const config = [
|
|
'[remote "upstream"]',
|
|
"\turl = https://github.com/acme/repo.git",
|
|
'[remote "fork"]',
|
|
"\turl = https://github.com/me/repo.git",
|
|
].join("\n");
|
|
expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
|
|
});
|
|
|
|
it("returns null when there is no remote section", () => {
|
|
expect(parseOriginUrlFromGitConfig("[core]\n\tbare = false\n")).toBeNull();
|
|
expect(parseOriginUrlFromGitConfig("")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
|
|
it("extracts the owner and repository from common GitHub remote shapes", () => {
|
|
expect(
|
|
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("[email protected]:T3Tools/T3Code.git"),
|
|
).toBe("T3Tools/T3Code");
|
|
expect(
|
|
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
|
|
).toBe("T3Tools/T3Code");
|
|
expect(
|
|
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("ssh://github.com/T3Tools/T3Code.git"),
|
|
).toBe("T3Tools/T3Code");
|
|
});
|
|
});
|
|
|
|
describe("isTemporaryWorktreeBranch", () => {
|
|
it("matches the generated temporary worktree refName format", () => {
|
|
expect(
|
|
isTemporaryWorktreeBranch(
|
|
buildTemporaryWorktreeBranchName((byteLength) => {
|
|
expect(byteLength).toBe(4);
|
|
return "DEADBEEF";
|
|
}),
|
|
),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("matches generated temporary worktree refs", () => {
|
|
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
|
|
expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
|
|
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
|
|
});
|
|
|
|
it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
|
|
expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
|
|
`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
|
|
);
|
|
});
|
|
|
|
it("matches legacy t3code temporary worktree refs", () => {
|
|
expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
|
|
expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
|
|
expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
|
|
expect(
|
|
isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
|
|
// version nibble is not 4
|
|
expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
|
|
// variant nibble is not [89ab]
|
|
expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
|
|
});
|
|
|
|
it("matches the flat fallback used when a plain t3 branch exists", () => {
|
|
const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
|
|
expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
|
|
expect(isTemporaryWorktreeBranch(flat)).toBe(true);
|
|
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
|
|
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
|
|
expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
|
|
`${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
|
|
);
|
|
expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
|
|
`${WORKTREE_BRANCH_PREFIX}-deadbeef`,
|
|
);
|
|
expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
|
|
`${WORKTREE_BRANCH_PREFIX}-deadbeef`,
|
|
);
|
|
});
|
|
|
|
it("rejects non-temporary refName names", () => {
|
|
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
|
|
expect(isTemporaryWorktreeBranch("main")).toBe(false);
|
|
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("applyGitStatusStreamEvent", () => {
|
|
it("treats a remote-only update as a repository when local state is missing", () => {
|
|
const remote: VcsStatusRemoteResult = {
|
|
hasUpstream: true,
|
|
aheadCount: 2,
|
|
behindCount: 1,
|
|
pr: null,
|
|
};
|
|
|
|
expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
|
|
isRepo: true,
|
|
hasPrimaryRemote: false,
|
|
isDefaultRef: false,
|
|
refName: null,
|
|
hasWorkingTreeChanges: false,
|
|
workingTree: { files: [], insertions: 0, deletions: 0 },
|
|
hasUpstream: true,
|
|
aheadCount: 2,
|
|
behindCount: 1,
|
|
pr: null,
|
|
});
|
|
});
|
|
|
|
it("preserves local-only fields when applying a remote update", () => {
|
|
const current: VcsStatusResult = {
|
|
isRepo: true,
|
|
sourceControlProvider: {
|
|
kind: "github",
|
|
name: "GitHub",
|
|
baseUrl: "https://github.com",
|
|
},
|
|
hasPrimaryRemote: true,
|
|
isDefaultRef: false,
|
|
refName: "feature/demo",
|
|
hasWorkingTreeChanges: true,
|
|
workingTree: {
|
|
files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
|
|
insertions: 1,
|
|
deletions: 0,
|
|
},
|
|
hasUpstream: false,
|
|
aheadCount: 0,
|
|
behindCount: 0,
|
|
pr: null,
|
|
};
|
|
|
|
const remote: VcsStatusRemoteResult = {
|
|
hasUpstream: true,
|
|
aheadCount: 2,
|
|
behindCount: 1,
|
|
pr: null,
|
|
};
|
|
|
|
expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
|
|
...current,
|
|
hasUpstream: true,
|
|
aheadCount: 2,
|
|
behindCount: 1,
|
|
pr: null,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("formatGeneratedBranchName", () => {
|
|
it.each(["t3code", "t3code/"])("joins static prefix %s with one slash", (prefix) => {
|
|
expect(
|
|
formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
|
|
).toBe("t3code/add-search");
|
|
});
|
|
it("supports an empty prefix and preserves user prefix casing", () => {
|
|
expect(
|
|
formatGeneratedBranchName("Add Search", { mode: "static", prefix: "", instructions: "" }),
|
|
).toBe("add-search");
|
|
expect(
|
|
formatGeneratedBranchName("Add Search", {
|
|
mode: "static",
|
|
prefix: "Team/Julius/",
|
|
instructions: "",
|
|
}),
|
|
).toBe("Team/Julius/add-search");
|
|
});
|
|
it.each([
|
|
["release..candidate", "release-candidate/add-search"],
|
|
[" Team / Jules.lock/", "Team/Jules-lock/add-search"],
|
|
["-team//feature@{new}", "team/feature-new/add-search"],
|
|
[" /?. / ", "add-search"],
|
|
])("normalizes invalid static prefix %s", (prefix, expected) => {
|
|
expect(
|
|
formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
|
|
).toBe(expected);
|
|
});
|
|
it("uses the model's semantic prefix without the stored static prefix", () => {
|
|
expect(
|
|
formatGeneratedBranchName("feat/Add Search", {
|
|
mode: "semantic",
|
|
prefix: "t3code",
|
|
instructions: "",
|
|
}),
|
|
).toBe("feat/add-search");
|
|
});
|
|
it("preserves the full custom name, including case, dots and length", () => {
|
|
const branch = `Julius/ABC-123/release.v2-${"x".repeat(70)}`;
|
|
expect(
|
|
formatGeneratedBranchName(` ${branch} `, {
|
|
mode: "custom",
|
|
prefix: "ignored",
|
|
instructions: "",
|
|
}),
|
|
).toBe(branch);
|
|
});
|
|
});
|