Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

test(desktop-ui): retry a launch that lost the free-port race

velixio27bc6626c1db
diff --git a/electron/package.json b/electron/package.json
index c7fda3dc9..7e902c39f 100644
--- a/electron/package.json
+++ b/electron/package.json
@@ -27,7 +27,7 @@
"format:check": "vp fmt --check",
"test": "bun run locale:check && vp test --run && vp test --run --config vite.shared.config.ts",
"test:watch": "vp test",
- "test:desktop-ui": "node tests/local-first.test.mjs && node tests/fake-health-backend.test.mjs && node tests/desktop-ui.test.mjs",
+ "test:desktop-ui": "node tests/local-first.test.mjs && node tests/fake-health-backend.test.mjs && node tests/desktop-harness-ports.test.mjs && node tests/desktop-ui.test.mjs",
"smoke:server": "vite --config tests/renderer-smoke.vite.config.mjs",
"smoke:live-routes": "node tests/live-route-health.mjs",
"smoke:account-live": "node tests/account-sign-in-live.mjs",
diff --git a/electron/tests/desktop-harness-ports.test.mjs b/electron/tests/desktop-harness-ports.test.mjs
new file mode 100644
index 000000000..490bc7770
--- /dev/null
+++ b/electron/tests/desktop-harness-ports.test.mjs
@@ -0,0 +1,66 @@
+import assert from 'node:assert/strict';
+import { createServer } from 'node:net';
+import test from 'node:test';
+import { freePort, isPortConflict, launchWithFreshPorts } from './desktop-harness.mjs';
+
+const conflict = () =>
+ new Error(
+ 'electron.launch: closed\n[startup] Error: listen EADDRINUSE: address already in use 127.0.0.1:39081',
+ );
+
+test('only a port collision is retried, with fresh ports each time', async () => {
+ let calls = 0;
+ let refreshed = 0;
+ const result = await launchWithFreshPorts(
+ async () => {
+ calls += 1;
+ if (calls < 3) throw conflict();
+ return 'launched';
+ },
+ async () => {
+ refreshed += 1;
+ },
+ );
+ assert.equal(result, 'launched');
+ assert.equal(calls, 3);
+ assert.equal(refreshed, 2);
+});
+
+test('gives up after the attempt limit and surfaces the collision', async () => {
+ let calls = 0;
+ await assert.rejects(
+ launchWithFreshPorts(
+ async () => {
+ calls += 1;
+ throw conflict();
+ },
+ async () => {},
+ ),
+ /EADDRINUSE/,
+ );
+ assert.equal(calls, 3);
+});
+
+test('any other launch failure is thrown immediately', async () => {
+ let calls = 0;
+ await assert.rejects(
+ launchWithFreshPorts(
+ async () => {
+ calls += 1;
+ throw new Error('renderer crashed');
+ },
+ async () => assert.fail('must not pick new ports'),
+ ),
+ /renderer crashed/,
+ );
+ assert.equal(calls, 1);
+ assert.equal(isPortConflict(new Error('boom')), false);
+});
+
+test('freePort returns a port that can be bound right away', async () => {
+ const port = await freePort();
+ await new Promise((resolve, reject) => {
+ const server = createServer().once('error', reject);
+ server.listen(port, '127.0.0.1', () => server.close(resolve));
+ });
+});
diff --git a/electron/tests/desktop-harness.mjs b/electron/tests/desktop-harness.mjs
index 710e0da4a..067c589be 100644
--- a/electron/tests/desktop-harness.mjs
+++ b/electron/tests/desktop-harness.mjs
@@ -55,6 +55,26 @@ export const freePort = () =>
});
});
+/** True when an app launch died because a port chosen by `freePort()` was taken meanwhile. */
+export const isPortConflict = (error) => /EADDRINUSE/.test(String(error?.message ?? error));
+
+/**
+ * `freePort()` can only report a port that was free a moment ago: between closing its
+ * probe and Electron binding the port, another process (a parallel test, the previous
+ * app still shutting down) can take it. Retry the launch with fresh ports rather than
+ * failing a test for a collision that has nothing to do with it.
+ */
+export async function launchWithFreshPorts(launchOnce, nextPorts, attempts = 3) {
+ for (let attempt = 1; ; attempt += 1) {
+ try {
+ return await launchOnce();
+ } catch (error) {
+ if (attempt >= attempts || !isPortConflict(error)) throw error;
+ await nextPorts();
+ }
+ }
+}
+
const slug = (name) =>
name
.replace(/[^a-z0-9]+/gi, '-')
@@ -110,6 +130,7 @@ async function launch({ profile, backendPort, proxyPort, seed }) {
try {
page = await app.firstWindow();
} catch (error) {
+ await app.close().catch(() => {});
// The app died before showing a window: its own output is the only evidence of why.
let journal = '(none)';
try {
@@ -160,12 +181,18 @@ export async function withApp(t, options, body) {
},
})
: null;
- const backendPort = backend ? backend.port : await freePort();
- const proxyPort = await freePort();
+ let backendPort = backend ? backend.port : await freePort();
+ let proxyPort = await freePort();
const live = [];
const session = { profile, backend, relaunch: null };
const start = async () => {
- const started = await launch({ profile, backendPort, proxyPort, seed });
+ const started = await launchWithFreshPorts(
+ () => launch({ profile, backendPort, proxyPort, seed }),
+ async () => {
+ proxyPort = await freePort();
+ if (!backend) backendPort = await freePort();
+ },
+ );
live.push(started);
Object.assign(session, started);
return session;