feat: add complete yiyezhqiu productivity suite
This commit is contained in:
commit
e422ca8b2a
155 files changed
+18959
No files matched your search
@@ -0,0 +1,314 @@
|
||||
import json
|
||||
import hashlib
|
||||
from dataclasses import replace
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.bridge import protocol
|
||||
from dx_productivity.bridge.protocol import (
|
||||
ProtocolError,
|
||||
load_apply_status,
|
||||
load_request,
|
||||
write_schedule,
|
||||
)
|
||||
from dx_productivity.domain import models
|
||||
from dx_productivity.domain.models import ApplyStep, RunPaths
|
||||
|
||||
|
||||
def test_load_request_parses_decimal_coordinates(fixture_path: Path) -> None:
|
||||
snapshot = load_request(fixture_path / "request-basic.json")
|
||||
|
||||
assert snapshot.format_version == 2
|
||||
assert snapshot.components[0].bridge_id == "c0001"
|
||||
assert snapshot.components[0].x == Decimal("125.400")
|
||||
assert snapshot.components[0].selected is True
|
||||
|
||||
|
||||
def test_load_request_rejects_unsupported_version(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["format_version"] = 3
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ProtocolError, match="format_version"):
|
||||
load_request(request)
|
||||
|
||||
|
||||
def test_load_request_rejects_duplicate_bridge_ids(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["components"][1]["bridge_id"] = "c0001"
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ProtocolError, match="duplicate bridge_id"):
|
||||
load_request(request)
|
||||
|
||||
|
||||
def test_load_request_rejects_integer_in_boolean_field(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["components"][0]["selected"] = 1
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ProtocolError, match="selected"):
|
||||
load_request(request)
|
||||
|
||||
|
||||
def test_load_request_requires_stable_or_path_identity(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["components"][0]["stable_uid"] = None
|
||||
data["components"][0]["full_path"] = ""
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ProtocolError, match="identity"):
|
||||
load_request(request)
|
||||
|
||||
|
||||
def test_load_request_preserves_empty_group_key_for_domain_validation(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["components"][0]["stable_uid"] = None
|
||||
data["components"][0]["logical_group_key"] = ""
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
snapshot = load_request(request)
|
||||
|
||||
assert snapshot.components[0].logical_group_key == ""
|
||||
|
||||
|
||||
def test_write_schedule_is_contiguous_utf8_tsv(tmp_path: Path) -> None:
|
||||
output = tmp_path / "result.tsv"
|
||||
|
||||
write_schedule(output, (ApplyStep(1, "c0001", "R?", "R1", "R1"),))
|
||||
|
||||
assert output.read_text(encoding="utf-8") == (
|
||||
"1\trefdes\tc0001\t\tR?\tR1\tR1\n"
|
||||
)
|
||||
|
||||
|
||||
def test_write_schedule_rejects_sequence_gap(tmp_path: Path) -> None:
|
||||
with pytest.raises(ProtocolError, match="contiguous"):
|
||||
write_schedule(
|
||||
tmp_path / "result.tsv",
|
||||
(ApplyStep(2, "c0001", "R?", "R1", "R1"),),
|
||||
)
|
||||
|
||||
|
||||
def test_write_schedule_rejects_more_than_two_steps_per_component(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
first = ApplyStep(1, "c0001", "R1", "R?", "R2")
|
||||
schedule = (
|
||||
first,
|
||||
replace(first, sequence=2, expected_refdes="R?", new_refdes="R3"),
|
||||
replace(first, sequence=3, expected_refdes="R3", new_refdes="R2"),
|
||||
)
|
||||
|
||||
with pytest.raises(ProtocolError, match="at most twice"):
|
||||
write_schedule(tmp_path / "result.tsv", schedule)
|
||||
|
||||
|
||||
def test_write_schedule_rejects_tsv_control_characters(tmp_path: Path) -> None:
|
||||
with pytest.raises(ProtocolError, match="tabs or line breaks"):
|
||||
write_schedule(
|
||||
tmp_path / "result.tsv",
|
||||
(ApplyStep(1, "c0001\tbad", "R?", "R1", "R1"),),
|
||||
)
|
||||
|
||||
|
||||
def test_load_apply_status_parses_optional_failure_fields(tmp_path: Path) -> None:
|
||||
status_path = tmp_path / "apply-status.json"
|
||||
status_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"format_version": 2,
|
||||
"run_id": "run-basic",
|
||||
"state": "rolled-back",
|
||||
"changed_count": 0,
|
||||
"backup_path": "C:\\Backups\\run-basic.json",
|
||||
"failing_sequence": 2,
|
||||
"error": "assignment failed",
|
||||
"rollback_state": "verified",
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
status = load_apply_status(status_path)
|
||||
|
||||
assert status.state == "rolled-back"
|
||||
assert status.failing_sequence == 2
|
||||
assert status.backup_path == Path("C:\\Backups\\run-basic.json")
|
||||
|
||||
|
||||
def test_apply_request_round_trip_preserves_unicode_absolute_backup(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
backup = (tmp_path / "备份.json").resolve()
|
||||
request = models.ApplyRequest(
|
||||
protocol.FORMAT_VERSION,
|
||||
"run-basic",
|
||||
"snapshot-basic",
|
||||
backup,
|
||||
"a" * 64,
|
||||
)
|
||||
output = tmp_path / "apply-request.json"
|
||||
|
||||
protocol.write_apply_request(output, request)
|
||||
|
||||
assert protocol.load_apply_request(output) == request
|
||||
assert "备份.json" in output.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def test_apply_request_rejects_relative_backup_path(tmp_path: Path) -> None:
|
||||
output = tmp_path / "apply-request.json"
|
||||
output.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"format_version": 2,
|
||||
"run_id": "run-basic",
|
||||
"snapshot_id": "snapshot-basic",
|
||||
"backup_path": "relative.json",
|
||||
"schedule_sha256": "a" * 64,
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(ProtocolError, match="absolute"):
|
||||
protocol.load_apply_request(output)
|
||||
|
||||
|
||||
def test_schedule_digest_matches_exact_written_utf8_bytes(tmp_path: Path) -> None:
|
||||
schedule = (ApplyStep(1, "c0001", "R?", "R1", "R1"),)
|
||||
output = tmp_path / "result.tsv"
|
||||
|
||||
protocol.write_schedule(output, schedule)
|
||||
|
||||
assert protocol.schedule_sha256(schedule) == hashlib.sha256(
|
||||
b"1\trefdes\tc0001\t\tR?\tR1\tR1\n"
|
||||
).hexdigest()
|
||||
assert hashlib.sha256(output.read_bytes()).hexdigest() == (
|
||||
protocol.schedule_sha256(schedule)
|
||||
)
|
||||
|
||||
|
||||
def test_run_paths_derives_apply_request_from_run_root(tmp_path: Path) -> None:
|
||||
paths = RunPaths(
|
||||
"run-basic",
|
||||
tmp_path,
|
||||
tmp_path / "request.json",
|
||||
tmp_path / "result.tsv",
|
||||
tmp_path / "locate.tsv",
|
||||
tmp_path / "apply-status.json",
|
||||
)
|
||||
|
||||
assert paths.apply_request == tmp_path / "apply-request.json"
|
||||
|
||||
|
||||
def test_load_request_parses_attributes_and_nets(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["components"][0]["attributes"] = [
|
||||
{"name": "Value", "value": "10K", "visible": True},
|
||||
{"name": "Part Name", "value": "RES", "visible": False},
|
||||
]
|
||||
data["nets"] = [{"name": "N1", "pages": [1, 2]}]
|
||||
data["selected_nets"] = ["N1"]
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
||||
|
||||
snapshot = load_request(request)
|
||||
|
||||
assert snapshot.components[0].attributes == (
|
||||
models.AttributeRecord(name="Value", value="10K", visible=True),
|
||||
models.AttributeRecord(name="Part Name", value="RES", visible=False),
|
||||
)
|
||||
assert snapshot.nets[0].name == "N1"
|
||||
assert snapshot.nets[0].pages == (1, 2)
|
||||
assert snapshot.selected_nets == ("N1",)
|
||||
|
||||
|
||||
def test_load_request_rejects_duplicate_net_names(
|
||||
tmp_path: Path, fixture_path: Path
|
||||
) -> None:
|
||||
data = json.loads((fixture_path / "request-basic.json").read_text("utf-8"))
|
||||
data["nets"] = [
|
||||
{"name": "N1", "pages": [1]},
|
||||
{"name": "n1", "pages": [2]},
|
||||
]
|
||||
request = tmp_path / "request.json"
|
||||
request.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ProtocolError, match="duplicate net"):
|
||||
load_request(request)
|
||||
|
||||
|
||||
def test_load_request_tolerates_missing_nets_and_attributes(
|
||||
fixture_path: Path,
|
||||
) -> None:
|
||||
snapshot = load_request(fixture_path / "request-basic.json")
|
||||
|
||||
assert snapshot.nets == ()
|
||||
assert snapshot.selected_nets == ()
|
||||
assert all(c.attributes == () for c in snapshot.components)
|
||||
|
||||
|
||||
def test_schedule_supports_property_operations(tmp_path: Path) -> None:
|
||||
output = tmp_path / "result.tsv"
|
||||
|
||||
write_schedule(
|
||||
output,
|
||||
(
|
||||
ApplyStep(
|
||||
1, "c0001", "10K", "47K", "47K",
|
||||
op=models.OpType.PROPERTY_VALUE, property_name="Value",
|
||||
),
|
||||
ApplyStep(
|
||||
2, "c0001", "1", "0", "0",
|
||||
op=models.OpType.PROPERTY_VISIBILITY, property_name="Part Name",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
text = output.read_text(encoding="utf-8")
|
||||
assert "1\tproperty\tc0001\tValue\t10K\t47K\t47K\n" in text
|
||||
assert "2\tvisibility\tc0001\tPart Name\t1\t0\t0\n" in text
|
||||
assert protocol.parse_schedule_text(text)[1].property_name == "Part Name"
|
||||
|
||||
|
||||
def test_parse_schedule_text_rejects_legacy_five_columns(tmp_path: Path) -> None:
|
||||
legacy = tmp_path / "legacy.tsv"
|
||||
legacy.write_text("1\tc0001\tR?\tR1\tR1\n", encoding="utf-8")
|
||||
|
||||
with pytest.raises(ProtocolError, match="7 columns"):
|
||||
protocol.parse_schedule_text(legacy.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def test_schedule_rejects_property_row_without_property_name(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
with pytest.raises(ProtocolError, match="property name"):
|
||||
write_schedule(
|
||||
tmp_path / "result.tsv",
|
||||
(
|
||||
ApplyStep(
|
||||
1, "c0001", "a", "b", "b",
|
||||
op=models.OpType.PROPERTY_VALUE,
|
||||
),
|
||||
),
|
||||
)
|
||||
Reference in new issue
Block a user