llm-browser-testkit 0.19.7

LLM-driven browser test framework — define browser test scenarios in TOML with natural language steps and LLM-powered assertions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! Automatic pricing lookup from providers that expose exact, public
//! per-token prices.
//!
//! Two sources are supported:
//!
//! - `OpenRouter`: `GET /api/v1/models` carries exact per-token `prompt`,
//!   `completion`, `input_cache_read` and `input_cache_write` prices with no
//!   authentication.
//! - AWS Bedrock: the public AWS Price List offers `AmazonBedrock` (Nova,
//!   Llama, Mistral, …) and `AmazonBedrockFoundationModels` (Anthropic
//!   Claude, Cohere, …) publish exact per-model input/output/cache-read/
//!   cache-write prices per region. Both are fetched at startup and mapped to
//!   the configured model id.
//!
//! Other providers either expose no public price API (`OpenAI`, Groq, xAI,
//! `DeepSeek`, Google) or expose prices that cannot be mapped to a model
//! exactly without heuristics (Azure Retail Prices).

use std::collections::HashMap;

use crate::scenario::{EndpointConfig, Provider};

/// Exact per-1M-token prices for a model.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ModelPricing {
    /// Input (prompt) price per 1M tokens.
    pub input_per_1m: f64,
    /// Output (completion) price per 1M tokens.
    pub output_per_1m: f64,
    /// Cached-input (cache read) price per 1M tokens, when offered.
    pub cached_input_per_1m: Option<f64>,
    /// Cache-write (cache creation) price per 1M tokens, when offered.
    pub cache_write_per_1m: Option<f64>,
}

/// Parses an `OpenRouter` `/api/v1/models` response for `model`.
///
/// Matches the full model id case-insensitively first, then falls back to the
/// leaf after the last `/` (so `claude-3.5-sonnet` matches
/// `anthropic/claude-3.5-sonnet`). Returns `None` when the model is absent or
/// its pricing is not a usable positive number.
#[must_use]
pub fn parse_openrouter_models(json: &serde_json::Value, model: &str) -> Option<ModelPricing> {
    let wanted = model.to_ascii_lowercase();
    let leaf = wanted.rsplit('/').next().unwrap_or(wanted.as_str());
    let data = json["data"].as_array()?;
    let entry = data.iter().find(|m| {
        let id = m["id"].as_str().unwrap_or_default().to_ascii_lowercase();
        id == wanted || id.rsplit('/').next().unwrap_or_default() == leaf
    })?;
    let pricing = &entry["pricing"];
    Some(ModelPricing {
        input_per_1m: per_million(pricing, "prompt")?,
        output_per_1m: per_million(pricing, "completion")?,
        cached_input_per_1m: per_million(pricing, "input_cache_read"),
        cache_write_per_1m: per_million(pricing, "input_cache_write"),
    })
}

/// Reads a per-token price (string or number) and converts it to per-1M
/// tokens. Negative values (`OpenRouter` uses `-1` for dynamic/auto prices)
/// and missing keys yield `None`.
fn per_million(pricing: &serde_json::Value, key: &str) -> Option<f64> {
    let value = match &pricing[key] {
        serde_json::Value::String(s) => s.parse::<f64>().ok()?,
        serde_json::Value::Number(n) => n.as_f64()?,
        _ => return None,
    };
    (value >= 0.0).then_some(value * 1_000_000.0)
}

/// Fetches exact `OpenRouter` pricing for `model`.
///
/// # Errors
///
/// Returns a description when the request fails, the response is not JSON, or
/// the model is not present in the catalog.
pub async fn fetch_openrouter_pricing(
    client: &reqwest::Client,
    model: &str,
) -> Result<ModelPricing, String> {
    let json = fetch_openrouter_models(client).await?;
    parse_openrouter_models(&json, model)
        .ok_or_else(|| format!("model `{model}` not found in OpenRouter pricing"))
}

/// Fetches the raw `OpenRouter` models catalog (one HTTP call, reusable for
/// many models).
///
/// # Errors
///
/// Returns a description when the request fails or the response is not JSON.
async fn fetch_openrouter_models(client: &reqwest::Client) -> Result<serde_json::Value, String> {
    let resp = client
        .get("https://openrouter.ai/api/v1/models")
        .send()
        .await
        .map_err(|e| format!("OpenRouter models request failed: {e}"))?;
    if !resp.status().is_success() {
        return Err(format!(
            "OpenRouter models request returned HTTP {}",
            resp.status()
        ));
    }
    resp.json()
        .await
        .map_err(|e| format!("OpenRouter models response was not JSON: {e}"))
}

/// Which exact-pricing source an endpoint opts into.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Source {
    /// `OpenRouter` public models API.
    OpenRouter,
    /// AWS Bedrock Price List (`AmazonBedrock` + `AmazonBedrockFoundationModels`).
    Bedrock,
}

/// Resolves the pricing source for an endpoint.
///
/// `pricing_source` is `"openrouter"`, `"bedrock"`, `"auto"`, or one of
/// `"off"`/`"none"`/`"disabled"`. When **unset** (the default) or `"auto"`,
/// the source is inferred: Bedrock endpoints use the Bedrock Price List and
/// `openrouter.ai` URLs use the `OpenRouter` models API; anything else is a
/// no-op.
#[must_use]
fn source_for(ec: &EndpointConfig) -> Option<Source> {
    let source = ec.pricing_source.as_deref().map(str::to_ascii_lowercase);
    match source.as_deref() {
        Some("openrouter") => Some(Source::OpenRouter),
        Some("bedrock") => Some(Source::Bedrock),
        Some("off" | "none" | "disabled") => None,
        _ => auto_source(ec),
    }
}

/// Infers the pricing source from the endpoint when `pricing_source` is unset
/// or `"auto"`.
#[must_use]
fn auto_source(ec: &EndpointConfig) -> Option<Source> {
    if ec.provider == Provider::Bedrock {
        Some(Source::Bedrock)
    } else if ec
        .url
        .as_deref()
        .is_some_and(|url| url.to_ascii_lowercase().contains("openrouter.ai"))
    {
        Some(Source::OpenRouter)
    } else {
        None
    }
}

/// Fills unset pricing fields on `ec` from fetched `pricing`; explicit values
/// win.
pub fn apply_pricing(ec: &mut EndpointConfig, pricing: &ModelPricing) {
    let p = ec.pricing.get_or_insert_with(Default::default);
    if p.input_per_1m_tokens == 0.0 {
        p.input_per_1m_tokens = pricing.input_per_1m;
    }
    if p.output_per_1m_tokens == 0.0 {
        p.output_per_1m_tokens = pricing.output_per_1m;
    }
    if p.cached_input_per_1m_tokens.is_none() {
        p.cached_input_per_1m_tokens = pricing.cached_input_per_1m;
    }
    if p.cache_write_per_1m_tokens.is_none() {
        p.cache_write_per_1m_tokens = pricing.cache_write_per_1m;
    }
}

/// Outcome of an automatic pricing pass.
#[derive(Debug, Default)]
pub struct PricingOutcome {
    /// Number of endpoints whose pricing was filled.
    pub priced: usize,
    /// Endpoint names where auto pricing was on but the provider has no
    /// supported exact source; their `pricing_source` is set to `off`.
    pub unsupported: Vec<String>,
    /// First lookup error (non-fatal), if any.
    pub error: Option<String>,
}

/// Whether pricing is missing on an endpoint (nothing to fill = no warning).
fn needs_pricing(ec: &EndpointConfig) -> bool {
    ec.pricing
        .as_ref()
        .is_none_or(|p| p.input_per_1m_tokens == 0.0 || p.output_per_1m_tokens == 0.0)
}

/// Whether the endpoint is in auto mode (unset or `"auto"`).
fn is_auto(ec: &EndpointConfig) -> bool {
    ec.pricing_source
        .as_deref()
        .is_none_or(|s| s.eq_ignore_ascii_case("auto"))
}

/// Applies automatic pricing to every endpoint.
///
/// Pricing lookup is on by default (`pricing_source` unset = `"auto"`). The
/// source is inferred from the provider: Bedrock endpoints use the AWS Price
/// List and `openrouter.ai` URLs use the `OpenRouter` models API. Endpoints
/// whose provider has no supported source are reported in
/// [`PricingOutcome::unsupported`] and have `pricing_source` set to `off`.
/// Lookup failures are collected in [`PricingOutcome::error`] and never abort
/// the pass.
#[allow(clippy::implicit_hasher, clippy::too_many_lines)]
pub async fn apply_auto_pricing(
    endpoints: &mut HashMap<String, EndpointConfig>,
    client: &reqwest::Client,
) -> PricingOutcome {
    let mut outcome = PricingOutcome::default();
    // Fetched once per region/endpoint-kind and reused.
    let mut bedrock_catalogs: HashMap<String, HashMap<String, ModelPricing>> = HashMap::new();
    let mut openrouter_catalog: Option<serde_json::Value> = None;
    for (name, ec) in endpoints.iter_mut() {
        let Some(model) = ec.model.clone() else {
            continue;
        };
        let result = match source_for(ec) {
            Some(Source::OpenRouter) => {
                if openrouter_catalog.is_none() {
                    match fetch_openrouter_models(client).await {
                        Ok(catalog) => openrouter_catalog = Some(catalog),
                        Err(e) => {
                            outcome.error.get_or_insert(e);
                            continue;
                        }
                    }
                }
                let catalog = openrouter_catalog.as_ref().expect("catalog fetched above");
                parse_openrouter_models(catalog, &model)
                    .ok_or_else(|| format!("model `{model}` not found in OpenRouter pricing"))
            }
            Some(Source::Bedrock) => {
                let region = bedrock_region(ec);
                if !bedrock_catalogs.contains_key(&region) {
                    match fetch_bedrock_catalog(client, &region).await {
                        Ok(catalog) => {
                            bedrock_catalogs.insert(region.clone(), catalog);
                        }
                        Err(e) => {
                            outcome.error.get_or_insert(e);
                            continue;
                        }
                    }
                }
                let catalog = &bedrock_catalogs[&region];
                lookup_bedrock(catalog, &model)
                    .ok_or_else(|| format!("model `{model}` not found in Bedrock pricing"))
            }
            None => {
                // Auto is on but this provider exposes no exact price source:
                // warn (when there is pricing to fill) and switch it off.
                if is_auto(ec) && needs_pricing(ec) {
                    outcome.unsupported.push(name.clone());
                }
                ec.pricing_source = Some("off".to_owned());
                continue;
            }
        };
        match result {
            Ok(pricing) => {
                apply_pricing(ec, &pricing);
                outcome.priced += 1;
            }
            Err(e) => {
                outcome.error.get_or_insert(e);
            }
        }
    }
    outcome
}

/// Resolves the region whose Bedrock price list applies to an endpoint:
/// the endpoint's explicit `aws.region`, else `AWS_REGION`/`AWS_DEFAULT_REGION`,
/// else `us-east-1`.
#[must_use]
fn bedrock_region(ec: &EndpointConfig) -> String {
    ec.aws
        .region
        .clone()
        .or_else(|| std::env::var("AWS_REGION").ok())
        .or_else(|| std::env::var("AWS_DEFAULT_REGION").ok())
        .filter(|r| !r.trim().is_empty())
        .unwrap_or_else(|| "us-east-1".to_owned())
}

/// Base host of the public AWS Price List (always `us-east-1`, global).
const PRICING_HOST: &str = "https://pricing.us-east-1.amazonaws.com";

/// AWS Price List offers that together cover all Bedrock models: AWS-published
/// models (Nova, Llama, Mistral, …) and Marketplace models (Anthropic, …).
const BEDROCK_OFFERS: [&str; 2] = ["AmazonBedrock", "AmazonBedrockFoundationModels"];

/// Fetches and merges the Bedrock price catalogs for `region` into a map keyed
/// by the normalized model name.
///
/// # Errors
///
/// Returns a description when either offer cannot be fetched or the region is
/// absent from its region index.
async fn fetch_bedrock_catalog(
    client: &reqwest::Client,
    region: &str,
) -> Result<HashMap<String, ModelPricing>, String> {
    let mut acc: HashMap<String, Partial> = HashMap::new();
    for offer in BEDROCK_OFFERS {
        let doc = fetch_offer_region(client, offer, region).await?;
        // The FoundationModels (Marketplace) offer wins on conflicts: it is
        // the current source for Anthropic and carries cache write prices.
        let overwrite = offer == "AmazonBedrockFoundationModels";
        parse_bedrock_offer(&doc, &mut acc, overwrite);
    }
    Ok(acc
        .into_iter()
        .filter_map(|(key, partial)| partial.finish().map(|p| (key, p)))
        .collect())
}

/// Fetches one offer's price file for `region` by following its region index.
async fn fetch_offer_region(
    client: &reqwest::Client,
    offer: &str,
    region: &str,
) -> Result<serde_json::Value, String> {
    let index_url = format!("{PRICING_HOST}/offers/v1.0/aws/{offer}/current/region_index.json");
    let index: serde_json::Value = client
        .get(&index_url)
        .send()
        .await
        .map_err(|e| format!("{offer} region index request failed: {e}"))?
        .json()
        .await
        .map_err(|e| format!("{offer} region index was not JSON: {e}"))?;
    let path = index["regions"][region]["currentVersionUrl"]
        .as_str()
        .ok_or_else(|| format!("region `{region}` not present in {offer} price list"))?;
    let url = format!("{PRICING_HOST}{path}");
    client
        .get(&url)
        .send()
        .await
        .map_err(|e| format!("{offer} price list request failed: {e}"))?
        .json()
        .await
        .map_err(|e| format!("{offer} price list was not JSON: {e}"))
}

/// Role of a price entry within a model.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Role {
    /// Ordinary input (prompt) tokens.
    Input,
    /// Output (completion) tokens.
    Output,
    /// Prompt cache read (cached input) tokens.
    Read,
    /// Prompt cache write (cache creation) tokens.
    Write,
}

/// Per-model accumulator while merging the two offers.
#[derive(Debug, Default, Clone, Copy)]
struct Partial {
    input: Option<f64>,
    output: Option<f64>,
    read: Option<f64>,
    write: Option<f64>,
}

impl Partial {
    const fn set(&mut self, role: Role, value: f64, overwrite: bool) {
        let slot = match role {
            Role::Input => &mut self.input,
            Role::Output => &mut self.output,
            Role::Read => &mut self.read,
            Role::Write => &mut self.write,
        };
        if overwrite || slot.is_none() {
            *slot = Some(value);
        }
    }

    fn finish(self) -> Option<ModelPricing> {
        Some(ModelPricing {
            input_per_1m: self.input?,
            output_per_1m: self.output?,
            cached_input_per_1m: self.read,
            cache_write_per_1m: self.write,
        })
    }
}

/// Parses one offer document into `acc`, keyed by normalized model name.
fn parse_bedrock_offer(
    doc: &serde_json::Value,
    acc: &mut HashMap<String, Partial>,
    overwrite: bool,
) {
    let Some(products) = doc["products"].as_object() else {
        return;
    };
    for (sku, product) in products {
        let a = &product["attributes"];
        let Some((model, role)) = bedrock_entry(a) else {
            continue;
        };
        let Some((usd, unit)) = on_demand_price(doc, sku) else {
            continue;
        };
        let Some(per_million) = to_per_million(usd, &unit) else {
            continue;
        };
        acc.entry(compact_key(model))
            .or_default()
            .set(role, per_million, overwrite);
    }
}

/// Extracts the model name and price role from a product's attributes, for
/// either offer shape. Returns `None` for non-standard tiers (batch, flex,
/// priority, global, latency-optimized, provisioned throughput).
fn bedrock_entry(attrs: &serde_json::Value) -> Option<(&str, Role)> {
    // Offer 1 (`AmazonBedrock`): a non-empty `model` attribute, role in
    // `inferenceType`, restricted to on-demand inference.
    if let Some(model) = attrs["model"].as_str().filter(|m| !m.is_empty()) {
        if attrs["feature"].as_str() != Some("On-demand Inference") {
            return None;
        }
        if attrs["batch"].as_str().is_some_and(|b| !b.is_empty()) {
            return None;
        }
        let role = offer1_role(attrs["inferenceType"].as_str().unwrap_or_default())?;
        return Some((model, role));
    }
    // Offer 2 (`AmazonBedrockFoundationModels`): model in `servicename`
    // (always suffixed `(Amazon Bedrock Edition)`), role in `usagetype`.
    if let Some(servicename) = attrs["servicename"].as_str() {
        if let Some(role) = offer2_role(attrs["usagetype"].as_str().unwrap_or_default()) {
            let model = servicename
                .strip_suffix(" (Amazon Bedrock Edition)")
                .unwrap_or(servicename);
            return Some((model, role));
        }
    }
    None
}

/// Lowercases and drops separators so hyphen/underscore/camel spellings match.
fn normalize_token(value: &str) -> String {
    value
        .chars()
        .filter(char::is_ascii_alphanumeric)
        .collect::<String>()
        .to_ascii_lowercase()
}

/// Maps an `AmazonBedrock` `inferenceType` to a role, excluding tier variants.
fn offer1_role(inference_type: &str) -> Option<Role> {
    let s = normalize_token(inference_type);
    if ["priority", "flex", "batch", "global", "latency"]
        .iter()
        .any(|t| s.contains(t))
    {
        return None;
    }
    if s.contains("cacheread") {
        Some(Role::Read)
    } else if s.contains("cachewrite") {
        Some(Role::Write)
    } else if s.contains("input")
        && !s.contains("image")
        && !s.contains("video")
        && !s.contains("audio")
    {
        Some(Role::Input)
    } else if s.contains("output") && !s.contains("image") && !s.contains("video") {
        Some(Role::Output)
    } else {
        None
    }
}

/// Maps an `AmazonBedrockFoundationModels` `usagetype` to a role, excluding
/// tier/TTL variants.
fn offer2_role(usagetype: &str) -> Option<Role> {
    let s = normalize_token(usagetype);
    if [
        "global", "batch", "priority", "flex", "latency", "1h", "30m", "custom",
    ]
    .iter()
    .any(|t| s.contains(t))
    {
        return None;
    }
    if s.contains("cacheread") {
        Some(Role::Read)
    } else if s.contains("cachewrite") {
        Some(Role::Write)
    } else if s.contains("inputtoken") {
        Some(Role::Input)
    } else if s.contains("outputtoken") {
        Some(Role::Output)
    } else {
        None
    }
}

/// Reads the first `OnDemand` price and its unit for a SKU.
fn on_demand_price(doc: &serde_json::Value, sku: &str) -> Option<(f64, String)> {
    let offers = doc["terms"]["OnDemand"][sku].as_object()?;
    let offer = offers.values().next()?;
    let dimension = offer["priceDimensions"].as_object()?.values().next()?;
    let usd = dimension["pricePerUnit"]["USD"]
        .as_str()?
        .parse::<f64>()
        .ok()?;
    let unit = dimension["unit"].as_str().unwrap_or_default().to_owned();
    Some((usd, unit))
}

/// Converts a price to USD per 1M tokens from its unit string.
fn to_per_million(price: f64, unit: &str) -> Option<f64> {
    let unit = unit.to_ascii_lowercase();
    if unit.contains("1m") || unit.contains("million") {
        Some(price)
    } else if unit.contains("1k") || unit.contains("thousand") {
        Some(price * 1_000.0)
    } else if unit.contains("token") {
        Some(price * 1_000_000.0)
    } else {
        None
    }
}

/// Inference-profile prefixes on Bedrock model ids (`us.`, `global.`, …).
const MODEL_PREFIXES: [&str; 12] = [
    "us", "eu", "apac", "global", "us-gov", "ca", "sa", "me", "af", "il", "ap", "gov",
];

/// Provider prefixes on Bedrock model ids (`anthropic.`, `amazon.`, …).
const MODEL_PROVIDERS: [&str; 21] = [
    "anthropic",
    "amazon",
    "meta",
    "mistral",
    "cohere",
    "ai21",
    "stability",
    "deepseek",
    "openai",
    "google",
    "qwen",
    "writer",
    "nvidia",
    "minimax",
    "moonshot",
    "moonshotai",
    "zai",
    "xai",
    "kimi",
    "twelvelabs",
    "luma",
];

/// Normalizes a model id or display name to a compact comparison key.
///
/// Strips inference-profile prefixes (`us.`, `global.`, …) and provider
/// prefixes (`anthropic.`, `amazon.`, …), the `:0` revision suffix, 8-digit
/// date tokens, and all separators, so `us.anthropic.claude-3-5-sonnet-
/// 20241022-v2:0` and `Claude 3.5 Sonnet v2` both become `claude35sonnetv2`.
#[must_use]
fn model_key(model: &str) -> String {
    let lower = model.to_ascii_lowercase();
    let no_revision = lower.split(':').next().unwrap_or(&lower);
    let kept: Vec<&str> = no_revision
        .split('.')
        .filter(|s| !MODEL_PREFIXES.contains(s) && !MODEL_PROVIDERS.contains(s))
        .collect();
    let joined = if kept.is_empty() {
        no_revision
    } else {
        &kept.join("-")
    };
    compact_key(joined)
}

/// Removes separators and 8-digit date tokens, lowercasing the result.
#[must_use]
fn compact_key(value: &str) -> String {
    let mut out = String::new();
    for token in value.split(['-', '_', ' ']).filter(|t| !t.is_empty()) {
        if token.len() == 8 && token.chars().all(|c| c.is_ascii_digit()) {
            continue;
        }
        out.push_str(token);
    }
    out.chars()
        .filter(char::is_ascii_alphanumeric)
        .collect::<String>()
        .to_ascii_lowercase()
}

/// Finds the catalog entry matching `model`: exact key first, then the closest
/// prefix match, then a legacy fallback that drops a trailing `vN`.
#[must_use]
fn lookup_bedrock(catalog: &HashMap<String, ModelPricing>, model: &str) -> Option<ModelPricing> {
    let key = model_key(model);
    if let Some(p) = catalog.get(&key) {
        return Some(*p);
    }
    if let Some(p) = prefix_lookup(catalog, &key) {
        return Some(p);
    }
    if let Some(stripped) = strip_trailing_version(&key) {
        if let Some(p) = catalog.get(&stripped) {
            return Some(*p);
        }
        if let Some(p) = prefix_lookup(catalog, &stripped) {
            return Some(p);
        }
    }
    None
}

/// Closest catalog key where one key is a prefix of the other.
fn prefix_lookup(catalog: &HashMap<String, ModelPricing>, key: &str) -> Option<ModelPricing> {
    let mut keys: Vec<&String> = catalog.keys().collect();
    keys.sort();
    let mut best: Option<(&String, usize)> = None;
    for candidate in keys {
        if key.starts_with(candidate.as_str()) || candidate.starts_with(key) {
            let diff = candidate.len().abs_diff(key.len());
            if best.as_ref().is_none_or(|(_, d)| diff < *d) {
                best = Some((candidate, diff));
            }
        }
    }
    best.and_then(|(k, _)| catalog.get(k).copied())
}

/// Drops a trailing `v<digits>` from a compact key (`claudev2` → `claude`).
fn strip_trailing_version(key: &str) -> Option<String> {
    let idx = key.rfind('v')?;
    let rest = &key[idx + 1..];
    if !rest.is_empty() && rest.chars().all(|c| c.is_ascii_digit()) {
        Some(key[..idx].to_owned())
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::{
        apply_pricing, lookup_bedrock, model_key, parse_bedrock_offer, parse_openrouter_models,
        to_per_million, Partial,
    };
    use crate::scenario::{EndpointConfig, PricingConfig};
    use std::collections::HashMap;

    /// Builds a Bedrock catalog from synthetic offer documents.
    fn bedrock_catalog() -> HashMap<String, super::ModelPricing> {
        let offer1 = serde_json::json!({
            "products": {
                "s1": {"attributes": {"model": "Nova Lite", "feature": "On-demand Inference",
                    "inferenceType": "Input tokens", "batch": ""}},
                "s2": {"attributes": {"model": "Nova Lite", "feature": "On-demand Inference",
                    "inferenceType": "Output tokens", "batch": ""}},
                "s3": {"attributes": {"model": "Nova Lite", "feature": "On-demand Inference",
                    "inferenceType": "Input tokens flex", "batch": ""}},
                "s4": {"attributes": {"model": "Nova Lite", "feature": "Batch Inference",
                    "inferenceType": "Input tokens", "batch": "true"}}
            },
            "terms": {"OnDemand": {
                "s1": {"o": {"priceDimensions": {"d": {"unit": "1K tokens",
                    "pricePerUnit": {"USD": "0.00006"}}}}},
                "s2": {"o": {"priceDimensions": {"d": {"unit": "1K tokens",
                    "pricePerUnit": {"USD": "0.00024"}}}}},
                "s3": {"o": {"priceDimensions": {"d": {"unit": "1K tokens",
                    "pricePerUnit": {"USD": "0.00005"}}}}},
                "s4": {"o": {"priceDimensions": {"d": {"unit": "1K tokens",
                    "pricePerUnit": {"USD": "0.00003"}}}}}
            }}
        });
        let offer2 = serde_json::json!({
            "products": {
                "c1": {"attributes": {"servicename": "Claude 3.5 Sonnet v2 (Amazon Bedrock Edition)",
                    "usagetype": "USE1-MP:USE1_InputTokenCount-Units"}},
                "c2": {"attributes": {"servicename": "Claude 3.5 Sonnet v2 (Amazon Bedrock Edition)",
                    "usagetype": "USE1-MP:USE1_OutputTokenCount-Units"}},
                "c3": {"attributes": {"servicename": "Claude 3.5 Sonnet v2 (Amazon Bedrock Edition)",
                    "usagetype": "USE1-MP:USE1_CacheReadInputTokenCount-Units"}},
                "c4": {"attributes": {"servicename": "Claude 3.5 Sonnet v2 (Amazon Bedrock Edition)",
                    "usagetype": "USE1-MP:USE1_CacheWriteInputTokenCount-Units"}},
                "c5": {"attributes": {"servicename": "Claude 3.5 Sonnet v2 (Amazon Bedrock Edition)",
                    "usagetype": "USE1-MP:USE1_CacheWrite1hInputTokenCount-Units"}},
                "c6": {"attributes": {"servicename": "Claude 3.5 Sonnet v2 (Amazon Bedrock Edition)",
                    "usagetype": "USE1-MP:USE1_InputTokenCount_Global-Units"}}
            },
            "terms": {"OnDemand": {
                "c1": {"o": {"priceDimensions": {"d": {"unit": "1M tokens",
                    "pricePerUnit": {"USD": "3.0"}}}}},
                "c2": {"o": {"priceDimensions": {"d": {"unit": "1M tokens",
                    "pricePerUnit": {"USD": "15.0"}}}}},
                "c3": {"o": {"priceDimensions": {"d": {"unit": "1M tokens",
                    "pricePerUnit": {"USD": "0.3"}}}}},
                "c4": {"o": {"priceDimensions": {"d": {"unit": "1M tokens",
                    "pricePerUnit": {"USD": "3.75"}}}}},
                "c5": {"o": {"priceDimensions": {"d": {"unit": "1M tokens",
                    "pricePerUnit": {"USD": "6.0"}}}}},
                "c6": {"o": {"priceDimensions": {"d": {"unit": "1M tokens",
                    "pricePerUnit": {"USD": "99.0"}}}}}
            }}
        });
        let mut acc: HashMap<String, Partial> = HashMap::new();
        parse_bedrock_offer(&offer1, &mut acc, false);
        parse_bedrock_offer(&offer2, &mut acc, true);
        acc.into_iter()
            .filter_map(|(k, p)| p.finish().map(|mp| (k, mp)))
            .collect()
    }

    #[test]
    fn bedrock_model_key_normalizes_profiles_dates_and_revisions() {
        assert_eq!(
            model_key("us.anthropic.claude-3-5-sonnet-20241022-v2:0"),
            "claude35sonnetv2"
        );
        assert_eq!(model_key("amazon.nova-lite-v1:0"), "novalitev1");
        assert_eq!(model_key("Claude 3.5 Sonnet v2"), "claude35sonnetv2");
        assert_eq!(
            model_key("anthropic.claude-3-haiku-20240307-v1:0"),
            "claude3haikuv1"
        );
    }

    #[test]
    fn bedrock_units_convert_to_per_million() {
        assert!((to_per_million(0.003, "1K tokens").unwrap() - 3.0).abs() < 1e-9);
        assert!((to_per_million(3.0, "1M tokens").unwrap() - 3.0).abs() < 1e-9);
        assert!((to_per_million(0.000_003, "tokens").unwrap() - 3.0).abs() < 1e-9);
        assert!(to_per_million(176.0, "hour").is_none());
    }

    #[test]
    fn bedrock_offer1_skips_tier_variants() {
        let catalog = bedrock_catalog();
        let nova = catalog.get("novalite").unwrap();
        // Standard on-demand input/output only; the flex variant must not win.
        assert!(
            (nova.input_per_1m - 0.06).abs() < 1e-9,
            "got {}",
            nova.input_per_1m
        );
        assert!((nova.output_per_1m - 0.24).abs() < 1e-9);
    }

    #[test]
    fn bedrock_offer2_includes_cache_and_skips_global_and_1h() {
        let catalog = bedrock_catalog();
        let claude = catalog.get("claude35sonnetv2").unwrap();
        assert!((claude.input_per_1m - 3.0).abs() < 1e-9);
        assert!((claude.output_per_1m - 15.0).abs() < 1e-9);
        assert!((claude.cached_input_per_1m.unwrap() - 0.3).abs() < 1e-9);
        // The 1h TTL variant must not overwrite the default (5m) cache write.
        assert!((claude.cache_write_per_1m.unwrap() - 3.75).abs() < 1e-9);
    }

    #[test]
    fn bedrock_lookup_matches_full_ids_and_prefixes() {
        let catalog = bedrock_catalog();
        assert!(lookup_bedrock(&catalog, "us.anthropic.claude-3-5-sonnet-20241022-v2:0").is_some());
        assert!(lookup_bedrock(&catalog, "anthropic.claude-3-5-sonnet-20240620-v1:0").is_some());
        assert!(lookup_bedrock(&catalog, "amazon.nova-lite-v1:0").is_some());
        assert!(lookup_bedrock(&catalog, "amazon.nova-pro-v1:0").is_none());
    }

    /// Live check against the real AWS Price List (network). Run with:
    /// `cargo test --all-features bedrock_live_prices -- --ignored`
    #[tokio::test]
    #[ignore = "hits the live AWS Price List"]
    async fn bedrock_live_prices() {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .build()
            .unwrap();
        let catalog = super::fetch_bedrock_catalog(&client, "us-east-1")
            .await
            .expect("fetch live catalog");
        let claude = lookup_bedrock(&catalog, "us.anthropic.claude-3-5-sonnet-20241022-v2:0")
            .expect("claude 3.5 sonnet v2");
        assert!((claude.input_per_1m - 3.0).abs() < 1e-6, "{claude:?}");
        assert!((claude.output_per_1m - 15.0).abs() < 1e-6, "{claude:?}");
        assert!(
            (claude.cached_input_per_1m.unwrap() - 0.3).abs() < 1e-6,
            "{claude:?}"
        );
        assert!(
            (claude.cache_write_per_1m.unwrap() - 3.75).abs() < 1e-6,
            "{claude:?}"
        );
        let nova = lookup_bedrock(&catalog, "amazon.nova-lite-v1:0").expect("nova lite");
        assert!(nova.input_per_1m > 0.0, "{nova:?}");
    }

    #[test]
    fn pricing_source_defaults_to_auto() {
        use super::{source_for, Source};
        use crate::scenario::Provider;
        // Unset -> inferred from the endpoint.
        let bedrock = EndpointConfig {
            provider: Provider::Bedrock,
            ..EndpointConfig::default()
        };
        assert_eq!(source_for(&bedrock), Some(Source::Bedrock));
        let openrouter = EndpointConfig {
            url: Some("https://openrouter.ai/api/v1".to_owned()),
            ..EndpointConfig::default()
        };
        assert_eq!(source_for(&openrouter), Some(Source::OpenRouter));
        let plain = EndpointConfig {
            url: Some("https://api.openai.com/v1".to_owned()),
            ..EndpointConfig::default()
        };
        assert_eq!(source_for(&plain), None);
        // Explicit off wins even for a Bedrock endpoint.
        let off = EndpointConfig {
            provider: Provider::Bedrock,
            pricing_source: Some("off".to_owned()),
            ..EndpointConfig::default()
        };
        assert_eq!(source_for(&off), None);
    }

    #[tokio::test]
    async fn auto_marks_unsupported_provider_off() {
        let client = reqwest::Client::new();
        let mut endpoints = HashMap::new();
        endpoints.insert(
            "oai".to_owned(),
            EndpointConfig {
                url: Some("https://api.openai.com/v1".to_owned()),
                model: Some("gpt-4o".to_owned()),
                ..EndpointConfig::default()
            },
        );
        let outcome = super::apply_auto_pricing(&mut endpoints, &client).await;
        assert_eq!(outcome.priced, 0);
        assert_eq!(outcome.unsupported, vec!["oai".to_owned()]);
        assert_eq!(endpoints["oai"].pricing_source.as_deref(), Some("off"));
    }

    fn catalog() -> serde_json::Value {
        serde_json::json!({
            "data": [
                {"id": "anthropic/claude-3.5-sonnet", "pricing": {
                    "prompt": "0.000003", "completion": "0.000015",
                    "input_cache_read": "0.0000003", "input_cache_write": "0.00000375"
                }},
                {"id": "openrouter/auto", "pricing": {"prompt": "-1", "completion": "-1"}}
            ]
        })
    }

    #[test]
    fn parses_exact_match_with_cache_prices() {
        let p = parse_openrouter_models(&catalog(), "anthropic/claude-3.5-sonnet").unwrap();
        assert!((p.input_per_1m - 3.0).abs() < 1e-9);
        assert!((p.output_per_1m - 15.0).abs() < 1e-9);
        assert!((p.cached_input_per_1m.unwrap() - 0.3).abs() < 1e-9);
        assert!((p.cache_write_per_1m.unwrap() - 3.75).abs() < 1e-9);
    }

    #[test]
    fn parses_by_leaf_when_namespaced_omitted() {
        let p = parse_openrouter_models(&catalog(), "claude-3.5-sonnet").unwrap();
        assert!((p.input_per_1m - 3.0).abs() < 1e-9);
    }

    #[test]
    fn dynamic_negative_prices_are_not_usable() {
        assert!(parse_openrouter_models(&catalog(), "openrouter/auto").is_none());
    }

    #[test]
    fn unknown_model_is_none() {
        assert!(parse_openrouter_models(&catalog(), "does/not-exist").is_none());
    }

    #[test]
    fn apply_pricing_keeps_explicit_values() {
        let mut ec = EndpointConfig {
            model: Some("anthropic/claude-3.5-sonnet".to_owned()),
            pricing: Some(PricingConfig {
                input_per_1m_tokens: 9.0,
                output_per_1m_tokens: 0.0,
                ..PricingConfig::default()
            }),
            ..EndpointConfig::default()
        };
        let p = parse_openrouter_models(&catalog(), "anthropic/claude-3.5-sonnet").unwrap();
        apply_pricing(&mut ec, &p);
        let pricing = ec.pricing.unwrap();
        assert!(
            (pricing.input_per_1m_tokens - 9.0).abs() < 1e-9,
            "explicit wins"
        );
        assert!(
            (pricing.output_per_1m_tokens - 15.0).abs() < 1e-9,
            "fetched fills gap"
        );
        assert!(pricing.cached_input_per_1m_tokens.is_some());
        assert!(pricing.cache_write_per_1m_tokens.is_some());
    }
}