The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ThreadId, type WorktreeSetupSnapshot } from "@t3tools/contracts"; |
| 2 | import { expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | import { act, fakeTimers, render } from "../../../test/render"; |
| 5 | import { WorktreeSetupCard } from "./WorktreeSetupCard"; |
| 6 | |
| 7 | const NOW = new Date("2026-10-26T12:00:10.000Z"); |
| 8 | |
| 9 | function snapshot(overrides: Partial<WorktreeSetupSnapshot>): WorktreeSetupSnapshot { |
| 10 | return { |
| 11 | threadId: ThreadId.make("thread-1"), |
| 12 | phase: "running", |
| 13 | startedAt: "2026-10-26T12:00:00.000Z", |
| 14 | endedAt: null, |
| 15 | branch: "t3/feature", |
| 16 | baseRef: "main", |
| 17 | worktreePath: "/repo/.t3/worktrees/feature", |
| 18 | setupScript: null, |
| 19 | stages: [ |
| 20 | { |
| 21 | id: "fetch", |
| 22 | status: "done", |
| 23 | startedAt: "2026-10-26T12:00:00.000Z", |
| 24 | endedAt: "2026-10-26T12:00:03.000Z", |
| 25 | percent: null, |
| 26 | detail: null, |
| 27 | tail: [], |
| 28 | }, |
| 29 | { |
| 30 | id: "checkout", |
| 31 | status: "running", |
| 32 | startedAt: "2026-10-26T12:00:03.000Z", |
| 33 | endedAt: null, |
| 34 | percent: null, |
| 35 | detail: null, |
| 36 | tail: [], |
| 37 | }, |
| 38 | ], |
| 39 | error: null, |
| 40 | sequence: 1, |
| 41 | ...overrides, |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | const card = (value: WorktreeSetupSnapshot) => ( |
| 46 | <WorktreeSetupCard snapshot={value} onCancel={null} onWorkLocally={null} onOpenTerminal={null} /> |
| 47 | ); |
| 48 | |
| 49 | it("ticks the running stage while finished stages keep their duration", async () => { |
| 50 | using _timers = fakeTimers(); |
| 51 | vi.setSystemTime(NOW); |
| 52 | await using screen = await render(card(snapshot({}))); |
| 53 | // Assert synchronously: expect.element polls on timers these tests fake. |
| 54 | const section = screen.getByRole("region", { name: "Worktree setup" }); |
| 55 | |
| 56 | expect(section.element().textContent).toContain("Fetch base branch3s"); |
| 57 | expect(section.element().textContent).toContain("Check out files7s"); |
| 58 | |
| 59 | await act(async () => { |
| 60 | await vi.advanceTimersByTimeAsync(5_000); |
| 61 | }); |
| 62 | expect(section.element().textContent).toContain("Fetch base branch3s"); |
| 63 | expect(section.element().textContent).toContain("Check out files12s"); |
| 64 | }); |
| 65 | |
| 66 | it("stops a stage interrupted by the setup ending at the setup's end", async () => { |
| 67 | using _timers = fakeTimers(); |
| 68 | vi.setSystemTime(NOW); |
| 69 | const failed = snapshot({ phase: "failed", endedAt: "2026-10-26T12:00:05.000Z" }); |
| 70 | await using screen = await render(card(failed)); |
| 71 | const section = screen.getByRole("region", { name: "Worktree setup" }); |
| 72 | |
| 73 | expect(section.element().textContent).toContain("Check out files2s"); |
| 74 | await act(async () => { |
| 75 | await vi.advanceTimersByTimeAsync(5_000); |
| 76 | }); |
| 77 | expect(section.element().textContent).toContain("Check out files2s"); |
| 78 | }); |