Skip to content
pi-c Artifact [f773329501]
Login

Artifact [f773329501]

Artifact f7733295015a9c60e3822d0995d02cd2a0a2f857a9123a1b5fe5e9bcc6734e69:


/* 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);
    /* Anthropic's spend limit, a 400 that names when access comes back (seen 2026-11-24) */
    assert(classify("anthropic", 400,
                    "{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"You have reached"
                    " your specified API usage limits. You will regain access on 2026-12-01 at 00:00 UTC.\"}}",
                    "", "", &h) == PIC_KEY_LIMITED &&
           h.until_ms == 1796083200000LL);
    /* the same words with no time to wait for: waiting does not help */
    assert(classify("anthropic", 400,
                    "{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"You have reached"
                    " your specified API usage limits.\"}}",
                    "", "", &h) == PIC_KEY_CREDIT);
    /* OpenAI's spent prepaid credit, a 429 with a short Retry-After (seen 2026-11-24) */
    assert(classify("openai", 429,
                    "{\"error\":{\"message\":\"You have no credits remaining. Add credits to continue.\",\"type\":"
                    "\"invalid_request_error\",\"param\":null,\"code\":\"credit_balance_exhausted\"}}",
                    "x-ratelimit-reset-requests: 20ms\n", "1", &h) == PIC_KEY_CREDIT);
    /* 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), "slm") == 0);
    assert(strcmp(pic_keyhealth_offers(PIC_KEY_INVALID, 0), "lm") == 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, "valid, checked 2 min ago") == 0);
    h.has_remaining = 1;
    h.remaining = 4.123;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "$4.12 left") == 0);
    snprintf(h.currency, sizeof h.currency, "\xc2\xa5");
    h.remaining = 110;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "\xc2\xa5" "110.00 left") == 0);
    snprintf(h.currency, sizeof h.currency, "EUR");
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "EUR 110.00 left") == 0);
    h.has_remaining = 0;
    h.currency[0] = '\0';
    h.state = PIC_KEY_OTHER;
    h.status = 404;
    snprintf(h.reason, sizeof h.reason, "no answer in time");
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "check failed (HTTP 404)") == 0);
    h.status = 0;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "check failed: no answer in time") == 0);
    h.state = PIC_KEY_OK;
    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)"));
    /* a free check said valid after a request failed: both facts */
    h.checked_ms = NOW - 3600000;
    h.valid_ms = NOW - 60000;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "valid \xc2\xb7 last request failed 10:06 (out of credit)") == 0);
    h.state = PIC_KEY_LIMITED;
    h.status = 400;
    h.until_ms = 1796083200000LL;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "valid \xc2\xb7 last request failed 10:06 (usage limit until Dec 1)") == 0);
    h.until_ms = NOW + 38 * 60000LL;
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "valid \xc2\xb7 last request failed 10:06 (limited until 11:44)") == 0);
    h.valid_ms = 0; /* no check since: as before */
    pic_keyhealth_phrase(&h, NOW, out, sizeof out);
    assert(strcmp(out, "limited until 11:44 (38 min)") == 0);
    puts("ok the words: headlines, messages, offers, phrases and the footer's warning");
}

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