|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * End-to-end self-test for "an edited setting takes effect". |
| 5 | + * |
| 6 | + * Stands up two fake DeepSQL servers on different loopback ports and drives the |
| 7 | + * real profiles + transport code against them. Editing a profile to point at the |
| 8 | + * second server must move the live connection to it; that is the whole bug this |
| 9 | + * covers, and it is invisible to any check that only inspects what was saved, |
| 10 | + * because saving was never the broken half. |
| 11 | + * |
| 12 | + * npm run selftest:settings |
| 13 | + */ |
| 14 | + |
| 15 | +const fs = require('node:fs'); |
| 16 | +const http = require('node:http'); |
| 17 | +const os = require('node:os'); |
| 18 | +const path = require('node:path'); |
| 19 | +const { app } = require('electron'); |
| 20 | + |
| 21 | +// Before anything reads it: the real store holds the user's live connections. |
| 22 | +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'deepsql-settings-test-')); |
| 23 | +app.setPath('userData', tmpDir); |
| 24 | + |
| 25 | +const results = []; |
| 26 | +function check(name, condition, detail = '') { |
| 27 | + results.push({ name, ok: Boolean(condition), detail }); |
| 28 | + process.stdout.write(` ${condition ? 'PASS' : 'FAIL'} ${name}${detail ? ` — ${detail}` : ''}\n`); |
| 29 | +} |
| 30 | + |
| 31 | +/** A stand-in for the DeepSQL nginx, tagged so we can tell the two apart. */ |
| 32 | +function fakeDeepSql(label) { |
| 33 | + return http.createServer((req, res) => { |
| 34 | + if (req.url === '/api/actuator/health') { |
| 35 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 36 | + res.end(JSON.stringify({ status: 'UP', server: label })); |
| 37 | + return; |
| 38 | + } |
| 39 | + res.writeHead(404); |
| 40 | + res.end(); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function listen(server) { |
| 45 | + return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(server.address().port))); |
| 46 | +} |
| 47 | + |
| 48 | +app.whenReady().then(async () => { |
| 49 | + const profiles = require('../src/main/profiles'); |
| 50 | + const transport = require('../src/main/transport'); |
| 51 | + |
| 52 | + const serverA = fakeDeepSql('A'); |
| 53 | + const serverB = fakeDeepSql('B'); |
| 54 | + const portA = await listen(serverA); |
| 55 | + const portB = await listen(serverB); |
| 56 | + const originA = `http://127.0.0.1:${portA}`; |
| 57 | + const originB = `http://127.0.0.1:${portB}`; |
| 58 | + |
| 59 | + process.stdout.write(`\nFake DeepSQL A=${originA} B=${originB}\n\n`); |
| 60 | + |
| 61 | + try { |
| 62 | + // ── The reported bug: change a setting, and the behaviour must follow ── |
| 63 | + process.stdout.write('Edited settings reach the live connection\n'); |
| 64 | + |
| 65 | + const saved = profiles.upsert({ name: 'Test', transport: 'direct', url: originA }); |
| 66 | + const id = saved.id; |
| 67 | + |
| 68 | + const first = await transport.connect(id); |
| 69 | + check('connects to the configured server', first.origin === originA, first.origin); |
| 70 | + |
| 71 | + const second = await transport.connect(id); |
| 72 | + check( |
| 73 | + 'an unchanged reconnect reuses the live connection', |
| 74 | + second.reused === true && second.origin === originA, |
| 75 | + `reused=${second.reused} origin=${second.origin}`, |
| 76 | + ); |
| 77 | + |
| 78 | + // Exactly what the launcher does on Connect: persist the form, then connect. |
| 79 | + profiles.upsert({ id, name: 'Test', transport: 'direct', url: originB }); |
| 80 | + const third = await transport.connect(id); |
| 81 | + check( |
| 82 | + 'connecting after an edit moves to the new server', |
| 83 | + third.origin === originB, |
| 84 | + `origin=${third.origin}${third.reused ? ' (REUSED STALE CONNECTION)' : ''}`, |
| 85 | + ); |
| 86 | + check( |
| 87 | + 'the stale connection was not reused', |
| 88 | + third.reused !== true, |
| 89 | + third.reused ? 'connect() returned the pre-edit origin' : '', |
| 90 | + ); |
| 91 | + |
| 92 | + // ── reconcile(): what makes a save take effect immediately ──────────── |
| 93 | + process.stdout.write('\nreconcile() applies a save without waiting for Connect\n'); |
| 94 | + |
| 95 | + profiles.upsert({ id, name: 'Test', transport: 'direct', url: originA }); |
| 96 | + const reconciled = await transport.reconcile(id); |
| 97 | + check( |
| 98 | + 'reconcile rebuilds onto the saved settings', |
| 99 | + reconciled.changed === true && reconciled.ok === true && reconciled.origin === originA, |
| 100 | + `changed=${reconciled.changed} ok=${reconciled.ok} origin=${reconciled.origin}`, |
| 101 | + ); |
| 102 | + check( |
| 103 | + 'the live origin now matches the saved profile', |
| 104 | + transport.originFor(id) === originA, |
| 105 | + transport.originFor(id), |
| 106 | + ); |
| 107 | + |
| 108 | + // A rename must not cost the user their session. |
| 109 | + const before = transport.get(id); |
| 110 | + profiles.upsert({ id, name: 'Renamed', transport: 'direct', url: originA }); |
| 111 | + const renamed = await transport.reconcile(id); |
| 112 | + check( |
| 113 | + 'renaming does not rebuild the connection', |
| 114 | + renamed.changed === false && transport.get(id) === before, |
| 115 | + `changed=${renamed.changed} sameEntry=${transport.get(id) === before}`, |
| 116 | + ); |
| 117 | + |
| 118 | + // ── A failed rebuild must not leave the old connection running ──────── |
| 119 | + process.stdout.write('\nA rebuild that fails leaves no half-applied state\n'); |
| 120 | + |
| 121 | + await new Promise((resolve) => serverB.close(resolve)); |
| 122 | + profiles.upsert({ id, name: 'Renamed', transport: 'direct', url: originB }); |
| 123 | + const failed = await transport.reconcile(id); |
| 124 | + check( |
| 125 | + 'reconcile reports the failure', |
| 126 | + failed.changed === true && failed.ok === false, |
| 127 | + `ok=${failed.ok} detail=${failed.detail}`, |
| 128 | + ); |
| 129 | + check( |
| 130 | + 'no connection is left serving the replaced settings', |
| 131 | + transport.isConnected(id) === false, |
| 132 | + transport.isConnected(id) ? `still connected to ${transport.originFor(id)}` : '', |
| 133 | + ); |
| 134 | + |
| 135 | + // ── test() must describe the settings on screen ─────────────────────── |
| 136 | + process.stdout.write('\nTest reports on current settings, not the live ones\n'); |
| 137 | + |
| 138 | + profiles.upsert({ id, name: 'Renamed', transport: 'direct', url: originA }); |
| 139 | + await transport.connect(id); |
| 140 | + const serverC = fakeDeepSql('C'); |
| 141 | + const portC = await listen(serverC); |
| 142 | + const originC = `http://127.0.0.1:${portC}`; |
| 143 | + profiles.upsert({ id, name: 'Renamed', transport: 'direct', url: originC }); |
| 144 | + const tested = await transport.test(id); |
| 145 | + check( |
| 146 | + 'testing after an edit exercises the edited settings', |
| 147 | + tested.ok === true && tested.rebuilt === true && transport.originFor(id) === originC, |
| 148 | + `ok=${tested.ok} rebuilt=${tested.rebuilt} live=${transport.originFor(id)}`, |
| 149 | + ); |
| 150 | + await new Promise((resolve) => serverC.close(resolve)); |
| 151 | + |
| 152 | + // ── The fingerprint itself ──────────────────────────────────────────── |
| 153 | + process.stdout.write('\nTransport fingerprint\n'); |
| 154 | + |
| 155 | + const base = profiles.get(id); |
| 156 | + const sameName = { ...base, name: 'Something else' }; |
| 157 | + check( |
| 158 | + 'name is not part of the transport identity', |
| 159 | + profiles.transportFingerprint(base) === profiles.transportFingerprint(sameName), |
| 160 | + ); |
| 161 | + const otherPort = { ...base, ssh: { ...base.ssh, remotePort: base.ssh.remotePort + 1 } }; |
| 162 | + check( |
| 163 | + 'the SSH remote port is part of it', |
| 164 | + profiles.transportFingerprint(base) !== profiles.transportFingerprint(otherPort), |
| 165 | + ); |
| 166 | + const otherLocal = { ...base, ssh: { ...base.ssh, localPort: 44444 } }; |
| 167 | + check( |
| 168 | + 'the pinned local port is part of it', |
| 169 | + profiles.transportFingerprint(base) !== profiles.transportFingerprint(otherLocal), |
| 170 | + ); |
| 171 | + const otherSticky = { ...base, ssh: { ...base.ssh, stickyLocalPort: 44444 } }; |
| 172 | + check( |
| 173 | + 'the sticky local port is NOT (we choose it, not the user)', |
| 174 | + profiles.transportFingerprint(base) === profiles.transportFingerprint(otherSticky), |
| 175 | + ); |
| 176 | + |
| 177 | + await transport.disconnectAll(); |
| 178 | + } catch (err) { |
| 179 | + check('self-test ran to completion', false, err.stack || err.message); |
| 180 | + } |
| 181 | + |
| 182 | + await new Promise((resolve) => serverA.close(resolve)); |
| 183 | + |
| 184 | + const failed = results.filter((r) => !r.ok); |
| 185 | + process.stdout.write( |
| 186 | + `\n${results.length - failed.length}/${results.length} checks passed\n`, |
| 187 | + ); |
| 188 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 189 | + app.exit(failed.length === 0 ? 0 : 1); |
| 190 | +}); |
0 commit comments