| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { DialogHost, fileFiltersFromPatterns, type DialogApi } from "./dialogs.js"; |
| 4 | |
| 5 | test("Wails filter patterns become Electron extension lists", () => { |
| 6 | assert.deepEqual(fileFiltersFromPatterns([ |
| 7 | { displayName: "Images", pattern: "*.png;*.jpg; *.JPEG" }, |
| 8 | { displayName: "", pattern: "*.tar.gz" }, |
| 9 | { displayName: "All", pattern: "*.*" }, |
| 10 | { pattern: "*" }, |
| 11 | { displayName: "broken" }, |
| 12 | "junk", |
| 13 | ]), [ |
| 14 | { name: "Images", extensions: ["png", "jpg", "JPEG"] }, |
| 15 | { name: "*.tar.gz", extensions: ["tar.gz"] }, |
| 16 | { name: "All", extensions: ["*"] }, |
| 17 | { name: "*", extensions: ["*"] }, |
| 18 | ]); |
| 19 | assert.deepEqual(fileFiltersFromPatterns(undefined), []); |
| 20 | }); |
| 21 | |
| 22 | test("message boxes map buttons by name and return the chosen label", async () => { |
| 23 | const seen: unknown[] = []; |
| 24 | const dialog = { |
| 25 | showOpenDialog: async () => ({ canceled: true, filePaths: [] }), |
| 26 | showSaveDialog: async () => ({ canceled: true }), |
| 27 | showMessageBox: async (...args: unknown[]) => { |
| 28 | seen.push(args[args.length - 1]); |
| 29 | return { response: 1, checkboxChecked: false }; |
| 30 | }, |
| 31 | } as unknown as DialogApi; |
| 32 | const host = new DialogHost(dialog, () => undefined); |
| 33 | const result = await host.message({ type: "question", title: "T", message: "M", buttons: ["Keep", "Discard"], defaultButton: "Keep", cancelButton: "Discard" }); |
| 34 | assert.deepEqual(result, { button: "Discard" }); |
| 35 | assert.deepEqual(seen[0], { type: "question", title: "T", message: "M", buttons: ["Keep", "Discard"], defaultId: 0, cancelId: 1, noLink: true }); |
| 36 | assert.deepEqual(await host.openDirectory({ title: "Pick" }), { path: "" }); |
| 37 | assert.deepEqual(await host.openFile({ multiple: true }), { paths: [] }); |
| 38 | assert.deepEqual(await host.saveFile({ defaultDirectory: "/tmp", defaultFilename: "a.txt" }), { path: "" }); |
| 39 | }); |
| 40 |