|
| 1 | +// Fork-only: the Fork menu item is built once the thread has run on a |
| 2 | +// configured provider, disabled while the thread is busy (active run or |
| 3 | +// pending request) or while a fork is in flight. |
| 4 | +import { ProviderDriverKind, ProviderInstanceId, RunId } from "@t3tools/contracts"; |
1 | 5 | import { describe, expect, it } from "vite-plus/test"; |
2 | 6 |
|
3 | | -import { buildThreadActionMenuItems, type ThreadActionMenuState } from "./threadActionMenu.logic"; |
4 | | - |
5 | | -const baseState: ThreadActionMenuState = { |
6 | | - branch: null, |
7 | | - projectFilter: null, |
8 | | - isPinned: false, |
9 | | - isSettled: false, |
10 | | - autoSettleEnabled: true, |
11 | | - isSnoozed: false, |
12 | | - canSnoozeNow: true, |
13 | | - isRegeneratingTitle: false, |
14 | | - isRunning: false, |
15 | | - hasManualOrder: false, |
16 | | - supports: { |
17 | | - settlement: true, |
18 | | - autoSettleOptOut: true, |
19 | | - snooze: true, |
20 | | - pinning: true, |
21 | | - titleRegeneration: true, |
22 | | - }, |
23 | | - snoozePresets: [ |
24 | | - { id: "hour", label: "In 1 hour", whenLabel: "3:00 PM", snoozedUntil: "2026-08-07T15:00:00Z" }, |
25 | | - ], |
| 7 | +import type { ThreadActionMenuState } from "./threadActionMenu.logic.ts"; |
| 8 | +import { |
| 9 | + forkThreadMenuItems, |
| 10 | + forkThreadMenuStateFork, |
| 11 | + type ForkableThreadProviderState, |
| 12 | + type ForkThreadShell, |
| 13 | +} from "./threadActionMenu.logic.fork.ts"; |
| 14 | + |
| 15 | +const CODEX = ProviderDriverKind.make("codex"); |
| 16 | +const OPENCODE = ProviderDriverKind.make("opencode"); |
| 17 | +const NOW = "2026-08-20T00:00:00.000Z"; |
| 18 | + |
| 19 | +/** Mirror of the upstream test's base state, with the fork slice filled in. */ |
| 20 | +const baseFork: ForkableThreadProviderState = { |
| 21 | + provider: CODEX, |
| 22 | + busy: false, |
| 23 | + inFlight: false, |
| 24 | +}; |
| 25 | + |
| 26 | +const baseState: Pick<ThreadActionMenuState, "fork"> = { fork: baseFork }; |
| 27 | + |
| 28 | +const completedRun: NonNullable<ForkThreadShell["latestRun"]> = { |
| 29 | + runId: RunId.make("run-1"), |
| 30 | + status: "completed", |
| 31 | + requestedAt: "2026-08-19T23:00:00.000Z", |
| 32 | + startedAt: "2026-08-19T23:00:01.000Z", |
| 33 | + completedAt: "2026-08-19T23:05:00.000Z", |
| 34 | + assistantMessageId: null, |
26 | 35 | }; |
27 | 36 |
|
28 | | -function ids(state: ThreadActionMenuState): string[] { |
29 | | - return buildThreadActionMenuItems(state).map((item) => item.id); |
30 | | -} |
31 | | - |
32 | | -describe("buildThreadActionMenuItems reset order (fork)", () => { |
33 | | - it("offers reset order only while the thread carries a manual order key", () => { |
34 | | - expect(ids(baseState)).not.toContain("reset-order"); |
35 | | - const item = buildThreadActionMenuItems({ ...baseState, hasManualOrder: true }).find( |
36 | | - (candidate) => candidate.id === "reset-order", |
37 | | - ); |
38 | | - expect(item).toMatchObject({ label: "Reset order", icon: "arrow-down-up" }); |
| 37 | +const runtime = ( |
| 38 | + status: NonNullable<ForkThreadShell["runtime"]>["status"], |
| 39 | +): ForkThreadShell["runtime"] => ({ |
| 40 | + status, |
| 41 | + activeRunId: status === "idle" ? null : RunId.make("run-2"), |
| 42 | + providerInstanceId: ProviderInstanceId.make("codex"), |
| 43 | + providerName: null, |
| 44 | + lastError: null, |
| 45 | + updatedAt: NOW, |
| 46 | +}); |
| 47 | + |
| 48 | +/** The quiescence slice the busy check reads: a quiet thread with one finished run. */ |
| 49 | +const shell = (overrides: Partial<ForkThreadShell> = {}): ForkThreadShell => ({ |
| 50 | + latestRun: completedRun, |
| 51 | + runtime: runtime("idle"), |
| 52 | + hasPendingApprovals: false, |
| 53 | + hasPendingUserInput: false, |
| 54 | + latestUserMessageAt: null, |
| 55 | + ...overrides, |
| 56 | +}); |
| 57 | + |
| 58 | +describe("forkThreadMenuStateFork", () => { |
| 59 | + it("builds state for any configured provider once the thread has run", () => { |
| 60 | + expect(forkThreadMenuStateFork(CODEX, false, shell(), NOW)).toEqual({ |
| 61 | + provider: CODEX, |
| 62 | + busy: false, |
| 63 | + inFlight: false, |
| 64 | + }); |
| 65 | + // V2 forks portably where the provider cannot fork natively. |
| 66 | + expect(forkThreadMenuStateFork(OPENCODE, false, shell(), NOW)).not.toBeNull(); |
| 67 | + expect(forkThreadMenuStateFork(null, false, shell(), NOW)).toBeNull(); |
| 68 | + expect(forkThreadMenuStateFork(CODEX, false, shell({ latestRun: null }), NOW)).toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + it.each([ |
| 72 | + { name: "a running run", shell: shell({ runtime: runtime("running") }) }, |
| 73 | + { name: "a starting run", shell: shell({ runtime: runtime("starting") }) }, |
| 74 | + { name: "a queued run", shell: shell({ runtime: runtime("queued") }) }, |
| 75 | + { name: "a run waiting on the user", shell: shell({ runtime: runtime("waiting") }) }, |
| 76 | + { name: "a pending approval", shell: shell({ hasPendingApprovals: true }) }, |
| 77 | + { name: "a pending user input", shell: shell({ hasPendingUserInput: true }) }, |
| 78 | + { |
| 79 | + name: "an unadopted user message", |
| 80 | + shell: shell({ latestUserMessageAt: "2026-08-19T23:59:30.000Z" }), |
| 81 | + }, |
| 82 | + ])("marks the thread busy for $name", ({ shell: busyShell }) => { |
| 83 | + expect(forkThreadMenuStateFork(CODEX, false, busyShell, NOW)?.busy).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("passes the in-flight flag through", () => { |
| 87 | + expect(forkThreadMenuStateFork(CODEX, true, shell(), NOW)?.inFlight).toBe(true); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("forkThreadMenuItems", () => { |
| 92 | + it("renders a disabled Fork item while busy or in flight", () => { |
| 93 | + expect(forkThreadMenuItems(baseState)).toEqual([ |
| 94 | + { id: "fork", label: "Fork thread", icon: "git-fork", disabled: false }, |
| 95 | + ]); |
| 96 | + expect(forkThreadMenuItems({ fork: { ...baseFork, busy: true } })[0]?.disabled).toBe(true); |
| 97 | + expect(forkThreadMenuItems({ fork: { ...baseFork, inFlight: true } })[0]?.disabled).toBe(true); |
39 | 98 | }); |
40 | 99 |
|
41 | | - it("orders reset before the lifecycle actions", () => { |
42 | | - const items = buildThreadActionMenuItems({ ...baseState, hasManualOrder: true }); |
43 | | - const resetIndex = items.findIndex((item) => item.id === "reset-order"); |
44 | | - const settleIndex = items.findIndex((item) => item.id === "settle"); |
45 | | - expect(resetIndex).toBeGreaterThanOrEqual(0); |
46 | | - expect(resetIndex).toBeLessThan(settleIndex); |
| 100 | + it("omits the item entirely without fork state", () => { |
| 101 | + expect(forkThreadMenuItems({ fork: null })).toEqual([]); |
| 102 | + expect(forkThreadMenuItems({})).toEqual([]); |
47 | 103 | }); |
48 | 104 | }); |
0 commit comments