|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { describeUpstreamConnectFailure } from "../src/server/responses/upstream-error"; |
| 3 | + |
| 4 | +function tlsAltnameError(url: string): Error { |
| 5 | + // Shape of what Bun's fetch actually rejects with on a certificate/hostname mismatch. |
| 6 | + return Object.assign(new Error(`ERR_TLS_CERT_ALTNAME_INVALID fetching "${url}"`), { |
| 7 | + code: "ERR_TLS_CERT_ALTNAME_INVALID", |
| 8 | + }); |
| 9 | +} |
| 10 | + |
| 11 | +describe("describeUpstreamConnectFailure", () => { |
| 12 | + test("a TLS altname mismatch names the host, the likely cause, and the check", () => { |
| 13 | + const msg = describeUpstreamConnectFailure( |
| 14 | + tlsAltnameError("https://api.individual.githubcopilot.com/chat/completions"), |
| 15 | + 30000, |
| 16 | + ); |
| 17 | + expect(msg).toContain("api.individual.githubcopilot.com"); |
| 18 | + expect(msg).toContain("TLS interception"); |
| 19 | + expect(msg).toContain("openssl s_client"); |
| 20 | + expect(msg).toContain("-servername api.individual.githubcopilot.com"); |
| 21 | + // The generic wording is what sent issue #553 hunting for an adapter URL bug. |
| 22 | + expect(msg).not.toContain("Provider unreachable"); |
| 23 | + }); |
| 24 | + |
| 25 | + test("the branch also fires when only the message carries the code", () => { |
| 26 | + const msg = describeUpstreamConnectFailure(new Error("ERR_TLS_CERT_ALTNAME_INVALID"), 30000); |
| 27 | + expect(msg).toContain("openssl s_client"); |
| 28 | + // No URL in the detail, so the message degrades to a placeholder rather than guessing. |
| 29 | + expect(msg).toContain("the provider host"); |
| 30 | + expect(msg).toContain("<host>"); |
| 31 | + }); |
| 32 | + |
| 33 | + test("text that merely quotes the code back does not take the TLS branch", () => { |
| 34 | + // Only transport failures reach these call sites, so this is defensive — but the message |
| 35 | + // fallback is anchored to the head so echoed text cannot produce wrong advice. |
| 36 | + const msg = describeUpstreamConnectFailure( |
| 37 | + new Error('upstream said: ERR_TLS_CERT_ALTNAME_INVALID somewhere'), |
| 38 | + 30000, |
| 39 | + ); |
| 40 | + expect(msg).toBe("Provider unreachable: upstream said: ERR_TLS_CERT_ALTNAME_INVALID somewhere"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("an ordinary connection failure keeps the existing wording", () => { |
| 44 | + expect(describeUpstreamConnectFailure(new Error("ECONNREFUSED"), 30000)) |
| 45 | + .toBe("Provider unreachable: ECONNREFUSED"); |
| 46 | + expect(describeUpstreamConnectFailure(new Error("ENOTFOUND api.example.com"), 30000)) |
| 47 | + .toBe("Provider unreachable: ENOTFOUND api.example.com"); |
| 48 | + }); |
| 49 | + |
| 50 | + test("a timeout keeps its own message", () => { |
| 51 | + const err = Object.assign(new Error("The operation timed out."), { name: "TimeoutError" }); |
| 52 | + expect(describeUpstreamConnectFailure(err, 12345)) |
| 53 | + .toBe("Provider connect timeout after 12345ms"); |
| 54 | + }); |
| 55 | + |
| 56 | + test("a non-Error rejection still produces the generic message", () => { |
| 57 | + expect(describeUpstreamConnectFailure("socket hang up", 30000)) |
| 58 | + .toBe("Provider unreachable: socket hang up"); |
| 59 | + }); |
| 60 | + |
| 61 | + test("URL userinfo is redacted on both branches", () => { |
| 62 | + // A provider base URL can carry credentials as userinfo, and the runtime error echoes |
| 63 | + // the URL it was fetching. Neither message may hand that back to the caller. |
| 64 | + // The secret is assembled at runtime so the privacy scanner does not read the literal |
| 65 | + // `user:token@host` form here as a real address. |
| 66 | + const secret = ["sk", "fixture", "token"].join("-"); |
| 67 | + const withCreds = `fetching "https://user:${secret}@api.example.com/v1"`; |
| 68 | + const tls = describeUpstreamConnectFailure( |
| 69 | + Object.assign(new Error(`ERR_TLS_CERT_ALTNAME_INVALID ${withCreds}`), { |
| 70 | + code: "ERR_TLS_CERT_ALTNAME_INVALID", |
| 71 | + }), |
| 72 | + 30000, |
| 73 | + ); |
| 74 | + expect(tls).not.toContain(secret); |
| 75 | + expect(tls).toContain("<redacted>@api.example.com"); |
| 76 | + expect(tls).toContain("does not match api.example.com"); |
| 77 | + |
| 78 | + const generic = describeUpstreamConnectFailure(new Error(`ECONNREFUSED ${withCreds}`), 30000); |
| 79 | + expect(generic).not.toContain(secret); |
| 80 | + expect(generic).toContain("<redacted>@api.example.com"); |
| 81 | + }); |
| 82 | + |
| 83 | + test("an IPv6 literal host is extracted in bracket form", () => { |
| 84 | + const msg = describeUpstreamConnectFailure( |
| 85 | + Object.assign(new Error('ERR_TLS_CERT_ALTNAME_INVALID fetching "https://[2001:db8::1]:8443/v1"'), { |
| 86 | + code: "ERR_TLS_CERT_ALTNAME_INVALID", |
| 87 | + }), |
| 88 | + 30000, |
| 89 | + ); |
| 90 | + expect(msg).toContain("[2001:db8::1]"); |
| 91 | + expect(msg).toContain("-servername [2001:db8::1]"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments