Fix a2amcp login error reporting
All checks were successful
build / test (push) Successful in 20s
publish / npm (push) Successful in 22s

This commit is contained in:
2026-05-31 21:03:15 -03:00
parent 2814112ab3
commit f0b2d9ead8
5 changed files with 87 additions and 16 deletions

50
tests/api.test.ts Normal file
View File

@@ -0,0 +1,50 @@
import { test, beforeEach, afterEach } from "node:test";
import assert from "node:assert/strict";
import { ApiError, ControlPlaneClient } from "../src/api.js";
const originalFetch = globalThis.fetch;
beforeEach(() => {
globalThis.fetch = originalFetch;
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
test("ControlPlaneClient reports non-JSON error bodies without rereading them", async () => {
globalThis.fetch = (async () =>
new Response("<h1>upstream unavailable</h1>", {
status: 502,
statusText: "Bad Gateway",
headers: { "content-type": "text/html" },
})) as typeof fetch;
await assert.rejects(
() => new ControlPlaneClient("https://api.example.test", "token").me(),
(err) => {
assert.ok(err instanceof ApiError);
assert.equal(err.status, 502);
assert.match(err.message, /upstream unavailable/);
assert.doesNotMatch(err.message, /Body is unusable/);
return true;
},
);
});
test("ControlPlaneClient forwards bearer tokens and extracts JSON error detail", async () => {
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
const headers = init?.headers as Record<string, string>;
assert.equal(headers.authorization, "Bearer access-token");
return new Response(JSON.stringify({ detail: "keycloak email is not verified" }), {
status: 403,
headers: { "content-type": "application/json" },
});
}) as typeof fetch;
await assert.rejects(
() => new ControlPlaneClient("https://api.example.test", "access-token").me(),
/API 403: keycloak email is not verified/,
);
});