217 lines
7.9 KiB
Python
217 lines
7.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, resume=True)
|
|
assert bridge.requests == ["B"]
|
|
assert {record.block_name for record in records} == {"A", "B"}
|
|
|
|
|
|
def test_default_scan_refreshes_cached_ready_after_payload_change(tmp_path):
|
|
lmc = tmp_path/'library.lmc'; lmc.write_bytes(b'library')
|
|
checkpoint = tmp_path/'scan.jsonl'
|
|
BatchStatusSession(FakeBridge()).scan(['A'], lmc, checkpoint)
|
|
# The lightweight fingerprint cannot prove payload status is unchanged.
|
|
fingerprint = fingerprint_library(lmc)
|
|
(tmp_path/'payload.pcb').write_bytes(b'native edit without LMC change')
|
|
assert fingerprint_library(lmc) == fingerprint
|
|
class PendingBridge(FakeBridge):
|
|
def query(self, *args, **kwargs):
|
|
self.requests.append(args[1])
|
|
return BridgeResult('PENDING', 'fake')
|
|
bridge = PendingBridge()
|
|
records = BatchStatusSession(bridge).scan(['A'], lmc, checkpoint)
|
|
assert [record.status for record in records] == ['PENDING']
|
|
assert bridge.requests == ['A']
|
|
assert len(checkpoint.read_text('utf-8').splitlines()) == 1
|
|
|
|
|
|
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_records_each_queried_item(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', 'B', 'C']
|
|
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']
|
|
|
|
|
|
def test_retry_rechecks_failed_scan_without_library_changes(tmp_path):
|
|
lmc = tmp_path/'library.lmc'; lmc.write_bytes(b'library')
|
|
checkpoint = tmp_path/'scan.jsonl'
|
|
first = BatchStatusSession(FakeBridge(timeout_name='A')).scan(['A', 'B'], lmc, checkpoint)
|
|
assert first[0].status == 'TIMEOUT'
|
|
bridge = FakeBridge()
|
|
second = BatchStatusSession(bridge).scan(['A', 'B'], lmc, checkpoint, resume=True)
|
|
assert bridge.requests == ['A']
|
|
assert second[0].status == 'READY'
|
|
|
|
|
|
def test_missing_x86_runtime_does_not_restart_for_every_cbb(tmp_path):
|
|
class BrokenBridge(FakeBridge):
|
|
def query(self, library, name, **kwargs):
|
|
self.requests.append(name)
|
|
raise BridgeResponseError('状态桥进程失败: -1073741515')
|
|
lmc = tmp_path/'library.lmc'; lmc.write_bytes(b'library')
|
|
bridge = BrokenBridge()
|
|
result = BatchStatusSession(bridge).scan(['A', 'B', 'C'], lmc, tmp_path/'scan.jsonl')
|
|
assert bridge.requests == ['A']
|
|
assert [row.status for row in result] == ['UNAVAILABLE'] * 3
|