feat: add complete yiyezhqiu productivity suite

This commit is contained in:
阿白叔 committed 2026-10-10 17:10:32 +08:00
commit e422ca8b2a
155 files changed
+18959

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
from pathlib import Path
from dx_productivity.bridge import launcher
def test_compute_assembly_refdes_end_to_end(tmp_path: Path) -> None:
request = tmp_path / "request.txt"
output = tmp_path / "plan.txt"
request.write_text(
"T|0|1|1|1|C1|0|C1\n"
"T|1|2|1|1|NOTE|0|C1\n"
"T|2|1|1|1|R100|90|R100\n"
"C|C1|10|20|0.5|0.3\n"
"C|R100|5|6|0.4|0.9\n",
encoding="ascii",
)
exit_code = launcher.main(
[
"compute",
"assembly-refdes",
"--request",
str(request),
"--output",
str(output),
]
)
assert exit_code == 0
plan = output.read_text(encoding="ascii")
assert "MOVE|0|10.0000|20.0000|0.00|0.3000|Arial\n" in plan
assert "DELETE|1\n" in plan
assert "MOVE|2|5.0000|6.0000|90.00|0.4000|Arial\n" in plan
+105
View File
@@ -0,0 +1,105 @@
import json
import os
from pathlib import Path
import subprocess
import sys
def _environment() -> dict[str, str]:
environment = os.environ.copy()
environment["QT_QPA_PLATFORM"] = "offscreen"
return environment
def test_module_launcher_dispatches_probe_without_starting_qt(tmp_path: Path) -> None:
output = tmp_path / "兼容性 结果.json"
result = subprocess.run(
[
sys.executable,
"-m",
"dx_productivity",
"probe",
"--output",
str(output),
"--route",
"named",
"--payload-json",
'{"portable":true}',
],
capture_output=True,
text=True,
encoding="utf-8",
env=_environment(),
timeout=15,
check=False,
)
assert result.returncode == 0, result.stderr
assert json.loads(output.read_text(encoding="utf-8")) == {
"format_version": 1,
"route": "named",
"portable": True,
}
def test_module_launcher_runs_ui_smoke_test_and_exits(
fixture_path: Path,
) -> None:
result = subprocess.run(
[
sys.executable,
"-m",
"dx_productivity",
"ui",
"--request",
str(fixture_path / "request-basic.json"),
"--smoke-test-ms",
"50",
],
capture_output=True,
text=True,
encoding="utf-8",
env=_environment(),
timeout=15,
check=False,
)
assert result.returncode == 0, result.stderr
def test_module_launcher_dispatches_update_without_starting_qt(tmp_path: Path) -> None:
import sys
import zipfile
current = tmp_path / "portable"
current.mkdir()
(current / "DxDesignerTools.exe").write_bytes(b"old")
staged = tmp_path / "update.zip"
with zipfile.ZipFile(staged, "w") as archive:
archive.writestr("DxDesignerTools.exe", b"new")
plan = tmp_path / "install-plan.json"
plan.write_text(
json.dumps(
{
"current_root": str(current),
"staged_zip": str(staged),
"backup_root": str(tmp_path / "backups"),
"restart_command": [sys.executable, "-c", "raise SystemExit(0)"],
}
),
encoding="utf-8",
)
result = subprocess.run(
[sys.executable, "-m", "dx_productivity", "update", "--plan", str(plan)],
capture_output=True,
text=True,
encoding="utf-8",
env=_environment(),
timeout=15,
check=False,
)
assert result.returncode == 0, result.stderr
assert (current / "DxDesignerTools.exe").read_bytes() == b"new"
+314
View File
@@ -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,
),
),
)
+106
View File
@@ -0,0 +1,106 @@
import json
from pathlib import Path
from dx_productivity.bridge.run_store import RunStore
from dx_productivity.domain.models import ApplyStatus
def test_create_run_returns_isolated_mailbox_paths(tmp_path: Path) -> None:
paths = RunStore(tmp_path).create_run()
assert paths.root.is_dir()
assert paths.root.parent == tmp_path / "runs"
assert paths.request == paths.root / "request.json"
assert paths.result == paths.root / "result.tsv"
assert paths.locate == paths.root / "locate.tsv"
assert paths.apply_status == paths.root / "apply-status.json"
def test_backup_contains_complete_old_and_new_mapping(
tmp_path, snapshot, plan
):
store = RunStore(tmp_path)
path = store.write_backup(snapshot, plan)
data = json.loads(path.read_text(encoding="utf-8"))
assert data["format_version"] == 2
assert data["design_id"] == snapshot.design_id
assert data["changes"][0] == {
"bridge_id": "c0001",
"stable_uid": "uid-c0001",
"full_path": "ROOT\\C0001",
"op": "refdes",
"property": "",
"old_value": "R?",
"new_value": "R1",
}
assert not list(path.parent.glob("*.tmp"))
def test_history_ignores_incomplete_temporary_files(tmp_path):
store = RunStore(tmp_path)
store.history_root.mkdir(parents=True)
(store.history_root / "orphan.tmp").write_text("partial", encoding="utf-8")
assert store.list_history("design-a") == ()
def test_record_status_creates_filterable_history(tmp_path, snapshot, plan):
store = RunStore(tmp_path)
backup = store.write_backup(snapshot, plan)
status = ApplyStatus(
format_version=1,
run_id=snapshot.run_id,
state="applied",
changed_count=1,
backup_path=backup,
)
store.record_status(status)
entries = store.list_history(snapshot.design_id)
assert len(entries) == 1
assert entries[0].state == "applied"
assert entries[0].backup_path == backup
assert store.list_history("other-design") == ()
def test_restore_request_contains_identity_preflight_and_old_value(
tmp_path, snapshot, plan
):
store = RunStore(tmp_path)
backup = store.write_backup(snapshot, plan)
status = ApplyStatus(1, snapshot.run_id, "applied", 1, backup)
store.record_status(status)
entry = store.list_history(snapshot.design_id)[0]
request = store.build_restore_settings(entry)
assert request.design_id == snapshot.design_id
assert request.source_backup == backup
assert len(request.changes) == 1
assert request.changes[0].bridge_id == "c0001"
assert request.changes[0].stable_uid == "uid-c0001"
assert request.changes[0].expected_value == "R1"
assert request.changes[0].restore_value == "R?"
assert request.changes[0].op.value == "refdes"
def test_backup_filename_does_not_contain_design_path_separators(
tmp_path, snapshot, plan
):
store = RunStore(tmp_path)
snapshot = type(snapshot)(
snapshot.format_version,
snapshot.run_id,
snapshot.snapshot_id,
"board:power/logic",
snapshot.design_path,
snapshot.components,
)
backup = store.write_backup(snapshot, plan)
assert "board_power_logic" in backup.name
assert ":" not in backup.name
+31
View File
@@ -0,0 +1,31 @@
from pathlib import Path
def test_generate_startup_assets_lists_all_feature_actions() -> None:
from dx_productivity.bridge.startup_assets import generate_startup_assets
assets = generate_startup_assets(Path("C:/portable/DxDesignerTools"))
assert set(assets.action_scripts) == {
"refdes",
"property-change",
"property-visibility",
"inspection",
"bom",
"compatibility",
"guide",
}
assert assets.action_scripts["refdes"] == "DxToolsOpenRefDes"
assert (
assets.action_scripts["property-change"]
== "DxToolsOpenPropertyChange"
)
assert assets.action_scripts["bom"] == "DxToolsOpenBom"
assert assets.action_scripts["compatibility"] == "DxToolsOpenCompatibility"
assert assets.action_scripts["guide"] == "DxToolsOpenGuide"
assert all(
"D:\\NET_CHECK" not in value for value in assets.action_scripts.values()
)
assert not any(
value.startswith("run ") for value in assets.action_scripts.values()
)