diff --git a/src/renderer/runtime/useMeshcoreRuntime.reconnect.test.ts b/src/renderer/runtime/useMeshcoreRuntime.reconnect.test.ts index 037c85e20..ced2262b2 100644 --- a/src/renderer/runtime/useMeshcoreRuntime.reconnect.test.ts +++ b/src/renderer/runtime/useMeshcoreRuntime.reconnect.test.ts @@ -521,9 +521,20 @@ describe('useMeshcoreRuntime manual disconnect must not auto-reconnect', () => { // and left status=reconnecting with no further attempts (n7eal / #792 MeshCore TCP). const abortIdx = reconnectBody.indexOf('isMeshcoreSetupAbortError(err)'); expect(abortIdx).toBeGreaterThan(-1); - const abortBlock = reconnectBody.slice(abortIdx, abortIdx + 900); + // Delimit the abort branch at the non-abort retry path rather than a fixed + // char window, so adding late-transport cleanup before the defer cannot push + // `return 'defer'` out of range. + const retryIdx = reconnectBody.indexOf("return 'retry'", abortIdx); + expect(retryIdx).toBeGreaterThan(abortIdx); + const abortBlock = reconnectBody.slice(abortIdx, retryIdx); expect(abortBlock).toContain('meshcoreDeferredReconnectRef.current = true'); - expect(abortBlock).toContain("return 'defer'"); + // Setup abort must clean up any transport this doomed attempt opened *before* deferring, + // otherwise a late-opened driver leaks while the deferred restart brings up a fresh one. + const cleanupIdx = abortBlock.indexOf('lateTransport.cleanup(openedDriverIdentityId)'); + const deferIdx = abortBlock.indexOf("return 'defer'"); + expect(cleanupIdx).toBeGreaterThan(-1); + expect(deferIdx).toBeGreaterThan(-1); + expect(cleanupIdx).toBeLessThan(deferIdx); expect(abortBlock).not.toMatch(/meshcoreIsReconnectingRef\.current = false/); });