Files
paperspec/tests/test_public_api.py
T
alexmandClaude 4fb8b4f5a7 Add paperspec correct, check math forms and datasets, and give the tools public names
The work the other paper tools planned for paperspec (paperdesk PLAN M5.4,
paperform PLAN 4.2):

- paperspec correct and paperspec.revision apply proposed corrections to a
  package, folder or zip, and write it only once it validates.
- doc.math.form is checked: label and style commands always, the form's own
  spelling when papermath is installed or forms= is passed.
- paperspec.datasets and seven doc.dataset.* rules check a package's datasets.
- fixtures/invalid gains suspect-witness and math-form-label (33 in all).
- Canonical JSON reads numbers as doubles, so 1e20 round-trips.
- The registries and the JSON-LD context ship in the wheel.
- Public names for what paperdesk and paperform imported as private ones,
  with the old names kept as aliases.
- The text and lossy-projection functions are about three times faster
  through the gate, tested equal to the old code on every code point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013XwDhn4LkrKQfw4ArDEr2E
2026-10-20 22:10:17 -04:00

115 lines
4.1 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Scienting
#
# Source Code: https://git.scient.ing/papers/paperspec
# See the LICENSE.md file for full license terms.
"""The public names other tools import instead of the private ones they once reached for."""
import zipfile
import pytest
from helpers_cli import HALE, TINY, TINY_ZIP, read_package
from paperspec import corrections, hashing, package, tree
from paperspec.model import Doc
from paperspec.tree import Refusal
def test_parse_canonical_reads_doubles_and_refuses_what_is_not_canonical():
value = package.parse_canonical(b'{"a":100000000000000000000}', "x")
assert value == {"a": 1e20} and isinstance(value["a"], float)
assert package._canonical_bytes is package.parse_canonical
with pytest.raises(Refusal) as e:
package.parse_canonical(b'{ "a":1}', "x:1")
assert e.value.rule == "package.file.canonical"
def test_parse_json_needs_one_final_newline():
assert package.parse_json(b'{"a":1}\n', "a.json") == {"a": 1}
for data in (b'{"a":1}', b'{"a":1}\n\n'):
with pytest.raises(Refusal):
package.parse_json(data, "a.json")
def test_parse_jsonl_names_the_line_at_fault():
assert package.parse_jsonl(b"", "a.jsonl") == []
assert package.parse_jsonl(b'{"a":1}\n{"b":2}\n', "a.jsonl") == [{"a": 1}, {"b": 2}]
with pytest.raises(Refusal) as e:
package.parse_jsonl(b'{"a":1}\n{"b": 2}\n', "a.jsonl")
assert "a.jsonl:2" in e.value.message
with pytest.raises(Refusal) as e:
package.parse_jsonl(b'{"a":1}', "a.jsonl")
assert e.value.rule == "package.file.newline"
def test_read_zip_files_holds_the_package_as_its_folder_does():
key, files = package.read_zip_files(TINY_ZIP)
assert key == TINY.name
# read_package leaves the manifest out of the files it lists.
assert files.pop("MANIFEST.json") == (TINY / "MANIFEST.json").read_bytes()
assert files == read_package(TINY)[0]
def test_read_zip_files_counts_every_byte_against_the_limit():
with pytest.raises(Refusal) as e:
package.read_zip_files(TINY_ZIP, limit=10)
assert e.value.rule == "package.zip.size"
def test_read_zip_files_refuses_what_is_not_a_zip(tmp_path):
bad = tmp_path / "bad.sct.zip"
bad.write_bytes(b"not a zip")
with pytest.raises(Refusal) as e:
package.read_zip_files(bad)
assert e.value.rule == "package.zip.entry"
def test_check_zip_entries_returns_the_folder_without_reading():
with zipfile.ZipFile(TINY_ZIP) as zf:
assert package.check_zip_entries(zf) == TINY.name
assert package._zip_entries is package.check_zip_entries
def test_flatten_keeps_private_keys_and_leaves_the_spec_alone():
spec = {
"kind": "document",
"_name": "root",
"children": [{"kind": "section", "children": [], "_name": "s"}],
}
flat = tree.flatten(spec)
assert [(f["kind"], f.get("_parent"), f["ord"], f["_name"]) for f in flat] == [
("document", None, 0, "root"),
("section", 0, 0, "s"),
]
assert "children" in spec and "_parent" not in spec
def test_establish_with_side_returns_the_private_keys_by_minted_id():
spec = {
"kind": "document",
"children": [
{"kind": "section", "_name": "intro", "children": []},
{"kind": "section", "children": []},
],
}
nodes, side = tree.establish_with_side("doc2026x-abcd", spec)
assert nodes == tree.establish("doc2026x-abcd", spec)
assert not any(k.startswith("_") for n in nodes for k in n)
assert [side[n["id"]] for n in nodes] == [{}, {"_name": "intro"}, {}]
def test_strict_runs_is_the_strict_projection_of_a_leaf():
nodes = package.parse_jsonl((HALE / "nodes.jsonl").read_bytes(), "nodes.jsonl")
doc = Doc(HALE.name, nodes)
leaves = [n for n in nodes if isinstance(n.get("content"), list)]
assert leaves
for n in leaves:
assert hashing.strict_runs(n["content"], doc) == hashing.strict(doc, n["id"])
def test_the_operations_and_emend_kinds_are_the_schemas():
assert len(set(corrections.OPS)) == len(corrections.OPS) == 12
assert {"emend", "relink", "resolve"} <= set(corrections.OPS)
assert "typo" in corrections.EMEND_KINDS