Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

74.3 KB
Raw Blame
import fs from 'fs'
import path from 'path'

import { parseJSON } from '~/common/utils/json-parse'
import { normalizeProviderKey } from '~/ingestion/pipelines/ai/costs/provider-matching'
import { openRouterCostsByModel } from '~/ingestion/pipelines/ai/costs/providers'
import type { ModelCost } from '~/ingestion/pipelines/ai/costs/providers/types'

import {
    type BuiltModelRow,
    DISCOUNT_REPORT_ROW_LIMIT,
    DISCOUNT_SUMMARY_ENV,
    type DiscountReportEntry,
    type EndpointCandidate,
    FLAT_FEE_FIELDS,
    MODEL_NAMESPACE_VENDORS,
    OPTIONAL_PRICING_FIELDS,
    type RouteRate,
    type RunTotals,
    UNCHECKED_WARN_FRACTION,
    accumulateModelRow,
    buildModelCost,
    buildModelRow,
    buildServedCost,
    collectModelRows,
    confirmDiscountAgainstSiblings,
    fetchOpenRouterCosts,
    finalizeTotals,
    foldModelIntoTotals,
    hasNoRefuter,
    isFirstPartyRoute,
    isVendorPromotion,
    parseDiscountRate,
    readEndpointsFromOpenRouter,
    renderDiscountReport,
    sanitizeReportCell,
    withoutDiscount,
    writeOutputs,
} from './update-ai-costs'

/** Build a cost the way the fetch loop does, so tests exercise the real de-discount. */
const cost = (promptPrice: string, discount = 0, completion = promptPrice): ReturnType<typeof buildModelCost> => {
    const built = buildModelCost({ prompt: promptPrice, completion, discount })
    if (!built) {
        throw new Error(`fixture priced at ${promptPrice} failed to build`)
    }
    return built
}

const candidate = (key: string, promptPrice: string, discount: number, vendorPromotion = false): EndpointCandidate => ({
    key,
    cost: cost(promptPrice, discount)!,
    servedCost: cost(promptPrice, 0)!,
    discount,
    vendorPromotion,
    unrefutable: false,
    servedPrompt: undefined,
    listPrompt: undefined,
})

/** An endpoints-payload entry shaped the way OpenRouter serves it. */
const endpoint = (tag: string, promptPrice: string, discount = 0): Record<string, unknown> => ({
    tag,
    provider_name: tag,
    pricing: { prompt: promptPrice, completion: promptPrice, discount },
})

describe('parseDiscountRate()', () => {
    it.each<{ description: string; discount: unknown; expected: number }>([
        { description: 'returns 0 when the field is absent', discount: undefined, expected: 0 },
        { description: 'returns 0 for null', discount: null, expected: 0 },
        { description: 'returns 0 for an explicit zero', discount: 0, expected: 0 },
        { description: 'reads a fractional rate', discount: 0.35, expected: 0.35 },
        { description: 'reads a rate served as a string', discount: '0.5', expected: 0.5 },
        { description: 'reads a negative rate as a markup', discount: -0.1, expected: -0.1 },
        { description: 'rejects a rate of exactly 1 (division by zero)', discount: 1, expected: 0 },
        { description: 'rejects a rate above 1 (would flip the sign)', discount: 1.5, expected: 0 },
    ])('$description', ({ discount, expected }) => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        expect(parseDiscountRate({ discount })).toBe(expected)
    })

    it('recovers list from a markup, which is a negative rate', () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        expect(parseDiscountRate({ discount: -0.1 }, 'x/y')).toBe(-0.1)
        expect(warn).not.toHaveBeenCalled()
        expect(buildModelCost({ prompt: '0.0000011', completion: '0.0000011', discount: -0.1 })!.prompt_token).toBe(
            0.000001
        )
    })

    it.each<{ description: string; discount: unknown }>([
        { description: 'an object', discount: {} },
        { description: 'an array', discount: ['0.5'] },
        { description: 'a boolean', discount: true },
        { description: 'an empty string', discount: '' },
        { description: 'a whitespace string', discount: '   ' },
        { description: 'an unparseable string', discount: 'abc' },
    ])('warns on a present but unusable rate: $description', ({ discount }) => {
        // Read as "no promotion", these store the promo price as list.
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        expect(parseDiscountRate({ discount }, 'x/y')).toBe(0)
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('x/y'))
    })

    it('stays silent when there is no rate at all', () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        parseDiscountRate({}, 'x/y')
        expect(warn).not.toHaveBeenCalled()
    })

    it('warns exactly once on an unparseable string rate', () => {
        // The decimal parser logs its own failure; one bad field, one line.
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        expect(parseDiscountRate({ discount: 'abc' }, 'x/y')).toBe(0)
        expect(warn).toHaveBeenCalledTimes(1)
    })

    it('stays silent on an unparseable rate when warnings are suppressed', () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        parseDiscountRate({ discount: 'abc' }, 'x/y', false)
        expect(warn).not.toHaveBeenCalled()
    })

    it('warns naming the model when the rate is out of range', () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        parseDiscountRate({ discount: 2 }, 'openai/gpt-5.6-luna (openai)')
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('openai/gpt-5.6-luna (openai)'))
    })
})

describe('withoutDiscount()', () => {
    it('returns undefined for absent pricing', () => {
        expect(withoutDiscount(undefined)).toBeUndefined()
    })

    it('drops the rate and keeps every other field', () => {
        expect(withoutDiscount({ prompt: '1', completion: '2', discount: 0.5 })).toEqual({
            prompt: '1',
            completion: '2',
        })
    })

    it('keeps a cost at the served price even when a rate is present', () => {
        // Without the strip the carve-out rests on the list payload never gaining a rate.
        const served = { prompt: '0.0000005', completion: '0.000003', discount: 0.5 }
        expect(buildModelCost(withoutDiscount(served))!.prompt_token).toBe(0.0000005)
        expect(buildModelCost(served)!.prompt_token).toBe(0.000001)
    })
})

describe('isFirstPartyRoute()', () => {
    it.each<{ description: string; model: string; key: string; expected: boolean }>([
        { description: 'the vendor route itself', model: 'openai/gpt-5.6-luna', key: 'openai', expected: true },
        { description: 'a vendor service tier', model: 'openai/gpt-5.6-luna', key: 'openai-flex', expected: true },
        {
            description: "the vendor's other hosting arm",
            model: 'google/gemini-3.7-flash',
            key: 'google-vertex-global',
            expected: true,
        },
        {
            description: 'a batch variant of the model',
            model: 'google/x:batch',
            key: 'google-ai-studio',
            expected: true,
        },
        { description: 'a vendor aliased to another name', model: 'qwen/qwen-plus', key: 'alibaba', expected: true },
        { description: 'a hyphenated vendor alias', model: 'x-ai/grok-5', key: 'xai-priority', expected: true },
        { description: 'a reseller', model: 'google/gemini-3.7-flash', key: 'novita-fp8', expected: false },
        { description: 'the vendor of a different model', model: 'qwen/qwen-plus', key: 'qwen', expected: false },
        { description: 'a reseller sharing the vendor prefix', model: 'openai/o5', key: 'openaint', expected: false },
        { description: 'an id with no namespace', model: 'gpt-5.6-luna', key: 'openai', expected: false },
        // Without the separator guard the whole id is read as its own vendor, and
        // this is the shape where that returns true instead of false.
        { description: 'a slug-only id that looks like a vendor', model: 'openai-x', key: 'openai', expected: false },
    ])('$description', ({ model, key, expected }) => {
        expect(isFirstPartyRoute(model, key)).toBe(expected)
    })

    it('holds the alias table already normalized on both sides', () => {
        // Neither side is normalized on lookup, so an entry that is not already
        // in key form never matches and reverts that vendor to de-discounting.
        for (const [namespace, vendor] of Object.entries(MODEL_NAMESPACE_VENDORS)) {
            expect(normalizeProviderKey(namespace)).toBe(namespace)
            expect(normalizeProviderKey(vendor)).toBe(vendor)
        }
    })

    it('maps every alias onto the top host of its own namespace', () => {
        // Binds the table to the price book rather than to itself. Mere presence
        // is too weak: resellers appear on other vendors' namespaces too, and
        // `qwen: 'together'` would pass that. A fixed share is too brittle too:
        // Alibaba serves only the closed Qwen models, under half the namespace,
        // with a reseller close behind. So the vendor must out-host every rival.
        // Rivals group by first key segment, which only overstates them.
        const book: Array<{ model: string; cost: Record<string, unknown> }> = parseJSON(
            fs.readFileSync(path.join(__dirname, '../providers/llm-costs.json'), 'utf8')
        )

        let checked = 0
        for (const [namespace, vendor] of Object.entries(MODEL_NAMESPACE_VENDORS)) {
            const models = book.filter((row) => row.model.startsWith(`${namespace}/`))
            // A namespace the catalogue drops makes its entry inert, not wrong.
            if (models.length === 0) {
                continue
            }
            const hostCounts = new Map<string, number>()
            for (const row of models) {
                const hosts = new Set(
                    Object.keys(row.cost)
                        .filter((key) => key !== 'default')
                        .map((key) => (key === vendor || key.startsWith(`${vendor}-`) ? vendor : key.split('-')[0]))
                )
                for (const host of hosts) {
                    hostCounts.set(host, (hostCounts.get(host) ?? 0) + 1)
                }
            }
            const hosted = hostCounts.get(vendor) ?? 0
            const topRival = Math.max(0, ...[...hostCounts].filter(([host]) => host !== vendor).map(([, n]) => n))
            expect({ namespace, vendorOutHostsRivals: hosted > topRival }).toEqual({
                namespace,
                vendorOutHostsRivals: true,
            })
            checked += 1
        }
        expect(checked).toBeGreaterThan(0)
    })
})

describe('isVendorPromotion()', () => {
    // Prices default well clear of any list price a case builds, so a case only
    // exercises the price refutation when it sets them.
    const route = (discount: number, firstParty: boolean, servedPrompt = 7e-7): RouteRate => ({
        discount,
        firstParty,
        servedPrompt,
        listPrompt: discount === 0 ? servedPrompt : parseFloat((servedPrompt / (1 - discount)).toPrecision(10)),
    })

    it.each<{ description: string; subject: RouteRate; routes: RouteRate[]; expected: boolean }>([
        {
            description: 'holds when only the vendor runs the rate',
            subject: route(0.5, true),
            routes: [route(0.5, true), route(0, false)],
            expected: true,
        },
        {
            description: 'fails when an unrelated host runs the same rate',
            subject: route(0.5, true),
            routes: [route(0.5, true), route(0.5, false)],
            expected: false,
        },
        {
            description: 'ignores an unrelated host on a different rate',
            subject: route(0.5, true),
            routes: [route(0.5, true), route(0.2, false)],
            expected: true,
        },
        {
            // 4e-8 / 0.2 is 2.0000000000000004e-7 in float, so this matches only
            // because both sides round first. Pins that rounding.
            description: 'refutes on a price whose division is inexact',
            subject: route(0.8, true, 4e-8),
            routes: [route(0.8, true, 4e-8), route(0, false, 2e-7)],
            expected: false,
        },
        {
            // A negative rate is a markup: the served price sits above list, and
            // dividing it out is what recovers list.
            description: 'never holds for a markup',
            subject: route(-0.2, true),
            routes: [route(-0.2, true)],
            expected: false,
        },
        {
            description: 'fails when de-discounting lands on an undiscounted price',
            // minimax-m2 live: the vendor's 0.15 divides out to exactly what two
            // undiscounted hosts charge, so the rate is a markdown off a real list.
            subject: route(0.15, true, 2.55e-7),
            routes: [route(0.15, true, 2.55e-7), route(0, false, 3e-7)],
            expected: false,
        },
        {
            description: 'ignores an undiscounted host at a different price',
            subject: route(0.15, true, 2.55e-7),
            routes: [route(0.15, true, 2.55e-7), route(0, false, 9e-7)],
            expected: true,
        },
        {
            description: 'never holds for a reseller route',
            subject: route(0.5, false),
            routes: [route(0.5, false)],
            expected: false,
        },
        {
            // Sole route on purpose: a second route would refute this on its own.
            description: 'never holds without a rate',
            subject: route(0, true),
            routes: [route(0, true)],
            expected: false,
        },
    ])('$description', ({ subject, routes, expected }) => {
        expect(isVendorPromotion(subject, routes)).toBe(expected)
    })

    it('holds on a vendor-only model, where nothing can refute it', () => {
        // gemini-3.7-flash is this shape: every route is Google's, so both
        // refutations scan an empty set. `hasNoRefuter` marks it in the report.
        const routes = [route(0.5, true), route(0.5, true, 3.5e-7)]
        expect(isVendorPromotion(routes[0], routes)).toBe(true)
        expect(hasNoRefuter(routes)).toBe(true)
        // An undiscounted route could refute on price even when it is the vendor's own.
        expect(hasNoRefuter([route(0.5, true), route(0, true)])).toBe(false)
        expect(hasNoRefuter([route(0.5, true), route(0.5, false)])).toBe(false)
    })
})

describe('buildServedCost()', () => {
    it('keeps the served price when the list payload carries a rate', () => {
        expect(buildServedCost({ prompt: '0.0000005', completion: '0.000003', discount: 0.5 })!.prompt_token).toBe(
            0.0000005
        )
    })

    it('behaves like the plain builder when there is no rate', () => {
        expect(buildServedCost({ prompt: '0.000001', completion: '0.000006' })).toEqual({
            prompt_token: 0.000001,
            completion_token: 0.000006,
        })
    })
})

describe('buildModelCost() discount handling', () => {
    it('returns null when pricing is absent', () => {
        expect(buildModelCost(undefined)).toBeNull()
    })

    it('leaves an undiscounted endpoint untouched', () => {
        expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', discount: 0 })).toEqual({
            prompt_token: 0.000001,
            completion_token: 0.000006,
        })
    })

    it('leaves pricing untouched when no discount field is present at all', () => {
        expect(buildModelCost({ prompt: '0.000001', completion: '0.000006' })).toEqual({
            prompt_token: 0.000001,
            completion_token: 0.000006,
        })
    })

    it('divides a promo price back up to list', () => {
        // Must land exactly on the undiscounted azure sibling.
        expect(buildModelCost({ prompt: '0.0000005', completion: '0.000003', discount: 0.5 })).toEqual({
            prompt_token: 0.000001,
            completion_token: 0.000006,
        })
    })

    it('de-discounts the per-token rates but carries flat fees across untouched', () => {
        // A promotional route serves web_search at the same price as its undiscounted sibling.
        expect(
            buildModelCost({
                prompt: '0.0000005',
                completion: '0.000003',
                input_cache_read: '0.00000005',
                web_search: '0.01',
                request: '0.004',
                discount: 0.5,
            })
        ).toEqual({
            prompt_token: 0.000001,
            completion_token: 0.000006,
            cache_read_token: 0.0000001,
            web_search: 0.01,
            request: 0.004,
        })
    })

    it.each([-0.25, 0, 0.1, 0.41, 0.5, 0.9])('leaves a flat fee alone at a rate of %d', (discount) => {
        expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', web_search: '0.01', discount })).toEqual(
            expect.objectContaining({ web_search: 0.01 })
        )
    })

    it('does not adjust prices when the rate is out of range', () => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', discount: 1 })).toEqual({
            prompt_token: 0.000001,
            completion_token: 0.000006,
        })
    })

    it('omits optional fields that are absent or zero, as before', () => {
        expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', web_search: '0', discount: 0.5 })).toEqual({
            prompt_token: 0.000002,
            completion_token: 0.000012,
        })
    })
})

describe('FLAT_FEE_FIELDS is bound to the field list it filters', () => {
    // Neither direction is a type error: a member outside the loop is dead, and a
    // loop field outside the set is silently de-discounted.
    const targetFields = OPTIONAL_PRICING_FIELDS.map(([targetField]) => targetField)

    it.each([...FLAT_FEE_FIELDS])('%s is a field the loop actually reads', (field) => {
        expect(targetFields).toContain(field)
    })

    it('reads the OpenRouter field names this list claims to read', () => {
        // Spelled out rather than derived: every other assertion feeds the source name
        // back into its own fixture, so only a literal catches a typo or a dropped pair.
        expect(OPTIONAL_PRICING_FIELDS).toEqual([
            ['cache_read_token', 'input_cache_read'],
            ['cache_write_token', 'input_cache_write'],
            ['request', 'request'],
            ['web_search', 'web_search'],
            ['image', 'image'],
            ['image_output', 'image_output'],
            ['audio', 'audio'],
            ['audio_output', 'audio_output'],
            ['input_audio_cache', 'input_audio_cache'],
            ['internal_reasoning', 'internal_reasoning'],
        ])
    })

    // Partial, so the absent-policy guard below is type-honest. A stray key
    // outside `keyof ModelCost` fails to compile; one inside it but outside the
    // loop fails the keys-equality test.
    const policy: Partial<Record<keyof ModelCost, 'flat' | 'de-discounted'>> = {
        cache_read_token: 'de-discounted',
        cache_write_token: 'de-discounted',
        request: 'flat',
        web_search: 'flat',
        image: 'de-discounted',
        image_output: 'de-discounted',
        audio: 'de-discounted',
        audio_output: 'de-discounted',
        input_audio_cache: 'de-discounted',
        internal_reasoning: 'de-discounted',
    }

    it('declares a policy for every field the loop reads, and no others', () => {
        expect(Object.keys(policy).sort()).toEqual([...targetFields].sort())
    })

    it.each(OPTIONAL_PRICING_FIELDS)('%s is priced as its declared policy says', (targetField, sourceField) => {
        const expected = policy[targetField]
        // A loop field with no policy here fails rather than silently inheriting the de-discount.
        expect(expected).toBeTruthy()

        const built = buildModelCost({
            prompt: '0.000001',
            completion: '0.000001',
            [sourceField]: '0.01',
            discount: 0.5,
        })

        expect(built?.[targetField]).toBe(expected === 'flat' ? 0.01 : 0.02)
        expect(FLAT_FEE_FIELDS.has(targetField)).toBe(expected === 'flat')
    })
})

describe('buildModelCost() recovers prices the cost book previously recorded', () => {
    // Expectations are the list prices the cost book itself held before each
    // promotion began, so a wrong divisor fails on values this suite did not pick.
    it.each<{ model: string; served: string; discount: number; list: number }>([
        { model: 'qwen/qwen-plus prompt', served: '0.000000169', discount: 0.35, list: 0.00000026 },
        { model: 'qwen/qwen-plus completion', served: '0.000000507', discount: 0.35, list: 0.00000078 },
        { model: 'qwen/qwen3-max prompt', served: '0.000000507', discount: 0.35, list: 0.00000078 },
        { model: 'qwen/qwen3-max completion', served: '0.000002535', discount: 0.35, list: 0.0000039 },
        { model: 'qwen/qwen3-14b prompt', served: '0.000000147875', discount: 0.35, list: 0.0000002275 },
        { model: 'qwen/qwen3-235b-a22b prompt', served: '0.00000029575', discount: 0.35, list: 0.000000455 },
        { model: 'qwen/qwen3-coder-flash prompt', served: '0.00000012675', discount: 0.35, list: 0.000000195 },
        { model: 'qwen/qwen3.6-max-preview prompt', served: '0.000000832', discount: 0.2, list: 0.00000104 },
        { model: 'qwen/qwen3.5-plus-20260420 prompt', served: '0.000000225', discount: 0.25, list: 0.0000003 },
        { model: 'qwen/qwen3.6-flash prompt', served: '0.000000140625', discount: 0.25, list: 0.0000001875 },
        { model: 'openai/gpt-5.6-luna prompt', served: '0.0000005', discount: 0.5, list: 0.000001 },
        { model: 'openai/gpt-5.6-terra prompt', served: '0.00000125', discount: 0.5, list: 0.0000025 },
    ])('$model', ({ served, discount, list }) => {
        expect(cost(served, discount)!.prompt_token).toBe(list)
    })
})

describe('buildModelRow()', () => {
    const listPricing = { prompt: '0.0000005', completion: '0.0000005' }
    const listCost = cost('0.0000005')!

    it('reports nothing was checked when there are no endpoints', () => {
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [])
        expect(built!.checked).toBe(false)
        expect(built!.discount).toBeUndefined()
        expect(built!.cost).toEqual({ default: listCost })
    })

    it('reports nothing was checked when every endpoint fails to parse', () => {
        // A non-empty payload can still yield no usable pricing.
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
            { tag: 'openai', pricing: { prompt: 'x' } },
            {},
        ])
        expect(built!.checked).toBe(false)
        expect(Object.keys(built!.cost)).toStrictEqual(['default'])
    })

    it('stores each reseller endpoint at its de-discounted list price', () => {
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
            endpoint('novita-fp8', '0.0000005', 0.5),
            endpoint('azure', '0.000001'),
        ])
        expect(built!.checked).toBe(true)
        expect(built!.cost['novita-fp8'].prompt_token).toBe(0.000001)
        expect(built!.cost.azure.prompt_token).toBe(0.000001)
    })

    it('leaves `default` exactly as OpenRouter served it', () => {
        // provider-matching.ts resolves `$ai_provider: openrouter` onto `default`,
        // and OpenRouter does bill the promo rate, so it must not move.
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [endpoint('openai', '0.0000005', 0.5)])
        expect(built!.cost.default).toEqual(listCost)
        expect(built!.cost.default.prompt_token).toBe(0.0000005)
    })

    it('reports the discounted endpoints and the confirmation verdict', () => {
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
            endpoint('novita-fp8', '0.0000005', 0.5),
            endpoint('azure', '0.000001'),
        ])
        expect(built!.discount).toEqual({
            model: 'openai/gpt-5.6-luna',
            endpoints: [
                {
                    key: 'novita-fp8',
                    discount: 0.5,
                    confirmation: 'confirmed' as const,
                    vendorPromotion: false,
                    unrefutable: false,
                    servedPrompt: 5e-7,
                    listPrompt: 0.000001,
                },
            ],
        })
    })

    it('reports no discount entry when every endpoint is at list price', () => {
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [endpoint('azure', '0.000001')])
        expect(built!.checked).toBe(true)
        expect(built!.discount).toBeUndefined()
    })

    it('never lets an endpoint claim the `default` key', () => {
        // Otherwise it overwrites the served price with a de-discounted one.
        const built = buildModelRow('evil/model', listPricing, [endpoint('default', '0.0000009', 0.5)])
        expect(built!.cost.default).toEqual(listCost)
        expect(built!.cost['provider-default'].prompt_token).toBe(0.0000018)
    })

    it('reports not-checkable when no undiscounted sibling exists', () => {
        // Reseller tag on purpose: a vendor route would keep its promotion instead.
        const built = buildModelRow('qwen/qwen-plus', listPricing, [endpoint('novita-fp8', '0.000000169', 0.35)])
        expect(built!.discount?.endpoints[0].confirmation).toBe('not-checkable')
    })

    it('leaves a vendor-run promotion at the price the vendor serves', () => {
        const built = buildModelRow('google/gemini-3.7-flash', listPricing, [
            endpoint('google-ai-studio', '0.0000005', 0.5),
            endpoint('google-vertex-global', '0.0000005', 0.5),
        ])
        expect(built!.cost['google-ai-studio'].prompt_token).toBe(0.0000005)
        expect(built!.cost['google-vertex-global'].prompt_token).toBe(0.0000005)
        expect(built!.discount?.endpoints.map((e) => [e.key, e.confirmation])).toEqual([
            ['google-ai-studio', 'not-applicable'],
            ['google-vertex-global', 'not-applicable'],
        ])
    })

    it('de-discounts a vendor route when unrelated hosts run the same rate', () => {
        // One rate across unrelated hosts is OpenRouter promoting the model, so
        // the vendor's route goes to list with the rest.
        const built = buildModelRow('z-ai/glm-5.3-flash', listPricing, [
            endpoint('z-ai-fp8', '0.000000075', 0.5),
            endpoint('novita-fp8', '0.000000075', 0.5),
            endpoint('deepinfra-fp8', '0.000000075', 0.5),
            endpoint('together', '0.00000015'),
        ])
        expect(built!.cost['z-ai-fp8'].prompt_token).toBe(1.5e-7)
        expect(built!.cost['novita-fp8'].prompt_token).toBe(1.5e-7)
        expect(built!.discount?.endpoints[0].confirmation).toBe('confirmed')
    })

    it('de-discounts a vendor route whose recovered price an undiscounted host charges', () => {
        // minimax-m2 live: 0.15 off 2.55e-7 recovers 3e-7, which two undiscounted
        // hosts charge outright, so the rate is a markdown and not MiniMax's promo.
        const built = buildModelRow('minimax/minimax-m2', listPricing, [
            endpoint('minimax-fp8', '0.000000255', 0.15),
            endpoint('novita-fp8', '0.0000003'),
        ])
        expect(built!.cost['minimax-fp8'].prompt_token).toBe(3e-7)
        expect(built!.discount?.endpoints[0].confirmation).toBe('confirmed')
    })

    it('marks a vendor-only model as having nothing that could refute it', () => {
        const built = buildModelRow('google/gemini-3.7-flash', listPricing, [
            endpoint('google-ai-studio', '0.0000005', 0.5),
        ])
        expect(built!.cost['google-ai-studio'].prompt_token).toBe(0.0000005)
        expect(built!.discount?.endpoints[0].unrefutable).toBe(true)
    })

    it('keeps a vendor promotion when a reseller runs a different rate', () => {
        const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
            endpoint('openai', '0.0000005', 0.5),
            endpoint('novita-fp8', '0.0000008', 0.2),
        ])
        expect(built!.cost.openai.prompt_token).toBe(0.0000005)
        expect(built!.cost['novita-fp8'].prompt_token).toBe(0.000001)
    })

    it('warns once, naming the model, on an out-of-range rate', () => {
        // Parsed twice per endpoint; a second warn would be unattributable.
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        buildModelRow('openai/gpt-5.6-luna', listPricing, [endpoint('openai', '0.000001', 1.5)])
        expect(warn).toHaveBeenCalledTimes(1)
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('openai/gpt-5.6-luna'))
    })

    it('returns null when the model has no usable list pricing', () => {
        expect(buildModelRow('x/y', { prompt: 'nonsense' }, [])).toBeNull()
    })

    it('keeps `default` at the served price even if the list payload gains a rate', () => {
        const built = buildModelRow('x/y', { ...listPricing, discount: 0.5 }, [endpoint('openai', '0.0000005', 0.5)])
        expect(built!.cost.default.prompt_token).toBe(0.0000005)
        expect(built!.cost.openai.prompt_token).toBe(0.000001)
    })

    it.each([
        { field: 'image_output' as const, rate: 0.00012 },
        { field: 'audio_output' as const, rate: 0.00006 },
    ])('backfills $field onto `default` from a provider variant', ({ field, rate }) => {
        const built = buildModelRow('google/gemini-3-pro-image-preview', listPricing, [
            {
                tag: 'google-vertex',
                provider_name: 'google-vertex',
                pricing: { prompt: '0.0000005', completion: '0.0000005', [field]: String(rate) },
            },
        ])
        expect(built!.cost.default[field]).toBe(rate)
    })

    it('leaves an existing `default` modality rate untouched', () => {
        const built = buildModelRow('x/y', { ...listPricing, image_output: '0.00012' }, [
            {
                tag: 'openai',
                provider_name: 'openai',
                pricing: { prompt: '0.0000005', completion: '0.0000005', image_output: '0.00009' },
            },
        ])
        expect(built!.cost.default.image_output).toBe(0.00012)
    })

    it('prefers an undiscounted variant when backfilling a modality rate', () => {
        const built = buildModelRow('x/y', listPricing, [
            {
                tag: 'discounted',
                provider_name: 'discounted',
                pricing: { prompt: '0.0000005', completion: '0.0000005', image_output: '0.00005', discount: 0.5 },
            },
            {
                tag: 'listed',
                provider_name: 'listed',
                pricing: { prompt: '0.0000005', completion: '0.0000005', image_output: '0.00012' },
            },
        ])
        expect(built!.cost.default.image_output).toBe(0.00012)
    })

    it('prefers the endpoint tier that matches the default prompt rate regardless of response order', () => {
        const standard = {
            tag: 'google-standard',
            provider_name: 'google-standard',
            pricing: { prompt: '0.0000005', completion: '0.0000005', image_output: '0.00012' },
        }
        const flex = {
            tag: 'google-flex',
            provider_name: 'google-flex',
            pricing: { prompt: '0.00000025', completion: '0.00000025', image_output: '0.00006' },
        }

        const rates = [
            buildModelRow('x/y', listPricing, [flex, standard])!.cost.default.image_output,
            buildModelRow('x/y', listPricing, [standard, flex])!.cost.default.image_output,
        ]

        expect(rates).toEqual([0.00012, 0.00012])
    })

    it('uses the provider key as a deterministic fallback when no endpoint matches the default prompt rate', () => {
        const firstByKey = {
            tag: 'a-provider',
            provider_name: 'a-provider',
            pricing: { prompt: '0.00000025', completion: '0.00000025', image_output: '0.00006', discount: 0.5 },
        }
        const lastByKey = {
            tag: 'z-provider',
            provider_name: 'z-provider',
            pricing: { prompt: '0.00000075', completion: '0.00000075', image_output: '0.00009', discount: 0.5 },
        }

        const rates = [
            buildModelRow('x/y', listPricing, [lastByKey, firstByKey])!.cost.default.image_output,
            buildModelRow('x/y', listPricing, [firstByKey, lastByKey])!.cost.default.image_output,
        ]

        expect(rates).toEqual([0.00006, 0.00006])
    })

    it('prefers an undiscounted variant when no endpoint matches the default prompt rate', () => {
        const discounted = {
            tag: 'a-discounted',
            provider_name: 'a-discounted',
            pricing: { prompt: '0.00000025', completion: '0.00000025', image_output: '0.00006', discount: 0.5 },
        }
        const undiscounted = {
            tag: 'z-undiscounted',
            provider_name: 'z-undiscounted',
            pricing: { prompt: '0.00000075', completion: '0.00000075', image_output: '0.00009' },
        }

        const rates = [
            buildModelRow('x/y', listPricing, [discounted, undiscounted])!.cost.default.image_output,
            buildModelRow('x/y', listPricing, [undiscounted, discounted])!.cost.default.image_output,
        ]

        expect(rates).toEqual([0.00009, 0.00009])
    })

    it('prefers a matching discounted variant over a nonmatching undiscounted variant', () => {
        const built = buildModelRow('x/y', listPricing, [
            {
                tag: 'a-nonmatching',
                provider_name: 'a-nonmatching',
                pricing: { prompt: '0.00000025', completion: '0.00000025', image_output: '0.00006' },
            },
            {
                tag: 'z-matching',
                provider_name: 'z-matching',
                pricing: { prompt: '0.0000005', completion: '0.0000005', image_output: '0.00005', discount: 0.5 },
            },
        ])
        expect(built!.cost.default.image_output).toBe(0.00005)
        expect(built!.cost['z-matching'].image_output).toBe(0.0001)
    })

    it('confines a hostile provider name to a safe key', () => {
        // Interpolated into generated TypeScript, so an apostrophe must not reach it.
        const built = buildModelRow('evil/model', listPricing, [
            { tag: '!!!', provider_name: "o'brien <script>", pricing: { prompt: '0.000001', completion: '0.000001' } },
        ])
        const key = Object.keys(built!.cost).find((k) => k !== 'default')!
        expect(key).toBe('provider-o-brien-script')
        expect(key).toMatch(/^[a-z0-9-]+$/)
    })
})

describe('confirmDiscountAgainstSiblings()', () => {
    it('is not checkable for an undiscounted route', () => {
        const azure = candidate('azure', '0.000001', 0)
        expect(confirmDiscountAgainstSiblings(azure, [azure])).toBe('not-checkable')
    })

    it('is not checkable without an undiscounted sibling', () => {
        // Every Qwen model: Alibaba is the only first-party route.
        const alibaba = candidate('alibaba-fp8', '0.000000169', 0.35)
        expect(confirmDiscountAgainstSiblings(alibaba, [alibaba])).toBe('not-checkable')
    })

    it('confirms when the de-discounted price lands on an undiscounted sibling', () => {
        const openai = candidate('openai', '0.0000005', 0.5)
        expect(confirmDiscountAgainstSiblings(openai, [openai, candidate('azure', '0.000001', 0)])).toBe('confirmed')
    })

    it('has nothing to corroborate for a route that kept its promotion', () => {
        // The matching sibling stays: without the guard it reads as corroborating
        // a division that never ran.
        const openai = candidate('openai', '0.0000005', 0.5, true)
        expect(confirmDiscountAgainstSiblings(openai, [openai, candidate('azure', '0.000001', 0)])).toBe(
            'not-applicable'
        )
    })

    it('reports unconfirmed when the recovered price matches no sibling', () => {
        const openai = candidate('openai', '0.0000005', 0.5)
        expect(confirmDiscountAgainstSiblings(openai, [openai, candidate('azure', '0.000009', 0)])).toBe('unconfirmed')
    })

    it('judges each promoted route on its own', () => {
        const flex = candidate('openai-flex', '0.00000025', 0.5)
        const openai = candidate('openai', '0.0000005', 0.5)
        const all = [flex, openai, candidate('azure', '0.000001', 0)]
        expect(confirmDiscountAgainstSiblings(openai, all)).toBe('confirmed')
        expect(confirmDiscountAgainstSiblings(flex, all)).toBe('unconfirmed')
    })

    it('compares on the prompt price, not the completion price', () => {
        // Every other fixture sets completion equal to prompt.
        const discounted: EndpointCandidate = {
            key: 'openai',
            cost: buildModelCost({ prompt: '0.0000005', completion: '0.000009', discount: 0.5 })!,
            servedCost: buildModelCost({ prompt: '0.0000005', completion: '0.000009' })!,
            discount: 0.5,
            vendorPromotion: false,
            unrefutable: false,
            servedPrompt: undefined,
            listPrompt: undefined,
        }
        const sibling: EndpointCandidate = {
            key: 'azure',
            cost: buildModelCost({ prompt: '0.000001', completion: '0.000002' })!,
            servedCost: buildModelCost({ prompt: '0.000001', completion: '0.000002' })!,
            discount: 0,
            vendorPromotion: false,
            unrefutable: false,
            servedPrompt: undefined,
            listPrompt: undefined,
        }
        expect(confirmDiscountAgainstSiblings(discounted, [discounted, sibling])).toBe('confirmed')
    })
})

describe('sanitizeReportCell()', () => {
    it.each<{ description: string; input: string; expected: string }>([
        {
            description: 'passes an ordinary model id through',
            input: 'openai/gpt-5.6-luna',
            expected: 'openai/gpt-5.6-luna',
        },
        { description: 'folds newlines so a row cannot be split', input: 'a\nb\r\nc', expected: 'a b c' },
        { description: 'strips pipes so a row cannot gain columns', input: 'a|b', expected: 'ab' },
        {
            description: 'strips angle brackets and everything else outside the allowlist',
            input: '<img src=x>',
            expected: 'img srcx',
        },
        { description: 'strips backticks so code spans cannot escape', input: 'a`b', expected: 'ab' },
        {
            description: 'strips image syntax so a remote image cannot load in the PR body',
            input: '![pricing](https://example.com/pixel)',
            expected: 'pricinghttps://example.com/pixel',
        },
        {
            description: 'strips link syntax so no clickable target survives',
            input: '[click](https://evil.example)',
            expected: 'clickhttps://evil.example',
        },
    ])('$description', ({ input, expected }) => {
        expect(sanitizeReportCell(input)).toBe(expected)
    })

    it('bounds the cell length', () => {
        expect(sanitizeReportCell('x'.repeat(500))).toHaveLength(120)
    })
})

describe('renderDiscountReport()', () => {
    const entry = (over: Partial<DiscountReportEntry> = {}): DiscountReportEntry => ({
        model: 'openai/gpt-5.6-luna',
        endpoints: [
            {
                key: 'openai',
                discount: 0.5,
                confirmation: 'confirmed' as const,
                vendorPromotion: false,
                unrefutable: false,
                servedPrompt: undefined,
                listPrompt: undefined,
            },
        ],
        ...over,
    })

    it('says so plainly when nothing is discounted', () => {
        expect(renderDiscountReport([])).toContain('No discounted endpoints found')
    })

    it('says that per-call fees are not de-discounted', () => {
        const report = renderDiscountReport([entry()])
        expect(report).toContain(
            'Per-call fees (`request`, `web_search`) carry across untouched: promotional routes in\n' +
                'the feed serve `web_search` at the same fee as their undiscounted siblings,\n' +
                'and every other per-call fee follows the same policy.'
        )
    })

    it('derives the flat-fee list from FLAT_FEE_FIELDS', () => {
        // The literal pin above catches a retyped sentence; this binds it to the set.
        expect(renderDiscountReport([entry()])).toContain(
            `Per-call fees (${[...FLAT_FEE_FIELDS]
                .sort()
                .map((field) => `\`${field}\``)
                .join(', ')}) carry across untouched`
        )
    })

    it('names each discounted model, endpoint and rate', () => {
        const report = renderDiscountReport([entry()])
        expect(report).toContain('openai/gpt-5.6-luna')
        expect(report).toContain('`openai`')
        expect(report).toContain('50%')
    })

    it('counts endpoints and models, and reports the confirmed ratio', () => {
        // Numerator and denominator must differ, or a checkable-count reads the same.
        const report = renderDiscountReport([
            entry(),
            entry({
                model: 'aaa/unconfirmed',
                endpoints: [
                    {
                        key: 'openai',
                        discount: 0.5,
                        confirmation: 'unconfirmed' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
            entry({
                model: 'qwen/qwen-plus',
                endpoints: [
                    {
                        key: 'alibaba-fp8',
                        discount: 0.35,
                        confirmation: 'not-checkable' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                    {
                        key: 'streamlake',
                        discount: 0.4,
                        confirmation: 'not-checkable' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
        ])
        expect(report).toContain('**4 endpoint(s)**')
        expect(report).toContain('**3 model(s)**')
        expect(report).toContain('1/2 checkable')
    })

    it('attributes a kept promotion to the vendor, not to OpenRouter', () => {
        // Without the vendorPromotion filters the headline claims 3 endpoints on
        // 2 models and the ratio reads 1/2, crediting routes nothing corroborated.
        const report = renderDiscountReport([
            entry(),
            entry({
                model: 'google/gemini-3.7-flash',
                endpoints: [
                    {
                        key: 'google-ai-studio',
                        discount: 0.5,
                        confirmation: 'not-applicable' as const,
                        vendorPromotion: true,
                        unrefutable: true,
                        servedPrompt: 7.5e-7,
                        listPrompt: 1.5e-6,
                    },
                    {
                        key: 'google-vertex-global',
                        discount: 0.5,
                        confirmation: 'not-applicable' as const,
                        vendorPromotion: true,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
        ])
        expect(report).toContain('**1 endpoint(s)**')
        expect(report).toContain('**1 model(s)**')
        expect(report).toContain('1/1 checkable')
        expect(report).toContain('**2 endpoint(s)**')
        expect(report).toContain('**1** of those had no route that could have refuted')
        expect(report).toContain('| `google-ai-studio` | 50% | served (unrefuted) | $0.75 vs $1.5 | not-applicable |')
        expect(report).toContain('| `google-vertex-global` | 50% | served |  | not-applicable |')
    })

    it('reads what was stored off the flag, not off the verdict', () => {
        // The two agree in production. Held apart here so a regression back to
        // deriving `Stored` from the enum cannot pass.
        const report = renderDiscountReport([
            entry({
                endpoints: [
                    {
                        key: 'openai',
                        discount: 0.5,
                        confirmation: 'not-applicable' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
        ])
        expect(report).toContain('| 50% | list |')
        expect(report).toContain('**1 endpoint(s)**')
    })

    it('puts the confirmation verdict on the row it belongs to', () => {
        const report = renderDiscountReport([
            entry({
                endpoints: [
                    {
                        key: 'openai',
                        discount: 0.5,
                        confirmation: 'unconfirmed' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
        ])
        expect(report).toContain('| unconfirmed |')
    })

    it('blanks the model on a continuation row and keeps that route verdict', () => {
        const report = renderDiscountReport([
            entry({
                endpoints: [
                    {
                        key: 'openai',
                        discount: 0.5,
                        confirmation: 'confirmed' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                    {
                        key: 'openai-flex',
                        discount: 0.5,
                        confirmation: 'confirmed' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
        ])
        const rows = report.split('\n').filter((line) => line.includes('50%'))
        expect(rows[0]).toContain('openai/gpt-5.6-luna')
        expect(rows[1]).not.toContain('openai/gpt-5.6-luna')
        expect(rows[1]).toContain('confirmed')
    })

    it('reports models it could not check rather than claiming a clean run', () => {
        // A run whose endpoint calls all failed must not read as "no promotions".
        expect(renderDiscountReport([], 17)).toContain('17 model(s) could not be checked')
        expect(renderDiscountReport([entry()], 17)).toContain('17 model(s) could not be checked')
    })

    it('says nothing about unchecked models when every model was checked', () => {
        expect(renderDiscountReport([entry()], 0)).not.toContain('could not be checked')
    })

    const manyEntries = (count: number, endpointsEach = 1): DiscountReportEntry[] =>
        Array.from({ length: count }, (_, index) =>
            entry({
                model: `vendor/model-${String(index).padStart(4, '0')}`,
                endpoints: Array.from({ length: endpointsEach }, (_, e) => ({
                    key: `k${e}`,
                    discount: 0.5,
                    confirmation: 'confirmed' as const,
                    vendorPromotion: false,
                    unrefutable: false,
                    servedPrompt: undefined,
                    listPrompt: undefined,
                })),
            })
        )

    it('pins the row limit to a literal, so the cap cannot drift with the code', () => {
        expect(DISCOUNT_REPORT_ROW_LIMIT).toBe(200)
    })

    it('lists everything and says nothing about omissions at exactly the limit', () => {
        const report = renderDiscountReport(manyEntries(DISCOUNT_REPORT_ROW_LIMIT))
        expect(report.split('\n').filter((line) => line.includes('50%'))).toHaveLength(DISCOUNT_REPORT_ROW_LIMIT)
        expect(report).not.toContain('more discounted model(s)')
    })

    it('omits exactly one model at one over the limit', () => {
        const report = renderDiscountReport(manyEntries(DISCOUNT_REPORT_ROW_LIMIT + 1))
        expect(report).toContain('and 1 more discounted model(s) not listed')
    })

    it('caps on rendered rows, not models, since a model can carry many endpoints', () => {
        // The widest model carries 32 endpoints, so a model cap under-counts rows.
        const report = renderDiscountReport(manyEntries(20, 32))
        expect(report.split('\n').filter((line) => line.includes('50%')).length).toBeLessThanOrEqual(
            DISCOUNT_REPORT_ROW_LIMIT
        )
        expect(report).toContain('more discounted model(s) not listed')
    })

    it('escapes a hostile model id so it cannot break the table', () => {
        const report = renderDiscountReport([entry({ model: 'evil/x|y\n| pwned | 1 | 2 |' })])
        expect(report).not.toContain('pwned | 1 | 2 |')
        expect(report.split('\n').filter((line) => line.includes('50%'))).toHaveLength(1)
    })

    it('escapes a hostile endpoint key so it cannot break the table', () => {
        // Sanitized separately from the model id, so it needs its own input.
        const report = renderDiscountReport([
            entry({
                endpoints: [
                    {
                        key: 'a|b\n| pwned | 9 | 9 |',
                        discount: 0.5,
                        confirmation: 'confirmed' as const,
                        vendorPromotion: false,
                        unrefutable: false,
                        servedPrompt: undefined,
                        listPrompt: undefined,
                    },
                ],
            }),
        ])
        expect(report).not.toContain('pwned | 9 | 9 |')
        expect(report.split('\n').filter((line) => line.includes('50%'))).toHaveLength(1)
    })

    it('sorts models so the report is stable across runs', () => {
        const report = renderDiscountReport([entry({ model: 'zzz/model' }), entry({ model: 'aaa/model' })])
        expect(report.indexOf('aaa/model')).toBeLessThan(report.indexOf('zzz/model'))
    })
})

describe('accumulateModelRow()', () => {
    const totals = (): RunTotals => ({ models: [], discounts: [], uncheckedModels: 0 })
    const built = (over: Partial<BuiltModelRow> = {}): BuiltModelRow => ({
        cost: { default: cost('0.000001')! },
        checked: true,
        ...over,
    })

    it('collects the row into the price book', () => {
        const acc = accumulateModelRow(built(), 'openai/gpt-5.6-luna', totals())
        expect(acc.models).toStrictEqual([{ model: 'openai/gpt-5.6-luna', cost: { default: cost('0.000001') } }])
    })

    it('collects a discount entry when the row reports one', () => {
        const discount = {
            model: 'm',
            endpoints: [
                {
                    key: 'openai',
                    discount: 0.5,
                    confirmation: 'confirmed' as const,
                    vendorPromotion: false,
                    unrefutable: false,
                    servedPrompt: undefined,
                    listPrompt: undefined,
                },
            ],
        }
        expect(accumulateModelRow(built({ discount }), 'm', totals()).discounts).toStrictEqual([discount])
    })

    it('counts a row that could not be checked', () => {
        // The wire from `checked` to the summary, not just its two ends.
        expect(accumulateModelRow(built({ checked: false }), 'm', totals()).uncheckedModels).toBe(1)
    })

    it('accumulates across successive calls', () => {
        // Every field must accumulate the same way, or one silently stays behind.
        let acc = totals()
        acc = accumulateModelRow(built({ checked: false }), 'a', acc)
        acc = accumulateModelRow(built({ checked: false }), 'b', acc)
        expect(acc.models).toHaveLength(2)
        expect(acc.uncheckedModels).toBe(2)
    })

    it('leaves the totals it was handed untouched', () => {
        const base = totals()
        const discount: DiscountReportEntry = {
            model: 'a',
            endpoints: [
                {
                    key: 'openai',
                    discount: 0.5,
                    confirmation: 'confirmed',
                    vendorPromotion: false,
                    unrefutable: false,
                    servedPrompt: undefined,
                    listPrompt: undefined,
                },
            ],
        }
        accumulateModelRow(built({ checked: false, discount }), 'a', base)
        expect(base.models).toHaveLength(0)
        expect(base.discounts).toHaveLength(0)
        expect(base.uncheckedModels).toBe(0)
    })

    it('does not count a row that was checked', () => {
        expect(accumulateModelRow(built({ checked: true }), 'm', totals()).uncheckedModels).toBe(0)
    })
})

describe('foldModelIntoTotals()', () => {
    const listPricing = { prompt: '0.0000005', completion: '0.0000005' }
    const start = (): RunTotals => ({ models: [], discounts: [], uncheckedModels: 0 })

    it('folds a priced model into the totals', () => {
        const out = foldModelIntoTotals('a/b', listPricing, [], start())
        expect(out.models).toHaveLength(1)
        expect(out.uncheckedModels).toBe(1)
    })

    it('skips a model whose list pricing will not parse', () => {
        // Without the skip the price book gains a row whose default is null.
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const out = foldModelIntoTotals('a/b', { prompt: 'nonsense' }, [], start())
        expect(out.models).toHaveLength(0)
    })

    it('leaves the totals it was handed untouched', () => {
        const base = start()
        foldModelIntoTotals('a/b', listPricing, [], base)
        expect(base.models).toHaveLength(0)
        expect(base.uncheckedModels).toBe(0)
    })

    it('keeps what was already collected when it skips', () => {
        // From empty, "added nothing" and "discarded everything" look the same.
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const seeded = foldModelIntoTotals('good/one', listPricing, [endpoint('openai', '0.000001', 0.5)], start())
        expect(seeded.models).toHaveLength(1)
        const after = foldModelIntoTotals('bad/one', { prompt: 'nonsense' }, [], seeded)
        expect(after.models).toHaveLength(1)
        expect(after.discounts).toHaveLength(1)
        expect(after.uncheckedModels).toBe(seeded.uncheckedModels)
    })

    it('files the row and the discount under the model id it was given', () => {
        const out = foldModelIntoTotals('a/b', listPricing, [endpoint('openai', '0.000001', 0.5)], start())
        expect(out.models[0].model).toBe('a/b')
        expect(out.discounts[0].model).toBe('a/b')
    })

    it('warns naming the model it skipped', () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        foldModelIntoTotals('a/b', undefined, [], start())
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('Skipping'), 'a/b')
    })
})

describe('finalizeTotals()', () => {
    const row = (model: string) => ({ model, cost: { default: cost('0.000001')! } })

    it('orders models by id so output does not follow response order', () => {
        const out = finalizeTotals({ models: [row('z/z'), row('a/a')], discounts: [], uncheckedModels: 0 })
        expect(out.models.map((m) => m.model)).toStrictEqual(['a/a', 'z/z'])
    })

    it('leaves the totals it was handed untouched', () => {
        // Same contract accumulateModelRow carries.
        const base: RunTotals = { models: [row('z/z'), row('a/a')], discounts: [], uncheckedModels: 0 }
        finalizeTotals(base)
        expect(base.models.map((m) => m.model)).toStrictEqual(['z/z', 'a/a'])
    })

    it('carries the other totals through unchanged', () => {
        const out = finalizeTotals({ models: [], discounts: [], uncheckedModels: 4 })
        expect(out.uncheckedModels).toBe(4)
    })
})

describe('collectModelRows()', () => {
    const priced = (id: string) => ({ id, pricing: { prompt: '0.0000005', completion: '0.0000005' } })
    const noEndpoints = (): Promise<unknown[]> => Promise.resolve([])

    it('orders the price book by model id', () => {
        // Response order rewrites the whole file each run and opens a PR on noise.
        return collectModelRows([priced('z/z'), priced('a/a')], noEndpoints).then((totals) => {
            expect(totals.models.map((m) => m.model)).toStrictEqual(['a/a', 'z/z'])
        })
    })

    it('skips an entry with no id and keeps going', async () => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const totals = await collectModelRows([{}, priced('a/a')], noEndpoints)
        expect(totals.models).toHaveLength(1)
        expect(totals.models.map((m) => m.model)).toStrictEqual(['a/a'])
    })

    it('skips an entry with no id even when its pricing is valid', async () => {
        // An id-less entry with parseable pricing is the only input that tells the
        // id guard apart from the pricing guard below it.
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const totals = await collectModelRows(
            [{ pricing: { prompt: '0.000001', completion: '0.000001' } }, priced('a/a')],
            noEndpoints
        )
        expect(totals.models.map((m) => m.model)).toStrictEqual(['a/a'])
    })

    it('reports the unchecked count with its denominator', async () => {
        // Assert the whole phrase: the per-model progress line also carries an
        // "n/m" and would satisfy a bare substring check.
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        jest.spyOn(console, 'log').mockImplementation(() => {})
        await collectModelRows([priced('a/a'), priced('b/b')], noEndpoints)
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('2/2 model(s) had no usable endpoint pricing'))
    })

    it('warns rather than logs once too much of the catalogue is unchecked', async () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        jest.spyOn(console, 'log').mockImplementation(() => {})
        await collectModelRows([priced('a/a')], noEndpoints)
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('degraded'))
    })

    it('stays at log level when the unchecked share is within the steady state', async () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        const log = jest.spyOn(console, 'log').mockImplementation(() => {})
        // One unchecked model in a catalogue large enough to stay under the share.
        const models = [priced('a/a'), ...Array.from({ length: 20 }, (_, i) => priced(`m/${i}`))]
        await collectModelRows(models, (id) => Promise.resolve(id === 'a/a' ? [] : [endpoint('openai', '0.000001')]))
        expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('degraded'))
        expect(log).toHaveBeenCalledWith(expect.stringContaining('1/21 model(s) had no usable endpoint pricing'))
    })

    it('pins the unchecked warn share to a literal', () => {
        expect(UNCHECKED_WARN_FRACTION).toBe(0.1)
    })

    it('counts models whose endpoints came back empty', async () => {
        const totals = await collectModelRows([priced('a/a'), priced('b/b')], noEndpoints)
        expect(totals.uncheckedModels).toBe(2)
    })

    it('collects discounts from the endpoints it is handed', async () => {
        const totals = await collectModelRows([priced('a/a')], () =>
            Promise.resolve([endpoint('openai', '0.0000005', 0.5), endpoint('azure', '0.000001')])
        )
        expect(totals.discounts).toHaveLength(1)
        expect(totals.discounts[0].endpoints[0].confirmation).toBe('confirmed')
        expect(totals.models[0].cost.openai.prompt_token).toBe(0.000001)
    })

    it('reads endpoints once per model, by id', async () => {
        const seen: string[] = []
        await collectModelRows([priced('a/a'), priced('b/b')], (id) => {
            seen.push(id)
            return Promise.resolve([])
        })
        expect(seen).toStrictEqual(['a/a', 'b/b'])
    })

    it.each([
        { field: 'image_output' as const, rate: 0.00012 },
        { field: 'audio_output' as const, rate: 0.00006 },
    ])('preserves the previous default $field rate when endpoint fetching fails', async ({ field, rate }) => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const previousDefaults = new Map<string, ModelCost>([
            ['a/a', { prompt_token: 0.0000005, completion_token: 0.0000005, [field]: rate }],
        ])

        const totals = await collectModelRows(
            [priced('a/a')],
            () => Promise.reject(new Error('socket hang up')),
            previousDefaults
        )

        expect(totals.models[0].cost.default[field]).toBe(rate)
    })

    it('keeps collecting other models after an endpoint fetch fails', async () => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const totals = await collectModelRows([priced('a/a'), priced('b/b')], (id) =>
            id === 'a/a' ? Promise.reject(new Error('socket hang up')) : Promise.resolve([])
        )

        expect(totals.models.map((model) => model.model)).toStrictEqual(['a/a', 'b/b'])
    })

    it('keeps a current model-level modality rate when endpoint fetching fails', async () => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        const previousDefaults = new Map<string, ModelCost>([
            ['a/a', { prompt_token: 0.0000005, completion_token: 0.0000005, image_output: 0.00012 }],
        ])
        const totals = await collectModelRows(
            [{ ...priced('a/a'), pricing: { ...priced('a/a').pricing, image_output: '0.00009' } }],
            () => Promise.reject(new Error('socket hang up')),
            previousDefaults
        )

        expect(totals.models[0].cost.default.image_output).toBe(0.00009)
    })

    it('does not preserve a previous modality rate after a successful empty response', async () => {
        const previousDefaults = new Map<string, ModelCost>([
            ['a/a', { prompt_token: 0.0000005, completion_token: 0.0000005, image_output: 0.00012 }],
        ])
        const totals = await collectModelRows([priced('a/a')], noEndpoints, previousDefaults)

        expect(totals.models[0].cost.default.image_output).toBeUndefined()
    })
})

describe('readEndpointsFromOpenRouter()', () => {
    const mockFetch = (impl: () => unknown): jest.SpyInstance =>
        jest.spyOn(global, 'fetch' as never).mockImplementation(impl as never)

    it('returns the endpoints the payload carries', async () => {
        mockFetch(() => Promise.resolve({ ok: true, json: () => Promise.resolve({ data: { endpoints: [1, 2] } }) }))
        await expect(readEndpointsFromOpenRouter('a/b')).resolves.toStrictEqual([1, 2])
    })

    it('rejects a non-ok response so the caller can preserve prior data', async () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        mockFetch(() => Promise.resolve({ ok: false, status: 429, statusText: 'Too Many Requests' }))
        await expect(readEndpointsFromOpenRouter('a/b')).rejects.toThrow('429 Too Many Requests')
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('a/b'))
    })

    it('rejects when the request throws so the caller can preserve prior data', async () => {
        const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
        mockFetch(() => Promise.reject(new Error('socket hang up')))
        await expect(readEndpointsFromOpenRouter('a/b')).rejects.toThrow('socket hang up')
        expect(warn).toHaveBeenCalledWith(expect.stringContaining('Error fetching'), 'a/b', expect.anything())
    })

    it('rejects malformed JSON so the caller can preserve prior data', async () => {
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        mockFetch(() => Promise.resolve({ ok: true, json: () => Promise.reject(new Error('bad JSON')) }))
        await expect(readEndpointsFromOpenRouter('a/b')).rejects.toThrow('bad JSON')
    })

    it('degrades to no endpoints when the payload has no endpoints key', async () => {
        mockFetch(() => Promise.resolve({ ok: true, json: () => Promise.resolve({}) }))
        await expect(readEndpointsFromOpenRouter('a/b')).resolves.toStrictEqual([])
    })

    it('encodes each path segment of the model id', async () => {
        let requested = ''
        mockFetch(((url: string) => {
            requested = url
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ data: { endpoints: [] } }) })
        }) as never)
        await readEndpointsFromOpenRouter('anthropic/claude-sonnet-5:batch')
        expect(requested).toContain('anthropic/claude-sonnet-5%3Abatch')
    })
})

describe('fetchOpenRouterCosts()', () => {
    const listPayload = {
        data: [{ id: 'a/b', pricing: { prompt: '0.0000005', completion: '0.0000005' } }],
    }
    const endpointsPayload = { data: { endpoints: [endpoint('openai', '0.0000005', 0.5)] } }

    const mockFetch = (): jest.SpyInstance =>
        jest.spyOn(global, 'fetch' as never).mockImplementation(((url: string) =>
            Promise.resolve({
                ok: true,
                json: () => Promise.resolve(url.includes('/endpoints') ? endpointsPayload : listPayload),
            })) as never)

    it('asks for decision and embedding models as well as text models', async () => {
        jest.spyOn(console, 'log').mockImplementation(() => {})
        const fetchSpy = mockFetch()
        await fetchOpenRouterCosts()
        const listUrl = new URL(fetchSpy.mock.calls[0][0])
        expect(listUrl.searchParams.get('output_modalities')).toBe('text,decisions,embeddings')
    })

    it('reads per-endpoint pricing, not just the list payload', async () => {
        // Pins that production hands the loop the real reader, not a fake.
        jest.spyOn(console, 'log').mockImplementation(() => {})
        mockFetch()
        const totals = await fetchOpenRouterCosts()
        expect(totals.models[0].cost.openai.prompt_token).toBe(0.000001)
        expect(totals.uncheckedModels).toBe(0)
    })

    it('reads the models out of the payload envelope', async () => {
        jest.spyOn(console, 'log').mockImplementation(() => {})
        mockFetch()
        const totals = await fetchOpenRouterCosts()
        expect(totals.models.map((m) => m.model)).toStrictEqual(['a/b'])
    })

    it('uses committed default modality rates when an endpoint request fails', async () => {
        jest.spyOn(console, 'log').mockImplementation(() => {})
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        jest.spyOn(global, 'fetch' as never).mockImplementation(((url: string) => {
            if (url.includes('/endpoints')) {
                return Promise.reject(new Error('socket hang up'))
            }
            return Promise.resolve({
                ok: true,
                json: () =>
                    Promise.resolve({
                        data: [
                            {
                                id: 'google/gemini-3-pro-image-preview',
                                pricing: { prompt: '0.000002', completion: '0.000012' },
                            },
                        ],
                    }),
            })
        }) as never)

        const totals = await fetchOpenRouterCosts()
        const committedImageOutputRate =
            openRouterCostsByModel['google/gemini-3-pro-image-preview'].cost.default.image_output

        expect(committedImageOutputRate).toBeDefined()
        expect(totals.models[0].cost.default.image_output).toBe(committedImageOutputRate)
    })

    it('throws when the models list cannot be fetched', async () => {
        jest.spyOn(global, 'fetch' as never).mockImplementation((() =>
            Promise.resolve({ ok: false, status: 503, statusText: 'Service Unavailable' })) as never)
        await expect(fetchOpenRouterCosts()).rejects.toThrow('Failed to fetch OpenRouter models')
    })

    it('throws when the models list is not JSON', async () => {
        jest.spyOn(global, 'fetch' as never).mockImplementation((() =>
            Promise.resolve({ ok: true, json: () => Promise.reject(new Error('bad json')) })) as never)
        await expect(fetchOpenRouterCosts()).rejects.toThrow('Failed to parse OpenRouter API response')
    })
})

describe('writeOutputs()', () => {
    const summaryPath = '/tmp/does-not-matter-discount-summary.md'

    afterEach(() => {
        delete process.env[DISCOUNT_SUMMARY_ENV]
    })

    /** Record both halves of every write, so content is pinned and not just the path. */
    const captureWrites = (): Array<[string, string]> => {
        const writes: Array<[string, string]> = []
        jest.spyOn(fs, 'writeFileSync').mockImplementation(((file: string, body: string) => {
            writes.push([String(file), String(body)])
        }) as never)
        return writes
    }

    it('writes the price book before the summary', () => {
        // Ordering is the protection: the summary only decorates a PR body, so it
        // must never be the write that discards a completed run.
        process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
        const writes = captureWrites()

        writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)

        const priceBook = writes.findIndex(([f]) => f.endsWith('llm-costs.json'))
        const summary = writes.findIndex(([f]) => f === summaryPath)
        // Both must actually happen; a missing price-book write would otherwise
        // satisfy a bare index comparison at -1.
        expect(priceBook).toBeGreaterThanOrEqual(0)
        expect(summary).toBeGreaterThanOrEqual(0)
        expect(priceBook).toBeLessThan(summary)
    })

    it('writes the rows it was handed into the price book', () => {
        process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
        const writes = captureWrites()
        const rows = [{ model: 'm', cost: { default: cost('0.000001')! } }]

        writeOutputs(rows, [], 0)

        const [, body] = writes.find(([f]) => f.endsWith('llm-costs.json'))!
        expect(parseJSON(body)).toEqual(rows)
    })

    it('carries the unchecked count into the summary it writes', () => {
        // The count is produced five hops upstream; this is the hop that hands it
        // to the report, and without it the body reads as a verified clean run.
        process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
        const writes = captureWrites()

        writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 17)

        const [, body] = writes.find(([f]) => f === summaryPath)!
        expect(body).toContain('17 model(s) could not be checked')
    })

    it('renders the promotions it was handed into the summary', () => {
        // Every other call passes an empty list, which pins nothing.
        process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
        const writes = captureWrites()
        const promo: DiscountReportEntry = {
            model: 'openai/gpt-5.6-luna',
            endpoints: [
                {
                    key: 'openai',
                    discount: 0.5,
                    confirmation: 'confirmed',
                    vendorPromotion: false,
                    unrefutable: false,
                    servedPrompt: undefined,
                    listPrompt: undefined,
                },
            ],
        }

        writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [promo], 0)

        const [, body] = writes.find(([f]) => f === summaryPath)!
        expect(body).toContain('openai/gpt-5.6-luna')
        expect(body).not.toContain('No discounted endpoints found')
    })

    it('generates the provider union alongside the price book', () => {
        process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
        const writes = captureWrites()

        writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)

        expect(writes.some(([f]) => f.endsWith('canonical-providers.ts'))).toBe(true)
    })

    it('does not throw when the summary write fails', () => {
        process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
        jest.spyOn(console, 'warn').mockImplementation(() => {})
        jest.spyOn(fs, 'writeFileSync').mockImplementation(((file: string) => {
            if (String(file) === summaryPath) {
                throw new Error('scratch disk full')
            }
        }) as never)

        expect(() => writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)).not.toThrow()
    })

    it('writes no summary when the env var is unset', () => {
        const writes = captureWrites()

        writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)

        expect(writes.some(([f]) => f === summaryPath)).toBe(false)
    })
})

describe('workflow contract', () => {
    it('sets the same env var the script reads', () => {
        // Two hand-maintained strings in different languages. Nothing else binds
        // them, and a rename drops the promotions section from the PR body.
        const workflow = fs.readFileSync(
            path.join(__dirname, '../../../../../../../.github/workflows/update-ai-costs.yml'),
            'utf8'
        )
        expect(workflow).toContain(`${DISCOUNT_SUMMARY_ENV}=`) // the step that sets it
        // The exact guard and the exact read, so renaming either one fails here.
        expect(workflow).toContain(`[ -f "$${DISCOUNT_SUMMARY_ENV}" ]`)
        expect(workflow).toContain(`cat "$${DISCOUNT_SUMMARY_ENV}" >> "$body_file"`)
        // The sink: the assembled body must be what reaches GitHub on both paths.
        expect(workflow.match(/--body-file "\$body_file"/g) ?? []).toHaveLength(2)
    })
})

describe('module import safety', () => {
    it('does not fetch or write generated files when imported', () => {
        // Without the guard, importing hits the live API and rewrites the committed
        // price book. Spies go in before the module loads so the assertion catches it.
        const fetchSpy = jest
            .spyOn(global, 'fetch' as never)
            .mockImplementation((() =>
                Promise.resolve({ ok: true, json: () => Promise.resolve({ data: [] }) })) as never)
        const writeSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
        const mkdirSpy = jest.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined)

        jest.isolateModules(() => {
            require('./update-ai-costs')
        })

        expect(fetchSpy).not.toHaveBeenCalled()
        expect(writeSpy).not.toHaveBeenCalled()
        expect(mkdirSpy).not.toHaveBeenCalled()
    })
})