|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { describe, it, beforeEach } from 'node:test'; |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | + |
| 6 | +// A stand in for the port chrome.runtime.connectNative hands back, recording |
| 7 | +// what was posted and letting a test fire the listeners the real one would. |
| 8 | +const calls = { connected: [], posted: [], errors: [] }; |
| 9 | +let failToConnect = null; |
| 10 | +let failToPost = null; |
| 11 | + |
| 12 | +function makePort() { |
| 13 | + const port = { |
| 14 | + posted: [], |
| 15 | + disconnectListeners: [], |
| 16 | + messageListeners: [], |
| 17 | + onDisconnect: { addListener: fn => port.disconnectListeners.push(fn) }, |
| 18 | + onMessage: { addListener: fn => port.messageListeners.push(fn) }, |
| 19 | + postMessage: message => { |
| 20 | + if (failToPost) throw new Error(failToPost); |
| 21 | + port.posted.push(message); |
| 22 | + calls.posted.push(message); |
| 23 | + }, |
| 24 | + disconnect: () => port.disconnectListeners.forEach(fn => fn()), |
| 25 | + reply: message => port.messageListeners.forEach(fn => fn(message)), |
| 26 | + }; |
| 27 | + return port; |
| 28 | +} |
| 29 | + |
| 30 | +globalThis.chrome = { |
| 31 | + runtime: { |
| 32 | + lastError: undefined, |
| 33 | + connectNative: name => { |
| 34 | + calls.connected.push(name); |
| 35 | + if (failToConnect) throw new Error(failToConnect); |
| 36 | + return makePort(); |
| 37 | + }, |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +console.error = (...args) => calls.errors.push(args.join(' ')); |
| 42 | + |
| 43 | +const { NetLogPort } = await import('../edge/net-log-port.js'); |
| 44 | + |
| 45 | +const ENTRY = { operation: 'browsing', url: 'https://example.com/' }; |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + calls.connected = []; |
| 49 | + calls.posted = []; |
| 50 | + calls.errors = []; |
| 51 | + failToConnect = null; |
| 52 | + failToPost = null; |
| 53 | + chrome.runtime.lastError = undefined; |
| 54 | + NetLogPort.port = null; |
| 55 | +}); |
| 56 | + |
| 57 | +describe('send', () => { |
| 58 | + it('carries the entry as the L command', () => { |
| 59 | + assert.equal(NetLogPort.send(ENTRY), true); |
| 60 | + |
| 61 | + assert.deepEqual(calls.posted, [{ message: 'L ' + JSON.stringify(ENTRY) }]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('names the native host it connects to', () => { |
| 65 | + NetLogPort.send(ENTRY); |
| 66 | + |
| 67 | + assert.deepEqual(calls.connected, ['com.clear_code.browser_guard']); |
| 68 | + }); |
| 69 | + |
| 70 | + // Opening a process per entry is the thing the port exists to avoid. |
| 71 | + it('opens the port once and keeps using it', () => { |
| 72 | + NetLogPort.send(ENTRY); |
| 73 | + NetLogPort.send(ENTRY); |
| 74 | + NetLogPort.send(ENTRY); |
| 75 | + |
| 76 | + assert.equal(calls.connected.length, 1); |
| 77 | + assert.equal(calls.posted.length, 3); |
| 78 | + }); |
| 79 | + |
| 80 | + it('opens a new port after the host has gone away', () => { |
| 81 | + NetLogPort.send(ENTRY); |
| 82 | + NetLogPort.port.disconnect(); |
| 83 | + |
| 84 | + NetLogPort.send(ENTRY); |
| 85 | + |
| 86 | + assert.equal(calls.connected.length, 2); |
| 87 | + assert.equal(calls.posted.length, 2); |
| 88 | + }); |
| 89 | + |
| 90 | + it('reports that it could not open the port', () => { |
| 91 | + failToConnect = 'host not found'; |
| 92 | + |
| 93 | + assert.equal(NetLogPort.send(ENTRY), false); |
| 94 | + |
| 95 | + assert.equal(NetLogPort.port, null); |
| 96 | + assert.match(calls.errors.join('\n'), /host not found/); |
| 97 | + }); |
| 98 | + |
| 99 | + // A port can go away between the last entry and this one. |
| 100 | + it('starts over when posting fails', () => { |
| 101 | + NetLogPort.send(ENTRY); |
| 102 | + failToPost = 'port closed'; |
| 103 | + |
| 104 | + assert.equal(NetLogPort.send(ENTRY), false); |
| 105 | + |
| 106 | + assert.equal(NetLogPort.port, null); |
| 107 | + failToPost = null; |
| 108 | + assert.equal(NetLogPort.send(ENTRY), true); |
| 109 | + assert.equal(calls.connected.length, 2); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('the port itself', () => { |
| 114 | + it('reports an entry the host could not write', () => { |
| 115 | + NetLogPort.send(ENTRY); |
| 116 | + |
| 117 | + NetLogPort.port.reply({ Success: false, Error: 'entry is not valid JSON' }); |
| 118 | + |
| 119 | + assert.match(calls.errors.join('\n'), /entry is not valid JSON/); |
| 120 | + }); |
| 121 | + |
| 122 | + // The host stays quiet when it wrote the entry, so nothing should be logged. |
| 123 | + it('says nothing about an entry that was written', () => { |
| 124 | + NetLogPort.send(ENTRY); |
| 125 | + |
| 126 | + NetLogPort.port.reply(undefined); |
| 127 | + |
| 128 | + assert.deepEqual(calls.errors, []); |
| 129 | + }); |
| 130 | + |
| 131 | + it('reports why the port was disconnected', () => { |
| 132 | + NetLogPort.send(ENTRY); |
| 133 | + const port = NetLogPort.port; |
| 134 | + chrome.runtime.lastError = { message: 'native host has exited' }; |
| 135 | + |
| 136 | + port.disconnect(); |
| 137 | + |
| 138 | + assert.match(calls.errors.join('\n'), /native host has exited/); |
| 139 | + }); |
| 140 | + |
| 141 | + // A disconnect for the previous port must not discard the current one. |
| 142 | + it('keeps a port opened after an earlier one was closed', () => { |
| 143 | + NetLogPort.send(ENTRY); |
| 144 | + const first = NetLogPort.port; |
| 145 | + first.disconnect(); |
| 146 | + NetLogPort.send(ENTRY); |
| 147 | + const second = NetLogPort.port; |
| 148 | + |
| 149 | + first.disconnect(); |
| 150 | + |
| 151 | + assert.equal(NetLogPort.port, second); |
| 152 | + }); |
| 153 | +}); |
0 commit comments