From 7f0bd0e9d3c71e5d8ddf6256378198a949341808 Mon Sep 17 00:00:00 2001 From: Afloat Date: Fri, 20 Nov 2026 15:08:39 +0800 Subject: [PATCH] fix: refresh skipped-proxy evidence in repeated alerts --- src/upstream-alerts.ts | 2 ++ tests/skipped-proxy-hint.test.ts | 55 ++++++++++++++++++++++++++++++++ tests/upstream-alerts.test.ts | 26 +++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/src/upstream-alerts.ts b/src/upstream-alerts.ts index 27e3fb2ec8..ecd12a09bb 100644 --- a/src/upstream-alerts.ts +++ b/src/upstream-alerts.ts @@ -121,6 +121,8 @@ export function recordUpstreamAlert(url: string, error: unknown, viaProxy: boole existing.count += 1; existing.hint = hint; if (code !== undefined) existing.code = code; + delete existing.skippedProxy; + delete existing.skippedSource; Object.assign(existing, skippedFields); return; } diff --git a/tests/skipped-proxy-hint.test.ts b/tests/skipped-proxy-hint.test.ts index 2ef26bfbcc..0e56b6c99b 100644 --- a/tests/skipped-proxy-hint.test.ts +++ b/tests/skipped-proxy-hint.test.ts @@ -181,6 +181,61 @@ test("recordUpstreamAlert: structured skipped fields ride along for the localize assert.equal(a.hint, UPSTREAM_FAIL_HINTS["connect-refused"]); }); +for (const mode of ["explicit-direct", "auto", "host-bypass"] as const) { + test(`recordUpstreamAlert: ${mode} replaces an earlier skipped-proxy diagnosis`, () => { + _resetUpstreamAlertsForTest(); + try { + const fallback = { biliPort: 42422, httpsProxy: ENV_PROXY, systemProxy: { enabled: false } }; + const first = resolveProxyDecision({}, "", TARGET, { ...fallback, explicitDirect: true, defaultDirect: true }); + const error = netError("UND_ERR_CONNECT_TIMEOUT", "connect timed out"); + recordUpstreamAlert(TARGET, error, !!first.proxy, first.skippedProxy); + const firstSeen = getUpstreamAlerts()[0].firstSeen; + assert.ok(getUpstreamAlerts()[0].skippedProxy); + + const next = mode === "auto" + ? resolveProxyDecision({}, undefined, TARGET, fallback) + : resolveProxyDecision({}, "", TARGET, { + ...fallback, explicitDirect: true, defaultDirect: mode === "host-bypass", + ...(mode === "host-bypass" ? { noProxy: "relay.example.com" } : {}), + }); + assert.equal(next.skippedProxy, undefined); + assert.equal(!!next.proxy, mode === "auto"); + recordUpstreamAlert(TARGET, error, !!next.proxy, next.skippedProxy); + + const alerts = getUpstreamAlerts(); + assert.equal(alerts.length, 1); + const [current] = alerts; + assert.equal(current.count, 2); + assert.equal(current.firstSeen, firstSeen); + assert.equal(current.code, "UND_ERR_CONNECT_TIMEOUT"); + assert.equal(current.hint, UPSTREAM_FAIL_HINTS["connect-timeout"]); + assert.equal(current.skippedProxy, undefined); + assert.equal(current.skippedSource, undefined); + assert.ok(!JSON.stringify(current).includes("7890")); + } finally { + _resetUpstreamAlertsForTest(); + } + }); +} + +test("recordUpstreamAlert: a later skipped proxy replaces the old redacted endpoint and source", () => { + _resetUpstreamAlertsForTest(); + try { + const error = netError("UND_ERR_CONNECT_TIMEOUT", "connect timed out"); + recordUpstreamAlert(TARGET, error, false, { proxy: ENV_PROXY, source: "HTTPS_PROXY" }); + recordUpstreamAlert(TARGET, error, false, { proxy: "http://new-user:new-password@127.0.0.1:7897", source: "windows-system" }); + const [current] = getUpstreamAlerts(); + assert.equal(current.count, 2); + assert.equal(current.skippedProxy, "http://***:***@127.0.0.1:7897"); + assert.equal(current.skippedSource, "windows-system"); + assert.ok(!JSON.stringify(current).includes("new-password")); + assert.ok(!JSON.stringify(current).includes("7890")); + assert.ok(current.hint.includes(current.skippedProxy)); + } finally { + _resetUpstreamAlertsForTest(); + } +}); + test("loadOptions: defaultDirect tracks UNSET mode only (model path)", () => { // unset mode + available env proxy → the full #2809 scenario end-to-end withSandboxEnv(() => {}, () => { diff --git a/tests/upstream-alerts.test.ts b/tests/upstream-alerts.test.ts index fd261a051a..07516732c6 100644 --- a/tests/upstream-alerts.test.ts +++ b/tests/upstream-alerts.test.ts @@ -612,3 +612,29 @@ test("#1682: banner renders active alerts, dismisses per instance, hides on reco _resetUpstreamAlertsForTest(); } }); + +test("banner drops the skipped-proxy advice when the latest failure did not skip a proxy", async () => { + _resetUpstreamAlertsForTest(); + const url = "https://relay.example.com/v1/messages"; + const error = netError("UND_ERR_CONNECT_TIMEOUT"); + const deadPort = await freePort(); + const { proxy, proxyPort } = await startProxyFor(deadPort, false); + const base = `http://127.0.0.1:${proxyPort}`; + try { + recordUpstreamAlert(url, error, false, { proxy: "http://127.0.0.1:7890", source: "HTTPS_PROXY" }); + const first = await runWebClient(base, "#/overview", new Map()); + await waitFor(() => first.els.get("alerts-banner")?.hidden === false, "initial skipped-proxy banner"); + assert.match(first.els.get("alerts-banner")!.children[1].children[0].textContent, /127\.0\.0\.1:7890/); + + recordUpstreamAlert(url, error, true); + const current = await runWebClient(base, "#/overview", new Map()); + await waitFor(() => current.els.get("alerts-banner")?.hidden === false, "current failure banner"); + const message = current.els.get("alerts-banner")!.children[1].children[0].textContent; + assert.match(message, /relay\.example\.com \(connect-timeout ×2/); + assert.ok(!message.includes("127.0.0.1:7890")); + assert.ok(!message.includes("HTTPS_PROXY")); + } finally { + await close(proxy); + _resetUpstreamAlertsForTest(); + } +});