From ad5b3a6a29b204cb25751cbeae57bb25b6f08dd8 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 6 May 2026 03:29:38 +0000 Subject: [PATCH] Use Effect services in shared git helpers Co-authored-by: Julius Marminge --- apps/web/src/components/ChatView.tsx | 3 +- packages/shared/src/git.test.ts | 41 +++++++++++++-- packages/shared/src/git.ts | 25 ++++++--- packages/shared/src/sourceControl.test.ts | 64 ++++++++++++++++------- packages/shared/src/sourceControl.ts | 21 +++++--- 5 files changed, 116 insertions(+), 38 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 9ef221e262..9908034bc0 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -72,6 +72,7 @@ import { togglePendingUserInputOptionSelection, type PendingUserInputDraftAnswer, } from "../pendingUserInput"; +import * as Effect from "effect/Effect"; import { selectProjectsAcrossEnvironments, selectThreadsAcrossEnvironments, @@ -2845,7 +2846,7 @@ export default function ChatView(props: ChatViewProps) { prepareWorktree: { projectCwd: activeProject.cwd, baseBranch: baseBranchForWorktree, - branch: buildTemporaryWorktreeBranchName(), + branch: Effect.runSync(buildTemporaryWorktreeBranchName), }, runSetupScript: true, } diff --git a/packages/shared/src/git.test.ts b/packages/shared/src/git.test.ts index 5eb9fd5124..99b6e7a329 100644 --- a/packages/shared/src/git.test.ts +++ b/packages/shared/src/git.test.ts @@ -1,5 +1,7 @@ import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts"; -import { describe, expect, it } from "vitest"; +import { assert, describe, expect, it } from "@effect/vitest"; +import { Effect } from "effect"; +import * as Random from "effect/Random"; import { applyGitStatusStreamEvent, @@ -10,6 +12,20 @@ import { WORKTREE_BRANCH_PREFIX, } from "./git.ts"; +function deterministicRandom(seed = 0x1234_5678): Random.Random { + 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, + shuffle: (elements) => Effect.succeed(elements), + }; +} + describe("normalizeGitRemoteUrl", () => { it("canonicalizes equivalent GitHub remotes across protocol variants", () => { expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe( @@ -54,9 +70,26 @@ describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => { }); describe("isTemporaryWorktreeBranch", () => { - it("matches the generated temporary worktree refName format", () => { - expect(isTemporaryWorktreeBranch(buildTemporaryWorktreeBranchName())).toBe(true); - }); + 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); diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts index 1ad17f52bd..2ee606d73b 100644 --- a/packages/shared/src/git.ts +++ b/packages/shared/src/git.ts @@ -7,6 +7,7 @@ import type { VcsStatusStreamEvent, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Random from "effect/Random"; import { detectSourceControlProviderFromRemoteUrl } from "./sourceControl.ts"; @@ -86,10 +87,12 @@ export function deriveLocalBranchNameFromRemoteRef(branchName: string): string { return branchName.slice(firstSeparatorIndex + 1); } -export function buildTemporaryWorktreeBranchName(): string { - const token = Effect.runSync(Random.nextUUIDv4).replace(/-/g, "").slice(0, 8).toLowerCase(); - return `${WORKTREE_BRANCH_PREFIX}/${token}`; -} +export const buildTemporaryWorktreeBranchName = Random.nextUUIDv4.pipe( + Effect.map((uuid) => { + const token = uuid.replace(/-/g, "").slice(0, 8).toLowerCase(); + return `${WORKTREE_BRANCH_PREFIX}/${token}`; + }), +); export function isTemporaryWorktreeBranch(refName: string): boolean { return TEMP_WORKTREE_BRANCH_PATTERN.test(refName.trim().toLowerCase()); @@ -131,10 +134,12 @@ export function normalizeGitRemoteUrl(value: string): string { /** * Best-effort parse of a GitHub `owner/repo` identifier from common remote URL shapes. */ -export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null { +export function parseGitHubRepositoryNameWithOwnerFromRemoteUrlOption( + url: string | null, +): Option.Option { const trimmed = url?.trim() ?? ""; if (trimmed.length === 0) { - return null; + return Option.none(); } const match = @@ -142,7 +147,13 @@ export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | nu trimmed, ); const repositoryNameWithOwner = match?.[1]?.trim() ?? ""; - return repositoryNameWithOwner.length > 0 ? repositoryNameWithOwner : null; + return repositoryNameWithOwner.length > 0 + ? Option.some(repositoryNameWithOwner) + : Option.none(); +} + +export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null { + return Option.getOrNull(parseGitHubRepositoryNameWithOwnerFromRemoteUrlOption(url)); } function deriveLocalBranchNameCandidatesFromRemoteRef( diff --git a/packages/shared/src/sourceControl.test.ts b/packages/shared/src/sourceControl.test.ts index 697e65f091..c578380f35 100644 --- a/packages/shared/src/sourceControl.test.ts +++ b/packages/shared/src/sourceControl.test.ts @@ -1,59 +1,83 @@ -import { describe, expect, it } from "vitest"; +import { assert, describe, it } from "@effect/vitest"; +import { Option } from "effect"; import { detectSourceControlProviderFromRemoteUrl, getChangeRequestTerminologyForKind, + parseRemoteHostForSourceControl, resolveChangeRequestPresentation, } from "./sourceControl.ts"; describe("source control presentation", () => { it("uses merge request terminology for GitLab", () => { - expect(getChangeRequestTerminologyForKind("gitlab")).toEqual({ + assert.deepEqual(getChangeRequestTerminologyForKind("gitlab"), { shortLabel: "MR", singular: "merge request", }); }); it("uses pull request terminology for GitHub-compatible providers", () => { - expect(getChangeRequestTerminologyForKind("github")).toEqual({ + assert.deepEqual(getChangeRequestTerminologyForKind("github"), { shortLabel: "PR", singular: "pull request", }); - expect(getChangeRequestTerminologyForKind("azure-devops")).toEqual({ + assert.deepEqual(getChangeRequestTerminologyForKind("azure-devops"), { shortLabel: "PR", singular: "pull request", }); - expect(getChangeRequestTerminologyForKind("bitbucket")).toEqual({ + assert.deepEqual(getChangeRequestTerminologyForKind("bitbucket"), { shortLabel: "PR", singular: "pull request", }); }); it("falls back to generic change request copy for unknown providers", () => { - expect( - resolveChangeRequestPresentation({ kind: "unknown", name: "forge", baseUrl: "" }), - ).toEqual( - expect.objectContaining({ - shortName: "change request", - longName: "change request", - }), - ); + const presentation = resolveChangeRequestPresentation({ + kind: "unknown", + name: "forge", + baseUrl: "", + }); + + assert.equal(presentation.shortName, "change request"); + assert.equal(presentation.longName, "change request"); }); }); describe("detectSourceControlProviderFromRemoteUrl", () => { it("detects common source control hosts", () => { - expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe( + assert.equal( + detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind, "github", ); - expect( + assert.equal( detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind, - ).toBe("gitlab"); - expect( + "gitlab", + ); + assert.equal( detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind, - ).toBe("azure-devops"); - expect( + "azure-devops", + ); + assert.equal( detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind, - ).toBe("bitbucket"); + "bitbucket", + ); + }); +}); + +describe("parseRemoteHostOption", () => { + it("returns parsed hosts as options", () => { + assert.deepEqual( + parseRemoteHostForSourceControl("git@github.com:owner/repo.git"), + Option.some("github.com"), + ); + assert.deepEqual( + parseRemoteHostForSourceControl("https://gitlab.com/group/repo.git"), + Option.some("gitlab.com"), + ); + }); + + it("returns none for empty or invalid remotes", () => { + assert.equal(Option.isNone(parseRemoteHostForSourceControl(" ")), true); + assert.equal(Option.isNone(parseRemoteHostForSourceControl("not a url")), true); }); }); diff --git a/packages/shared/src/sourceControl.ts b/packages/shared/src/sourceControl.ts index 24ff3a0da8..ba42eabd37 100644 --- a/packages/shared/src/sourceControl.ts +++ b/packages/shared/src/sourceControl.ts @@ -1,4 +1,5 @@ import type { SourceControlProviderInfo, SourceControlProviderKind } from "@t3tools/contracts"; +import { Option } from "effect"; export interface ChangeRequestPresentation { readonly icon: "github" | "gitlab" | "azure-devops" | "bitbucket" | "change-request"; @@ -133,28 +134,36 @@ export function getChangeRequestTerminologyForKind( }; } -function parseRemoteHost(remoteUrl: string): string | null { +function parseRemoteHostOption(remoteUrl: string): Option.Option { const trimmed = remoteUrl.trim(); if (trimmed.length === 0) { - return null; + return Option.none(); } if (trimmed.startsWith("git@")) { const hostWithPath = trimmed.slice("git@".length); const separatorIndex = hostWithPath.search(/[:/]/); if (separatorIndex <= 0) { - return null; + return Option.none(); } - return hostWithPath.slice(0, separatorIndex).toLowerCase(); + return Option.some(hostWithPath.slice(0, separatorIndex).toLowerCase()); } try { - return new URL(trimmed).hostname.toLowerCase(); + return Option.some(new URL(trimmed).hostname.toLowerCase()); } catch { - return null; + return Option.none(); } } +function parseRemoteHost(remoteUrl: string): string | null { + return Option.getOrNull(parseRemoteHostOption(remoteUrl)); +} + +export function parseRemoteHostForSourceControl(remoteUrl: string): Option.Option { + return parseRemoteHostOption(remoteUrl); +} + function toBaseUrl(host: string): string { return `https://${host}`; }