|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import test from "node:test"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +import { closeWebSocket } from "./relayWebSocketClose.ts"; |
| 8 | + |
| 9 | +test("closeWebSocket sends a Close frame through plugin:websocket|send", async () => { |
| 10 | + const calls = []; |
| 11 | + await closeWebSocket(42, "workspace switch", async (cmd, args) => { |
| 12 | + calls.push({ cmd, args }); |
| 13 | + }); |
| 14 | + |
| 15 | + assert.equal(calls.length, 1); |
| 16 | + assert.equal(calls[0].cmd, "plugin:websocket|send"); |
| 17 | + assert.deepEqual(calls[0].args, { |
| 18 | + id: 42, |
| 19 | + message: { |
| 20 | + type: "Close", |
| 21 | + data: { code: 1000, reason: "workspace switch" }, |
| 22 | + }, |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +test("closeWebSocket swallows send failures (socket already gone)", async () => { |
| 27 | + await closeWebSocket(7, "connection reset", async () => { |
| 28 | + throw new Error("WebSocket connection not found"); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +// Regression guard: tauri-plugin-websocket registers only `connect` and |
| 33 | +// `send` — there is no `disconnect` command. Invoking one rejects silently |
| 34 | +// and leaks the socket (relay zombie pile, workspace-switch disconnects). |
| 35 | +// Any socket teardown must go through closeWebSocket. |
| 36 | +test("no source file invokes the nonexistent plugin:websocket|disconnect command", () => { |
| 37 | + const srcRoot = path.resolve( |
| 38 | + path.dirname(fileURLToPath(import.meta.url)), |
| 39 | + "..", |
| 40 | + "..", |
| 41 | + ); |
| 42 | + const offenders = []; |
| 43 | + |
| 44 | + const walk = (dir) => { |
| 45 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 46 | + const full = path.join(dir, entry.name); |
| 47 | + if (entry.isDirectory()) { |
| 48 | + walk(full); |
| 49 | + continue; |
| 50 | + } |
| 51 | + if (!/\.(ts|tsx|js|jsx|mjs)$/.test(entry.name)) continue; |
| 52 | + if (full === fileURLToPath(import.meta.url)) continue; |
| 53 | + if ( |
| 54 | + fs.readFileSync(full, "utf8").includes("plugin:websocket|disconnect") |
| 55 | + ) { |
| 56 | + offenders.push(path.relative(srcRoot, full)); |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + walk(srcRoot); |
| 61 | + |
| 62 | + assert.deepEqual( |
| 63 | + offenders, |
| 64 | + [], |
| 65 | + "plugin:websocket|disconnect does not exist in tauri-plugin-websocket — use closeWebSocket (Close frame via plugin:websocket|send) instead", |
| 66 | + ); |
| 67 | +}); |
0 commit comments