Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

24.3 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import {
  resolveModelRates, isFreeProvider, isFreeModelId, estimateCostUsd, estimateGrokImagineCost,
  estimateAgyImageCost, AGY_IMAGEN_IMAGE_MODEL,
  pricingAsOfForModel, PRICING_AS_OF, GROK_IMAGINE_IMAGE_MODEL, GROK_IMAGINE_VIDEO_MODEL,
} from './modelPricing.js';

describe('resolveModelRates', () => {
  it('matches exact model ids', () => {
    const r = resolveModelRates('claude-code', 'claude-opus-4-8');
    expect(r).toMatchObject({ rateModel: 'claude-opus-4-8', inputPer1M: 5, outputPer1M: 25, matched: 'exact' });
    const r5 = resolveModelRates('claude-code', 'claude-opus-5');
    expect(r5).toMatchObject({ rateModel: 'claude-opus-5', inputPer1M: 5, outputPer1M: 25, matched: 'exact' });
    const rh55 = resolveModelRates('claude-code', 'claude-haiku-5-5');
    expect(rh55).toMatchObject({ rateModel: 'claude-haiku-5-5', inputPer1M: 0.1, outputPer1M: 0.5, matched: 'exact' });
    const rg4 = resolveModelRates('antigravity-cli', 'gemini-4-argon-high');
    expect(rg4).toMatchObject({ rateModel: 'gemini-4-argon', inputPer1M: 2, outputPer1M: 10, matched: 'family' });
    const rs55 = resolveModelRates('claude-code', 'claude-sonnet-5-5');
    expect(rs55).toMatchObject({ rateModel: 'claude-sonnet-5-5', inputPer1M: 2, outputPer1M: 10, matched: 'exact' });
  });

  it('resolves CLI shorthand model names via family rules', () => {
    // Asserted by RATES, not by pointer id: the whole opus tier shares one rate
    // pair, so which listed opus id gets reported is a bump-to-bump detail the
    // suite should not have to be edited for. The pointer's validity is pinned
    // by the dedicated test below.
    expect(resolveModelRates('claude-code', 'opus')).toMatchObject({ inputPer1M: 5, outputPer1M: 25, matched: 'family' });
    expect(resolveModelRates('claude-code', 'sonnet')).toMatchObject({ rateModel: 'claude-sonnet-4-5', matched: 'family' });
    expect(resolveModelRates('claude-code', 'haiku')).toMatchObject({ rateModel: 'claude-haiku-4-5', matched: 'family' });
  });

  // #4163: the opus tier is one shared rate pair, and the `/opus/i` family rule
  // derives its pointer from the model list instead of naming an id by hand.
  // These three pin the properties that made the hand-maintained pointer safe,
  // so a future opus bump is a one-line prepend with no other edit anywhere.
  describe('opus tier', () => {
    const LISTED_OPUS_IDS = [
      'claude-opus-5', 'claude-opus-4-8', 'claude-opus-4-7', 'claude-opus-4-6', 'claude-opus-4-5',
    ];

    it('bills every listed opus generation at the same tier rates', () => {
      for (const id of LISTED_OPUS_IDS) {
        expect(resolveModelRates('claude-code', id)).toMatchObject({
          rateModel: id, inputPer1M: 5, outputPer1M: 25, matched: 'exact',
        });
      }
    });

    // The point of the dedup: an opus id the table has never heard of still
    // prices at $5/$25 without anyone re-pointing a family rule at it.
    it('prices an unlisted opus id at tier rates without a pointer bump', () => {
      for (const id of [
        'opus', 'claude-opus-9', 'claude-opus-6-2', 'global.anthropic.claude-opus-7-20270101-v1:0',
      ]) {
        expect(resolveModelRates('claude-code', id)).toMatchObject({
          inputPer1M: 5, outputPer1M: 25, matched: 'family',
        });
      }
    });

    // The family rule reports a LABEL, and downstream code leans on it: the UI
    // prints "Priced as <rateModel>", and usageReconciler compares two ids'
    // rateModel to decide whether they are the same family. So the pointer must
    // stay a non-null, exactly-resolvable key whose rates agree with the tier —
    // a null or synthetic label would silently break both.
    it('reports a family label that is itself an exact table id at the same rates', () => {
      const viaFamily = resolveModelRates('claude-code', 'opus');
      // Shape-matched rather than hardcoded to the current head id, so the next
      // opus bump doesn't have to edit this suite — that per-bump edit is what
      // the refactor removed. The exact-resolution check below is what actually
      // pins the pointer to a real table key.
      expect(viaFamily.rateModel).toMatch(/^claude-opus-\d/);
      expect(resolveModelRates('claude-code', viaFamily.rateModel)).toMatchObject({
        rateModel: viaFamily.rateModel,
        inputPer1M: viaFamily.inputPer1M,
        outputPer1M: viaFamily.outputPer1M,
        matched: 'exact',
      });
      // …and every opus id agrees on that label, which is what lets the
      // reconciler treat `opus` and `claude-opus-N` as one model.
      expect(resolveModelRates('claude-code', 'claude-opus-9').rateModel).toBe(viaFamily.rateModel);
    });
  });

  it('resolves fable/mythos to the Claude 5 flagship rates', () => {
    expect(resolveModelRates('claude-code', 'claude-fable-5')).toMatchObject({ inputPer1M: 10, outputPer1M: 50 });
    expect(resolveModelRates('claude-code', 'claude-fable-5-1')).toMatchObject({ inputPer1M: 10, outputPer1M: 50 });
    // Bare "fable" (no version) resolves to the newest generation, same convention as opus.
    expect(resolveModelRates('claude-code', 'fable')).toMatchObject({ rateModel: 'claude-fable-5-1', matched: 'family' });
  });

  it('prices Fable 5.1 cache reads at its own discounted rate, leaving Fable 5 unchanged', () => {
    const fable51 = resolveModelRates('claude-code', 'claude-fable-5-1');
    expect(fable51).toMatchObject({ cacheReadPer1M: 0.25, cacheWritePer1M: 12.5 });
    const fable5 = resolveModelRates('claude-code', 'claude-fable-5');
    expect(fable5).toMatchObject({ cacheReadPer1M: 1.0, cacheWritePer1M: 12.5 });
  });

  it('prices Opus 5.5 at its own launch rate, not the Opus tier rate', () => {
    expect(resolveModelRates('claude-code', 'claude-opus-5-5')).toMatchObject({
      matched: 'exact', inputPer1M: 4, outputPer1M: 20, cacheReadPer1M: 0.2, cacheWritePer1M: 5,
    });
    expect(resolveModelRates('claude-code', 'claude-opus-5')).toMatchObject({ inputPer1M: 5, outputPer1M: 25 });
  });

  it('prices Sonnet 5.5 cache reads at the rate halved on 2026-10-07', () => {
    expect(resolveModelRates('claude-code', 'claude-sonnet-5-5')).toMatchObject({
      matched: 'exact', inputPer1M: 2, outputPer1M: 10, cacheReadPer1M: 0.1, cacheWritePer1M: 2.5,
    });
    expect(resolveModelRates('claude-code', 'claude-sonnet-5')).toMatchObject({ cacheReadPer1M: 0.2 });
  });

  it('resolves Bedrock-prefixed ids through family rules', () => {
    const r = resolveModelRates('claude-code-bedrock', 'global.anthropic.claude-opus-4-8');
    expect(r).toMatchObject({ rateModel: 'claude-opus-4-8', matched: 'family' });
    const r5 = resolveModelRates('claude-code-bedrock', 'global.anthropic.claude-opus-5');
    expect(r5).toMatchObject({ rateModel: 'claude-opus-5', matched: 'family' });
  });

  it('resolves configured-default sentinels to their provider family', () => {
    expect(resolveModelRates('codex', 'codex-configured-default')).toMatchObject({ rateModel: 'gpt-5.3-codex', matched: 'family' });
    expect(resolveModelRates('grok', 'grok-configured-default')).toMatchObject({ rateModel: 'grok-4.7', matched: 'family' });
    expect(resolveModelRates('antigravity-cli', 'antigravity-configured-default')).toMatchObject({ rateModel: 'gemini-3.1-pro-preview', matched: 'family' });
  });

  it('prices GPT-6.1 Sol with its lower cached-input rate', () => {
    expect(resolveModelRates('codex', 'gpt-6.1-sol')).toMatchObject({
      rateModel: 'gpt-6.1-sol', matched: 'exact',
      inputPer1M: 2, outputPer1M: 10, cacheReadPer1M: 0.1, cacheWritePer1M: 2.5,
    });
    expect(resolveModelRates('codex', 'gpt-6-sol').cacheReadPer1M).toBe(0.2);
  });

  it('prices GPT-6 Astra at standard rates with derived cache tiers', () => {
    expect(resolveModelRates('codex', 'gpt-6-astra')).toMatchObject({
      rateModel: 'gpt-6-astra',
      inputPer1M: 10,
      outputPer1M: 50,
      cacheReadPer1M: 1.0,
      cacheWritePer1M: 12.5,
      matched: 'exact',
    });
    expect(resolveModelRates('codex', 'astra')).toMatchObject({
      rateModel: 'gpt-6-astra',
      inputPer1M: 10,
      outputPer1M: 50,
      matched: 'family',
    });
  });

  it('resolves suffixed gpt-5.6 ids to their base rates', () => {
    expect(resolveModelRates('codex', 'gpt-5.6-terra-2026-06-01')).toMatchObject({ rateModel: 'gpt-5.6-terra', matched: 'family' });
  });

  it('prices Cerebras gpt-oss-120b at open-weights rates, not OpenAI GPT rates', () => {
    expect(resolveModelRates('cerebras', 'gpt-oss-120b')).toMatchObject({
      rateModel: 'gpt-oss-120b (cerebras)', inputPer1M: 0.35, outputPer1M: 0.75,
    });
  });

  it('keeps other gpt-oss sizes on open-weights rates rather than the proprietary /gpt/ rule', () => {
    expect(resolveModelRates('cerebras', 'gpt-oss-20b')).toMatchObject({ rateModel: 'gpt-oss-120b (cerebras)', matched: 'family' });
  });

  // gpt-oss is open-weights: the id does not identify the host, and rates differ
  // per host — so no bare gpt-oss id may report `exact` (that would strip the
  // UI's `~` approximate marker and claim a published rate we don't have).
  it('never reports an exact match for an open-weights gpt-oss id on any host', () => {
    for (const providerId of ['cerebras', 'groq', 'openrouter', 'some-custom-host']) {
      expect(resolveModelRates(providerId, 'gpt-oss-120b').matched).not.toBe('exact');
    }
  });

  it('still resolves proprietary OpenAI ids through the gpt family rule', () => {
    expect(resolveModelRates('codex', 'gpt-4.1')).toMatchObject({ rateModel: 'gpt-5.4', matched: 'family' });
  });

  it('estimates an unrecognized Cerebras model at the Cerebras flagship rate', () => {
    expect(resolveModelRates('cerebras', 'zai-glm-4.7')).toMatchObject({ rateModel: 'gpt-oss-120b (cerebras)', matched: 'providerDefault' });
  });

  it('falls back to a provider default when the model is unknown', () => {
    const r = resolveModelRates('claude-code', 'my-custom-alias');
    expect(r).toMatchObject({ rateModel: 'claude-sonnet-4-5', matched: 'providerDefault' });
  });

  it('falls back to the generic blended rate when nothing matches', () => {
    const r = resolveModelRates('mystery-provider', 'mystery-model');
    expect(r).toMatchObject({ rateModel: null, inputPer1M: 3, outputPer1M: 15, matched: 'fallback' });
  });

  it('handles null/undefined inputs without throwing', () => {
    expect(resolveModelRates(null, null).matched).toBe('fallback');
    expect(resolveModelRates(undefined, undefined).matched).toBe('fallback');
  });

  it('exposes the verification date', () => {
    expect(PRICING_AS_OF).toMatch(/^\d{4}-\d{2}-\d{2}$/);
  });
});

describe('isFreeProvider — declared servicePlan (#7563)', () => {
  it('answers from the declared plan alone, ahead of every id and endpoint rule', () => {
    // Two NVIDIA instances: same endpoint, same ids, different plans.
    const nim = { id: 'nvidia-nim', type: 'api', endpoint: 'https://integrate.api.nvidia.com/v1' };
    expect(isFreeProvider({ ...nim, servicePlan: 'free' })).toBe(true);
    expect(isFreeProvider({ ...nim, servicePlan: 'paid' })).toBe(false);
    expect(isFreeProvider({ id: 'ollama-remote', servicePlan: 'local' })).toBe(true);
    expect(isFreeProvider({ id: 'claude-code', command: 'claude', servicePlan: 'subscription' })).toBe(false);
    // A declared paid plan beats an id the regex would have called free.
    expect(isFreeProvider({ id: 'opencode-zen-paid', endpoint: 'https://opencode.ai/zen/v1', servicePlan: 'paid' })).toBe(false);
    // No plan: the legacy inference is untouched.
    expect(isFreeProvider({ id: 'opencode-zen', endpoint: 'https://opencode.ai/zen/v1' })).toBe(true);
  });
});

describe('isFreeProvider', () => {
  it('classifies ollama and lmstudio ids as free (object and string forms)', () => {
    expect(isFreeProvider('ollama')).toBe(true);
    expect(isFreeProvider('lmstudio')).toBe(true);
    expect(isFreeProvider({ id: 'ollama', type: 'api' })).toBe(true);
  });

  it('classifies ollamaBacked CLI wrappers as free', () => {
    expect(isFreeProvider({ id: 'claude-ollama', ollamaBacked: true, command: 'claude' })).toBe(true);
  });

  it('classifies MTPLX-backed OpenCode wrappers as free', () => {
    expect(isFreeProvider({ id: 'opencode-mtplx', mtplxBacked: true, command: 'opencode' })).toBe(true);
  });

  it('classifies loopback-alias endpoints (127.0.0.2, [::]) as free', () => {
    expect(isFreeProvider({ id: 'my-local', type: 'api', endpoint: 'http://127.0.0.2:11434/v1' })).toBe(true);
    expect(isFreeProvider({ id: 'my-local', type: 'api', endpoint: 'http://[::]:8080' })).toBe(true);
  });

  it('classifies localhost API endpoints as free', () => {
    expect(isFreeProvider({ id: 'my-local', type: 'api', endpoint: 'http://localhost:1234/v1' })).toBe(true);
    expect(isFreeProvider({ id: 'my-local', type: 'api', endpoint: 'http://127.0.0.1:11434/v1' })).toBe(true);
  });

  it('does not classify paid providers as free', () => {
    expect(isFreeProvider({ id: 'claude-code', type: 'cli', command: 'claude' })).toBe(false);
    expect(isFreeProvider({ id: 'grok', type: 'api', endpoint: 'https://api.x.ai/v1' })).toBe(false);
    expect(isFreeProvider('codex')).toBe(false);
    expect(isFreeProvider(null)).toBe(false);
  });

  it('classifies OpenCode Zen providers as free-tier (id, endpoint, and string forms)', () => {
    expect(isFreeProvider('opencode-zen')).toBe(true);
    expect(isFreeProvider('opencode-zen-cli')).toBe(true);
    expect(isFreeProvider('opencode-zen-tui')).toBe(true);
    expect(isFreeProvider({ id: 'opencode-zen', type: 'api', endpoint: 'https://opencode.ai/zen/v1' })).toBe(true);
    expect(isFreeProvider({ id: 'my-zen', type: 'api', endpoint: 'https://opencode.ai/zen/v1' })).toBe(true);
  });

  it('does not classify an unrelated id containing "zen" as free', () => {
    expect(isFreeProvider('zen-model-paid')).toBe(false);
    expect(isFreeProvider({ id: 'kozen-api', type: 'api', endpoint: 'https://api.example.com/v1' })).toBe(false);
  });
});

describe('estimateCostUsd', () => {
  it('computes input + output cost per 1M tokens', () => {
    const rates = { inputPer1M: 3, outputPer1M: 15 };
    expect(estimateCostUsd(1_000_000, 1_000_000, rates)).toBeCloseTo(18);
    expect(estimateCostUsd(500_000, 0, rates)).toBeCloseTo(1.5);
  });

  it('treats missing counts and rates as zero', () => {
    expect(estimateCostUsd(null, undefined, { inputPer1M: 3, outputPer1M: 15 })).toBe(0);
    expect(estimateCostUsd(1000, 1000, null)).toBe(0);
  });

  it('prices cache read and cache write at their own per-1M rates', () => {
    const rates = { inputPer1M: 5, outputPer1M: 25, cacheReadPer1M: 0.5, cacheWritePer1M: 6.25 };
    const cost = estimateCostUsd(0, 0, rates, { cacheReadTokens: 1_000_000, cacheWriteTokens: 1_000_000 });
    expect(cost).toBeCloseTo(0.5 + 6.25);
  });

  it('omits cache cost entirely when the 4th argument is absent (back-compat)', () => {
    const rates = resolveModelRates('claude-code', 'claude-opus-5');
    expect(estimateCostUsd(1_000_000, 0, rates)).toBeCloseTo(5);
  });

  it('bills a cache read at 10% of the input rate for an Anthropic model', () => {
    const rates = resolveModelRates('claude-code', 'claude-opus-5');
    const inputCost = estimateCostUsd(1_000_000, 0, rates);
    const cacheReadCost = estimateCostUsd(0, 0, rates, { cacheReadTokens: 1_000_000 });
    expect(cacheReadCost).toBeCloseTo(inputCost * 0.1);
    expect(cacheReadCost).toBeCloseTo(0.5); // $5/MTok input → $0.50/MTok cache hit
  });

  it('bills a 5-minute cache write at 1.25x the input rate', () => {
    const rates = resolveModelRates('claude-code', 'claude-opus-5');
    expect(estimateCostUsd(0, 0, rates, { cacheWriteTokens: 1_000_000 })).toBeCloseTo(6.25);
  });

  // The #3124 regression in one assertion: charging cache reads at the standard
  // input rate overstates them 10x, and not counting them at all understates the
  // whole run by ~90% of its real input volume.
  it('prices a cache-heavy agentic run far below the same volume as fresh input', () => {
    const rates = resolveModelRates('claude-code', 'claude-opus-5');
    const asCacheReads = estimateCostUsd(0, 0, rates, { cacheReadTokens: 10_000_000 });
    const asFreshInput = estimateCostUsd(10_000_000, 0, rates);
    expect(asCacheReads).toBeCloseTo(asFreshInput * 0.1);
    // …but is emphatically NOT free, which is what omitting the tier implied.
    expect(asCacheReads).toBeGreaterThan(0);
  });
});

describe('cache-tier rates on resolveModelRates', () => {
  it('estimates Grok 4.7 standard and cached tokens without changing historical Grok rates', () => {
    const rates = resolveModelRates('grok', 'grok-4.7');
    expect(rates).toMatchObject({ inputPer1M: 2, outputPer1M: 6, cacheReadPer1M: 0.5, cacheWritePer1M: 2, matched: 'exact' });
    expect(estimateCostUsd(100_000, 10_000, rates, { cacheReadTokens: 100_000 })).toBeCloseTo(0.31);
    expect(resolveModelRates('grok-cli', 'grok-4.6')).toMatchObject({ inputPer1M: 2, outputPer1M: 6, cacheReadPer1M: 0.5, cacheWritePer1M: 2 });
  });
  it('derives both cache tiers from the input rate on every match tier', () => {
    for (const [providerId, model] of [
      ['claude-code', 'claude-opus-5'],        // exact
      ['claude-code', 'opus'],                 // family
      ['claude-code', 'no-such-model'],        // providerDefault
      ['who-knows', 'no-such-model']           // fallback
    ]) {
      const rates = resolveModelRates(providerId, model);
      expect(rates.cacheReadPer1M).toBeCloseTo(rates.inputPer1M * 0.1);
      expect(rates.cacheWritePer1M).toBeCloseTo(rates.inputPer1M * 1.25);
    }
  });

  it('uses xAI\'s published cached-input prices and does not surcharge cache writes', () => {
    const grok45 = resolveModelRates('grok', 'grok-4.5');
    expect(grok45.cacheReadPer1M).toBeCloseTo(0.3);
    expect(grok45.cacheWritePer1M).toBe(grok45.inputPer1M);
    expect(resolveModelRates('grok', 'grok-4.3')).toMatchObject({ cacheReadPer1M: 0.2, cacheWritePer1M: 1.25 });
    expect(resolveModelRates('grok', 'grok-4.20-reasoning')).toMatchObject({ inputPer1M: 1.25, cacheReadPer1M: 0.2, cacheWritePer1M: 1.25 });
    expect(resolveModelRates('grok', 'grok-build-0.1')).toMatchObject({ cacheReadPer1M: 0.2, cacheWritePer1M: 1 });
    // Unknown grok ids follow grok-4.7's cache pair, not the old 0.15× / 1.25× guess.
    expect(resolveModelRates('grok', 'grok-5')).toMatchObject({ cacheReadPer1M: 0.5, cacheWritePer1M: 2, matched: 'family' });
  });

  it('keeps the OpenAI/Codex 10% cached-input ratio', () => {
    const rates = resolveModelRates('codex', 'gpt-5.3-codex');
    expect(rates.cacheReadPer1M).toBeCloseTo(rates.inputPer1M * 0.1);
  });
});

describe('isFreeModelId', () => {
  it('treats Ollama/LM Studio family:tag ids as local (free)', () => {
    for (const id of ['qwen3.6:35b', 'llama3.1:8b-instruct-q8_0', 'deepseek-r1:7b', 'codestral:latest', 'mistral-small3.2:24b']) {
      expect(isFreeModelId(id)).toBe(true);
    }
  });

  // A tagged id that still resolves to a KNOWN hosted family is not free — the
  // `gpt-oss` rule prices it at its Cerebras host rate, so treating the Ollama
  // tag as authoritative would zero out a real bill.
  it('defers to a known hosted family over the local-tag shape', () => {
    expect(isFreeModelId('gpt-oss:120b')).toBe(false);
  });

  // Regression: Bedrock model ids carry a `:0` version suffix, so a bare
  // "contains a colon" test priced real paid Bedrock usage at $0.
  it('does NOT treat a Bedrock versioned id as local, despite the colon', () => {
    for (const id of [
      'us.anthropic.claude-opus-4-5-20251101-v1:0',
      'anthropic.claude-sonnet-4-5-20250929-v1:0',
      'us.anthropic.claude-haiku-4-5-20251001-v1:0'
    ]) {
      expect(isFreeModelId(id)).toBe(false);
    }
  });

  // An `org/repo` id is NOT self-evidently local: paid hosted catalogs use the
  // same shape (`cohere/command-r`, OpenRouter's `provider/model`), and calling
  // an unknown one free would under-bill real usage. Locality for that shape has
  // to come from the provider (isFreeProvider), not the model syntax.
  it('does NOT infer local from a bare org/repo shape', () => {
    for (const id of ['unsloth/Qwen3-30B', 'cohere/command-r', 'meta-llama/Llama-3-70B', 'mistralai/Mistral-Large']) {
      expect(isFreeModelId(id)).toBe(false);
    }
  });

  it('does NOT treat hosted model ids as local', () => {
    for (const id of [
      'claude-opus-5', 'claude-fable-5', 'gpt-5.3-codex', 'grok-4.5',
      'gemini-3.1-pro-preview', 'global.anthropic.claude-opus-5', 'opus', 'sonnet'
    ]) {
      expect(isFreeModelId(id)).toBe(false);
    }
  });

  it('is false for empty/nullish input', () => {
    expect(isFreeModelId(null)).toBe(false);
    expect(isFreeModelId('')).toBe(false);
    expect(isFreeModelId(undefined)).toBe(false);
  });

  // Regression: a Claude-Code CLI pointed at an Ollama backend records the LOCAL
  // model id in its transcript. Billing it through the `claude` provider default
  // invented ~$166 of cost on a real install.
  it('keeps a local model free even when it arrives via a claude provider', () => {
    expect(isFreeModelId('qwen3.6:35b')).toBe(true);
    // The provider-level check can't catch this on its own — hence the model check.
    expect(isFreeProvider('claude-code')).toBe(false);
  });

  // Zen free-tier ids are provider-side free labels (#7408): the harness's own
  // `opencode/*` namespace and an explicit `-free` suffix resolve to $0, while
  // paid ids that merely contain those substrings as part of a longer name do
  // not (the exact-rate guard still wins for a literally-priced id).
  it('treats Zen free-tier model ids as free', () => {
    for (const id of [
      'opencode/big-pickle',
      'opencode/mimo-v2.5-free',
      'opencode/muse-spark-1.3-contributor-free',
      'deepseek-v4-flash-free'
    ]) {
      expect(isFreeModelId(id)).toBe(true);
    }
  });

  it('does NOT treat a paid id with a free-looking substring as free', () => {
    expect(isFreeModelId('claude-opus-5')).toBe(false);
    expect(isFreeModelId('gpt-5.3-codex')).toBe(false);
    expect(isFreeModelId('unsloth/Qwen3-30B-free-quant')).toBe(false);
  });
});

describe('Grok Imagine rates', () => {
  it('prices the recommended image and video models at the headline tier', () => {
    expect(estimateGrokImagineCost(GROK_IMAGINE_IMAGE_MODEL, { images: 1 })).toBeCloseTo(0.04);
    expect(estimateGrokImagineCost(GROK_IMAGINE_IMAGE_MODEL, { images: 1, inputImages: 2 })).toBeCloseTo(0.06);
    expect(estimateGrokImagineCost(GROK_IMAGINE_VIDEO_MODEL, { videoSeconds: 6, inputImages: 1 })).toBeCloseTo(0.49);
    expect(estimateGrokImagineCost('grok-imagine-image', { images: 1 })).toBeCloseTo(0.02);
    expect(pricingAsOfForModel('grok-4.7')).toBe('2026-10-24');
  });

  it('does not let the shorter Imagine id swallow a longer one, or bill Imagine as grok-4.7 text', () => {
    expect(resolveModelRates('grok', 'grok-imagine-image-2.0').rateModel).toBe('grok-imagine-image-2.0');
    expect(resolveModelRates('grok', 'grok-imagine-image-quality').rateModel).toBe('grok-imagine-image-quality');
    expect(resolveModelRates('grok', 'grok-imagine-video-1.5').rateModel).toBe('grok-imagine-video-1.5');
    expect(resolveModelRates('grok', 'grok-imagine-video-1.5-lite').inputPer1M).toBe(0);
    expect(isFreeModelId('grok-imagine-image-2.0')).toBe(false);
    expect(estimateGrokImagineCost('grok-4.7', { images: 1 })).toBe(0);
  });
});

describe('Antigravity Imagen rates', () => {
  it('prices the recommended Imagen 3 model at published rates ($0.03/image)', () => {
    expect(estimateAgyImageCost(AGY_IMAGEN_IMAGE_MODEL, { images: 1 })).toBeCloseTo(0.03);
    expect(estimateAgyImageCost(AGY_IMAGEN_IMAGE_MODEL, { images: 5 })).toBeCloseTo(0.15);
    expect(estimateAgyImageCost('imagen-2', { images: 1 })).toBeCloseTo(0.02);
    expect(estimateAgyImageCost('gemini-3.8-flash', { images: 1 })).toBe(0);
  });

  it('resolves Imagen model rates with zero token pricing so images are not billed as text', () => {
    const r = resolveModelRates('antigravity-cli', AGY_IMAGEN_IMAGE_MODEL);
    expect(r.rateModel).toBe(AGY_IMAGEN_IMAGE_MODEL);
    expect(r.inputPer1M).toBe(0);
    expect(r.outputPer1M).toBe(0);
  });

  it('resolves gemini-3.8-flash exact and family variants', () => {
    const exact = resolveModelRates('antigravity-cli', 'gemini-3.8-flash');
    expect(exact).toMatchObject({ rateModel: 'gemini-3.8-flash', inputPer1M: 1.5, outputPer1M: 9.0, matched: 'exact' });
    const low = resolveModelRates('antigravity-cli', 'gemini-3.8-flash-low');
    expect(low).toMatchObject({ rateModel: 'gemini-3.8-flash', inputPer1M: 1.5, outputPer1M: 9.0, matched: 'family' });
  });
});