|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 3 | +import { ResumePrompt } from "../ResumePrompt"; |
| 4 | +import type { SessionState } from "../../../types/task"; |
| 5 | + |
| 6 | +function makeSession(overrides: Partial<SessionState> = {}): SessionState { |
| 7 | + return { |
| 8 | + task_id: 1, |
| 9 | + session_id: "sess-abc", |
| 10 | + last_prompt: "Implement the user authentication flow", |
| 11 | + working_dir: "/home/user/project", |
| 12 | + saved_at: "2026-03-23T10:00:00Z", |
| 13 | + ...overrides, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +describe("ResumePrompt", () => { |
| 18 | + const onResume = vi.fn(); |
| 19 | + const onFresh = vi.fn(); |
| 20 | + const onDismiss = vi.fn(); |
| 21 | + |
| 22 | + it("renders nothing when sessions array is empty", () => { |
| 23 | + const { container } = render( |
| 24 | + <ResumePrompt sessions={[]} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 25 | + ); |
| 26 | + expect(container.innerHTML).toBe(""); |
| 27 | + }); |
| 28 | + |
| 29 | + it("renders a banner for each interrupted session", () => { |
| 30 | + const sessions = [ |
| 31 | + makeSession({ task_id: 1 }), |
| 32 | + makeSession({ task_id: 2, last_prompt: "Fix the build pipeline" }), |
| 33 | + ]; |
| 34 | + render( |
| 35 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 36 | + ); |
| 37 | + const resumeButtons = screen.getAllByTestId("resume-btn"); |
| 38 | + expect(resumeButtons).toHaveLength(2); |
| 39 | + }); |
| 40 | + |
| 41 | + it("shows truncated prompt text (first 80 chars + '...')", () => { |
| 42 | + const longPrompt = |
| 43 | + "This is a very long prompt that exceeds eighty characters and should be truncated with an ellipsis at the end"; |
| 44 | + const sessions = [makeSession({ last_prompt: longPrompt })]; |
| 45 | + render( |
| 46 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 47 | + ); |
| 48 | + expect(screen.getByText(longPrompt.slice(0, 80) + "...")).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("shows full prompt if under 80 chars (no ellipsis)", () => { |
| 52 | + const shortPrompt = "Fix the login bug"; |
| 53 | + const sessions = [makeSession({ last_prompt: shortPrompt })]; |
| 54 | + render( |
| 55 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 56 | + ); |
| 57 | + expect(screen.getByText(shortPrompt)).toBeInTheDocument(); |
| 58 | + // Should not have an ellipsis |
| 59 | + expect(screen.queryByText(shortPrompt + "...")).not.toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("shows working directory", () => { |
| 63 | + const sessions = [makeSession({ working_dir: "/projects/my-app" })]; |
| 64 | + render( |
| 65 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 66 | + ); |
| 67 | + expect(screen.getByText("/projects/my-app")).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("Resume button calls onResume with correct taskId", () => { |
| 71 | + const sessions = [makeSession({ task_id: 42 })]; |
| 72 | + render( |
| 73 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 74 | + ); |
| 75 | + fireEvent.click(screen.getByTestId("resume-btn")); |
| 76 | + expect(onResume).toHaveBeenCalledWith(42); |
| 77 | + }); |
| 78 | + |
| 79 | + it("Fresh button calls onFresh with correct taskId", () => { |
| 80 | + const sessions = [makeSession({ task_id: 7 })]; |
| 81 | + render( |
| 82 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 83 | + ); |
| 84 | + fireEvent.click(screen.getByTestId("fresh-btn")); |
| 85 | + expect(onFresh).toHaveBeenCalledWith(7); |
| 86 | + }); |
| 87 | + |
| 88 | + it("Dismiss button calls onDismiss with correct taskId", () => { |
| 89 | + const sessions = [makeSession({ task_id: 99 })]; |
| 90 | + render( |
| 91 | + <ResumePrompt sessions={sessions} onResume={onResume} onFresh={onFresh} onDismiss={onDismiss} />, |
| 92 | + ); |
| 93 | + fireEvent.click(screen.getByTestId("dismiss-btn")); |
| 94 | + expect(onDismiss).toHaveBeenCalledWith(99); |
| 95 | + }); |
| 96 | +}); |
0 commit comments