|
| 1 | +// `openscreen mcp` sends the GUI's anonymous usage ping: an install driven only by an agent |
| 2 | +// counts as active. runCli runs for real here; only Electron and the heavy services around |
| 3 | +// the MCP server are stubbed. |
| 4 | + |
| 5 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 6 | +import os from "node:os"; |
| 7 | +import path from "node:path"; |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 9 | +import { sendUsagePing, setUsagePingEnabled } from "../usage-ping"; |
| 10 | +import { runCli } from "./cliMain"; |
| 11 | + |
| 12 | +const mocks = vi.hoisted(() => ({ userData: "", serve: vi.fn(async () => undefined) })); |
| 13 | + |
| 14 | +vi.mock("electron", () => ({ |
| 15 | + app: { |
| 16 | + getPath: () => mocks.userData, |
| 17 | + getVersion: () => "2.1.0", |
| 18 | + exit: vi.fn(), |
| 19 | + on: vi.fn(), |
| 20 | + whenReady: () => Promise.resolve(), |
| 21 | + commandLine: { appendSwitch: vi.fn() }, |
| 22 | + }, |
| 23 | + BrowserWindow: class {}, |
| 24 | + ipcMain: { handle: vi.fn() }, |
| 25 | + Menu: { setApplicationMenu: vi.fn(), buildFromTemplate: vi.fn() }, |
| 26 | + session: { |
| 27 | + defaultSession: { |
| 28 | + setPermissionCheckHandler: vi.fn(), |
| 29 | + setPermissionRequestHandler: vi.fn(), |
| 30 | + setDisplayMediaRequestHandler: vi.fn(), |
| 31 | + }, |
| 32 | + }, |
| 33 | + systemPreferences: {}, |
| 34 | +})); |
| 35 | +vi.mock("../install-channel", () => ({ getInstallChannel: () => "nsis" })); |
| 36 | +vi.mock("../ipc/handlers", () => ({ |
| 37 | + getSelectedDesktopSource: () => null, |
| 38 | + registerIpcHandlers: () => ({ agentServices: {} }), |
| 39 | +})); |
| 40 | +vi.mock("../mcp/hidden-video", () => ({ openVideoInHiddenWindow: vi.fn() })); |
| 41 | +vi.mock("../mcp/stdio-server", () => ({ serveMcpOverStdio: mocks.serve })); |
| 42 | +vi.mock("../stt", () => ({ registerSttIpc: vi.fn(), shutdownStt: vi.fn() })); |
| 43 | +vi.mock("../windows", () => ({ createRunnerWindow: vi.fn() })); |
| 44 | +vi.mock("./runnerHost", () => ({ createRunnerHost: () => ({}) })); |
| 45 | +vi.mock("./sessionData", () => ({ isolateCliSessionData: vi.fn() })); |
| 46 | + |
| 47 | +const fetchMock = vi.fn(async (_url: string, _init: RequestInit) => ({ ok: true })); |
| 48 | +const savedConsole = { ...console }; |
| 49 | +let stdoutWrite: ReturnType<typeof vi.spyOn>; |
| 50 | + |
| 51 | +beforeEach(() => { |
| 52 | + mocks.userData = mkdtempSync(path.join(os.tmpdir(), "cli-usage-ping-")); |
| 53 | + mocks.serve.mockClear(); |
| 54 | + fetchMock.mockClear(); |
| 55 | + vi.stubGlobal("fetch", fetchMock); |
| 56 | + vi.stubEnv("DO_NOT_TRACK", ""); |
| 57 | + vi.stubEnv("CI", ""); |
| 58 | + // The darwin branch touches the dock and menu, which this test is not about. |
| 59 | + vi.spyOn(process, "platform", "get").mockReturnValue("linux"); |
| 60 | + // runCli's fatal-error handlers would outlive the test and exit the worker. |
| 61 | + vi.spyOn(process, "on").mockReturnValue(process); |
| 62 | + stdoutWrite = vi.spyOn(process.stdout, "write"); |
| 63 | +}); |
| 64 | + |
| 65 | +afterEach(() => { |
| 66 | + vi.unstubAllGlobals(); |
| 67 | + vi.unstubAllEnvs(); |
| 68 | + vi.restoreAllMocks(); |
| 69 | + Object.assign(console, savedConsole); |
| 70 | + rmSync(mocks.userData, { recursive: true, force: true }); |
| 71 | +}); |
| 72 | + |
| 73 | +/** Runs `openscreen mcp` up to the point its server is listening and the ping went out. */ |
| 74 | +async function runMcp() { |
| 75 | + runCli({ kind: "mcp" }); |
| 76 | + await vi.waitFor(() => expect(mocks.serve).toHaveBeenCalled()); |
| 77 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 78 | +} |
| 79 | + |
| 80 | +describe("openscreen mcp usage ping", () => { |
| 81 | + it("pings once, as the mcp surface, and writes nothing to stdout", async () => { |
| 82 | + await runMcp(); |
| 83 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 84 | + const body = JSON.parse(String(fetchMock.mock.calls[0][1].body)); |
| 85 | + expect(body.properties).toMatchObject({ |
| 86 | + version: "2.1.0", |
| 87 | + channel: "nsis", |
| 88 | + surface: "mcp", |
| 89 | + }); |
| 90 | + expect(stdoutWrite).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("honours the tray toggle and DO_NOT_TRACK", async () => { |
| 94 | + setUsagePingEnabled(mocks.userData, false); |
| 95 | + await runMcp(); |
| 96 | + setUsagePingEnabled(mocks.userData, true); |
| 97 | + vi.stubEnv("DO_NOT_TRACK", "1"); |
| 98 | + await runMcp(); |
| 99 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("counts a day the app already counted only once", async () => { |
| 103 | + const facts = { version: "2.1.0", os: "linux", arch: "x64", channel: "nsis" } as const; |
| 104 | + await sendUsagePing(mocks.userData, { ...facts, surface: "app" }); |
| 105 | + fetchMock.mockClear(); |
| 106 | + await runMcp(); |
| 107 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments