732 lines · 21.2 KB · TypeScript
RawDownload
import { EnvironmentId, type VcsRef } from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test";import {  dedupeRemoteBranchesWithLocalMatches,  deriveLocalBranchNameFromRemoteRef,  resolveEnvironmentOptionLabel,  resolveBranchSelectionTarget,  resolveCurrentWorkspaceLabel,  resolveDraftEnvModeAfterBranchChange,  resolveEffectiveEnvMode,  resolveEnvModeLabel,  resolveBranchTriggerLabel,  resolveBranchToolbarPrBranch,  resolveBranchToolbarValue,  resolveLockedWorkspaceLabel,  resolveLocalCheckoutBranchMismatch,  resolvePreviousWorktreeLabel,  resolvePreviousWorktreeSeed,  shouldIncludeBranchPickerItem,  shouldShowComposerContextStrip,  shouldShowEnvironmentIndicator,} from "./BranchToolbar.logic"; const localEnvironmentId = EnvironmentId.make("environment-local");const remoteEnvironmentId = EnvironmentId.make("environment-remote"); describe("resolvePreviousWorktreeSeed", () => {  it("picks the most recently updated worktree thread", () => {    expect(      resolvePreviousWorktreeSeed({        threads: [          {            branch: "t3/older",            worktreePath: "/repo/.t3/worktrees/older",            updatedAt: "2026-07-20T00:00:00.000Z",          },          {            branch: "t3/newer",            worktreePath: "/repo/.t3/worktrees/newer",            updatedAt: "2026-07-22T00:00:00.000Z",          },          { branch: "main", worktreePath: null, updatedAt: "2026-07-23T00:00:00.000Z" },        ],        currentWorktreePath: null,      }),    ).toEqual({ branch: "t3/newer", worktreePath: "/repo/.t3/worktrees/newer" });  });   it("skips the worktree the composer already points at", () => {    expect(      resolvePreviousWorktreeSeed({        threads: [          {            branch: "t3/current",            worktreePath: "/repo/.t3/worktrees/current",            updatedAt: "2026-07-22T00:00:00.000Z",          },        ],        currentWorktreePath: "/repo/.t3/worktrees/current",      }),    ).toBeNull();  });   it("returns null when no thread has a worktree", () => {    expect(      resolvePreviousWorktreeSeed({        threads: [{ branch: "main", worktreePath: null, updatedAt: "2026-07-22T00:00:00.000Z" }],        currentWorktreePath: null,      }),    ).toBeNull();  });   it("ignores archived threads and threads with unparseable timestamps", () => {    expect(      resolvePreviousWorktreeSeed({        threads: [          {            branch: "t3/archived",            worktreePath: "/repo/.t3/worktrees/archived",            updatedAt: "2026-07-23T00:00:00.000Z",            archivedAt: "2026-07-23T01:00:00.000Z",          },          {            branch: "t3/garbage-timestamp",            worktreePath: "/repo/.t3/worktrees/garbage",            updatedAt: "not-a-date",          },          {            branch: "t3/live",            worktreePath: "/repo/.t3/worktrees/live",            updatedAt: "2026-07-21T00:00:00.000Z",            archivedAt: null,          },        ],        currentWorktreePath: null,      }),    ).toEqual({ branch: "t3/live", worktreePath: "/repo/.t3/worktrees/live" });  });}); describe("resolvePreviousWorktreeLabel", () => {  it("includes the branch when known", () => {    expect(resolvePreviousWorktreeLabel({ branch: "t3/fix-thing", worktreePath: "/wt" })).toBe(      "Previous worktree (t3/fix-thing)",    );    expect(resolvePreviousWorktreeLabel({ branch: null, worktreePath: "/wt" })).toBe(      "Previous worktree",    );  });}); describe("resolveDraftEnvModeAfterBranchChange", () => {  it("switches to local mode when returning from an existing worktree to the main worktree", () => {    expect(      resolveDraftEnvModeAfterBranchChange({        nextWorktreePath: null,        currentWorktreePath: "/repo/.t3/worktrees/feature-a",        effectiveEnvMode: "worktree",      }),    ).toBe("local");  });   it("keeps new-worktree mode when selecting a base ref before worktree creation", () => {    expect(      resolveDraftEnvModeAfterBranchChange({        nextWorktreePath: null,        currentWorktreePath: null,        effectiveEnvMode: "worktree",      }),    ).toBe("worktree");  });   it("uses worktree mode when selecting a ref already attached to a worktree", () => {    expect(      resolveDraftEnvModeAfterBranchChange({        nextWorktreePath: "/repo/.t3/worktrees/feature-a",        currentWorktreePath: null,        effectiveEnvMode: "local",      }),    ).toBe("worktree");  });}); describe("resolveBranchToolbarValue", () => {  it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {    expect(      resolveBranchToolbarValue({        envMode: "worktree",        activeWorktreePath: null,        activeThreadBranch: null,        currentGitBranch: "main",      }),    ).toBe("main");  });   it("keeps an explicitly selected worktree base ref", () => {    expect(      resolveBranchToolbarValue({        envMode: "worktree",        activeWorktreePath: null,        activeThreadBranch: "feature/base",        currentGitBranch: "main",      }),    ).toBe("feature/base");  });   it("shows the actual checked-out ref when not selecting a new worktree base", () => {    expect(      resolveBranchToolbarValue({        envMode: "local",        activeWorktreePath: null,        activeThreadBranch: "feature/base",        currentGitBranch: "main",      }),    ).toBe("main");  });}); describe("resolveBranchTriggerLabel", () => {  it("shows the origin ref when a new worktree will start from origin", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: "main",        resolvedActiveBranchIsRemote: false,        startFromOrigin: true,      }),    ).toBe("From origin/main");  });   it("shows the origin ref for local branch names that contain slashes", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: "feature/demo",        resolvedActiveBranchIsRemote: false,        startFromOrigin: true,      }),    ).toBe("From origin/feature/demo");  });   it("shows the local ref when start from origin is disabled", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: "main",        resolvedActiveBranchIsRemote: false,        startFromOrigin: false,      }),    ).toBe("From main");  });   it("does not duplicate the origin prefix for an explicit remote ref", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: "origin/feature/demo",        resolvedActiveBranchIsRemote: true,        startFromOrigin: true,      }),    ).toBe("From origin/feature/demo");  });   it("preserves an explicit ref from a non-origin remote", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: "upstream/feature/demo",        resolvedActiveBranchIsRemote: true,        startFromOrigin: true,      }),    ).toBe("From upstream/feature/demo");  });   it("keeps current-checkout labels and empty state unchanged", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "local",        resolvedActiveBranch: "main",        resolvedActiveBranchIsRemote: false,        startFromOrigin: true,      }),    ).toBe("main");    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: null,        resolvedActiveBranchIsRemote: null,        startFromOrigin: true,      }),    ).toBe("Select ref");  });   it("does not fabricate an origin ref while branch metadata is loading", () => {    expect(      resolveBranchTriggerLabel({        activeWorktreePath: null,        effectiveEnvMode: "worktree",        resolvedActiveBranch: "upstream/feature/demo",        resolvedActiveBranchIsRemote: null,        startFromOrigin: true,      }),    ).toBe("From upstream/feature/demo");  });}); describe("resolveBranchToolbarPrBranch", () => {  it("uses the explicit thread branch when it matches the displayed branch", () => {    expect(      resolveBranchToolbarPrBranch({        activeThreadBranch: "feature/current",        resolvedActiveBranch: "feature/current",      }),    ).toBe("feature/current");  });   it("hides PR state while an optimistic branch switch is in flight", () => {    expect(      resolveBranchToolbarPrBranch({        activeThreadBranch: "feature/current",        resolvedActiveBranch: "feature/next",      }),    ).toBeNull();  });   it("does not infer PR state without an explicit thread branch", () => {    expect(      resolveBranchToolbarPrBranch({        activeThreadBranch: null,        resolvedActiveBranch: "feature/current",      }),    ).toBeNull();  });}); describe("resolveLocalCheckoutBranchMismatch", () => {  it("detects when a local thread is associated with a different branch than the checkout", () => {    expect(      resolveLocalCheckoutBranchMismatch({        effectiveEnvMode: "local",        activeWorktreePath: null,        activeThreadBranch: "feature/thread",        currentGitBranch: "feature/current",      }),    ).toEqual({      threadBranch: "feature/thread",      currentBranch: "feature/current",    });  });   it("ignores matching local checkout state", () => {    expect(      resolveLocalCheckoutBranchMismatch({        effectiveEnvMode: "local",        activeWorktreePath: null,        activeThreadBranch: "feature/thread",        currentGitBranch: "feature/thread",      }),    ).toBeNull();  });   it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {    expect(      resolveLocalCheckoutBranchMismatch({        effectiveEnvMode: "worktree",        activeWorktreePath: "/repo/.t3/worktrees/feature-thread",        activeThreadBranch: "feature/thread",        currentGitBranch: "feature/current",      }),    ).toBeNull();  });   it("ignores new-worktree base selection before a worktree exists", () => {    expect(      resolveLocalCheckoutBranchMismatch({        effectiveEnvMode: "worktree",        activeWorktreePath: null,        activeThreadBranch: "feature/base",        currentGitBranch: "main",      }),    ).toBeNull();  });}); describe("resolveEnvironmentOptionLabel", () => {  it("prefers the primary environment's machine label", () => {    expect(      resolveEnvironmentOptionLabel({        isPrimary: true,        environmentId: localEnvironmentId,        runtimeLabel: "Julius's Mac mini",        savedLabel: "Local environment",      }),    ).toBe("Julius's Mac mini");  });   it("falls back to 'This device' for generic primary labels", () => {    expect(      resolveEnvironmentOptionLabel({        isPrimary: true,        environmentId: localEnvironmentId,        runtimeLabel: "Local environment",        savedLabel: "Local",      }),    ).toBe("This device");  });   it("keeps configured labels for non-primary environments", () => {    expect(      resolveEnvironmentOptionLabel({        isPrimary: false,        environmentId: remoteEnvironmentId,        runtimeLabel: null,        savedLabel: "Build box",      }),    ).toBe("Build box");  });}); describe("shouldShowEnvironmentIndicator", () => {  it("shows the indicator whenever multiple environments are pickable", () => {    expect(      shouldShowEnvironmentIndicator({        activeEnvironment: { isPrimary: true },        canPickEnvironment: true,      }),    ).toBe(true);  });   it("shows a sole remote environment so the user knows where the project runs", () => {    expect(      shouldShowEnvironmentIndicator({        activeEnvironment: { isPrimary: false },        canPickEnvironment: false,      }),    ).toBe(true);  });   it("hides a sole primary (this-device) environment", () => {    expect(      shouldShowEnvironmentIndicator({        activeEnvironment: { isPrimary: true },        canPickEnvironment: false,      }),    ).toBe(false);  });   it("hides the indicator when the active environment is unknown", () => {    expect(      shouldShowEnvironmentIndicator({        activeEnvironment: null,        canPickEnvironment: false,      }),    ).toBe(false);  });}); describe("shouldShowComposerContextStrip", () => {  it("keeps the environment indicator visible for a non-Git project", () => {    expect(      shouldShowComposerContextStrip({        hasActiveProject: true,        isGitRepo: false,        showEnvironmentIndicator: true,      }),    ).toBe(true);  });   it("hides the strip when a non-Git project has no environment indicator", () => {    expect(      shouldShowComposerContextStrip({        hasActiveProject: true,        isGitRepo: false,        showEnvironmentIndicator: false,      }),    ).toBe(false);  });   it("shows Git controls without requiring an environment indicator", () => {    expect(      shouldShowComposerContextStrip({        hasActiveProject: true,        isGitRepo: true,        showEnvironmentIndicator: false,      }),    ).toBe(true);  });}); describe("resolveEffectiveEnvMode", () => {  it("treats draft threads already attached to a worktree as current-checkout mode", () => {    expect(      resolveEffectiveEnvMode({        activeWorktreePath: "/repo/.t3/worktrees/feature-a",        hasServerThread: false,        draftThreadEnvMode: "worktree",      }),    ).toBe("local");  });   it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {    expect(      resolveEffectiveEnvMode({        activeWorktreePath: null,        hasServerThread: false,        draftThreadEnvMode: "worktree",      }),    ).toBe("worktree");  });}); describe("resolveEnvModeLabel", () => {  it("uses explicit workspace labels", () => {    expect(resolveEnvModeLabel("local")).toBe("Current checkout");    expect(resolveEnvModeLabel("worktree")).toBe("New worktree");  });}); describe("resolveCurrentWorkspaceLabel", () => {  it("describes the main repo checkout when no worktree path is active", () => {    expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");  });   it("describes the active checkout as a worktree when one is attached", () => {    expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");  });}); describe("resolveLockedWorkspaceLabel", () => {  it("uses a shorter label for the main repo checkout", () => {    expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");  }); 

Lines 1–500 of 732.