|
1 | | -import { EnvironmentId, type VcsRef } from "@t3tools/contracts"; |
| 1 | +import type { ThreadRuntimeSummary } from "@t3tools/client-runtime/state/shell"; |
| 2 | +import { EnvironmentId, ProviderInstanceId, RunId, type VcsRef } from "@t3tools/contracts"; |
2 | 3 | import { describe, expect, it } from "vite-plus/test"; |
3 | 4 | import { |
4 | 5 | dedupeRemoteBranchesWithLocalMatches, |
5 | 6 | deriveLocalBranchNameFromRemoteRef, |
| 7 | + isWorktreeChangeBlocked, |
6 | 8 | resolveEnvironmentOptionLabel, |
7 | 9 | resolveBranchSelectionTarget, |
8 | 10 | resolveCurrentWorkspaceLabel, |
@@ -896,3 +898,88 @@ describe("sanitizeNewRefName", () => { |
896 | 898 | expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar"); |
897 | 899 | }); |
898 | 900 | }); |
| 901 | + |
| 902 | +describe("isWorktreeChangeBlocked", () => { |
| 903 | + const runtime = ( |
| 904 | + status: ThreadRuntimeSummary["status"], |
| 905 | + activeRunId: string | null = "run-1", |
| 906 | + ): ThreadRuntimeSummary => ({ |
| 907 | + status, |
| 908 | + activeRunId: activeRunId === null ? null : RunId.make(activeRunId), |
| 909 | + providerInstanceId: ProviderInstanceId.make("codex"), |
| 910 | + providerName: null, |
| 911 | + lastError: null, |
| 912 | + updatedAt: "2026-10-02T00:00:00.000Z", |
| 913 | + }); |
| 914 | + const secondaryWorktree = "/repo/.t3/worktrees/feature-a"; |
| 915 | + |
| 916 | + it.each(["preparing", "starting", "running", "waiting"] as const)( |
| 917 | + "blocks a worktree change while the run is %s", |
| 918 | + (status) => { |
| 919 | + expect( |
| 920 | + isWorktreeChangeBlocked({ |
| 921 | + runtime: runtime(status), |
| 922 | + currentWorktreePath: null, |
| 923 | + nextWorktreePath: secondaryWorktree, |
| 924 | + }), |
| 925 | + ).toBe(true); |
| 926 | + }, |
| 927 | + ); |
| 928 | + |
| 929 | + it("blocks a worktree change while a queued run waits on an attached run", () => { |
| 930 | + expect( |
| 931 | + isWorktreeChangeBlocked({ |
| 932 | + runtime: runtime("queued"), |
| 933 | + currentWorktreePath: null, |
| 934 | + nextWorktreePath: secondaryWorktree, |
| 935 | + }), |
| 936 | + ).toBe(true); |
| 937 | + }); |
| 938 | + |
| 939 | + it.each([ |
| 940 | + ["the thread has no runtime", null], |
| 941 | + ["the thread is idle", runtime("idle", null)], |
| 942 | + ["the last run completed", runtime("completed", null)], |
| 943 | + ["the last run was interrupted", runtime("interrupted", null)], |
| 944 | + ["only a queued run remains", runtime("queued", null)], |
| 945 | + ])("allows a worktree change when %s", (_label, threadRuntime) => { |
| 946 | + expect( |
| 947 | + isWorktreeChangeBlocked({ |
| 948 | + runtime: threadRuntime, |
| 949 | + currentWorktreePath: null, |
| 950 | + nextWorktreePath: secondaryWorktree, |
| 951 | + }), |
| 952 | + ).toBe(false); |
| 953 | + }); |
| 954 | + |
| 955 | + it.each([ |
| 956 | + ["the main checkout", null], |
| 957 | + ["a secondary worktree", secondaryWorktree], |
| 958 | + ])("allows staying in %s mid-run", (_label, worktreePath) => { |
| 959 | + expect( |
| 960 | + isWorktreeChangeBlocked({ |
| 961 | + runtime: runtime("running"), |
| 962 | + currentWorktreePath: worktreePath, |
| 963 | + nextWorktreePath: worktreePath, |
| 964 | + }), |
| 965 | + ).toBe(false); |
| 966 | + }); |
| 967 | + |
| 968 | + it.each([ |
| 969 | + ["a ref checked out in the main repo", { isDefault: false, worktreePath: "/repo" }], |
| 970 | + ["the default ref with no checkout", { isDefault: true, worktreePath: null }], |
| 971 | + ])("blocks returning to the main checkout via %s mid-run", (_label, refName) => { |
| 972 | + const { nextWorktreePath } = resolveBranchSelectionTarget({ |
| 973 | + activeProjectCwd: "/repo", |
| 974 | + activeWorktreePath: secondaryWorktree, |
| 975 | + refName, |
| 976 | + }); |
| 977 | + expect( |
| 978 | + isWorktreeChangeBlocked({ |
| 979 | + runtime: runtime("running"), |
| 980 | + currentWorktreePath: secondaryWorktree, |
| 981 | + nextWorktreePath, |
| 982 | + }), |
| 983 | + ).toBe(true); |
| 984 | + }); |
| 985 | +}); |
0 commit comments