Skip to content
pi-c Documentation
Login

Documentation

/* Model registry tests: built-in providers, provider/model resolution, Pi's
 * compat detection, and a user models file (via PIC_MODELS_FILE). */
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "models.h"

static pic_model resolve(const char *name) {
    pic_model m;
    char err[256];
    if (pic_model_resolve(name, &m, err, sizeof err) != 0) {
        fprintf(stderr, "resolve %s: %s\n", name, err);
        assert(0);
    }
    return m;
}

static void refused(const char *name, const char *want) {
    pic_model m;
    char err[256] = "";
    assert(pic_model_resolve(name, &m, err, sizeof err) != 0);
    if (!strstr(err, want)) {
        fprintf(stderr, "resolve %s: error [%s] lacks [%s]\n", name, err, want);
        assert(0);
    }
}

static void write_file(const char *path, const char *text) {
    FILE *f = fopen(path, "w");
    assert(f && fputs(text, f) >= 0 && fclose(f) == 0);
}

int main(void) {
    char path[512], url[512];
    pic_model m;
    const char *tmp = getenv("TMPDIR");
    (void)snprintf(path, sizeof path, "%s/pic-models-test-%ld.json", tmp && *tmp ? tmp : "/tmp", (long)getpid());
    setenv("PIC_MODELS_FILE", "/nonexistent/models.json", 1); /* no user file */

    /* Built-ins and naming. */
    m = resolve("anthropic/claude-opus-5-5");
    assert(m.api == PIC_API_ANTHROPIC && m.context_window == 1000000 && m.reasoning);
    assert(strcmp(m.key_env, "ANTHROPIC_API_KEY") == 0);
    pic_model_url(&m, url, sizeof url);
    assert(strcmp(url, "https://api.anthropic.com/v1/messages") == 0);
    m = resolve("claude-haiku-4-5"); /* a bare Claude id means Anthropic */
    assert(strcmp(m.provider, "anthropic") == 0 && m.context_window == 200000);
    m = resolve("openai/gpt-5.1-codex");
    assert(m.api == PIC_API_RESPONSES && strcmp(m.key_env, "OPENAI_API_KEY") == 0);
    assert(m.context_window == 128000 && m.max_tokens == 16384); /* Pi's defaults */
    pic_model_url(&m, url, sizeof url);
    assert(strcmp(url, "https://api.openai.com/v1/responses") == 0);
    m = resolve("google/gemini-2.5-flash");
    assert(m.api == PIC_API_GEMINI && strcmp(m.key_env, "GEMINI_API_KEY") == 0 && m.reasoning);
    assert(strcmp(pic_api_name(m.api), "google-generative-ai") == 0);
    pic_model_url(&m, url, sizeof url);
    assert(strcmp(url, "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse") == 0);
    m = resolve("openrouter/qwen/qwen3-coder"); /* model ids keep their own slashes */
    assert(m.api == PIC_API_CHAT && strcmp(m.id, "qwen/qwen3-coder") == 0);
    pic_model_url(&m, url, sizeof url);
    assert(strcmp(url, "https://openrouter.ai/api/v1/chat/completions") == 0);

    /* Pi's compat detection. */
    assert(strcmp(m.compat.thinking_format, "openrouter") == 0 && !m.compat.supports_developer_role);
    assert(!m.compat.anthropic_cache_control);
    m = resolve("openrouter/anthropic/claude-opus-5-5");
    assert(m.compat.anthropic_cache_control && m.compat.supports_developer_role);
    m = resolve("deepseek/deepseek-reasoner");
    assert(m.compat.requires_reasoning_content && m.compat.max_tokens_field_legacy && !m.compat.supports_store);
    assert(strcmp(m.compat.thinking_format, "deepseek") == 0);
    m = resolve("groq/llama-4");
    assert(m.compat.supports_store && !m.compat.max_tokens_field_legacy);

    refused("gpt-5", "provider/model");
    refused("nobody/model", "provider/model");
    refused("openai/", "no model");

    /* A user file: a local server, a model override and a provider override. */
    write_file(path,
               "{\"providers\":{"
               "\"local\":{\"baseUrl\":\"http://127.0.0.1:11434/v1/\",\"api\":\"openai-completions\","
               "\"apiKey\":\"$LOCAL_KEY\",\"headers\":{\"X-Team\":\"pic\"},\"compat\":{\"maxTokensField\":\"max_tokens\"},"
               "\"models\":[{\"id\":\"qwen3:32b\",\"reasoning\":true,\"contextWindow\":40960,\"maxTokens\":8192,"
               "\"compat\":{\"supportsStore\":false}}]},"
               "\"openai\":{\"baseUrl\":\"https://proxy.example/v1\","
               "\"modelOverrides\":{\"gpt-5\":{\"contextWindow\":400000}}},"
               "\"bad\":{\"baseUrl\":\"http://x\",\"api\":\"bedrock-converse-stream\"},"
               "\"cmd\":{\"baseUrl\":\"http://x\",\"api\":\"openai-completions\",\"apiKey\":\"!pass show key\"}"
               "}}");
    setenv("PIC_MODELS_FILE", path, 1);
    m = resolve("local/qwen3:32b");
    assert(m.api == PIC_API_CHAT && strcmp(m.base_url, "http://127.0.0.1:11434/v1") == 0);
    assert(strcmp(m.key_env, "LOCAL_KEY") == 0 && m.key_literal[0] == '\0');
    assert(m.reasoning && m.context_window == 40960 && m.max_tokens == 8192);
    assert(m.compat.max_tokens_field_legacy && !m.compat.supports_store);
    assert(strcmp(m.headers, "X-Team: pic") == 0);
    m = resolve("local/other"); /* unknown id on a user provider: provider defaults */
    assert(m.context_window == 128000 && m.compat.supports_store);
    m = resolve("openai/gpt-5");
    assert(strcmp(m.base_url, "https://proxy.example/v1") == 0 && m.context_window == 400000);
    assert(m.api == PIC_API_RESPONSES && strcmp(m.key_env, "OPENAI_API_KEY") == 0);
    refused("bad/x", "not supported");
    refused("cmd/x", "not supported");
    write_file(path, "{not json");
    refused("local/x", "not valid JSON");
    remove(path);
    /* Pi's getSupportedThinkingLevels and clampThinkingLevel. */
    {
        const char *lv[PIC_THINKING_LEVELS];
        setenv("PIC_MODELS_FILE", "/nonexistent/models.json", 1);
        m = resolve("anthropic/claude-fable-5-2"); /* off null, xhigh and max mapped */
        assert(m.compat.force_adaptive_thinking && pic_model_thinking_levels(&m, lv) == 6);
        assert(strcmp(lv[0], "minimal") == 0 && strcmp(lv[5], "max") == 0);
        assert(strcmp(pic_model_thinking_clamp(&m, "off"), "minimal") == 0);
        assert(strcmp(pic_model_thinking_clamp(&m, "medium"), "medium") == 0);
        m = resolve("anthropic/claude-haiku-4-5"); /* no map: no xhigh or max */
        assert(!m.compat.force_adaptive_thinking && pic_model_thinking_levels(&m, lv) == 5);
        assert(strcmp(pic_model_thinking_clamp(&m, "max"), "high") == 0);
        m = resolve("deepseek/deepseek-v4-pro"); /* minimal..medium null: medium goes up to high */
        assert(strcmp(pic_model_thinking_clamp(&m, "medium"), "high") == 0);
        assert(strcmp(pic_model_thinking_clamp(&m, "xhigh"), "max") == 0);
        m = resolve("openai/not-a-reasoner");
        assert(pic_model_thinking_levels(&m, lv) == 1 && strcmp(pic_model_thinking_clamp(&m, "high"), "off") == 0);
    }
    /* Pi's calculateCost: catalog prices, request-wide tiers, 1h cache writes. */
    {
        pic_cost c;
        pic_tokens t = {0, 0, 0, 0, 0, 0};
        char *priced;
        setenv("PIC_MODELS_FILE", "/nonexistent/models.json", 1);
        m = resolve("anthropic/claude-opus-4-8"); /* input 5, output 25, cacheRead 0.5, cacheWrite 6.25 per Mtok */
        assert(m.price.base.input == 5 && m.price.base.output == 25 && m.price.base.cache_read == 0.5 &&
               m.price.base.cache_write == 6.25);
        /* Pi's anthropic-cache-write-1h-cost test: 100 input, 1M cache writes, 5 output. */
        t.input = 100;
        t.output = 5;
        t.cache_write = 1000000;
        pic_cost_calc(&m.price, &t, &c);
        assert(fabs(c.cache_write - 6.25) < 1e-9 && fabs(c.input - 0.0005) < 1e-12 && fabs(c.output - 0.000125) < 1e-12);
        t.cache_write_1h = 1000000; /* 2x input: 10 */
        pic_cost_calc(&m.price, &t, &c);
        assert(fabs(c.cache_write - 10) < 1e-9 && fabs(c.total - (c.input + c.output + c.cache_read + c.cache_write)) < 1e-12);
        /* Tiers: the highest threshold the request's input exceeds replaces every rate. */
        write_file(path, "{\"providers\":{\"local\":{\"baseUrl\":\"http://x\",\"api\":\"openai-completions\",\"apiKey\":\"k\","
                         "\"models\":[{\"id\":\"t\",\"cost\":{\"input\":1,\"output\":2,\"cacheRead\":0.1,\"cacheWrite\":0.5,"
                         "\"tiers\":[{\"inputTokensAbove\":200000,\"input\":4,\"output\":18,\"cacheRead\":0.4,\"cacheWrite\":2},"
                         "{\"inputTokensAbove\":100000,\"input\":2,\"output\":9,\"cacheRead\":0.2,\"cacheWrite\":1}]}}]}}}");
        setenv("PIC_MODELS_FILE", path, 1);
        m = resolve("local/t");
        assert(m.price.ntiers == 2);
        memset(&t, 0, sizeof t);
        t.input = 100000; /* not above 100000: base */
        t.output = 1000000;
        pic_cost_calc(&m.price, &t, &c);
        assert(fabs(c.output - 2) < 1e-9 && fabs(c.input - 0.1) < 1e-9);
        t.input = 50000;
        t.cache_read = 60000; /* 110000 in all: the 100000 tier */
        pic_cost_calc(&m.price, &t, &c);
        assert(fabs(c.output - 9) < 1e-9 && fabs(c.input - 0.1) < 1e-9 && fabs(c.cache_read - 0.012) < 1e-9);
        t.input = 250000; /* the 200000 tier, though 100000 matches too */
        t.cache_read = 0;
        pic_cost_calc(&m.price, &t, &c);
        assert(fabs(c.output - 18) < 1e-9 && fabs(c.input - 1) < 1e-9);
        /* Raw usage by wire API: openai-completions input excludes cached tokens. */
        write_file(path, "{\"providers\":{\"local\":{\"baseUrl\":\"http://x\",\"api\":\"openai-completions\",\"apiKey\":\"k\","
                         "\"models\":[{\"id\":\"c\",\"cost\":{\"input\":1,\"output\":2,\"cacheRead\":0.5,\"cacheWrite\":0}}]}}}");
        m = resolve("local/c");
        assert(pic_usage_cost(&m, "{\"prompt_tokens\":1000000,\"completion_tokens\":500000,"
                                  "\"prompt_tokens_details\":{\"cached_tokens\":400000}}", &c, NULL) == 0);
        {
            pic_tokens k;
            pic_cost c2;
            assert(pic_usage_cost(&m, "{\"prompt_tokens\":1000000,\"completion_tokens\":500000,"
                                      "\"prompt_tokens_details\":{\"cached_tokens\":400000,\"cache_write_tokens\":100000}}", &c2, &k) == 0);
            assert(k.input == 500000 && k.cache_read == 400000 && k.cache_write == 100000 && k.output == 500000 &&
                   k.total == 1500000);
        }
        assert(fabs(c.input - 0.6) < 1e-9 && fabs(c.output - 1) < 1e-9 && fabs(c.cache_read - 0.2) < 1e-9 &&
               fabs(c.total - 1.8) < 1e-9);
        priced = pic_usage_priced(&m, "{\"prompt_tokens\":1000000,\"completion_tokens\":0}");
        assert(priced && strstr(priced, "\"pic_cost\":{\"input\":1,\"output\":0,\"cacheRead\":0,\"cacheWrite\":0,\"total\":1}"));
        assert(strstr(priced, "\"pic_tokens\":{\"input\":1000000,\"output\":0,\"cacheRead\":0,\"cacheWrite\":0,\"totalTokens\":1000000}"));
        free(priced);
        /* OpenRouter's billed cost replaces the total only when its accounting says so. */
        m = resolve("openrouter/some/model");
        m.price.base.input = 1;
        assert(pic_usage_cost(&m, "{\"prompt_tokens\":1000000,\"completion_tokens\":0,\"cost\":0.25,\"is_byok\":false}", &c, NULL) == 0);
        assert(c.total == 0.25 && c.total_from_provider && fabs(c.input - 1) < 1e-9);
        assert(pic_usage_cost(&m, "{\"prompt_tokens\":1000000,\"cost\":0.25,\"is_byok\":true,"
                                  "\"cost_details\":{\"upstream_inference_cost\":null,\"upstream_inference_prompt_cost\":0.5,"
                                  "\"upstream_inference_completions_cost\":0.25}}", &c, NULL) == 0);
        assert(fabs(c.total - 1) < 1e-9 && c.total_from_provider);
        assert(pic_usage_cost(&m, "{\"prompt_tokens\":1000000,\"cost\":-1,\"is_byok\":false}", &c, NULL) == 0);
        assert(!c.total_from_provider && fabs(c.total - 1) < 1e-9);
        remove(path);
    }
    puts("models: all cases pass");
    return 0;
}