| 1 | // Run: tsx src/__tests__/bridge-drag-rejection.test.ts |
| 2 | |
| 3 | import { isTransientWailsIPCError, isWailsNonFileDragError, isWailsNonFileDragErrorEvent } from "../lib/bridge"; |
| 4 | |
| 5 | let passed = 0; |
| 6 | let failed = 0; |
| 7 | |
| 8 | function eq(a: unknown, b: unknown, label: string) { |
| 9 | if (a === b) { |
| 10 | process.stdout.write(` PASS ${label}\n`); |
| 11 | passed += 1; |
| 12 | } else { |
| 13 | process.stdout.write(` FAIL ${label}: expected ${JSON.stringify(b)}, got ${JSON.stringify(a)}\n`); |
| 14 | failed += 1; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | console.log("\nbridge drag rejection filtering"); |
| 19 | |
| 20 | eq( |
| 21 | isWailsNonFileDragError(new Error("additional File object is not a file on the disk")), |
| 22 | true, |
| 23 | "suppresses Wails' explicit non-file drag error", |
| 24 | ); |
| 25 | eq(isWailsNonFileDragError(new Error("invalid argument")), false, "does not suppress generic invalid argument"); |
| 26 | eq( |
| 27 | isWailsNonFileDragError(new Error("invalid argument"), true), |
| 28 | true, |
| 29 | "suppresses invalid argument only after a native file drag", |
| 30 | ); |
| 31 | eq( |
| 32 | isWailsNonFileDragError(new TypeError("invalid argument"), false), |
| 33 | false, |
| 34 | "keeps non-drag TypeError invalid argument visible", |
| 35 | ); |
| 36 | eq( |
| 37 | isWailsNonFileDragError("network invalid argument", true), |
| 38 | false, |
| 39 | "does not suppress broader messages that merely contain invalid argument", |
| 40 | ); |
| 41 | eq( |
| 42 | isWailsNonFileDragError("Uncaught TypeError: invalid argument", true), |
| 43 | true, |
| 44 | "normalizes Chromium's window.error message prefix", |
| 45 | ); |
| 46 | eq( |
| 47 | isWailsNonFileDragErrorEvent({ message: "Uncaught TypeError: invalid argument", error: undefined }, true), |
| 48 | true, |
| 49 | "suppresses invalid argument delivered through ErrorEvent.message", |
| 50 | ); |
| 51 | eq( |
| 52 | isWailsNonFileDragErrorEvent({ message: "Uncaught TypeError: invalid argument", error: new TypeError("invalid argument") }, true), |
| 53 | true, |
| 54 | "suppresses invalid argument delivered through ErrorEvent.error.message", |
| 55 | ); |
| 56 | eq( |
| 57 | isWailsNonFileDragErrorEvent({ message: "Uncaught TypeError: invalid argument", error: new TypeError("invalid argument") }, false), |
| 58 | false, |
| 59 | "keeps ErrorEvent invalid argument visible without a recent native file drag", |
| 60 | ); |
| 61 | eq( |
| 62 | isTransientWailsIPCError(new DOMException("Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.", "InvalidStateError")), |
| 63 | true, |
| 64 | "suppresses Wails IPC calls made before the websocket is open", |
| 65 | ); |
| 66 | eq( |
| 67 | isTransientWailsIPCError(new TypeError("Cannot read properties of null (reading 'send')")), |
| 68 | true, |
| 69 | "suppresses Wails IPC calls made after the websocket is torn down", |
| 70 | ); |
| 71 | eq( |
| 72 | isTransientWailsIPCError(new Error("backend returned an application error")), |
| 73 | false, |
| 74 | "keeps ordinary bridge failures visible", |
| 75 | ); |
| 76 | |
| 77 | console.log(`\n${passed} passed, ${failed} failed`); |
| 78 | if (failed > 0) process.exit(1); |
| 79 |