import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";import { assert, describe, expect, it } from "@effect/vitest";import { Effect } from "effect";import * as Random from "effect/Random"; import { applyGitStatusStreamEvent, buildTemporaryWorktreeBranchName, isTemporaryWorktreeBranch, normalizeGitRemoteUrl, parseGitHubRepositoryNameWithOwnerFromRemoteUrl, WORKTREE_BRANCH_PREFIX,} from "./git.ts"; function deterministicRandom(seed = 0x1234_5678): { nextIntUnsafe: () => number; nextDoubleUnsafe: () => number;} { let state = seed >>> 0; const nextIntUnsafe = (): number => { state = (Math.imul(1_664_525, state) + 1_013_904_223) >>> 0; return state; }; return { nextIntUnsafe, nextDoubleUnsafe: () => nextIntUnsafe() / 0x1_0000_0000, };} describe("normalizeGitRemoteUrl", () => { it("canonicalizes equivalent GitHub remotes across protocol variants", () => { expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe( "github.com/t3tools/t3code", ); expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe( "github.com/t3tools/t3code", ); expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe( "github.com/t3tools/t3code", ); }); it("preserves nested group paths for providers like GitLab", () => { expect(normalizeGitRemoteUrl("git@gitlab.com: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://git@gitlab.company.com:2222/team/project.git")).toBe( "gitlab.company.com/team/project", ); });}); describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => { it("extracts the owner and repository from common GitHub remote shapes", () => { expect( parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"), ).toBe("T3Tools/T3Code"); expect( parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"), ).toBe("T3Tools/T3Code"); });}); describe("isTemporaryWorktreeBranch", () => { it.effect("matches the generated temporary worktree refName format", () => Effect.gen(function* () { const branch = yield* buildTemporaryWorktreeBranchName; assert.equal(isTemporaryWorktreeBranch(branch), true); }), ); it.effect("uses the Effect Random service for deterministic temporary refs", () => Effect.gen(function* () { const firstBranch = yield* buildTemporaryWorktreeBranchName.pipe( Effect.provideService(Random.Random, deterministicRandom()), ); const secondBranch = yield* buildTemporaryWorktreeBranchName.pipe( Effect.provideService(Random.Random, deterministicRandom()), ); assert.equal(firstBranch, secondBranch); assert.equal(isTemporaryWorktreeBranch(firstBranch), 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("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, }); });});
import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
import { assert, describe, expect, it } from "@effect/vitest";
import { Effect } from "effect";
import * as Random from "effect/Random";
import {
applyGitStatusStreamEvent,
buildTemporaryWorktreeBranchName,
isTemporaryWorktreeBranch,
normalizeGitRemoteUrl,
parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
WORKTREE_BRANCH_PREFIX,
} from "./git.ts";
function deterministicRandom(seed = 0x1234_5678): {
nextIntUnsafe: () => number;
nextDoubleUnsafe: () => number;
} {
let state = seed >>> 0;
const nextIntUnsafe = (): number => {
state = (Math.imul(1_664_525, state) + 1_013_904_223) >>> 0;
return state;
};
return {
nextIntUnsafe,
nextDoubleUnsafe: () => nextIntUnsafe() / 0x1_0000_0000,
};
}
describe("normalizeGitRemoteUrl", () => {
it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe(
"github.com/t3tools/t3code",
);
expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
"github.com/t3tools/t3code",
);
expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe(
"github.com/t3tools/t3code",
);
});
it("preserves nested group paths for providers like GitLab", () => {
expect(normalizeGitRemoteUrl("git@gitlab.com: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://git@gitlab.company.com:2222/team/project.git")).toBe(
"gitlab.company.com/team/project",
);
});
});
describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
it("extracts the owner and repository from common GitHub remote shapes", () => {
expect(
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
).toBe("T3Tools/T3Code");
expect(
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
).toBe("T3Tools/T3Code");
});
});
describe("isTemporaryWorktreeBranch", () => {
it.effect("matches the generated temporary worktree refName format", () =>
Effect.gen(function* () {
const branch = yield* buildTemporaryWorktreeBranchName;
assert.equal(isTemporaryWorktreeBranch(branch), true);
}),
);
it.effect("uses the Effect Random service for deterministic temporary refs", () =>
Effect.gen(function* () {
const firstBranch = yield* buildTemporaryWorktreeBranchName.pipe(
Effect.provideService(Random.Random, deterministicRandom()),
);
const secondBranch = yield* buildTemporaryWorktreeBranchName.pipe(
Effect.provideService(Random.Random, deterministicRandom()),
);
assert.equal(firstBranch, secondBranch);
assert.equal(isTemporaryWorktreeBranch(firstBranch), 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("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,
});
});
});