| 1 | // Mock useTerminalSize hook |
| 2 | vi.mock("./hooks/useTerminalSize.js", () => ({ |
| 3 | useTerminalSize: () => ({ columns: 80, rows: 24 }), |
| 4 | })); |
| 5 | |
| 6 | // Mock sentry to prevent initialization issues |
| 7 | vi.mock("../sentry.js", () => ({ |
| 8 | sentryService: { |
| 9 | captureException: vi.fn(), |
| 10 | captureMessage: vi.fn(), |
| 11 | setUser: vi.fn(), |
| 12 | setTag: vi.fn(), |
| 13 | setContext: vi.fn(), |
| 14 | addBreadcrumb: vi.fn(), |
| 15 | startSpan: vi.fn(), |
| 16 | flush: vi.fn(), |
| 17 | close: vi.fn(), |
| 18 | isEnabled: vi.fn().mockReturnValue(false), |
| 19 | }, |
| 20 | })); |
| 21 | |
| 22 | import { render } from "ink-testing-library"; |
| 23 | import React from "react"; |
| 24 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 25 | |
| 26 | import { UpdateStatus } from "../services/types.js"; |
| 27 | |
| 28 | import { UpdateNotification } from "./UpdateNotification.js"; |
| 29 | |
| 30 | // Mock useServices hook |
| 31 | const mockUseServices = vi.fn(); |
| 32 | vi.mock("../hooks/useService.js", () => ({ |
| 33 | useServices: () => mockUseServices(), |
| 34 | })); |
| 35 | |
| 36 | describe("UpdateNotification", () => { |
| 37 | beforeEach(() => { |
| 38 | vi.clearAllMocks(); |
| 39 | // Default mock implementation - no update available |
| 40 | mockUseServices.mockReturnValue({ |
| 41 | services: { |
| 42 | update: { |
| 43 | autoUpdate: true, |
| 44 | status: UpdateStatus.IDLE, |
| 45 | message: "Continue CLI v1.0.0", |
| 46 | error: null, |
| 47 | isUpdateAvailable: false, |
| 48 | latestVersion: null, |
| 49 | currentVersion: "1.0.0", |
| 50 | }, |
| 51 | }, |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | afterEach(() => { |
| 56 | vi.clearAllMocks(); |
| 57 | }); |
| 58 | |
| 59 | it("should show default message when no update is available", () => { |
| 60 | const { lastFrame } = render(<UpdateNotification />); |
| 61 | |
| 62 | expect(lastFrame()).toContain("◉ Continue CLI v1.0.0"); |
| 63 | }); |
| 64 | |
| 65 | it("should show update available message when update is available", () => { |
| 66 | mockUseServices.mockReturnValue({ |
| 67 | services: { |
| 68 | update: { |
| 69 | autoUpdate: true, |
| 70 | status: UpdateStatus.IDLE, |
| 71 | message: "Update available: v1.0.1", |
| 72 | error: null, |
| 73 | isUpdateAvailable: true, |
| 74 | latestVersion: "1.0.1", |
| 75 | currentVersion: "1.0.0", |
| 76 | }, |
| 77 | }, |
| 78 | }); |
| 79 | |
| 80 | const { lastFrame } = render(<UpdateNotification />); |
| 81 | |
| 82 | expect(lastFrame()).toContain("◉ Update available: v1.0.1"); |
| 83 | }); |
| 84 | |
| 85 | it("should show checking message when checking for updates", () => { |
| 86 | mockUseServices.mockReturnValue({ |
| 87 | services: { |
| 88 | update: { |
| 89 | autoUpdate: true, |
| 90 | status: UpdateStatus.CHECKING, |
| 91 | message: "Checking for updates", |
| 92 | error: null, |
| 93 | isUpdateAvailable: false, |
| 94 | latestVersion: null, |
| 95 | currentVersion: "1.0.0", |
| 96 | }, |
| 97 | }, |
| 98 | }); |
| 99 | |
| 100 | const { lastFrame } = render(<UpdateNotification />); |
| 101 | |
| 102 | expect(lastFrame()).toContain("◉ Checking for updates"); |
| 103 | }); |
| 104 | |
| 105 | it("should show updating message when updating", () => { |
| 106 | mockUseServices.mockReturnValue({ |
| 107 | services: { |
| 108 | update: { |
| 109 | autoUpdate: true, |
| 110 | status: UpdateStatus.UPDATING, |
| 111 | message: "Updating to v1.0.1", |
| 112 | error: null, |
| 113 | isUpdateAvailable: false, |
| 114 | latestVersion: "1.0.1", |
| 115 | currentVersion: "1.0.0", |
| 116 | }, |
| 117 | }, |
| 118 | }); |
| 119 | |
| 120 | const { lastFrame } = render(<UpdateNotification />); |
| 121 | |
| 122 | expect(lastFrame()).toContain("◉ Updating to v1.0.1"); |
| 123 | }); |
| 124 | |
| 125 | it("should show updated message when update completes", () => { |
| 126 | mockUseServices.mockReturnValue({ |
| 127 | services: { |
| 128 | update: { |
| 129 | autoUpdate: true, |
| 130 | status: UpdateStatus.UPDATED, |
| 131 | message: "Auto-updated to v1.0.1", |
| 132 | error: null, |
| 133 | isUpdateAvailable: false, |
| 134 | latestVersion: "1.0.1", |
| 135 | currentVersion: "1.0.0", |
| 136 | }, |
| 137 | }, |
| 138 | }); |
| 139 | |
| 140 | const { lastFrame } = render(<UpdateNotification />); |
| 141 | |
| 142 | expect(lastFrame()).toContain("◉ Auto-updated to v1.0.1"); |
| 143 | }); |
| 144 | |
| 145 | it("should show error message when update fails", () => { |
| 146 | mockUseServices.mockReturnValue({ |
| 147 | services: { |
| 148 | update: { |
| 149 | autoUpdate: true, |
| 150 | status: UpdateStatus.ERROR, |
| 151 | message: "Update failed", |
| 152 | error: new Error("Update error"), |
| 153 | isUpdateAvailable: true, |
| 154 | latestVersion: "1.0.1", |
| 155 | currentVersion: "1.0.0", |
| 156 | }, |
| 157 | }, |
| 158 | }); |
| 159 | |
| 160 | const { lastFrame } = render(<UpdateNotification />); |
| 161 | |
| 162 | expect(lastFrame()).toContain("◉ Update failed"); |
| 163 | }); |
| 164 | |
| 165 | it("should show remote mode when in remote mode and no update available", () => { |
| 166 | mockUseServices.mockReturnValue({ |
| 167 | services: { |
| 168 | update: { |
| 169 | autoUpdate: true, |
| 170 | status: UpdateStatus.IDLE, |
| 171 | message: "Continue CLI v1.0.0", |
| 172 | error: null, |
| 173 | isUpdateAvailable: false, |
| 174 | latestVersion: null, |
| 175 | currentVersion: "1.0.0", |
| 176 | }, |
| 177 | }, |
| 178 | }); |
| 179 | |
| 180 | const { lastFrame } = render(<UpdateNotification isRemoteMode={true} />); |
| 181 | |
| 182 | expect(lastFrame()).toContain("◉ Remote Mode"); |
| 183 | }); |
| 184 | |
| 185 | it("should show update message even in remote mode when update is available", () => { |
| 186 | mockUseServices.mockReturnValue({ |
| 187 | services: { |
| 188 | update: { |
| 189 | autoUpdate: true, |
| 190 | status: UpdateStatus.IDLE, |
| 191 | message: "Update available: v1.0.1", |
| 192 | error: null, |
| 193 | isUpdateAvailable: true, |
| 194 | latestVersion: "1.0.1", |
| 195 | currentVersion: "1.0.0", |
| 196 | }, |
| 197 | }, |
| 198 | }); |
| 199 | |
| 200 | const { lastFrame } = render(<UpdateNotification isRemoteMode={true} />); |
| 201 | |
| 202 | expect(lastFrame()).toContain("◉ Update available: v1.0.1"); |
| 203 | }); |
| 204 | |
| 205 | it("should show default message when update service has no message", () => { |
| 206 | mockUseServices.mockReturnValue({ |
| 207 | services: { |
| 208 | update: { |
| 209 | autoUpdate: true, |
| 210 | status: UpdateStatus.IDLE, |
| 211 | message: "", |
| 212 | error: null, |
| 213 | isUpdateAvailable: false, |
| 214 | latestVersion: null, |
| 215 | currentVersion: "1.0.0", |
| 216 | }, |
| 217 | }, |
| 218 | }); |
| 219 | |
| 220 | const { lastFrame } = render(<UpdateNotification />); |
| 221 | |
| 222 | expect(lastFrame()).toContain("◉ Continue CLI"); |
| 223 | }); |
| 224 | }); |