| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | import { vi } from "vitest"; |
| 4 | |
| 5 | import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js"; |
| 6 | |
| 7 | // Mock the 'open' module to prevent actual URL opening during tests |
| 8 | vi.mock("open", () => ({ |
| 9 | default: vi.fn(), |
| 10 | })); |
| 11 | |
| 12 | // Mock the NavigationContext |
| 13 | vi.mock("../context/NavigationContext.js", () => ({ |
| 14 | useNavigation: () => ({ |
| 15 | navigateTo: vi.fn(), |
| 16 | closeCurrentScreen: vi.fn(), |
| 17 | isScreenActive: vi.fn(() => false), |
| 18 | state: { currentScreen: "free-trial", screenData: null }, |
| 19 | }), |
| 20 | })); |
| 21 | |
| 22 | describe("FreeTrialTransitionUI - Rendering and Props Tests", () => { |
| 23 | const mockOnReload = vi.fn(); |
| 24 | |
| 25 | beforeEach(() => { |
| 26 | vi.clearAllMocks(); |
| 27 | }); |
| 28 | |
| 29 | describe("Initial Choice Screen Rendering", () => { |
| 30 | it("displays all three options with proper formatting", () => { |
| 31 | const { lastFrame } = render( |
| 32 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 33 | ); |
| 34 | |
| 35 | const frame = lastFrame(); |
| 36 | expect(frame).toContain("🚀 Free trial limit reached!"); |
| 37 | expect(frame).toContain("1. 💳 Sign up for models add-on"); |
| 38 | expect(frame).toContain("2. 🔑 Enter your Anthropic API key"); |
| 39 | expect(frame).toContain("3. ⚙️ Switch to a different configuration"); |
| 40 | expect(frame).toContain("↑/↓ to navigate or 1/2/3 to select"); |
| 41 | }); |
| 42 | |
| 43 | it("highlights first option by default", () => { |
| 44 | const { lastFrame } = render( |
| 45 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 46 | ); |
| 47 | |
| 48 | const frame = lastFrame(); |
| 49 | expect(frame).toContain("➤ 1. 💳 Sign up for models add-on"); |
| 50 | expect(frame).not.toContain("➤ 2. 🔑 Enter your Anthropic API key"); |
| 51 | expect(frame).not.toContain( |
| 52 | "➤ 3. ⚙️ Switch to a different configuration", |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | it("renders without crashing when all props are provided", () => { |
| 57 | const { lastFrame } = render( |
| 58 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 59 | ); |
| 60 | |
| 61 | const frame = lastFrame(); |
| 62 | expect(frame).toBeDefined(); |
| 63 | expect(frame?.length).toBeGreaterThan(0); |
| 64 | }); |
| 65 | |
| 66 | it("renders without crashing when onShowConfigSelector is not provided", () => { |
| 67 | const { lastFrame } = render( |
| 68 | <FreeTrialTransitionUI |
| 69 | onReload={mockOnReload} |
| 70 | // onShowConfigSelector not provided |
| 71 | />, |
| 72 | ); |
| 73 | |
| 74 | const frame = lastFrame(); |
| 75 | expect(frame).toBeDefined(); |
| 76 | expect(frame?.length).toBeGreaterThan(0); |
| 77 | expect(frame).toContain("3. ⚙️ Switch to a different configuration"); |
| 78 | }); |
| 79 | |
| 80 | it("includes proper user instructions", () => { |
| 81 | const { lastFrame } = render( |
| 82 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 83 | ); |
| 84 | |
| 85 | const frame = lastFrame(); |
| 86 | expect(frame).toContain("Choose how you'd like to continue:"); |
| 87 | expect(frame).toContain( |
| 88 | "↑/↓ to navigate or 1/2/3 to select, Enter to confirm", |
| 89 | ); |
| 90 | }); |
| 91 | |
| 92 | it("shows all options with proper descriptions", () => { |
| 93 | const { lastFrame } = render( |
| 94 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 95 | ); |
| 96 | |
| 97 | const frame = lastFrame(); |
| 98 | expect(frame).toContain("Sign up for models add-on (recommended)"); |
| 99 | expect(frame).toContain("Enter your Anthropic API key"); |
| 100 | expect(frame).toContain("Switch to a different configuration"); |
| 101 | }); |
| 102 | }); |
| 103 | |
| 104 | describe("Component Props and Interface", () => { |
| 105 | it("accepts all required callback props", () => { |
| 106 | // Test that component accepts and renders with all props |
| 107 | expect(() => { |
| 108 | render(<FreeTrialTransitionUI onReload={mockOnReload} />); |
| 109 | }).not.toThrow(); |
| 110 | }); |
| 111 | |
| 112 | it("accepts optional onShowConfigSelector prop", () => { |
| 113 | // Test that component works without onShowConfigSelector |
| 114 | expect(() => { |
| 115 | render(<FreeTrialTransitionUI onReload={mockOnReload} />); |
| 116 | }).not.toThrow(); |
| 117 | }); |
| 118 | }); |
| 119 | |
| 120 | describe("Visual Styling and Layout", () => { |
| 121 | it("renders with proper border styling", () => { |
| 122 | const { lastFrame } = render( |
| 123 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 124 | ); |
| 125 | |
| 126 | const frame = lastFrame(); |
| 127 | // Check for box border characters (unicode box drawing) |
| 128 | expect(frame).toMatch(/[╭╮╯╰│─]/); |
| 129 | }); |
| 130 | |
| 131 | it("displays with proper spacing and formatting", () => { |
| 132 | const { lastFrame } = render( |
| 133 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 134 | ); |
| 135 | |
| 136 | const frame = lastFrame(); |
| 137 | // Should have proper spacing between lines |
| 138 | expect(frame?.split("\n").length).toBeGreaterThan(5); |
| 139 | }); |
| 140 | |
| 141 | it("shows selection indicator on first option by default", () => { |
| 142 | const { lastFrame } = render( |
| 143 | <FreeTrialTransitionUI onReload={mockOnReload} />, |
| 144 | ); |
| 145 | |
| 146 | const frame = lastFrame(); |
| 147 | const lines = frame?.split("\n") || []; |
| 148 | const modelsLine = lines.find((line) => |
| 149 | line.includes("Sign up for models add-on"), |
| 150 | ); |
| 151 | const apiKeyLine = lines.find((line) => |
| 152 | line.includes("Enter your Anthropic API key"), |
| 153 | ); |
| 154 | const configLine = lines.find((line) => |
| 155 | line.includes("Switch to a different configuration"), |
| 156 | ); |
| 157 | |
| 158 | expect(modelsLine).toContain("➤"); |
| 159 | expect(apiKeyLine).not.toContain("➤"); |
| 160 | expect(configLine).not.toContain("➤"); |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | describe("URL Opening Security", () => { |
| 165 | it("should use mocked open function to prevent actual URL opening", async () => { |
| 166 | // This test verifies that we properly mock the 'open' module |
| 167 | // to prevent actual URLs from being opened during test runs |
| 168 | const openModule = await import("open"); |
| 169 | expect(openModule.default).toBeDefined(); |
| 170 | expect(typeof openModule.default).toBe("function"); |
| 171 | }); |
| 172 | }); |
| 173 | }); |