feat: read authoritative cell catalogs from owned library clones
This commit is contained in:
1 parent
bb506a3550
commit
d10356ee3d
4 files changed
+821
No files matched your search
@@ -0,0 +1,239 @@
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import json
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center import cell_catalog as catalog
|
||||
from dx_productivity.trial_task_center import cell_catalog_worker as worker
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def library(tmp_path):
|
||||
root = tmp_path / 'source'
|
||||
root.mkdir()
|
||||
files = {'lib.lmc': b'PartsDBLibs\\RES.pdb\0CellDBLibs\\RES.cel\0',
|
||||
'lib.cfg': b'config', 'CentLib.prp': b'properties',
|
||||
'upgrade.bin': b'upgrade', 'CLibv38.upd': b'',
|
||||
'PartsDBLibs/RES.pdb': b'parts', 'CellDBLibs/RES.cel': b'cells'}
|
||||
for name, data in files.items():
|
||||
path = root / name
|
||||
path.parent.mkdir(exist_ok=True)
|
||||
path.write_bytes(data)
|
||||
return root / 'lib.lmc'
|
||||
|
||||
|
||||
def proof(clone, part, partition, root):
|
||||
return {'verified': True, 'library': str(clone), 'part_number': part,
|
||||
'part_partition': partition or 'RES',
|
||||
'cells': [{'partition': 'RES', 'name': 'RESC1005N_0402'}], 'quit_verified': True,
|
||||
'cleanup': {'errors': [], 'desktop_handle_closed': True,
|
||||
'desktop_confirmation': {'released': True},
|
||||
'processes': [{'errors': [], 'job_active_processes': 0,
|
||||
'process_handle_closed': True, 'job_handle_closed': True}]}}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def reader(monkeypatch):
|
||||
calls = []
|
||||
def run(clone, part, partition, root):
|
||||
calls.append(clone)
|
||||
source = clone.parents[4] / 'source'
|
||||
# Inspect the clone before its required disposal, not after return.
|
||||
for path in source.rglob('*'):
|
||||
if path.is_file():
|
||||
copied = clone.parent / path.relative_to(source)
|
||||
assert copied.read_bytes() == path.read_bytes()
|
||||
assert copied.stat().st_mtime_ns == path.stat().st_mtime_ns
|
||||
return proof(clone, part, partition, root)
|
||||
monkeypatch.setattr(catalog, '_run_isolated_reader', run)
|
||||
return calls
|
||||
|
||||
|
||||
def test_complete_clone_preserves_metadata_and_cache_rechecks_source(library, tmp_path, reader):
|
||||
root = tmp_path / 'evidence'
|
||||
first = catalog.read_library_cell_catalog(library, 'PN', root)
|
||||
clone = reader[0]
|
||||
assert first.cells == (catalog.CellChoice('RES', 'RESC1005N_0402'),)
|
||||
assert first.library_path == library.resolve()
|
||||
assert first.evidence_path.is_file()
|
||||
assert not clone.parent.exists()
|
||||
assert catalog.read_library_cell_catalog(library, 'PN', root) == first
|
||||
assert len(reader) == 1
|
||||
(library.parent / 'CellDBLibs/RES.cel').write_bytes(b'changed')
|
||||
changed = catalog.read_library_cell_catalog(library, 'PN', root)
|
||||
assert changed.source_fingerprint != first.source_fingerprint
|
||||
assert len(reader) == 2
|
||||
|
||||
|
||||
def test_missing_physical_dependency_fails_before_process(library, tmp_path, reader):
|
||||
(library.parent / 'CellDBLibs/RES.cel').unlink()
|
||||
with pytest.raises(ValueError, match='dependency'):
|
||||
catalog.read_library_cell_catalog(library, 'PN', tmp_path / 'out')
|
||||
assert not reader
|
||||
|
||||
|
||||
def test_source_mutation_during_worker_cannot_publish_cache(library, tmp_path, monkeypatch):
|
||||
def mutate(clone, part, partition, root):
|
||||
(library.parent / 'CentLib.prp').write_bytes(b'changed')
|
||||
return proof(clone, part, partition, root)
|
||||
monkeypatch.setattr(catalog, '_run_isolated_reader', mutate)
|
||||
with pytest.raises(ValueError, match='changed'):
|
||||
catalog.read_library_cell_catalog(library, 'PN', tmp_path / 'out')
|
||||
assert not list((tmp_path / 'out').glob('cache/*.json'))
|
||||
|
||||
|
||||
def test_tampered_cache_evidence_is_rejected(library, tmp_path, reader):
|
||||
first = catalog.read_library_cell_catalog(library, 'PN', tmp_path / 'out')
|
||||
first.evidence_path.write_text('{}')
|
||||
second = catalog.read_library_cell_catalog(library, 'PN', tmp_path / 'out')
|
||||
assert len(reader) == 2
|
||||
assert second.evidence_path != first.evidence_path
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cells', [[], [{'partition':'A','name':'same'}, {'partition':'B','name':'same'}]])
|
||||
def test_zero_or_ambiguous_cells_are_not_authoritative(library, tmp_path, monkeypatch, cells):
|
||||
def run(clone, part, partition, root):
|
||||
return dict(proof(clone, part, partition, root), cells=cells)
|
||||
monkeypatch.setattr(catalog, '_run_isolated_reader', run)
|
||||
with pytest.raises(ValueError, match='associated|ambiguous'):
|
||||
catalog.read_library_cell_catalog(library, 'PN', tmp_path / 'out')
|
||||
|
||||
|
||||
class Collection:
|
||||
def __init__(self, items):
|
||||
self.items = items
|
||||
self.Count = len(items)
|
||||
def Item(self, index):
|
||||
assert type(index) is int, 'COM string lookup silently returns None'
|
||||
return self.items[index - 1]
|
||||
|
||||
|
||||
def fake_library(parts):
|
||||
return SimpleNamespace(Partitions=lambda kind: Collection(parts if kind == 1 else [SimpleNamespace(Name='RES')]))
|
||||
|
||||
|
||||
def part_partition(name, parts):
|
||||
collection = Collection(parts)
|
||||
collection.Name = name
|
||||
return collection
|
||||
|
||||
|
||||
def part(name, cells=None):
|
||||
return SimpleNamespace(Name=name, AssociatedCells=Collection(cells or [SimpleNamespace(Name='CELL', PartitionName='RES')]))
|
||||
|
||||
|
||||
def test_numeric_enumeration_matches_exact_part_name_and_rejects_duplicate_partitions(library):
|
||||
lib = fake_library([part_partition('RES', [part('PN-prefix'), part('PN')])])
|
||||
assert worker._read_associations(lib, 'PN', None, library.parent)['part_partition'] == 'RES'
|
||||
lib = fake_library([part_partition('RES', [part('PN')]), part_partition('OTHER', [part('PN')])])
|
||||
(library.parent / 'PartsDBLibs/OTHER.pdb').write_bytes(b'other')
|
||||
with pytest.raises(ValueError, match='ambiguous'):
|
||||
worker._read_associations(lib, 'PN', None, library.parent)
|
||||
assert worker._read_associations(lib, 'PN', 'RES', library.parent)['cells'][0]['name'] == 'CELL'
|
||||
|
||||
|
||||
def test_com_partition_requires_exact_physical_database(library):
|
||||
(library.parent / 'PartsDBLibs/RES.pdb').rename(library.parent / 'PartsDBLibs/RES-other.pdb')
|
||||
with pytest.raises(ValueError, match='dependency'):
|
||||
worker._read_associations(fake_library([part_partition('RES', [part('PN')])]), 'PN', None, library.parent)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('text', ['', 'KEY CentralLibrary "a.lmc"\nKEY CentralLibrary "b.lmc"', 'KEY CentralLibrary ""', 'KEY CentralLibrary broken value'])
|
||||
def test_project_library_config_is_strict(tmp_path, text):
|
||||
project = tmp_path / 'a.prj'
|
||||
project.write_text(text)
|
||||
with pytest.raises(ValueError, match='CentralLibrary'):
|
||||
catalog.project_library_path(project)
|
||||
|
||||
|
||||
def test_project_relative_library_and_candidate_intersection(tmp_path, monkeypatch):
|
||||
projects = []
|
||||
for index in range(2):
|
||||
project = tmp_path / f'{index}.prj'
|
||||
project.write_text(f'KEY CentralLibrary "lib{index}/lib.lmc"')
|
||||
projects.append(project)
|
||||
assert catalog.project_library_path(projects[0]) == (tmp_path / 'lib0/lib.lmc').resolve()
|
||||
def read(path, pn, root):
|
||||
names = ('CELL', 'OTHER') if path.parent.name == 'lib0' else ('CELL',)
|
||||
return catalog.CellCatalog(path, 'RES', pn, tuple(catalog.CellChoice('RES', n) for n in names), 'fingerprint', root)
|
||||
monkeypatch.setattr(catalog, 'read_library_cell_catalog', read)
|
||||
assert catalog.read_project_cell_choices(tuple(projects), 'PN', tmp_path) == ('CELL',)
|
||||
|
||||
|
||||
def test_hidden_worker_dispatch_and_frozen_command(tmp_path, monkeypatch):
|
||||
from dx_productivity.trial_task_center import application
|
||||
request = tmp_path / 'request.json'
|
||||
monkeypatch.setattr(worker, 'run', lambda path: 17 if path == request else 2)
|
||||
assert application.main(['--cell-catalog-worker', str(request)]) == 17
|
||||
monkeypatch.setattr(catalog.sys, 'frozen', True, raising=False)
|
||||
assert catalog._worker_command(request) == (catalog.sys.executable, '--cell-catalog-worker', str(request))
|
||||
|
||||
|
||||
def test_unowned_or_dialog_window_rejects_before_com_read(monkeypatch, tmp_path):
|
||||
request = {'desktop': 'dx-cell-test', 'pid': 123, 'library': str(tmp_path/'lib.lmc')}
|
||||
monkeypatch.setattr(worker, '_current_desktop', lambda: 'Default')
|
||||
with pytest.raises(ValueError, match='desktop'):
|
||||
worker._verify_owned_window(request, [])
|
||||
monkeypatch.setattr(worker, '_current_desktop', lambda: 'dx-cell-test')
|
||||
with pytest.raises(ValueError, match='dialog'):
|
||||
worker._verify_owned_window(request, [{'pid':123,'handle':1,'class':'#32770','title':'Migration'}])
|
||||
|
||||
|
||||
def test_existing_library_manager_blocks_owned_launch(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(catalog, '_existing_library_manager_pids', lambda: [777])
|
||||
with pytest.raises(ValueError, match='existing.*Library Manager'):
|
||||
catalog._require_no_existing_manager()
|
||||
|
||||
|
||||
def test_unproven_cleanup_is_not_cached(library, tmp_path, monkeypatch):
|
||||
def run(clone, part, partition, root):
|
||||
result = proof(clone, part, partition, root)
|
||||
result['cleanup']['processes'][0]['job_active_processes'] = 1
|
||||
return result
|
||||
monkeypatch.setattr(catalog, '_run_isolated_reader', run)
|
||||
with pytest.raises(ValueError, match='cleanup'):
|
||||
catalog.read_library_cell_catalog(library, 'PN', tmp_path / 'out')
|
||||
|
||||
|
||||
def test_missing_com_cell_partition_dependency_blocks_partial_associations(library):
|
||||
lib = SimpleNamespace(Partitions=lambda kind: Collection(
|
||||
[part_partition('RES', [part('PN')])] if kind == 1 else
|
||||
[SimpleNamespace(Name='RES'), SimpleNamespace(Name='MISSING')]))
|
||||
with pytest.raises(ValueError, match='dependency'):
|
||||
worker._read_associations(lib, 'PN', None, library.parent)
|
||||
|
||||
|
||||
def test_owned_title_can_use_library_basename_for_library_working_directory(library, monkeypatch):
|
||||
request = {'desktop':'dx-cell-test', 'pid':123, 'library':str(library)}
|
||||
monkeypatch.setattr(worker, '_current_desktop', lambda: 'dx-cell-test')
|
||||
windows = [{'pid':123, 'handle':1, 'class':'Afx:main', 'title':'Library Manager: lib.lmc'}]
|
||||
assert worker._verify_owned_window(request, windows)
|
||||
windows[0]['title'] = 'Library Manager: other.lmc'
|
||||
assert not worker._verify_owned_window(request, windows)
|
||||
|
||||
|
||||
def test_clone_allows_normal_cfg_timestamp_touch_but_rejects_content_migration(library):
|
||||
expected = catalog._content_fingerprint(catalog._library_manifest(library))
|
||||
cfg = library.with_suffix('.cfg')
|
||||
info = cfg.stat()
|
||||
os.utime(cfg, ns=(info.st_atime_ns, info.st_mtime_ns + 1_000_000_000))
|
||||
worker._verify_clone_content(library, expected)
|
||||
(library.parent / 'CellDBLibs/RES.cel').write_bytes(b'migrated')
|
||||
with pytest.raises(ValueError, match='content'):
|
||||
worker._verify_clone_content(library, expected)
|
||||
|
||||
|
||||
def test_owned_title_truncation_still_requires_exact_com_library(library, monkeypatch):
|
||||
request = {'desktop':'dx-cell-test', 'pid':123, 'library':str(library)}
|
||||
monkeypatch.setattr(worker, '_current_desktop', lambda: 'dx-cell-test')
|
||||
title = ('Library Manager: ' + str(library))[:159]
|
||||
windows = [{'pid':123, 'handle':1, 'class':'Afx:main', 'title':title}]
|
||||
monkeypatch.setattr(worker, '_windows', lambda: windows)
|
||||
assert worker._verify_owned_window(request, windows)
|
||||
other = library.with_suffix('.cfg')
|
||||
with pytest.raises(ValueError, match='differs'):
|
||||
worker._verify_app(SimpleNamespace(ActiveLibrary=SimpleNamespace(FullName=str(other))), request)
|
||||
expected = SimpleNamespace(FullName=str(library))
|
||||
assert worker._verify_app(SimpleNamespace(ActiveLibrary=expected), request) is expected
|
||||
Reference in new issue
Block a user