|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { dirname, join } from "node:path" |
| 3 | +import { fileURLToPath } from "node:url" |
| 4 | +import { runInNewContext } from "node:vm" |
| 5 | + |
| 6 | +const source = await Bun.file(new URL("./tray.ts", import.meta.url)).text() |
| 7 | +const compiled = new Bun.Transpiler({ loader: "ts" }).transformSync( |
| 8 | + source |
| 9 | + .replace(/^import .*$/gm, "") |
| 10 | + .replace("import.meta.url", JSON.stringify(new URL("./tray.ts", import.meta.url).href)) |
| 11 | + .replace(/^export /gm, ""), |
| 12 | +) |
| 13 | + |
| 14 | +for (const platform of ["darwin", "win32", "linux"]) { |
| 15 | + for (const packaged of [false, true]) { |
| 16 | + test(`tray image on ${platform}, packaged=${packaged}`, () => { |
| 17 | + const loaded: string[] = [] |
| 18 | + const resized: object[] = [] |
| 19 | + const image = { |
| 20 | + resize(options: object) { |
| 21 | + resized.push(options) |
| 22 | + return small |
| 23 | + }, |
| 24 | + } |
| 25 | + const small = {} |
| 26 | + const received: unknown[] = [] |
| 27 | + runInNewContext(`${compiled}\ncreateTray(() => {})`, { |
| 28 | + dirname, |
| 29 | + join, |
| 30 | + fileURLToPath, |
| 31 | + process: { platform, resourcesPath: "/packaged/resources" }, |
| 32 | + app: { isPackaged: packaged, getName: () => "CookieMonster" }, |
| 33 | + Menu: { buildFromTemplate: () => [] }, |
| 34 | + nativeImage: { |
| 35 | + createFromPath(path: string) { |
| 36 | + loaded.push(path) |
| 37 | + return image |
| 38 | + }, |
| 39 | + }, |
| 40 | + Tray: class { |
| 41 | + constructor(value: unknown) { |
| 42 | + received.push(value) |
| 43 | + } |
| 44 | + setToolTip() {} |
| 45 | + setContextMenu() {} |
| 46 | + on() {} |
| 47 | + }, |
| 48 | + }) |
| 49 | + const directory = packaged |
| 50 | + ? "/packaged/resources/icons" |
| 51 | + : join(dirname(fileURLToPath(new URL("./tray.ts", import.meta.url))), "../../resources/icons") |
| 52 | + expect(loaded).toEqual([ |
| 53 | + join(directory, platform === "darwin" ? "32x32.png" : platform === "win32" ? "icon.ico" : "icon.png"), |
| 54 | + ]) |
| 55 | + expect(resized).toEqual(platform === "darwin" ? [{ width: 16, height: 16 }] : []) |
| 56 | + expect(received).toEqual([platform === "darwin" ? small : image]) |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +test("every build channel includes the small tray asset", async () => { |
| 62 | + for (const channel of ["dev", "beta", "prod"]) { |
| 63 | + const bytes = await Bun.file(new URL(`../../icons/${channel}/32x32.png`, import.meta.url)).bytes() |
| 64 | + expect([...bytes.subarray(0, 8)]).toEqual([137, 80, 78, 71, 13, 10, 26, 10]) |
| 65 | + const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) |
| 66 | + expect(header.getUint32(16)).toBe(32) |
| 67 | + expect(header.getUint32(20)).toBe(32) |
| 68 | + } |
| 69 | +}) |
0 commit comments