|
| 1 | +/** |
| 2 | + * Tests for electron/dashboard-token.cjs. |
| 3 | + * |
| 4 | + * Run with: node --test electron/dashboard-token.test.cjs |
| 5 | + * (Wired into npm test:desktop:platforms in package.json.) |
| 6 | + */ |
| 7 | + |
| 8 | +const test = require('node:test') |
| 9 | +const assert = require('node:assert/strict') |
| 10 | + |
| 11 | +const { |
| 12 | + adoptServedDashboardToken, |
| 13 | + dashboardIndexUrl, |
| 14 | + extractInjectedDashboardToken, |
| 15 | + fetchPublicText, |
| 16 | + isForeignBackendToken, |
| 17 | + resolveServedDashboardToken |
| 18 | +} = require('./dashboard-token.cjs') |
| 19 | + |
| 20 | +test('extractInjectedDashboardToken reads the JSON-encoded dashboard token', () => { |
| 21 | + const html = '<script>window.__HERMES_SESSION_TOKEN__="served-token";window.__HERMES_BASE_PATH__=""</script>' |
| 22 | + assert.equal(extractInjectedDashboardToken(html), 'served-token') |
| 23 | +}) |
| 24 | + |
| 25 | +test('extractInjectedDashboardToken handles escaped token strings', () => { |
| 26 | + const html = '<script>window.__HERMES_SESSION_TOKEN__="served\\\\token\\"quoted";</script>' |
| 27 | + assert.equal(extractInjectedDashboardToken(html), 'served\\token"quoted') |
| 28 | +}) |
| 29 | + |
| 30 | +test('extractInjectedDashboardToken returns null for missing or malformed values', () => { |
| 31 | + assert.equal(extractInjectedDashboardToken('<html></html>'), null) |
| 32 | + assert.equal(extractInjectedDashboardToken('<script>window.__HERMES_SESSION_TOKEN__={bad}</script>'), null) |
| 33 | +}) |
| 34 | + |
| 35 | +test('dashboardIndexUrl preserves dashboard path prefixes', () => { |
| 36 | + assert.equal(dashboardIndexUrl('http://127.0.0.1:9120'), 'http://127.0.0.1:9120/') |
| 37 | + assert.equal(dashboardIndexUrl('https://host.example/hermes/'), 'https://host.example/hermes/') |
| 38 | +}) |
| 39 | + |
| 40 | +test('resolveServedDashboardToken uses the served token and logs when it differs', async () => { |
| 41 | + const logs = [] |
| 42 | + const token = await resolveServedDashboardToken('http://127.0.0.1:9120', 'spawn-token', { |
| 43 | + fetchText: async url => { |
| 44 | + assert.equal(url, 'http://127.0.0.1:9120/') |
| 45 | + return '<script>window.__HERMES_SESSION_TOKEN__="served-token";</script>' |
| 46 | + }, |
| 47 | + rememberLog: line => logs.push(line) |
| 48 | + }) |
| 49 | + |
| 50 | + assert.equal(token, 'served-token') |
| 51 | + assert.equal(logs.length, 1) |
| 52 | + assert.match(logs[0], /served a different session token/) |
| 53 | +}) |
| 54 | + |
| 55 | +test('resolveServedDashboardToken falls back when the served HTML has no token', async () => { |
| 56 | + const token = await resolveServedDashboardToken('http://127.0.0.1:9120', 'spawn-token', { |
| 57 | + fetchText: async () => '<html></html>', |
| 58 | + rememberLog: () => { |
| 59 | + throw new Error('should not log when no served token is present') |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + assert.equal(token, 'spawn-token') |
| 64 | +}) |
| 65 | + |
| 66 | +test('resolveServedDashboardToken does not log when served token matches fallback', async () => { |
| 67 | + const token = await resolveServedDashboardToken('http://127.0.0.1:9120', 'same-token', { |
| 68 | + fetchText: async () => '<script>window.__HERMES_SESSION_TOKEN__="same-token";</script>', |
| 69 | + rememberLog: () => { |
| 70 | + throw new Error('should not log when token already matches') |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + assert.equal(token, 'same-token') |
| 75 | +}) |
| 76 | + |
| 77 | +test('resolveServedDashboardToken propagates fetch errors so callers can fall back explicitly', async () => { |
| 78 | + await assert.rejects( |
| 79 | + () => |
| 80 | + resolveServedDashboardToken('http://127.0.0.1:9120', 'spawn-token', { |
| 81 | + fetchText: async () => { |
| 82 | + throw new Error('boom') |
| 83 | + } |
| 84 | + }), |
| 85 | + /boom/ |
| 86 | + ) |
| 87 | +}) |
| 88 | + |
| 89 | +test('fetchPublicText rejects unsupported protocols', async () => { |
| 90 | + await assert.rejects(() => fetchPublicText('file:///tmp/index.html'), /Unsupported Hermes backend URL protocol/) |
| 91 | +}) |
| 92 | + |
| 93 | +test('isForeignBackendToken only flags a mismatched token from a dead child', () => { |
| 94 | + const cases = [ |
| 95 | + [{ servedToken: 'other', spawnToken: 'mine', childAlive: false }, true], |
| 96 | + // Live child + drift = our backend regenerated the token (env pin lost). |
| 97 | + [{ servedToken: 'other', spawnToken: 'mine', childAlive: true }, false], |
| 98 | + [{ servedToken: 'mine', spawnToken: 'mine', childAlive: false }, false], |
| 99 | + [{ servedToken: 'mine', spawnToken: 'mine', childAlive: true }, false], |
| 100 | + [{ servedToken: null, spawnToken: 'mine', childAlive: false }, false], |
| 101 | + [{ servedToken: '', spawnToken: 'mine', childAlive: false }, false] |
| 102 | + ] |
| 103 | + for (const [input, expected] of cases) { |
| 104 | + assert.equal(isForeignBackendToken(input), expected, JSON.stringify(input)) |
| 105 | + } |
| 106 | +}) |
| 107 | + |
| 108 | +test('adoptServedDashboardToken adopts drift from a live child', async () => { |
| 109 | + const token = await adoptServedDashboardToken('http://127.0.0.1:9120', 'spawn-token', { |
| 110 | + childAlive: () => true, |
| 111 | + fetchText: async () => '<script>window.__HERMES_SESSION_TOKEN__="served-token";</script>' |
| 112 | + }) |
| 113 | + |
| 114 | + assert.equal(token, 'served-token') |
| 115 | +}) |
| 116 | + |
| 117 | +test('adoptServedDashboardToken refuses a foreign token when our child is dead', async () => { |
| 118 | + await assert.rejects( |
| 119 | + () => |
| 120 | + adoptServedDashboardToken('http://127.0.0.1:9120', 'spawn-token', { |
| 121 | + childAlive: () => false, |
| 122 | + fetchText: async () => '<script>window.__HERMES_SESSION_TOKEN__="squatter-token";</script>', |
| 123 | + label: 'Hermes backend for profile "work"' |
| 124 | + }), |
| 125 | + /profile "work".*process we did not spawn/ |
| 126 | + ) |
| 127 | +}) |
| 128 | + |
| 129 | +test('adoptServedDashboardToken falls back to the spawn token when the fetch fails', async () => { |
| 130 | + const logs = [] |
| 131 | + const token = await adoptServedDashboardToken('http://127.0.0.1:9120', 'spawn-token', { |
| 132 | + childAlive: () => true, |
| 133 | + fetchText: async () => { |
| 134 | + throw new Error('boom') |
| 135 | + }, |
| 136 | + rememberLog: line => logs.push(line) |
| 137 | + }) |
| 138 | + |
| 139 | + assert.equal(token, 'spawn-token') |
| 140 | + assert.equal(logs.length, 1) |
| 141 | + assert.match(logs[0], /could not read served dashboard token \(Hermes backend\): boom/) |
| 142 | +}) |
0 commit comments