Files
cookbook/tools/tests/test_cookbook.py
T
ClaudeandClaude Opus 5.5 562c7f2fb8
ci / check (push) Successful in 10s
One folder per GPU, with a GPU index in the top-level README
Recipes, tried logs and result folders move under <gpu>/: recipes at
<gpu>/<model>/<engine>-<profile>.md, tried logs at <gpu>/<model>/tried.md,
runs at <gpu>/results/<run>/. build generates a README for every GPU in
cookbook.toml and the GPU table in README.md. validate checks that a run
sits in the folder of the GPU it ran on and warns about stray top-level
folders. Format docs and host setup move to docs/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 15:38:54 +01:00

148 lines
5.4 KiB
Python

"""cookbook.py: command matching, generated blocks, Markdown, and validate on
copies of this repo with one thing broken."""
import json
import os
import shutil
import sys
from pathlib import Path
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import cookbook # noqa: E402
RECIPE = "arc-pro-b65/qwen3.8-27b/vllm-single-user.md"
def test_command_words_ignores_layout_and_comments():
a = "docker run --rm \\\n -e A=1 img \\\n --flag 'x y'\n"
b = "# start it\ndocker run --rm -e A=1 img --flag 'x y'"
assert cookbook.command_words(a) == cookbook.command_words(b)
assert cookbook.command_words("x --flag 1") != cookbook.command_words("x --flag 2")
def test_command_words_redacts_like_bench_sh():
assert cookbook.command_words("HF_TOKEN=abc vllm serve m --api-key s3cret") == \
["HF_TOKEN=<redacted>", "vllm", "serve", "m", "--api-key", "<redacted>"]
def test_slug_matches_bench_sh():
assert cookbook.slug("Arc Pro B65") == "arc-pro-b65"
assert cookbook.slug("llama.cpp") == "llama.cpp"
assert cookbook.slug("Qwen3.8-27B") == "qwen3.8-27b"
def test_fill_replaces_only_the_block():
text = "a\n<!-- cookbook:x -->\nold\n<!-- /cookbook:x -->\nb\n"
assert cookbook.fill(text, "x", "new") == "a\n<!-- cookbook:x -->\nnew\n<!-- /cookbook:x -->\nb\n"
with pytest.raises(ValueError):
cookbook.fill(text, "y", "new")
def test_launch_block_takes_the_first_sh_block_under_launch():
body = "## Requirements\n```sh\nnot this\n```\n## Launch\ntext\n```sh\nthis\n```\n```sh\nnor this\n```\n"
assert cookbook.launch_block(body) == "this\n"
assert cookbook.launch_block("## Other\n```sh\nx\n```\n") is None
def test_md_to_html():
out = cookbook.md_to_html("## Head\n\n| a | b |\n|---|---|\n| `x` | [l](u.md) |\n\n- one\n- **two**\n\n"
"```sh\n<cmd>\n```\n", link=lambda u: "L:" + u)
assert '<h2 id="head">Head</h2>' in out
assert "<td><code>x</code></td>" in out and '<a href="L:u.md">l</a>' in out
assert "<ul><li>one</li><li><strong>two</strong></li></ul>" in out
assert "<pre><code>&lt;cmd&gt;</code></pre>" in out
# ---------------------------------------------------------------- the repo itself
def test_repo_validates_and_is_built():
cb = cookbook.Cookbook()
assert cb.errors == []
for path, text in cb.outputs().items():
assert path.read_text(encoding="utf-8") == text, f"{path} is out of date: run cookbook.py build"
def test_verified_needs_two_submitters():
cb = cookbook.Cookbook()
status = {r["id"]: r["status"] for r in cb.recipes}
assert status["arc-pro-b65/qwen3.8-27b/vllm-single-user"] == "verified by 2"
assert status["arc-pro-b65/qwen3.8-27b/vllm-throughput"] == "one submitter"
# ---------------------------------------------------------------- broken copies
@pytest.fixture
def repo(tmp_path):
root = tmp_path / "cookbook"
shutil.copytree(cookbook.ROOT, root, ignore=shutil.ignore_patterns(".git", "__pycache__", "*.html"))
for d in root.glob("*/results/*"): # validate only needs the reports to exist
if d.is_dir():
(d / f"{d.name}.html").touch()
return root
def edit(path, old, new):
text = path.read_text(encoding="utf-8")
assert old in text
path.write_text(text.replace(old, new, 1), encoding="utf-8")
def errors(root):
return "\n".join(cookbook.Cookbook(root).errors)
def test_copy_is_clean(repo):
assert errors(repo) == ""
def test_quick_run_cannot_back_a_recipe(repo):
edit(repo / RECIPE, '"261026_demo-2_qwen3.8-27b_vllm_1x-arc-pro-b65"',
'"261022_demo_qwen3.8-27b_vllm_1x-arc-pro-b65_quick_2"')
assert "is a quick run" in errors(repo)
def test_declared_values_must_match(repo):
edit(repo / RECIPE, 'kv = "fp16"', 'kv = "fp8"')
assert "kv is 'fp8' in the recipe but 'fp16' in the run" in errors(repo)
def test_launch_command_must_be_the_benchmarked_one(repo):
edit(repo / RECIPE, "--max-num-seqs 1", "--max-num-seqs 2")
assert "server-cmd.txt is not the recipe's launch command" in errors(repo)
def test_only_arc_gpus(repo):
edit(repo / RECIPE, 'gpu = "Arc Pro B65"', 'gpu = "RTX 4090"')
e = errors(repo)
assert "not an Intel Arc GPU" in e and "should be rtx-4090/" in e
def test_edited_results_are_caught(repo):
d = repo / "arc-pro-b65" / "results" / "261022_demo_qwen3.8-27b_vllm_1x-arc-pro-b65"
meta = json.loads((d / "meta.json").read_text(encoding="utf-8"))
meta["notes"]["spec"] = "none"
(d / "meta.json").write_text(json.dumps(meta), encoding="utf-8")
assert "differ (edited?)" in errors(repo)
def test_placeholder_digest_only_for_demo(repo):
(repo / "arc-pro-b65" / "results" / "261019_demo_qwen3.5-9b_llama.cpp_1x-arc-pro-b65" / "DEMO").unlink()
e = errors(repo)
assert "all-zero placeholder" in e
def test_tried_needs_an_existing_result(repo):
edit(repo / "arc-pro-b65/qwen3.5-9b/tried.md", "_quick_2", "_quick_9")
assert "is not in results/" in errors(repo)
def test_result_must_sit_in_its_gpu_folder(repo):
run = "261024_demo_qwen3.5-9b_llama.cpp_1x-arc-b580"
shutil.move(repo / "arc-b580" / "results" / run, repo / "arc-pro-b65" / "results" / run)
e = errors(repo)
assert "but it is in the Arc Pro B65 folder" in e and f"result {run} is not in results/" not in e
def test_stray_top_level_folder_warns(repo):
(repo / "arc-b999").mkdir()
assert any("arc-b999: not a GPU" in w for w in cookbook.Cookbook(repo).warnings)