import test from "node:test";
import assert from "node:assert/strict";
import { classifyGraphError } from "../lib/graph-client.js";
test("classifyGraphError 401 → auth", () => {
    const cls = classifyGraphError({
        statusCode: 401,
        code: "InvalidAuthenticationToken",
        body: '{"error":{"code":"InvalidAuthenticationToken"}}',
    });
    assert.equal(cls.kind, "auth");
    assert.equal(cls.statusCode, 401);
});
test("classifyGraphError 429 with Retry-After → rate-limit-recoverable", () => {
    const cls = classifyGraphError({
        statusCode: 429,
        headers: { "Retry-After": "30" },
    });
    assert.equal(cls.kind, "rate-limit-recoverable");
    if (cls.kind === "rate-limit-recoverable") {
        assert.equal(cls.retryAfterMs, 30000);
    }
});
test("classifyGraphError 429 lowercase Retry-After header still parsed", () => {
    const cls = classifyGraphError({
        statusCode: 429,
        headers: { "retry-after": "5" },
    });
    assert.equal(cls.kind, "rate-limit-recoverable");
    if (cls.kind === "rate-limit-recoverable") {
        assert.equal(cls.retryAfterMs, 5000);
    }
});
test("classifyGraphError 429 without Retry-After → rate-limit-terminal", () => {
    const cls = classifyGraphError({
        statusCode: 429,
        headers: {},
    });
    assert.equal(cls.kind, "rate-limit-terminal");
});
test("classifyGraphError 503 + MailboxNotEnabledForRESTAPI → on-prem", () => {
    const cls = classifyGraphError({
        statusCode: 503,
        code: "MailboxNotEnabledForRESTAPI",
        body: '{"error":{"code":"MailboxNotEnabledForRESTAPI","message":"REST API is not yet supported for this mailbox."}}',
        headers: { "x-mailbox-server": "ex2019.acme.local" },
    });
    assert.equal(cls.kind, "on-prem");
    if (cls.kind === "on-prem") {
        assert.equal(cls.statusCode, 503);
        assert.equal(cls.mailServer, "ex2019.acme.local");
    }
});
test("classifyGraphError 503 without MailboxNotEnabledForRESTAPI → 5xx (NOT on-prem)", () => {
    // Critical: on-prem detection keys off the structured code field, not body
    // prose. A generic 503 must NOT be misclassified as on-prem.
    const cls = classifyGraphError({
        statusCode: 503,
        code: "ServiceUnavailable",
        body: '{"error":{"code":"ServiceUnavailable","message":"REST API on-premises mailboxes are temporarily unavailable for everyone, somehow"}}',
    });
    assert.equal(cls.kind, "5xx", "must not infer on-prem from prose body");
});
test("classifyGraphError parses code from body when not on top-level", () => {
    const cls = classifyGraphError({
        statusCode: 503,
        body: '{"error":{"code":"MailboxNotEnabledForRESTAPI","message":"foo"}}',
    });
    assert.equal(cls.kind, "on-prem");
});
test("classifyGraphError 500 → 5xx", () => {
    const cls = classifyGraphError({ statusCode: 500, code: "InternalServerError" });
    assert.equal(cls.kind, "5xx");
});
test("classifyGraphError 404 → other", () => {
    const cls = classifyGraphError({ statusCode: 404, code: "ItemNotFound" });
    assert.equal(cls.kind, "other");
    if (cls.kind === "other") {
        assert.equal(cls.statusCode, 404);
        assert.equal(cls.code, "ItemNotFound");
    }
});
test("classifyGraphError reads Retry-After via fetch-style response.headers.get", () => {
    const cls = classifyGraphError({
        statusCode: 429,
        response: {
            headers: { get: (h) => (h.toLowerCase() === "retry-after" ? "10" : null) },
        },
    });
    assert.equal(cls.kind, "rate-limit-recoverable");
    if (cls.kind === "rate-limit-recoverable") {
        assert.equal(cls.retryAfterMs, 10000);
    }
});
//# sourceMappingURL=graph-client.test.js.map