|
| 1 | +import { afterAll, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test" |
| 2 | + |
| 3 | +let createTray: (iconPath: string, onShow: () => void) => unknown |
| 4 | +let destroyTray: () => void |
| 5 | + |
| 6 | +const state = { |
| 7 | + trayCreated: 0, |
| 8 | + trayDestroyed: 0, |
| 9 | + toolTip: "", |
| 10 | + menu: undefined as undefined | { label?: string; type?: string; click?: () => void }[], |
| 11 | + clickHandler: undefined as undefined | (() => void), |
| 12 | + doubleClickHandler: undefined as undefined | (() => void), |
| 13 | + quitCalls: 0, |
| 14 | +} |
| 15 | + |
| 16 | +beforeAll(async () => { |
| 17 | + const api = { |
| 18 | + app: { |
| 19 | + quit: () => { |
| 20 | + state.quitCalls++ |
| 21 | + }, |
| 22 | + }, |
| 23 | + Menu: { |
| 24 | + buildFromTemplate: (template: { label?: string; type?: string; click?: () => void }[]) => { |
| 25 | + state.menu = template |
| 26 | + return template |
| 27 | + }, |
| 28 | + }, |
| 29 | + nativeImage: { |
| 30 | + createFromPath: () => ({ isEmpty: () => false }), |
| 31 | + }, |
| 32 | + Tray: function Tray() { |
| 33 | + state.trayCreated++ |
| 34 | + return { |
| 35 | + setToolTip: (tip: string) => { |
| 36 | + state.toolTip = tip |
| 37 | + }, |
| 38 | + setContextMenu: () => {}, |
| 39 | + on: (event: string, handler: () => void) => { |
| 40 | + if (event === "click") state.clickHandler = handler |
| 41 | + if (event === "double-click") state.doubleClickHandler = handler |
| 42 | + }, |
| 43 | + destroy: () => { |
| 44 | + state.trayDestroyed++ |
| 45 | + }, |
| 46 | + } |
| 47 | + }, |
| 48 | + } |
| 49 | + mock.module("electron", () => ({ ...api, default: api })) |
| 50 | + const mod = await import("./tray") |
| 51 | + createTray = mod.createTray |
| 52 | + destroyTray = mod.destroyTray |
| 53 | +}) |
| 54 | + |
| 55 | +const originalPlatform = process.platform |
| 56 | + |
| 57 | +function setPlatform(platform: NodeJS.Platform) { |
| 58 | + Object.defineProperty(process, "platform", { value: platform, configurable: true }) |
| 59 | +} |
| 60 | + |
| 61 | +beforeEach(() => { |
| 62 | + // Reset the module-level tray singleton so every test starts clean; |
| 63 | + // otherwise the idempotent createTray returns the instance created by an |
| 64 | + // earlier test and no new Tray is constructed. |
| 65 | + destroyTray() |
| 66 | + state.trayCreated = 0 |
| 67 | + state.trayDestroyed = 0 |
| 68 | + state.toolTip = "" |
| 69 | + state.menu = undefined |
| 70 | + state.clickHandler = undefined |
| 71 | + state.doubleClickHandler = undefined |
| 72 | + state.quitCalls = 0 |
| 73 | + setPlatform("win32") |
| 74 | +}) |
| 75 | + |
| 76 | +afterAll(() => { |
| 77 | + setPlatform(originalPlatform) |
| 78 | + mock.restore() |
| 79 | +}) |
| 80 | + |
| 81 | +describe("tray", () => { |
| 82 | + test("creates a tray with Show and Quit entries on windows", () => { |
| 83 | + const tray = createTray("/fake/icon.ico", () => {}) |
| 84 | + expect(tray).toBeDefined() |
| 85 | + expect(state.trayCreated).toBe(1) |
| 86 | + expect(state.toolTip).toBe("OpenCode") |
| 87 | + const labels = state.menu?.map((item) => item.label ?? item.type) |
| 88 | + expect(labels).toEqual(["Show OpenCode", "separator", "Quit"]) |
| 89 | + }) |
| 90 | + |
| 91 | + test("is idempotent: creating again returns the same instance", () => { |
| 92 | + createTray("/fake/icon.ico", () => {}) |
| 93 | + createTray("/fake/icon.ico", () => {}) |
| 94 | + expect(state.trayCreated).toBe(1) |
| 95 | + }) |
| 96 | + |
| 97 | + test("single and double click both restore the window", () => { |
| 98 | + const show = mock(() => {}) |
| 99 | + createTray("/fake/icon.ico", show) |
| 100 | + state.clickHandler?.() |
| 101 | + state.doubleClickHandler?.() |
| 102 | + expect(show).toHaveBeenCalledTimes(2) |
| 103 | + }) |
| 104 | + |
| 105 | + test("tray Show entry restores the window", () => { |
| 106 | + const show = mock(() => {}) |
| 107 | + createTray("/fake/icon.ico", show) |
| 108 | + const showEntry = state.menu?.find((item) => item.label === "Show OpenCode") |
| 109 | + showEntry?.click?.() |
| 110 | + expect(show).toHaveBeenCalledTimes(1) |
| 111 | + }) |
| 112 | + |
| 113 | + test("tray Quit entry quits the app", () => { |
| 114 | + createTray("/fake/icon.ico", () => {}) |
| 115 | + const quitEntry = state.menu?.find((item) => item.label === "Quit") |
| 116 | + quitEntry?.click?.() |
| 117 | + expect(state.quitCalls).toBe(1) |
| 118 | + }) |
| 119 | + |
| 120 | + test("does not create a tray on macOS", () => { |
| 121 | + setPlatform("darwin") |
| 122 | + expect(createTray("/fake/icon.png", () => {})).toBeUndefined() |
| 123 | + expect(state.trayCreated).toBe(0) |
| 124 | + }) |
| 125 | + |
| 126 | + test("destroy removes the tray instance", () => { |
| 127 | + createTray("/fake/icon.ico", () => {}) |
| 128 | + destroyTray() |
| 129 | + expect(state.trayDestroyed).toBe(1) |
| 130 | + // A destroyed tray can be recreated. |
| 131 | + createTray("/fake/icon.ico", () => {}) |
| 132 | + expect(state.trayCreated).toBe(2) |
| 133 | + }) |
| 134 | +}) |
0 commit comments