dpc-tau-cli 0.2.1

A minimal Unix-first coding agent.
Documentation
use super::*;

/// Preserves availability and independently compact-formats self and subtree
/// costs so an absent stats snapshot never appears as a known zero.
#[test]
fn compact_cost_pairs_preserve_component_availability() {
    let self_cost = EstimatedApiCost::from_picodollars(3 * PICODOLLARS_PER_CENT);
    let subtree_cost = EstimatedApiCost::from_picodollars(21 * PICODOLLARS_PER_CENT);
    assert_eq!(
        format_snapshot(Some(AgentCostSnapshot::new(self_cost, subtree_cost))),
        "$.03/$.21"
    );
    assert_eq!(format_snapshot(None), "-/-");
    assert_eq!(format_compact_pair(Some(self_cost), None), "$.03/-");
    assert_eq!(format_compact_pair(None, Some(subtree_cost)), "-/-");
}

fn dollars(value: u64) -> EstimatedApiCost {
    EstimatedApiCost::from_picodollars(value.saturating_mul(PICODOLLARS_PER_DOLLAR))
}

/// Zero and sub-dollar estimates omit the leading zero while retaining
/// cents.
#[test]
fn compact_cost_formats_zero_and_cents() {
    assert_eq!(format_compact(EstimatedApiCost::default()), "$.00");
    assert_eq!(
        format_compact(EstimatedApiCost::from_picodollars(3 * PICODOLLARS_PER_CENT)),
        "$.03"
    );
}

/// Exact half-up boundaries promote instead of overflowing a precision
/// band.
#[test]
fn compact_cost_rounding_transitions_are_deterministic() {
    let just_below_one = EstimatedApiCost::from_picodollars(
        99 * PICODOLLARS_PER_CENT + PICODOLLARS_PER_CENT / 2 - 1,
    );
    let rounds_to_one =
        EstimatedApiCost::from_picodollars(99 * PICODOLLARS_PER_CENT + PICODOLLARS_PER_CENT / 2);
    assert_eq!(format_compact(just_below_one), "$.99");
    assert_eq!(format_compact(rounds_to_one), "$1.0");

    assert_eq!(
        format_compact(EstimatedApiCost::from_picodollars(
            99 * PICODOLLARS_PER_TENTH_DOLLAR + PICODOLLARS_PER_TENTH_DOLLAR / 2
        )),
        "$10"
    );
    assert_eq!(
        format_compact(EstimatedApiCost::from_picodollars(
            999 * PICODOLLARS_PER_DOLLAR + PICODOLLARS_PER_DOLLAR / 2
        )),
        "$1k"
    );

    for (cost, expected) in [
        (
            EstimatedApiCost::from_picodollars(
                99 * PICODOLLARS_PER_THOUSAND_DOLLARS + PICODOLLARS_PER_THOUSAND_DOLLARS / 2 - 1,
            ),
            "$99k",
        ),
        (
            EstimatedApiCost::from_picodollars(
                99 * PICODOLLARS_PER_THOUSAND_DOLLARS + PICODOLLARS_PER_THOUSAND_DOLLARS / 2,
            ),
            "$.1m",
        ),
        (
            EstimatedApiCost::from_picodollars(
                9 * PICODOLLARS_PER_TENTH_MILLION_DOLLARS
                    + PICODOLLARS_PER_TENTH_MILLION_DOLLARS / 2
                    - 1,
            ),
            "$.9m",
        ),
        (
            EstimatedApiCost::from_picodollars(
                9 * PICODOLLARS_PER_TENTH_MILLION_DOLLARS
                    + PICODOLLARS_PER_TENTH_MILLION_DOLLARS / 2,
            ),
            "$1m",
        ),
    ] {
        let rendered = format_compact(cost);
        assert_eq!(rendered, expected);
        assert!(rendered.chars().count() <= 4);
    }
}

/// Large estimates retain a scale suffix without exceeding the strict
/// width.
#[test]
fn compact_cost_formats_large_values_within_four_columns() {
    for (cost, expected) in [
        (dollars(23), "$23"),
        (dollars(12_400), "$12k"),
        (dollars(120_000), "$.1m"),
        (dollars(2_400_000), "$2m"),
        (EstimatedApiCost::from_picodollars(u64::MAX), "$18m"),
    ] {
        let rendered = format_compact(cost);
        assert_eq!(rendered, expected);
        assert!(rendered.chars().count() <= 4);
    }
}