|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { formatHttpError } from "../lib/util"; |
| 4 | + |
| 5 | +describe("formatHttpError", () => { |
| 6 | + test("appends auth remediation hint on 401 with JSON body", () => { |
| 7 | + const msg = formatHttpError( |
| 8 | + "Failed to fetch issues", |
| 9 | + 401, |
| 10 | + '{"hint": "Please check validity of token", "details": "Invalid token"}' |
| 11 | + ); |
| 12 | + |
| 13 | + expect(msg).toContain("HTTP 401"); |
| 14 | + expect(msg).toContain("Run 'postgresai auth'"); |
| 15 | + expect(msg).toContain("PGAI_API_KEY"); |
| 16 | + }); |
| 17 | + |
| 18 | + test("appends auth remediation hint on 401 with HTML body (early-return path)", () => { |
| 19 | + const msg = formatHttpError("Failed to fetch issues", 401, "<html><body>Unauthorized</body></html>"); |
| 20 | + |
| 21 | + expect(msg).toContain("HTTP 401"); |
| 22 | + expect(msg).toContain("Run 'postgresai auth'"); |
| 23 | + }); |
| 24 | + |
| 25 | + test("appends auth remediation hint on 401 without body", () => { |
| 26 | + const msg = formatHttpError("Failed to fetch issues", 401); |
| 27 | + |
| 28 | + expect(msg).toContain("Run 'postgresai auth'"); |
| 29 | + }); |
| 30 | + |
| 31 | + test("does not append auth hint for non-401 statuses", () => { |
| 32 | + expect(formatHttpError("Failed to fetch issues", 403, '{"message": "denied"}')).not.toContain( |
| 33 | + "Run 'postgresai auth'" |
| 34 | + ); |
| 35 | + expect(formatHttpError("Failed to fetch issues", 500)).not.toContain("Run 'postgresai auth'"); |
| 36 | + }); |
| 37 | + |
| 38 | + test("keeps structured JSON error details before the hint", () => { |
| 39 | + const msg = formatHttpError("Failed to fetch issues", 401, '{"message": "Invalid token"}'); |
| 40 | + |
| 41 | + expect(msg.indexOf("Invalid token")).toBeGreaterThan(-1); |
| 42 | + expect(msg.indexOf("Invalid token")).toBeLessThan(msg.indexOf("Run 'postgresai auth'")); |
| 43 | + }); |
| 44 | +}); |
0 commit comments