|
| 1 | +// Copyright (C) 2025 by Posit Software, PBC. |
| 2 | + |
| 3 | +import { describe, expect, beforeEach, test, vi } from "vitest"; |
| 4 | +import { window } from "vscode"; |
| 5 | +import { |
| 6 | + showErrorMessageWithTroubleshoot, |
| 7 | + showInformationMsg, |
| 8 | + taskWithProgressMsg, |
| 9 | + runTerminalCommand, |
| 10 | +} from "./window"; |
| 11 | + |
| 12 | +const terminalMock = { |
| 13 | + sendText: vi.fn(), |
| 14 | + show: vi.fn(), |
| 15 | + exitStatus: { |
| 16 | + code: 0, |
| 17 | + }, |
| 18 | +}; |
| 19 | + |
| 20 | +vi.mock("vscode", () => { |
| 21 | + // mock Disposable |
| 22 | + const disposableMock = vi.fn(); |
| 23 | + disposableMock.prototype.dispose = vi.fn(); |
| 24 | + |
| 25 | + // mock window |
| 26 | + const windowMock = { |
| 27 | + showErrorMessage: vi.fn(), |
| 28 | + showWarningMessage: vi.fn(), |
| 29 | + showInformationMessage: vi.fn(), |
| 30 | + withProgress: vi.fn().mockImplementation((_, progressCallback) => { |
| 31 | + progressCallback(); |
| 32 | + }), |
| 33 | + createTerminal: vi.fn().mockImplementation(() => { |
| 34 | + terminalMock.sendText = vi.fn(); |
| 35 | + terminalMock.show = vi.fn(); |
| 36 | + return terminalMock; |
| 37 | + }), |
| 38 | + onDidCloseTerminal: vi.fn().mockImplementation((fn) => { |
| 39 | + setTimeout(() => fn(terminalMock), 100); |
| 40 | + return new disposableMock(); |
| 41 | + }), |
| 42 | + }; |
| 43 | + |
| 44 | + return { |
| 45 | + Disposable: disposableMock, |
| 46 | + window: windowMock, |
| 47 | + ProgressLocation: { |
| 48 | + SourceControl: 1, |
| 49 | + Window: 10, |
| 50 | + Notification: 15, |
| 51 | + }, |
| 52 | + }; |
| 53 | +}); |
| 54 | + |
| 55 | +describe("Consumers of vscode window", () => { |
| 56 | + beforeEach(() => { |
| 57 | + terminalMock.exitStatus.code = 0; |
| 58 | + }); |
| 59 | + |
| 60 | + test("showErrorMessageWithTroubleshoot", () => { |
| 61 | + showErrorMessageWithTroubleshoot("Bad things happened"); |
| 62 | + expect(window.showErrorMessage).toHaveBeenCalledWith( |
| 63 | + "Bad things happened. See [Troubleshooting docs](https://github.com/posit-dev/publisher/blob/main/docs/troubleshooting.md) for help.", |
| 64 | + ); |
| 65 | + |
| 66 | + showErrorMessageWithTroubleshoot( |
| 67 | + "Bad things happened.", |
| 68 | + "one", |
| 69 | + "two", |
| 70 | + "three", |
| 71 | + ); |
| 72 | + expect(window.showErrorMessage).toHaveBeenCalledWith( |
| 73 | + "Bad things happened. See [Troubleshooting docs](https://github.com/posit-dev/publisher/blob/main/docs/troubleshooting.md) for help.", |
| 74 | + "one", |
| 75 | + "two", |
| 76 | + "three", |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + test("showInformationMsg", () => { |
| 81 | + showInformationMsg("Good thing happened"); |
| 82 | + expect(window.showInformationMessage).toHaveBeenCalledWith( |
| 83 | + "Good thing happened", |
| 84 | + ); |
| 85 | + |
| 86 | + showInformationMsg("Good thing happened", "one", "two", "three"); |
| 87 | + expect(window.showInformationMessage).toHaveBeenCalledWith( |
| 88 | + "Good thing happened", |
| 89 | + "one", |
| 90 | + "two", |
| 91 | + "three", |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + test("taskWithProgressMsg", () => { |
| 96 | + const taskMock = vi.fn(); |
| 97 | + taskWithProgressMsg( |
| 98 | + "Running a task with a progress notification", |
| 99 | + taskMock, |
| 100 | + ); |
| 101 | + expect(window.withProgress).toHaveBeenCalledWith( |
| 102 | + { |
| 103 | + location: 15, |
| 104 | + title: "Running a task with a progress notification", |
| 105 | + cancellable: false, |
| 106 | + }, |
| 107 | + expect.any(Function), |
| 108 | + ); |
| 109 | + expect(taskMock).toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("runTerminalCommand", () => { |
| 113 | + test("showing the terminal", async () => { |
| 114 | + await runTerminalCommand("stat somefile.txt", true); |
| 115 | + expect(terminalMock.sendText).toHaveBeenCalledWith("stat somefile.txt"); |
| 116 | + expect(terminalMock.show).toHaveBeenCalled(); |
| 117 | + // For terminals that we open, we don't track close events |
| 118 | + expect(window.onDidCloseTerminal).not.toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + test("NOT showing the terminal", async () => { |
| 122 | + await runTerminalCommand("stat somefile.txt"); |
| 123 | + expect(terminalMock.sendText).toHaveBeenCalledWith("stat somefile.txt"); |
| 124 | + // For terminals that we DO NOT open, we DO track close events |
| 125 | + expect(terminalMock.show).not.toHaveBeenCalled(); |
| 126 | + expect(window.onDidCloseTerminal).toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + test("catch non zero exit status", async () => { |
| 130 | + terminalMock.exitStatus.code = 1; |
| 131 | + try { |
| 132 | + await runTerminalCommand("stat somefile.txt"); |
| 133 | + } catch (_) { |
| 134 | + expect(terminalMock.sendText).toHaveBeenCalledWith("stat somefile.txt"); |
| 135 | + // For terminals that we DO NOT open, we DO track close events |
| 136 | + expect(terminalMock.show).not.toHaveBeenCalled(); |
| 137 | + expect(window.onDidCloseTerminal).toHaveBeenCalled(); |
| 138 | + } |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments