| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { readFile } from 'node:fs/promises'; |
| 4 | import { randomUUID } from 'node:crypto'; |
| 5 | import vm from 'node:vm'; |
| 6 | |
| 7 | const source = await readFile(new URL('../desktop/windows/MainWindow.cs', import.meta.url), 'utf8'); |
| 8 | const script = source.match(/private const string BridgeScript = """\s*([\s\S]*?)\s*""";/)?.[1]; |
| 9 | assert.ok(script); |
| 10 | |
| 11 | function bridge() { |
| 12 | let reply, sent; |
| 13 | const timers = new Map(); |
| 14 | const window = {}; |
| 15 | window.top = window; |
| 16 | vm.runInNewContext(script, { |
| 17 | window, location: { protocol: 'http:', hostname: 'localhost' }, crypto: { randomUUID }, |
| 18 | chrome: { webview: { |
| 19 | addEventListener: (_, callback) => { reply = callback; }, |
| 20 | postMessage: value => { sent = value; }, |
| 21 | } }, |
| 22 | setTimeout: (callback, delay) => { const id = randomUUID(); timers.set(id, { callback, delay }); return id; }, |
| 23 | clearTimeout: id => timers.delete(id), |
| 24 | }); |
| 25 | return { |
| 26 | call: request => window.webkit.messageHandlers.cage.postMessage(request), |
| 27 | reply: value => reply({ data: { id: sent.id, ...value } }), |
| 28 | timers, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | test('native file replies remain pending beyond four minutes and through the backend deadline', async () => { |
| 33 | for (const action of ['uploadHostFiles', 'chooseUploadFiles', 'clipboardSyncRead', 'clipboardSyncWrite', |
| 34 | 'saveBrowserDownload', 'browserDownloadAction', 'desktopFileAction', 'exportDiagnostics', 'importTheme', 'exportTheme', |
| 35 | 'shareLocalProject', 'chooseFolder', 'chooseGuestMedia']) { |
| 36 | const fixture = bridge(); |
| 37 | const pending = fixture.call({ action }); |
| 38 | const timer = [...fixture.timers.values()][0]; |
| 39 | assert.ok(timer.delay > 30 * 60 * 1000, action); |
| 40 | fixture.reply({ result: { saved: true } }); |
| 41 | assert.equal((await pending).saved, true, action); |
| 42 | assert.equal(fixture.timers.size, 0); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | test('file failure reaches the caller and the following request can succeed', async () => { |
| 47 | const fixture = bridge(); |
| 48 | const first = fixture.call({ action: 'saveBrowserDownload' }); |
| 49 | fixture.reply({ error: '磁盘空间不足' }); |
| 50 | await assert.rejects(first, /磁盘空间不足/); |
| 51 | const second = fixture.call({ action: 'clipboardSyncRead' }); |
| 52 | fixture.reply({ result: { text: 'next copy' } }); |
| 53 | assert.equal((await second).text, 'next copy'); |
| 54 | assert.equal(fixture.timers.size, 0); |
| 55 | }); |
| 56 | |
| 57 | test('ordinary native commands retain their bounded timeout', async () => { |
| 58 | const fixture = bridge(); |
| 59 | const pending = fixture.call({ action: 'accountDevice' }); |
| 60 | const timer = [...fixture.timers.values()][0]; |
| 61 | assert.equal(timer.delay, 240_000); |
| 62 | timer.callback(); |
| 63 | await assert.rejects(pending, /原生操作超时/); |
| 64 | fixture.reply({ result: true }); |
| 65 | }); |