From 7d7626151bcf30268636734be919eced298fb9a3 Mon Sep 17 00:00:00 2001 From: Yuan Li Date: Thu, 2 Jul 2026 09:00:18 +0800 Subject: [PATCH] fix(db): preserve healthCheckInterval=0 across create/update (#5822) Integrated into release/v3.8.43 --- src/lib/db/providers.ts | 5 +- ...nnection-healthcheck-interval-zero.test.ts | 120 ++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 tests/unit/provider-connection-healthcheck-interval-zero.test.ts diff --git a/src/lib/db/providers.ts b/src/lib/db/providers.ts index 9323dbc10e..f4ed72524f 100644 --- a/src/lib/db/providers.ts +++ b/src/lib/db/providers.ts @@ -318,6 +318,7 @@ export async function createProviderConnection(data: JsonRecord) { "perKeyProxyEnabled", "quotaWindowThresholds", "rateLimitOverrides", + "healthCheckInterval", ]; for (const field of optionalFields) { if (data[field] !== undefined && data[field] !== null) { @@ -410,7 +411,7 @@ function _insertConnectionRow(db: DbLike, conn: JsonRecord) { lastErrorSource: conn.lastErrorSource || null, backoffLevel: conn.backoffLevel || 0, rateLimitedUntil: conn.rateLimitedUntil || null, - healthCheckInterval: conn.healthCheckInterval || null, + healthCheckInterval: conn.healthCheckInterval ?? null, lastHealthCheckAt: conn.lastHealthCheckAt || null, lastTested: conn.lastTested || null, apiKey: conn.apiKey || null, @@ -488,7 +489,7 @@ function _updateConnectionRow(db: DbLike, id: string, data: JsonRecord) { lastErrorSource: data.lastErrorSource || null, backoffLevel: data.backoffLevel || 0, rateLimitedUntil: data.rateLimitedUntil || null, - healthCheckInterval: data.healthCheckInterval || null, + healthCheckInterval: data.healthCheckInterval ?? null, lastHealthCheckAt: data.lastHealthCheckAt || null, lastTested: data.lastTested || null, apiKey: data.apiKey || null, diff --git a/tests/unit/provider-connection-healthcheck-interval-zero.test.ts b/tests/unit/provider-connection-healthcheck-interval-zero.test.ts new file mode 100644 index 0000000000..0633f7e7d1 --- /dev/null +++ b/tests/unit/provider-connection-healthcheck-interval-zero.test.ts @@ -0,0 +1,120 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +process.env.NODE_ENV = "test"; + +const TEST_DATA_DIR = fs.mkdtempSync( + path.join(os.tmpdir(), "omniroute-hci-zero-") +); +process.env.DATA_DIR = TEST_DATA_DIR; + +const core = await import("../../src/lib/db/core.ts"); +const providersDb = await import("../../src/lib/db/providers.ts"); + +async function resetStorage() { + core.resetDbInstance(); + for (let attempt = 0; attempt < 10; attempt++) { + try { + if (fs.existsSync(TEST_DATA_DIR)) { + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); + } + break; + } catch (error: any) { + if ((error?.code === "EBUSY" || error?.code === "EPERM") && attempt < 9) { + await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1))); + } else { + throw error; + } + } + } + fs.mkdirSync(TEST_DATA_DIR, { recursive: true }); +} + +test.after(async () => { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); +}); + +// Regression: the `_insertConnectionRow` and `_updateConnectionRow` bind helpers +// used `data.healthCheckInterval || null`, which collapses an explicit `0` +// ("disable the proactive sweep", see src/lib/tokenHealthCheck.ts: +// `if (intervalMin <= 0) return;`) to `null`. Once persisted as NULL, reading +// the row back yields `undefined`/`null`, and the dashboard's +// `connection.healthCheckInterval ?? 60` fallback renders a misleading `60` +// instead of the user's chosen `0`. Using `?? null` preserves `0`. +test("createProviderConnection persists healthCheckInterval=0 (not coerced to null)", async () => { + await resetStorage(); + + const connection = await providersDb.createProviderConnection({ + provider: "antigravity", + authType: "oauth", + name: "Antigravity Disabled HC", + email: "hc-zero-create@example.com", + accessToken: "access-token", + refreshToken: "refresh-token", + expiresAt: new Date(Date.now() + 3600 * 1000).toISOString(), + tokenExpiresAt: new Date(Date.now() + 3600 * 1000).toISOString(), + testStatus: "active", + isActive: true, + healthCheckInterval: 0, + }); + + const stored = await providersDb.getProviderConnectionById((connection as any).id); + assert.equal(stored?.healthCheckInterval, 0); +}); + +test("updateProviderConnection persists healthCheckInterval=0 (not coerced to null)", async () => { + await resetStorage(); + + const connection = await providersDb.createProviderConnection({ + provider: "antigravity", + authType: "oauth", + name: "Antigravity Default HC", + email: "hc-zero-update@example.com", + accessToken: "access-token", + refreshToken: "refresh-token", + expiresAt: new Date(Date.now() + 3600 * 1000).toISOString(), + tokenExpiresAt: new Date(Date.now() + 3600 * 1000).toISOString(), + testStatus: "active", + isActive: true, + // Start from the default (60). Updating to 0 below must round-trip. + healthCheckInterval: 60, + }); + + await providersDb.updateProviderConnection((connection as any).id, { + healthCheckInterval: 0, + }); + + const stored = await providersDb.getProviderConnectionById((connection as any).id); + assert.equal(stored?.healthCheckInterval, 0); +}); + +// Sanity: a nonzero value still round-trips (the `||` → `??` change preserves 60 +// because 60 is truthy, and `?? null` also keeps it). +test("updateProviderConnection still persists a nonzero healthCheckInterval", async () => { + await resetStorage(); + + const connection = await providersDb.createProviderConnection({ + provider: "antigravity", + authType: "oauth", + name: "Antigravity Nonzero HC", + email: "hc-nonzero-update@example.com", + accessToken: "access-token", + refreshToken: "refresh-token", + expiresAt: new Date(Date.now() + 3600 * 1000).toISOString(), + tokenExpiresAt: new Date(Date.now() + 3600 * 1000).toISOString(), + testStatus: "active", + isActive: true, + healthCheckInterval: 0, + }); + + await providersDb.updateProviderConnection((connection as any).id, { + healthCheckInterval: 60, + }); + + const stored = await providersDb.getProviderConnectionById((connection as any).id); + assert.equal(stored?.healthCheckInterval, 60); +}); \ No newline at end of file