diff --git a/src/shared/utils/connectionStatus.ts b/src/shared/utils/connectionStatus.ts index a2a4ff1446..4a6baaf4de 100644 --- a/src/shared/utils/connectionStatus.ts +++ b/src/shared/utils/connectionStatus.ts @@ -16,13 +16,14 @@ export interface ConnectionActiveFlag { /** * Filters out connections that have been explicitly disabled * (`isActive === false`). Connections without an `isActive` field are - * treated as active for backward compatibility. + * treated as active for backward compatibility. Nullish entries are + * dropped so callers can safely read properties off the result. */ export function filterActiveConnections( connections: T[] | null | undefined ): T[] { if (!Array.isArray(connections)) return []; - return connections.filter((connection) => connection?.isActive !== false); + return connections.filter((connection) => !!connection && connection.isActive !== false); } /** diff --git a/tests/unit/connection-status-filter-active-2526.test.ts b/tests/unit/connection-status-filter-active-2526.test.ts index ff0e5eb2e3..9e60485fc3 100644 --- a/tests/unit/connection-status-filter-active-2526.test.ts +++ b/tests/unit/connection-status-filter-active-2526.test.ts @@ -25,6 +25,18 @@ test("filterActiveConnections returns an empty list for invalid input", () => { assert.deepEqual(filterActiveConnections(null), []); }); +test("filterActiveConnections drops nullish entries instead of passing them through", () => { + // A nullish element must not survive: callers read properties off the + // result (e.g. `connection.testStatus`) and would throw a TypeError. + const active = { id: "active", isActive: true }; + + assert.deepEqual(filterActiveConnections([null, active, undefined]), [active]); + assert.doesNotThrow(() => filterUsableConnections([null, undefined])); + assert.deepEqual(filterUsableConnections([null, { id: "ok", testStatus: "active" }]), [ + { id: "ok", testStatus: "active" }, + ]); +}); + test("filterUsableConnections applies the isActive gate before the testStatus gate", () => { // Regression for the exact bug: a disabled connection with a stale // "active" testStatus must NOT survive the combined filter that