package._canonical_bytes, package._zip_entries, anchors._follow and traces._anchors are gone; paperdesk and paperforms use the public names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YCVvg4F3wpYQjq1NQUSmxN
115 lines
4.2 KiB
Python
115 lines
4.2 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 not hasattr(package, "_canonical_bytes") # no alias for the old name
|
|
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 not hasattr(package, "_zip_entries") # no alias for the old name
|
|
|
|
|
|
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
|