from __future__ import annotations import json import pytest from shiftedx_bench.model_card import ( END_MARKER, START_MARKER, render_benchmark_block, update_model_card, validate_post_publish_run, ) MODEL_REPO = "Shiftedx/example-quant" MODEL_REVISION = "a" * 40 BENCHMARK_REVISION = "b" * 40 VARIANT = "example" def _write_run(tmp_path, *, omit_lane=None): counts = {"long-context": 15, "quality": 10, "tools": 6, "agentic": 2, "vision": 4} names = {"long-context": "context", "quality": "quality", "tools": "tools", "agentic": "agentic", "vision": "vision"} for lane, count in counts.items(): if lane == omit_lane: count -= 1 rows = [] for index in range(count): metadata = {} if lane == "long-context": metadata = { "target_prompt_tokens": [4096, 16384, 65536, 131072][min(index // 4, 3)], "requested_position": 0.5, "family": "single_key", } rows.append( { "schema_version": "1.0", "variant": VARIANT, "lane": lane, "case_id": f"{lane}-{index}", "passed": index != count - 1 if lane == "quality" else True, "metadata": metadata, "telemetry": { "wall_s": 2.0, "decode_tokens_per_second": 20.0, "active_memory_bytes": 16 * 1024 ** 3, }, } ) (tmp_path / f"{names[lane]}.jsonl").write_text( "".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8" ) manifest = { "benchmark_version": "0.3.0", "variants": [{"label": VARIANT, "request_overrides": {}}], "evaluated_artifact": {"repo_id": MODEL_REPO, "revision": MODEL_REVISION}, "benchmark_source": { "repo_id": "Shiftedx/shiftedx-bench", "revision": BENCHMARK_REVISION, }, } (tmp_path / f"{names[lane]}.manifest.json").write_text(json.dumps(manifest), encoding="utf-8") def test_post_publish_model_card_block_is_complete_and_idempotent(tmp_path): _write_run(tmp_path) rows, version = validate_post_publish_run( tmp_path, variant=VARIANT, profile="vision", model_repo=MODEL_REPO, model_revision=MODEL_REVISION, benchmark_revision=BENCHMARK_REVISION, ) block = render_benchmark_block( rows, variant=VARIANT, model_repo=MODEL_REPO, model_revision=MODEL_REVISION, benchmark_revision=BENCHMARK_REVISION, benchmark_version=version, host="Apple Silicon, 64 GiB", runtime="MTPLX 2.7.1, reasoning on/medium, native tools", kv_cache="off", mtp_depth="3", max_window_status="host-limited", ) readme = "# Example\n\n## Provenance and integrity\n" first = update_model_card(readme, block) second = update_model_card(first, block) assert first == second assert first.count(START_MARKER) == first.count(END_MARKER) == 1 assert "15/15" in first assert "host watchdog" in first assert MODEL_REVISION in first and BENCHMARK_REVISION in first def test_model_card_export_rejects_incomplete_or_wrong_revision(tmp_path): _write_run(tmp_path, omit_lane="vision") with pytest.raises(ValueError, match="Incomplete"): validate_post_publish_run( tmp_path, variant=VARIANT, profile="vision", model_repo=MODEL_REPO, model_revision=MODEL_REVISION, benchmark_revision=BENCHMARK_REVISION, ) with pytest.raises(ValueError, match="provenance mismatch"): validate_post_publish_run( tmp_path, variant=VARIANT, profile="text", model_repo=MODEL_REPO, model_revision="c" * 40, benchmark_revision=BENCHMARK_REVISION, ) def test_model_card_renders_null_effective_context_as_not_established(tmp_path): _write_run(tmp_path) context_path = tmp_path / "context.jsonl" rows = [json.loads(line) for line in context_path.read_text().splitlines()] for row in rows: row["passed"] = False context_path.write_text( "".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8" ) validated, version = validate_post_publish_run( tmp_path, variant=VARIANT, profile="vision", model_repo=MODEL_REPO, model_revision=MODEL_REVISION, benchmark_revision=BENCHMARK_REVISION, ) block = render_benchmark_block( validated, variant=VARIANT, model_repo=MODEL_REPO, model_revision=MODEL_REVISION, benchmark_revision=BENCHMARK_REVISION, benchmark_version=version, host="Apple Silicon, 64 GiB", runtime="MTPLX 2.7.1", kv_cache="off", mtp_depth="2", max_window_status="not-run", ) assert "effective tested context: not established at the benchmark's 90% threshold" in block