From c0248cbe8e5e63478cd542f4a4b719861cf8a49f Mon Sep 17 00:00:00 2001 From: fagao Date: Mon, 22 Jun 2026 21:43:01 +0800 Subject: [PATCH] fix: handle connection loss errors consistently --- apps/desktop/src/lib/connectionHealth.ts | 17 ++- apps/desktop/src/stores/connectionStore.ts | 59 ++++--- apps/desktop/src/stores/queryStore.ts | 2 +- .../connectionStoreErrorState.test.ts | 37 +++++ packages/app-tests/queryStore.test.ts | 144 ++++++++++++------ 5 files changed, 185 insertions(+), 74 deletions(-) diff --git a/apps/desktop/src/lib/connectionHealth.ts b/apps/desktop/src/lib/connectionHealth.ts index 91e1b47890..d43f70a199 100644 --- a/apps/desktop/src/lib/connectionHealth.ts +++ b/apps/desktop/src/lib/connectionHealth.ts @@ -1,4 +1,19 @@ -const CONNECTION_ERROR_PATTERNS = ["connection", "broken pipe", "reset by peer", "timed out", "closed", "eof", "i/o error"]; +const CONNECTION_ERROR_PATTERNS = [ + "connection reset", + "connection refused", + "connection timed out", + "connection closed", + "connection lost", + "connection not found", + "connection config not found", + "not connected", + "broken pipe", + "reset by peer", + "socket closed", + "unexpected eof", + "end-of-file on communication channel", + "i/o error", +]; export function staleConnectionMessage(error: unknown): string { if (error instanceof Error) return error.message; diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts index 279968c50d..72594b3b11 100644 --- a/apps/desktop/src/stores/connectionStore.ts +++ b/apps/desktop/src/stores/connectionStore.ts @@ -260,11 +260,23 @@ export const useConnectionStore = defineStore("connection", () => { return message; } - function recordConnectionLostError(connectionId: string, error: unknown) { + function markConnectionLost(connectionId: string, error: unknown) { + connectedIds.value.delete(connectionId); + if (activeConnectionId.value === connectionId) activeConnectionId.value = null; + recordConnectionError(connectionId, error); + } + + function recordConnectionLostError(connectionId: string, error: unknown): boolean { if (shouldMarkDisconnected(error)) { - connectedIds.value.delete(connectionId); - if (activeConnectionId.value === connectionId) activeConnectionId.value = null; + markConnectionLost(connectionId, error); + return true; } + return false; + } + + // Metadata loaders keep this internal: match connection-loss errors before recording generic errors. + function recordMetadataLoadError(connectionId: string, error: unknown) { + if (recordConnectionLostError(connectionId, error)) return; recordConnectionError(connectionId, error); } @@ -1030,7 +1042,7 @@ export const useConnectionStore = defineStore("connection", () => { } node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1073,7 +1085,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1107,7 +1119,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1137,7 +1149,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1201,7 +1213,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1233,7 +1245,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1265,7 +1277,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1293,7 +1305,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1332,7 +1344,7 @@ export const useConnectionStore = defineStore("connection", () => { await savePersistedTreeChildren(cacheKey, children); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1368,7 +1380,7 @@ export const useConnectionStore = defineStore("connection", () => { await savePersistedTreeChildren(cacheKey, children); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1401,7 +1413,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1436,7 +1448,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1478,7 +1490,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(node.connectionId, e); + recordMetadataLoadError(node.connectionId, e); throw e; } finally { node.isLoading = false; @@ -1534,7 +1546,7 @@ export const useConnectionStore = defineStore("connection", () => { await savePersistedTreeChildren(cacheKey, children); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1594,7 +1606,7 @@ export const useConnectionStore = defineStore("connection", () => { } node.isExpanded = true; } catch (e) { - recordConnectionLostError(node.connectionId, e); + recordMetadataLoadError(node.connectionId, e); throw e; } finally { node.isLoading = false; @@ -1633,7 +1645,7 @@ export const useConnectionStore = defineStore("connection", () => { await savePersistedTreeChildren(objectGroupCacheKey(parent), nextChildren); parent.isExpanded = true; } catch (e) { - recordConnectionLostError(parent.connectionId, e); + recordMetadataLoadError(parent.connectionId, e); throw e; } finally { node.isLoading = false; @@ -1765,7 +1777,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1802,7 +1814,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1843,7 +1855,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -1880,7 +1892,7 @@ export const useConnectionStore = defineStore("connection", () => { ); node.isExpanded = true; } catch (e) { - recordConnectionLostError(connectionId, e); + recordMetadataLoadError(connectionId, e); throw e; } finally { node.isLoading = false; @@ -3049,6 +3061,7 @@ export const useConnectionStore = defineStore("connection", () => { setConnectionError, clearConnectionError, recordConnectionError, + markConnectionLost, recordConnectionLostError, sidebarLayout, getConfig, diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 6f2243bb8d..6410c76a59 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -1845,7 +1845,7 @@ export const useQueryStore = defineStore("query", () => { tab.executionId = undefined; const error = new Error(t("editor.connectionMayBeLost")); tab.result = toErrorResult(error); - connStore.recordConnectionLostError(tab.connectionId, error); + connStore.markConnectionLost(tab.connectionId, error); }); } } diff --git a/packages/app-tests/connectionStoreErrorState.test.ts b/packages/app-tests/connectionStoreErrorState.test.ts index a1e93d74d6..5479298ade 100644 --- a/packages/app-tests/connectionStoreErrorState.test.ts +++ b/packages/app-tests/connectionStoreErrorState.test.ts @@ -83,3 +83,40 @@ test("failed disconnect keeps the existing connection error", async () => { restoreStorage(); } }); + +test("query errors mentioning connection do not mark the connection disconnected", async () => { + const restoreStorage = installMemoryStorage(); + try { + setActivePinia(createPinia()); + const store = useConnectionStore(); + store.addEphemeralConnection(conn("conn-1")); + store.activeConnectionId = "conn-1"; + + store.recordConnectionLostError("conn-1", new Error('relation "connection" does not exist')); + + assert.equal(store.connectedIds.has("conn-1"), true); + assert.equal(store.activeConnectionId, "conn-1"); + await new Promise((resolve) => setTimeout(resolve, 0)); + } finally { + restoreStorage(); + } +}); + +test("explicit lost-connection marker clears state without relying on error text", async () => { + const restoreStorage = installMemoryStorage(); + try { + setActivePinia(createPinia()); + const store = useConnectionStore(); + store.addEphemeralConnection(conn("conn-1")); + store.activeConnectionId = "conn-1"; + + store.markConnectionLost("conn-1", new Error("连接可能已断开,请刷新数据重试")); + + assert.equal(store.connectedIds.has("conn-1"), false); + assert.equal(store.activeConnectionId, null); + assert.equal(store.connectionErrors["conn-1"], "连接可能已断开,请刷新数据重试"); + await new Promise((resolve) => setTimeout(resolve, 0)); + } finally { + restoreStorage(); + } +}); diff --git a/packages/app-tests/queryStore.test.ts b/packages/app-tests/queryStore.test.ts index 43f3ae7a5f..cc91a0fcc0 100644 --- a/packages/app-tests/queryStore.test.ts +++ b/packages/app-tests/queryStore.test.ts @@ -46,6 +46,15 @@ function oracleConn(id: string): ConnectionConfig { }; } +function withConnectionHealthMock(handler: typeof fetch): typeof fetch { + return (async (input, init) => { + if (String(input) === "/api/connection/check-health") { + return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } }); + } + return handler(input, init); + }); +} + async function waitFor(predicate: () => boolean, timeoutMs = 1000) { const started = Date.now(); while (!predicate()) { @@ -383,7 +392,7 @@ test("completed query executions append result runs and select the latest run", let executeCount = 0; connectionStore.addEphemeralConnection(conn("conn-1")); - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { const body = JSON.parse(String(init?.body ?? "{}")); @@ -406,7 +415,7 @@ test("completed query executions append result runs and select the latest run", }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const tabId = store.createTab("conn-1", "db", "Query"); @@ -438,7 +447,7 @@ test("failed query executions append switchable error result runs", async () => const originalFetch = globalThis.fetch; connectionStore.addEphemeralConnection(conn("conn-1")); - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { const body = JSON.parse(String(init?.body ?? "{}")); @@ -451,7 +460,7 @@ test("failed query executions append switchable error result runs", async () => return new Response("backend exploded", { status: 500 }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const tabId = store.createTab("conn-1", "db", "Query"); @@ -469,6 +478,43 @@ test("failed query executions append switchable error result runs", async () => } }); +test("query execution errors mentioning connection keep the connection active", async () => { + const restoreStorage = installMemoryStorage(); + setActivePinia(createPinia()); + const connectionStore = useConnectionStore(); + const store = useQueryStore(); + const originalFetch = globalThis.fetch; + + connectionStore.addEphemeralConnection(conn("conn-1")); + connectionStore.activeConnectionId = "conn-1"; + globalThis.fetch = withConnectionHealthMock(async (input, init) => { + const url = String(input); + if (url === "/api/query/prepare-pagination-plan") { + const body = JSON.parse(String(init?.body ?? "{}")); + return new Response(JSON.stringify({ sqlToExecute: body.options.sql, useAgentResultSession: false }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + } + if (url === "/api/query/execute-multi") { + return new Response('relation "connection" does not exist', { status: 500 }); + } + return new Response("unexpected request", { status: 500 }); + }); + + try { + const tabId = store.createTab("conn-1", "db", "Query"); + await store.executeTabSql(tabId, "select connection from missing_table"); + + assert.equal(connectionStore.connectedIds.has("conn-1"), true); + assert.equal(connectionStore.activeConnectionId, "conn-1"); + assert.equal(connectionStore.connectionErrors["conn-1"], undefined); + } finally { + globalThis.fetch = originalFetch; + restoreStorage(); + } +}); + test("statement result switching is scoped to the active result run", async () => { setActivePinia(createPinia()); const store = useQueryStore(); @@ -524,7 +570,7 @@ test("normalizes unquoted Oracle query identifiers before loading editable metad connectionStore.addEphemeralConnection(oracleConn("oracle-1")); - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/execute-multi") { return new Response( @@ -596,7 +642,7 @@ test("normalizes unquoted Oracle query identifiers before loading editable metad }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const tabId = store.createTab("oracle-1", "ORCL", "Query 1", "query", "app"); @@ -626,7 +672,7 @@ test("evicting cached tab results releases multi-result payloads and sessions", connectionStore.addEphemeralConnection(conn("conn-1")); - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/execute-multi") { executeCount++; @@ -675,7 +721,7 @@ test("evicting cached tab results releases multi-result payloads and sessions", ); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const tabIds: string[] = []; @@ -709,7 +755,7 @@ test("result cache eviction keeps recently accessed inactive tabs", async () => connectionStore.addEphemeralConnection(conn("conn-1")); - globalThis.fetch = (async (input) => { + globalThis.fetch = withConnectionHealthMock(async (input) => { const url = String(input); if (url === "/api/query/execute-multi") { executeCount++; @@ -743,7 +789,7 @@ test("result cache eviction keeps recently accessed inactive tabs", async () => }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const tabIds: string[] = []; @@ -775,9 +821,9 @@ test("result cache eviction keeps recently accessed inactive tabs", async () => test("closing tabs clears removed result payloads before dropping tab references", async () => { const restoreStorage = installMemoryStorage(); const originalFetch = globalThis.fetch; - globalThis.fetch = (async () => { + globalThis.fetch = withConnectionHealthMock(async () => { return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } }); - }) as typeof fetch; + }); try { setActivePinia(createPinia()); const store = useQueryStore(); @@ -813,9 +859,9 @@ test("closing tabs clears removed result payloads before dropping tab references test("closing database tabs removes browser tabs for that database only", async () => { const restoreStorage = installMemoryStorage(); const originalFetch = globalThis.fetch; - globalThis.fetch = (async () => { + globalThis.fetch = withConnectionHealthMock(async () => { return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } }); - }) as typeof fetch; + }); try { setActivePinia(createPinia()); @@ -863,9 +909,9 @@ test("closing database tabs removes browser tabs for that database only", async test("closing connection tabs removes every tab for that connection only", async () => { const restoreStorage = installMemoryStorage(); const originalFetch = globalThis.fetch; - globalThis.fetch = (async () => { + globalThis.fetch = withConnectionHealthMock(async () => { return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } }); - }) as typeof fetch; + }); try { setActivePinia(createPinia()); @@ -910,9 +956,9 @@ test("closing connection tabs removes every tab for that connection only", async test("releasing connection tabs keeps SQL tabs and closes object tabs", async () => { const restoreStorage = installMemoryStorage(); const originalFetch = globalThis.fetch; - globalThis.fetch = (async () => { + globalThis.fetch = withConnectionHealthMock(async () => { return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } }); - }) as typeof fetch; + }); try { setActivePinia(createPinia()); @@ -970,9 +1016,9 @@ test("releasing connection tabs keeps SQL tabs and closes object tabs", async () test("releasing database tabs keeps SQL tabs and closes table tabs for that database only", async () => { const restoreStorage = installMemoryStorage(); const originalFetch = globalThis.fetch; - globalThis.fetch = (async () => { + globalThis.fetch = withConnectionHealthMock(async () => { return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } }); - }) as typeof fetch; + }); try { setActivePinia(createPinia()); @@ -1015,9 +1061,9 @@ test("releasing database tabs keeps SQL tabs and closes table tabs for that data test("disconnecting a connection closes every tab for that connection", async () => { const restoreStorage = installMemoryStorage(); const originalFetch = globalThis.fetch; - globalThis.fetch = (async () => { + globalThis.fetch = withConnectionHealthMock(async () => { return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } }); - }) as typeof fetch; + }); try { setActivePinia(createPinia()); @@ -1066,7 +1112,7 @@ test("starting a new query clears the previous result payload immediately", asyn execution_time_ms: 1, }; - globalThis.fetch = (async (input) => { + globalThis.fetch = withConnectionHealthMock(async (input) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response(JSON.stringify({ sqlToExecute: "select 1", useAgentResultSession: false }), { @@ -1087,7 +1133,7 @@ test("starting a new query clears the previous result payload immediately", asyn }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const execution = store.executeTabSql(tabId, "select 1"); @@ -1120,7 +1166,7 @@ test("grid refreshes can preserve the previous result while loading", async () = }; tab.result = previousResult; - globalThis.fetch = (async (input) => { + globalThis.fetch = withConnectionHealthMock(async (input) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response(JSON.stringify({ sqlToExecute: "select 1 order by name", useAgentResultSession: false }), { @@ -1141,7 +1187,7 @@ test("grid refreshes can preserve the previous result while loading", async () = }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const execution = store.executeTabSql(tabId, "select 1 order by name", { @@ -1172,7 +1218,7 @@ test("data tab execution preserves pagination offset metadata", async () => { const tab = store.tabs.find((item) => item.id === tabId); assert.ok(tab); - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { preparedPagination = true; @@ -1186,7 +1232,7 @@ test("data tab execution preserves pagination offset metadata", async () => { }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, 'SELECT * FROM "users" LIMIT 100 OFFSET 100;', { @@ -1224,7 +1270,7 @@ test("activating an empty data tab waits for explicit execution", async () => { tab.resultPageLimit = 50; tab.resultPageOffset = 50; - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/execute-multi") { executeBody = JSON.parse(String(init?.body ?? "{}")); @@ -1234,7 +1280,7 @@ test("activating an empty data tab waits for explicit execution", async () => { }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.reloadEvictedTab(tabId); @@ -1275,7 +1321,7 @@ test("query result export fetches every paginated page", async () => { has_more: true, }; - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { const body = JSON.parse(String(init?.body ?? "{}")); @@ -1306,7 +1352,7 @@ test("query result export fetches every paginated page", async () => { }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const exported = await store.fetchTabResultForExport(tabId); @@ -1350,7 +1396,7 @@ test("query result export treats the known query total as a progress estimate", has_more: true, }; - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { const body = JSON.parse(String(init?.body ?? "{}")); @@ -1378,7 +1424,7 @@ test("query result export treats the known query total as a progress estimate", }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const exported = await store.fetchTabResultForExport(tabId, (info) => progress.push(info)); @@ -1416,7 +1462,7 @@ test("jdbc query pagination uses result sessions without capping max rows to one const tab = store.tabs.find((item) => item.id === tabId); assert.ok(tab); - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { prepareBody = JSON.parse(String(init?.body ?? "{}")); @@ -1453,7 +1499,7 @@ test("jdbc query pagination uses result sessions without capping max rows to one }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, "SELECT * FROM CT_Loc"); @@ -1518,7 +1564,7 @@ test("table data export fetches every filtered page", async () => { primaryKeys: ["id"], }; - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/build-table-select-sql") { const body = JSON.parse(String(init?.body ?? "{}")); @@ -1541,7 +1587,7 @@ test("table data export fetches every filtered page", async () => { }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { const exported = await store.fetchTabResultForExport(tabId); @@ -1599,7 +1645,7 @@ test("query execution finishes without waiting for metadata analysis", async () assert.ok(tab); let resolveMetadata: ((value: Response) => void) | undefined; - globalThis.fetch = (async (input) => { + globalThis.fetch = withConnectionHealthMock(async (input) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response(JSON.stringify({ sqlToExecute: "select id from users", useAgentResultSession: false }), { @@ -1619,7 +1665,7 @@ test("query execution finishes without waiting for metadata analysis", async () }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, "select id from users"); @@ -1652,7 +1698,7 @@ test("query execution is scoped to the tab client session", async () => { const tabId = store.createTab("conn-1", "db", "Query"); let executeBody: any; - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response(JSON.stringify({ sqlToExecute: "select 1", useAgentResultSession: false }), { @@ -1674,7 +1720,7 @@ test("query execution is scoped to the tab client session", async () => { }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, "select 1"); @@ -1701,7 +1747,7 @@ test("query execution keeps automatically counting total rows in the background" let resolveCount: ((value: Response) => void) | undefined; let countBody: any; - globalThis.fetch = (async (input, init) => { + globalThis.fetch = withConnectionHealthMock(async (input, init) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response( @@ -1742,7 +1788,7 @@ test("query execution keeps automatically counting total rows in the background" }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, "select id from users"); @@ -1786,7 +1832,7 @@ test("paginated query execution keeps the previous total while refreshing it in tab.resultTotalRowCount = 250; let resolveCount: ((value: Response) => void) | undefined; - globalThis.fetch = (async (input) => { + globalThis.fetch = withConnectionHealthMock(async (input) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response( @@ -1826,7 +1872,7 @@ test("paginated query execution keeps the previous total while refreshing it in }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, "select id from users", { @@ -1867,7 +1913,7 @@ test("multi statement execution shows the first result set by default", async () connectionStore.addEphemeralConnection(conn("conn-1")); const tabId = store.createTab("conn-1", "db", "Query"); - globalThis.fetch = (async (input) => { + globalThis.fetch = withConnectionHealthMock(async (input) => { const url = String(input); if (url === "/api/query/prepare-pagination-plan") { return new Response(JSON.stringify({ sqlToExecute: "set @id = 1; select @id", useAgentResultSession: false }), { @@ -1891,7 +1937,7 @@ test("multi statement execution shows the first result set by default", async () }); } return new Response("unexpected request", { status: 500 }); - }) as typeof fetch; + }); try { await store.executeTabSql(tabId, "set @id = 1; select @id"); @@ -2005,7 +2051,7 @@ test("reorderTab preserves relative order within pinned group", () => { const tabD = store.createTab("conn-1", "db", "D", "query"); const tabE = store.createTab("conn-1", "db", "E", "query"); - // Pin A, B, C — leave D, E unpinned + // Pin A, B, C; leave D, E unpinned store.togglePinnedTab(tabA); // toggle so orderPinnedFirst runs: [A, B, C, D, E] store.togglePinnedTab(tabB);