| """The shipped problem-id whitelist (app/data/problem_ids.json). |
| |
| Two properties matter and neither is about counting: the set has to be the real |
| canonical set (a regex would accept ids that do not exist), and the file must not |
| carry the ORACLE — era 2 removes statements and answers from the service path, |
| and this backend is the public face of the collab. |
| """ |
| from __future__ import annotations |
|
|
| import json |
|
|
| import pytest |
|
|
| from app.problems import DEFAULT_PATH, is_known_problem, problem_ids |
|
|
|
|
| def test_the_shipped_set_is_the_canonical_1669(): |
| ids = problem_ids() |
| assert len(ids) == 1669 |
| counts = { |
| prefix: sum(1 for i in ids if i.startswith(prefix + "_")) |
| for prefix in ("normal", "hard1", "hard2", "hard3") |
| } |
| assert counts == {"normal": 1000, "hard1": 69, "hard2": 200, "hard3": 400} |
|
|
|
|
| def test_ids_that_only_match_the_grammar_are_not_in_the_set(): |
| """`^(normal|hard[123])_\\d{4}$` matches every one of these; the sets do |
| not contain any of them. This is why the whitelist ships as data.""" |
| for absent in ("hard1_0070", "hard2_0201", "hard3_0401", "normal_1001", "normal_0000"): |
| assert not is_known_problem(absent), absent |
| for present in ("normal_0001", "normal_1000", "hard1_0069", "hard2_0141", "hard3_0400"): |
| assert is_known_problem(present), present |
|
|
|
|
| def test_the_shipped_file_carries_no_statements_and_no_answers(): |
| doc = json.loads(DEFAULT_PATH.read_text()) |
| assert sorted(doc) == ["count", "ids", "provenance"] |
| assert all(isinstance(i, str) for i in doc["ids"]) |
| |
| assert sorted(doc["provenance"]["files"]) == [ |
| "hard1.jsonl", "hard2.jsonl", "hard3.jsonl", "normal.jsonl" |
| ] |
| for name, meta in doc["provenance"]["files"].items(): |
| assert len(meta["sha256"]) == 64, name |
|
|
|
|
| def test_an_overridden_path_is_honoured_and_a_broken_one_refuses_to_load(tmp_path): |
| good = tmp_path / "ids.json" |
| good.write_text(json.dumps({"count": 2, "ids": ["normal_0001", "hard2_0141"]})) |
| assert problem_ids(str(good)) == frozenset({"normal_0001", "hard2_0141"}) |
| assert is_known_problem("hard2_0141", str(good)) |
| assert not is_known_problem("hard3_0400", str(good)) |
|
|
| |
| lying = tmp_path / "lying.json" |
| lying.write_text(json.dumps({"count": 99, "ids": ["normal_0001"]})) |
| with pytest.raises(ValueError): |
| problem_ids(str(lying)) |
|
|
| empty = tmp_path / "empty.json" |
| empty.write_text(json.dumps({"count": 0, "ids": []})) |
| with pytest.raises(ValueError): |
| problem_ids(str(empty)) |
|
|
| with pytest.raises(FileNotFoundError): |
| problem_ids(str(tmp_path / "nope.json")) |
|
|