Skip to content
pi-c Artifact [99667719f7]
Login

Artifact [99667719f7]

Artifact 99667719f71b81c19ec770e5278fe1bd8237a97b0450bec106d886d43a1c7739:


/* The bundled reading of refusals: each class from a status, a vendor's
 * error body or its rate-limit headers, and the words for each state. */
#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "keyhealth.h"

#define NOW 1795000000000LL /* 2026-11-18, any fixed instant */

static pic_key_state classify(const char *provider, int status, const char *body, const char *ratelimit,
                              const char *retry_after, pic_key_health *h) {
    pic_keyhealth_classify(provider, status, body, "", ratelimit, retry_after, NOW, h);
    return h->state;
}

static void test_classes(void) {
    pic_key_health h;
    /* invalid */
    assert(classify("anthropic", 401,
                    "{\"type\":\"error\",\"error\":{\"type\":\"authentication_error\",\"message\":\"invalid x-api-key\"}}", "",
                    "", &h) == PIC_KEY_INVALID);
    assert(classify("openai", 403, "{}", "", "", &h) == PIC_KEY_INVALID);
    /* out of credit: Anthropic's billing error as 400 or 402, OpenAI's quota as a 429, OpenRouter's 402 */
    assert(classify("anthropic", 400,
                    "{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"Your credit balance is"
                    " too low to access the Anthropic API.\"}}",
                    "", "", &h) == PIC_KEY_CREDIT);
    assert(classify("anthropic", 402, "{\"type\":\"error\",\"error\":{\"type\":\"billing_error\",\"message\":\"x\"}}", "", "",
                    &h) == PIC_KEY_CREDIT);
    assert(classify("openai", 429,
                    "{\"error\":{\"message\":\"You exceeded your current quota\",\"type\":\"insufficient_quota\","
                    "\"code\":\"insufficient_quota\"}}",
                    "", "0", &h) == PIC_KEY_CREDIT);
    assert(classify("openrouter", 402, "{\"error\":{\"message\":\"Key limit exceeded\",\"metadata\":{\"limit_source\":\"key\"}}}",
                    "", "", &h) == PIC_KEY_CREDIT);
    /* limited until a time: Retry-After, Anthropic's reset times, the x-ratelimit durations */
    assert(classify("anthropic", 429, "{}", "", "900", &h) == PIC_KEY_LIMITED && h.until_ms == NOW + 900000);
    /* NOW is 11:06:40 UTC: the later reset counts */
    assert(classify("anthropic", 429, "{}",
                    "anthropic-ratelimit-requests-reset: 2026-11-18T11:07:00Z\n"
                    "anthropic-ratelimit-tokens-reset: 2026-11-18T11:26:40Z\n",
                    "", &h) == PIC_KEY_LIMITED &&
           h.until_ms == NOW + 20 * 60000LL);
    assert(classify("anthropic", 429, "{}", "anthropic-ratelimit-requests-reset: 2026-11-18T11:07:00.5Z\n", "", &h) ==
           PIC_KEY_TRANSIENT);
    assert(classify("openai", 429, "{}", "x-ratelimit-reset-requests: 6m0s\nx-ratelimit-reset-tokens: 1s\n", "", &h) ==
               PIC_KEY_LIMITED &&
           h.until_ms == NOW + 360000);
    /* transient: a short 429 (today's retries), 5xx, Anthropic's 529 */
    assert(classify("anthropic", 429, "{}", "", "0", &h) == PIC_KEY_TRANSIENT);
    assert(classify("openai", 429, "{}", "x-ratelimit-reset-requests: 20ms\n", "", &h) == PIC_KEY_TRANSIENT);
    assert(classify("anthropic", 529, "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\"}}", "", "", &h) ==
           PIC_KEY_TRANSIENT);
    assert(classify("openai", 503, "", "", "", &h) == PIC_KEY_TRANSIENT);
    /* other */
    assert(classify("anthropic", 400, "{\"error\":{\"type\":\"invalid_request_error\",\"message\":\"bad\"}}", "", "", &h) ==
           PIC_KEY_OTHER);
    puts("ok each class from a status, a body or the rate-limit headers");
}

static void test_words(void) {
    pic_key_health h;
    char out[400];
    memset(&h, 0, sizeof h);
    h.state = PIC_KEY_CREDIT;
    h.status = 402;
    pic_keyhealth_headline("anthropic", "main", &h, NOW, out, sizeof out);
    assert(strcmp(out, "The key \"main\" is out of credit. Nothing was lost.") == 0);
    pic_keyhealth_message("anthropic", "main", &h, NOW, out, sizeof out);
    assert(strcmp(out, "The key \"main\" for anthropic is out of credit (HTTP 402).") == 0);
    h.state = PIC_KEY_INVALID;
    h.status = 401;
    pic_keyhealth_headline("anthropic", "main", &h, NOW, out, sizeof out);
    assert(strcmp(out, "anthropic refused the key \"main\" (HTTP 401).") == 0);
    h.state = PIC_KEY_LIMITED;
    h.status = 429;
    h.until_ms = NOW + 38 * 60000LL;
    pic_keyhealth_headline("anthropic", "main", &h, NOW, out, sizeof out);
    assert(strncmp(out, "Limited until ", 14) == 0 && strstr(out, " (38 min).") != NULL);
    assert(strcmp(pic_keyhealth_offers(PIC_KEY_CREDIT, 1), "sm") == 0);
    assert(strcmp(pic_keyhealth_offers(PIC_KEY_CREDIT, 0), "m") == 0);
    assert(strcmp(pic_keyhealth_offers(PIC_KEY_INVALID, 1), "sl") == 0);
    assert(strcmp(pic_keyhealth_offers(PIC_KEY_LIMITED, 1), "wsm") == 0);
    pic_keyhealth_alert(&h, NOW, out, sizeof out);
    assert(strncmp(out, "! limited until ", 16) == 0);
    assert(pic_keyhealth_bad(&h, NOW) && !pic_keyhealth_bad(&h, NOW + 39 * 60000LL));
    h.state = PIC_KEY_OK;
    h.checked_ms = NOW - 120000;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "ok, checked 2 min ago") == 0);
    pic_keyhealth_alert(&h, NOW, out, sizeof out);
    assert(out[0] == '\0');
    h.state = PIC_KEY_CREDIT;
    h.status = 402;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strncmp(out, "out of credit since ", 20) == 0 && strstr(out, "(HTTP 402)"));
    puts("ok the words: headlines, messages, offers, phrases and the footer's warning");
}

int main(void) {
    test_classes();
    test_words();
    return 0;
}