Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
175 lines
5.9 KiB
Python
175 lines
5.9 KiB
Python
from pathlib import Path
|
|
|
|
from cbb_refresh_verify.batch_status import (BatchCheckpoint, BatchStatusSession,
|
|
fingerprint_library)
|
|
from cbb_refresh_verify.status_bridge import BridgeResponseError, BridgeResult
|
|
|
|
|
|
def test_fingerprint_changes_when_lmc_changes(tmp_path):
|
|
lmc = tmp_path / "mentor_lib.lmc"
|
|
lmc.write_bytes(b"one")
|
|
first = fingerprint_library(lmc)
|
|
lmc.write_bytes(b"two")
|
|
second = fingerprint_library(lmc)
|
|
assert first.sha256 != second.sha256
|
|
|
|
|
|
def test_fingerprint_preserves_mapped_drive_without_resolve(tmp_path, monkeypatch):
|
|
lmc = tmp_path / 'mentor_lib.lmc'
|
|
lmc.write_bytes(b'library')
|
|
monkeypatch.setattr(
|
|
Path,
|
|
'resolve',
|
|
lambda self: (_ for _ in ()).throw(AssertionError(f'resolve called: {self}')),
|
|
)
|
|
|
|
assert fingerprint_library(lmc).sha256
|
|
|
|
|
|
def test_fingerprint_changes_when_top_level_block_set_changes(tmp_path):
|
|
lmc = tmp_path / "mentor_lib.lmc"
|
|
lmc.write_bytes(b"library")
|
|
root = tmp_path / "ReusableBlocksLP"
|
|
(root / "CBB_A").mkdir(parents=True)
|
|
first = fingerprint_library(lmc)
|
|
(root / "CBB_B").mkdir()
|
|
second = fingerprint_library(lmc)
|
|
assert first.sha256 != second.sha256
|
|
|
|
|
|
def test_fingerprint_does_not_walk_cbb_descendants(tmp_path, monkeypatch):
|
|
import cbb_refresh_verify.batch_status as batch_status
|
|
|
|
lmc = tmp_path / "mentor_lib.lmc"
|
|
lmc.write_bytes(b"library")
|
|
root = tmp_path / "ReusableBlocksLP"
|
|
(root / "CBB_A" / "Work").mkdir(parents=True)
|
|
real_scandir = batch_status.os.scandir
|
|
|
|
def guarded_scandir(path):
|
|
if Path(path) != root:
|
|
raise AssertionError(f"descendant scan: {path}")
|
|
return real_scandir(path)
|
|
|
|
monkeypatch.setattr(batch_status.os, "scandir", guarded_scandir)
|
|
assert fingerprint_library(lmc).sha256
|
|
|
|
|
|
def test_checkpoint_ignores_truncated_json_line(tmp_path):
|
|
path = tmp_path / "scan.jsonl"
|
|
path.write_text('{"block_name":"A","status":"READY"}\n{"block_name":"B"', encoding="utf-8")
|
|
checkpoint = BatchCheckpoint.load(path)
|
|
assert checkpoint.completed_names == {"A"}
|
|
|
|
|
|
class FakeBridge:
|
|
def __init__(self, timeout_name=None):
|
|
self.requests = []
|
|
self.timeout_name = timeout_name
|
|
|
|
def query(self, lmc_file, block_name, *, block_type="RULP", timeout_seconds=10):
|
|
self.requests.append(block_name)
|
|
if block_name == self.timeout_name:
|
|
raise BridgeResponseError("状态桥响应超时")
|
|
return BridgeResult("READY" if block_name == "A" else "PENDING", "fake")
|
|
|
|
|
|
def test_scan_reuses_checkpoint_and_queries_only_missing(tmp_path):
|
|
lmc = tmp_path / "mentor_lib.lmc"
|
|
lmc.write_bytes(b"library")
|
|
checkpoint = tmp_path / "scan.jsonl"
|
|
fingerprint = fingerprint_library(lmc).sha256
|
|
checkpoint.write_text(
|
|
'{"block_name":"A","status":"READY","fingerprint":"' + fingerprint + '"}\n',
|
|
encoding="utf-8",
|
|
)
|
|
bridge = FakeBridge()
|
|
records = BatchStatusSession(bridge).scan(["A", "B"], lmc, checkpoint)
|
|
assert bridge.requests == ["B"]
|
|
assert {record.block_name for record in records} == {"A", "B"}
|
|
|
|
|
|
def test_timeout_isolated_and_later_item_continues(tmp_path):
|
|
lmc = tmp_path / "mentor_lib.lmc"
|
|
lmc.write_bytes(b"library")
|
|
bridge = FakeBridge(timeout_name="A")
|
|
records = BatchStatusSession(bridge, timeout_seconds=0.1).scan(
|
|
["A", "B"], lmc, tmp_path / "scan.jsonl")
|
|
assert records[0].status == "TIMEOUT"
|
|
assert records[1].status == "PENDING"
|
|
|
|
|
|
def test_protocol_path_mismatch_stops_repeating_same_global_failure(tmp_path):
|
|
class BrokenProtocolBridge:
|
|
def __init__(self):
|
|
self.requests = []
|
|
|
|
def query(self, lmc_file, block_name, **kwargs):
|
|
self.requests.append(block_name)
|
|
raise BridgeResponseError('状态桥中心库路径不匹配')
|
|
|
|
lmc = tmp_path / 'mentor_lib.lmc'
|
|
lmc.write_bytes(b'library')
|
|
bridge = BrokenProtocolBridge()
|
|
|
|
records = BatchStatusSession(bridge).scan(
|
|
['A', 'B', 'C'], lmc, tmp_path / 'scan.jsonl'
|
|
)
|
|
|
|
assert bridge.requests == ['A']
|
|
assert [record.status for record in records] == ['MALFORMED'] * 3
|
|
|
|
|
|
def test_authoritative_status_map_marks_missing_bridge_unavailable(tmp_path, monkeypatch):
|
|
import cbb_refresh_verify.batch_status as batch_status
|
|
from cbb_refresh_verify.status_bridge import BridgeUnavailable
|
|
|
|
class MissingBridge:
|
|
def __init__(self, *args, **kwargs):
|
|
pass
|
|
def start(self):
|
|
raise BridgeUnavailable(tmp_path / "missing.exe")
|
|
def restart(self):
|
|
raise BridgeUnavailable(tmp_path / "missing.exe")
|
|
def close(self):
|
|
pass
|
|
|
|
monkeypatch.setattr(batch_status, "StatusBridgeServer", MissingBridge)
|
|
lmc = tmp_path / "mentor_lib.lmc"
|
|
lmc.write_bytes(b"library")
|
|
result = batch_status.authoritative_status_map(
|
|
["A", "B"], lmc, tmp_path / "scan.jsonl")
|
|
assert result == {"A": "UNAVAILABLE", "B": "UNAVAILABLE"}
|
|
|
|
|
|
def test_authoritative_status_map_never_reuses_previous_scan_status(tmp_path, monkeypatch):
|
|
import cbb_refresh_verify.batch_status as batch_status
|
|
|
|
calls = []
|
|
|
|
class Bridge:
|
|
def __init__(self, *args, **kwargs):
|
|
pass
|
|
def start(self):
|
|
pass
|
|
def restart(self):
|
|
pass
|
|
def close(self):
|
|
pass
|
|
def query(self, lmc_file, block_name, **kwargs):
|
|
calls.append(block_name)
|
|
status = 'PENDING' if len(calls) == 1 else 'READY'
|
|
return BridgeResult(status, 'fake')
|
|
|
|
monkeypatch.setattr(batch_status, 'StatusBridgeServer', Bridge)
|
|
lmc = tmp_path / 'mentor_lib.lmc'
|
|
lmc.write_bytes(b'library')
|
|
checkpoint = tmp_path / 'scan.jsonl'
|
|
|
|
first = batch_status.authoritative_status_map(['A'], lmc, checkpoint)
|
|
second = batch_status.authoritative_status_map(['A'], lmc, checkpoint)
|
|
|
|
assert first == {'A': 'PENDING'}
|
|
assert second == {'A': 'READY'}
|
|
assert calls == ['A', 'A']
|