|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +// vscode must be mocked before importing any module that imports it. |
| 4 | +// vi.mock is hoisted — factory cannot reference outer-scope variables. |
| 5 | +vi.mock("vscode", () => { |
| 6 | + const ThemeColor = class { |
| 7 | + constructor(public readonly id: string) {} |
| 8 | + }; |
| 9 | + |
| 10 | + const statusBarItem = { |
| 11 | + text: "", |
| 12 | + tooltip: undefined as string | undefined, |
| 13 | + backgroundColor: undefined as unknown, |
| 14 | + command: undefined as string | undefined, |
| 15 | + show: vi.fn(), |
| 16 | + hide: vi.fn(), |
| 17 | + dispose: vi.fn(), |
| 18 | + }; |
| 19 | + |
| 20 | + const terminal = { |
| 21 | + show: vi.fn(), |
| 22 | + sendText: vi.fn(), |
| 23 | + }; |
| 24 | + |
| 25 | + return { |
| 26 | + window: { |
| 27 | + createStatusBarItem: vi.fn(() => statusBarItem), |
| 28 | + showWarningMessage: vi.fn(), |
| 29 | + createTerminal: vi.fn(() => terminal), |
| 30 | + }, |
| 31 | + workspace: { |
| 32 | + getConfiguration: vi.fn(() => ({ |
| 33 | + get: (_key: string, def: unknown) => def, |
| 34 | + })), |
| 35 | + }, |
| 36 | + env: { |
| 37 | + openExternal: vi.fn(), |
| 38 | + }, |
| 39 | + StatusBarAlignment: { Left: 1 }, |
| 40 | + ThemeColor, |
| 41 | + Uri: { |
| 42 | + parse: (s: string) => ({ toString: () => s }), |
| 43 | + }, |
| 44 | + }; |
| 45 | +}); |
| 46 | + |
| 47 | +import * as vscode from "vscode"; |
| 48 | +import { DoctorStatusProvider } from "./doctor.status.js"; |
| 49 | +import { StatusBar } from "../status/bar.js"; |
| 50 | +import type { KaganClient } from "../api/client.js"; |
| 51 | +import type { DoctorReportResponse } from "../api/types.js"; |
| 52 | + |
| 53 | +function makeReport(overrides: Partial<DoctorReportResponse> = {}): DoctorReportResponse { |
| 54 | + return { |
| 55 | + checks: [], |
| 56 | + ok: true, |
| 57 | + fail_count: 0, |
| 58 | + warn_count: 0, |
| 59 | + ...overrides, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function makeClient(report?: DoctorReportResponse | null): Pick<KaganClient, "getDoctor"> { |
| 64 | + if (report === null) { |
| 65 | + return { getDoctor: vi.fn().mockRejectedValue(new Error("ECONNREFUSED")) }; |
| 66 | + } |
| 67 | + return { getDoctor: vi.fn().mockResolvedValue(report ?? makeReport()) }; |
| 68 | +} |
| 69 | + |
| 70 | +describe("DoctorStatusProvider", () => { |
| 71 | + let statusBar: StatusBar; |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + vi.clearAllMocks(); |
| 75 | + statusBar = new StatusBar(); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + vi.restoreAllMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("shows ready when all checks pass", async () => { |
| 83 | + const client = makeClient(makeReport({ ok: true, fail_count: 0, warn_count: 0 })); |
| 84 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 85 | + |
| 86 | + await provider.runPreflight(); |
| 87 | + |
| 88 | + const item = (vscode.window.createStatusBarItem as ReturnType<typeof vi.fn>).mock.results[0] |
| 89 | + .value as { text: string }; |
| 90 | + expect(item.text).toBe("Kagan: ready"); |
| 91 | + expect(vscode.window.showWarningMessage).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("shows degraded and no notification on WARN", async () => { |
| 95 | + const client = makeClient(makeReport({ ok: true, fail_count: 0, warn_count: 2 })); |
| 96 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 97 | + |
| 98 | + await provider.runPreflight(); |
| 99 | + |
| 100 | + const item = (vscode.window.createStatusBarItem as ReturnType<typeof vi.fn>).mock.results[0] |
| 101 | + .value as { text: string }; |
| 102 | + expect(item.text).toBe("$(alert) Kagan: degraded"); |
| 103 | + expect(vscode.window.showWarningMessage).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("shows setup needed and fires notification on FAIL", async () => { |
| 107 | + vi.mocked(vscode.window.showWarningMessage).mockResolvedValue(undefined as never); |
| 108 | + |
| 109 | + const client = makeClient(makeReport({ ok: false, fail_count: 1, warn_count: 0 })); |
| 110 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 111 | + |
| 112 | + await provider.runPreflight(); |
| 113 | + |
| 114 | + const item = (vscode.window.createStatusBarItem as ReturnType<typeof vi.fn>).mock.results[0] |
| 115 | + .value as { text: string }; |
| 116 | + expect(item.text).toBe("$(warning) Kagan: setup needed"); |
| 117 | + await vi.waitFor(() => |
| 118 | + expect(vscode.window.showWarningMessage).toHaveBeenCalledWith( |
| 119 | + "Kagan: setup needed — one or more required checks failed.", |
| 120 | + "Open TUI", |
| 121 | + "Open Web", |
| 122 | + ), |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it("does not show notification on WARN (only on FAIL)", async () => { |
| 127 | + const client = makeClient(makeReport({ ok: true, fail_count: 0, warn_count: 3 })); |
| 128 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 129 | + |
| 130 | + await provider.runPreflight(); |
| 131 | + |
| 132 | + expect(vscode.window.showWarningMessage).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it("opens a terminal with 'kagan tui' when Open TUI is chosen", async () => { |
| 136 | + vi.mocked(vscode.window.showWarningMessage).mockResolvedValue("Open TUI" as never); |
| 137 | + |
| 138 | + const client = makeClient(makeReport({ ok: false, fail_count: 1, warn_count: 0 })); |
| 139 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 140 | + |
| 141 | + await provider.runPreflight(); |
| 142 | + await vi.waitFor(() => expect(vscode.window.createTerminal).toHaveBeenCalled()); |
| 143 | + |
| 144 | + expect(vscode.window.createTerminal).toHaveBeenCalledWith({ name: "Kagan TUI" }); |
| 145 | + const terminal = vi.mocked(vscode.window.createTerminal).mock.results[0].value as { |
| 146 | + show: () => void; |
| 147 | + sendText: (text: string, addNewLine: boolean) => void; |
| 148 | + }; |
| 149 | + expect(terminal.show).toHaveBeenCalled(); |
| 150 | + expect(terminal.sendText).toHaveBeenCalledWith("kagan tui", true); |
| 151 | + }); |
| 152 | + |
| 153 | + it("calls openExternal when Open Web is chosen", async () => { |
| 154 | + vi.mocked(vscode.window.showWarningMessage).mockResolvedValue("Open Web" as never); |
| 155 | + |
| 156 | + const client = makeClient(makeReport({ ok: false, fail_count: 1, warn_count: 0 })); |
| 157 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 158 | + |
| 159 | + await provider.runPreflight(); |
| 160 | + await vi.waitFor(() => expect(vscode.env.openExternal).toHaveBeenCalled()); |
| 161 | + |
| 162 | + const calledWith = vi.mocked(vscode.env.openExternal).mock.calls[0][0] as { |
| 163 | + toString(): string; |
| 164 | + }; |
| 165 | + expect(calledWith.toString()).toBe("http://localhost:8765"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("silently stays offline when server is unreachable", async () => { |
| 169 | + const client = makeClient(null); |
| 170 | + const provider = new DoctorStatusProvider(client as unknown as KaganClient, statusBar); |
| 171 | + |
| 172 | + // Must not throw and must not show a notification. |
| 173 | + await expect(provider.runPreflight()).resolves.toBeUndefined(); |
| 174 | + expect(vscode.window.showWarningMessage).not.toHaveBeenCalled(); |
| 175 | + }); |
| 176 | +}); |
0 commit comments