Files
OBLITERATUS/tests/test_app_analysis_charts.py
Joseph Magly 265b6b7865 fix(ui): reserve analysis chart temporary files
Reserve each chart output atomically and close its descriptor before plotting. Preserve successful images; remove only the reserved output when plotting fails.

Refs #408
2026-10-29 05:11:25 -04:00

189 lines
7.2 KiB
Python

"""Exercise actual app chart functions without importing optional Gradio UI."""
import ast
import errno
import os
from pathlib import Path
import stat
import tempfile
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus.analysis import visualization
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
TITLES = [
"Cross-Layer Direction Alignment — fixture",
"Refusal Direction Angular Drift — fixture",
"Refusal Topology Map — fixture",
]
@pytest.fixture
def charts(tmp_path, monkeypatch):
# Match the AST execution pattern in test_app_benchmark_lifecycle.py, using
# unchanged function bodies and their real plotting/tensor dependencies.
source = Path(__file__).resolve().parents[1] / "app.py"
names = {"_plot_analysis_chart", "_generate_analysis_figs", "_figs_to_gallery"}
functions = [node for node in ast.parse(source.read_text()).body
if isinstance(node, ast.FunctionDef) and node.name in names]
assert {node.name for node in functions} == names
namespace = {"os": os, "torch": torch}
exec(compile(ast.Module(body=functions, type_ignores=[]), str(source), "exec"), namespace)
reserved = {}
mkstemp = tempfile.mkstemp
def reserve(*args, **kwargs):
fd, name = mkstemp(*args, **kwargs, dir=tmp_path)
info = os.fstat(fd)
assert stat.S_ISREG(info.st_mode) and info.st_size == 0
reserved[name] = (fd, info.st_dev, info.st_ino)
return fd, name
monkeypatch.setattr(tempfile, "mkstemp", reserve)
previous_figures = set(plt.get_fignums())
yield SimpleNamespace(**namespace, reserved=reserved)
for number in set(plt.get_fignums()) - previous_figures:
plt.close(number)
def pipeline():
return SimpleNamespace(
refusal_directions={0: torch.tensor([1., 0., 0.]),
1: torch.tensor([1., 1., 0.]),
2: torch.tensor([0., 1., 1.])},
_strong_layers=[1, 2],
)
def assert_reserved_and_closed(charts, path):
fd, device, inode = charts.reserved[str(path)]
info = Path(path).stat()
assert (info.st_dev, info.st_ino, info.st_size) == (device, inode, 0)
with pytest.raises(OSError) as error:
os.fstat(fd)
assert error.value.errno == errno.EBADF
def test_real_analysis_and_gallery_keep_successful_reserved_pngs(charts, monkeypatch):
entered = []
for name in ("plot_cross_layer_heatmap", "plot_angular_drift", "plot_refusal_topology"):
original = getattr(visualization, name)
def plot(*args, _plot=original, **kwargs):
path = kwargs["output_path"]
assert_reserved_and_closed(charts, path)
entered.append(path)
return _plot(*args, **kwargs)
monkeypatch.setattr(visualization, name, plot)
figures = charts._generate_analysis_figs(pipeline(), "fixture")
assert len(figures) == len(entered) == 3
assert len(set(entered)) == 3
assert [figure.axes[0].get_title() for figure in figures] == TITLES
assert all(Path(path).read_bytes().startswith(b"\x89PNG\r\n\x1a\n") for path in entered)
gallery = charts._figs_to_gallery(figures)
assert [caption for _, caption in gallery] == TITLES
assert len(gallery) == 3
assert len(charts.reserved) == 6
assert all(Path(path).read_bytes().startswith(b"\x89PNG\r\n\x1a\n") for path in charts.reserved)
assert not set(entered) & {path for path, _ in gallery}
@pytest.mark.parametrize("failed", TITLES)
def test_failed_real_chart_removes_only_its_partial_output(charts, tmp_path, monkeypatch, failed):
unrelated = tmp_path / "existing-chart.png"
unrelated.write_bytes(b"existing artifact")
savefig = Figure.savefig
failed_paths = []
def save(figure, path, *args, **kwargs):
_, device, inode = charts.reserved[str(path)]
info = Path(path).stat()
assert (info.st_dev, info.st_ino, info.st_size) == (device, inode, 0)
if figure.axes[0].get_title() == failed:
Path(path).write_bytes(b"partial PNG")
failed_paths.append(path)
raise OSError("plot write failed")
return savefig(figure, path, *args, **kwargs)
monkeypatch.setattr(Figure, "savefig", save)
figures = charts._generate_analysis_figs(pipeline(), "fixture")
expected = [TITLES[2]] if failed == TITLES[0] else [title for title in TITLES if title != failed]
assert [figure.axes[0].get_title() for figure in figures] == expected
assert len(failed_paths) == 1 and not Path(failed_paths[0]).exists()
successful = set(charts.reserved) - set(failed_paths)
assert len(successful) == len(expected)
assert all(Path(path).read_bytes().startswith(b"\x89PNG\r\n\x1a\n") for path in successful)
assert unrelated.read_bytes() == b"existing artifact"
def test_atomic_reservation_retries_collision_without_touching_existing_file(charts, tmp_path, monkeypatch):
existing = tmp_path / "obliteratus_analysis_occupied.png"
existing.write_bytes(b"prior image")
monkeypatch.setattr(tempfile, "_get_candidate_names", lambda: iter(["occupied", "fresh"]))
result = object()
def plot(*, output_path):
assert Path(output_path).name == "obliteratus_analysis_fresh.png"
assert_reserved_and_closed(charts, output_path)
Path(output_path).write_bytes(b"new image")
return result
assert charts._plot_analysis_chart(plot) is result
assert existing.read_bytes() == b"prior image"
assert (tmp_path / "obliteratus_analysis_fresh.png").read_bytes() == b"new image"
@pytest.mark.parametrize("failure", [RuntimeError("plot failed"), KeyboardInterrupt()])
def test_helper_cleans_failure_and_propagates_original_exception(charts, failure):
def plot(*, output_path):
assert_reserved_and_closed(charts, output_path)
Path(output_path).write_bytes(b"partial")
raise failure
with pytest.raises(type(failure)) as error:
charts._plot_analysis_chart(plot)
assert error.value is failure
assert len(charts.reserved) == 1
assert all(not Path(path).exists() for path in charts.reserved)
def test_failed_cleanup_preserves_plotting_error(charts, monkeypatch):
failure = RuntimeError("plot failed")
def plot(*, output_path):
assert_reserved_and_closed(charts, output_path)
raise failure
def denied(path):
assert path in charts.reserved
raise PermissionError("cleanup unavailable")
monkeypatch.setattr(os, "unlink", denied)
with pytest.raises(RuntimeError) as error:
charts._plot_analysis_chart(plot)
assert error.value is failure
assert len(charts.reserved) == 1
assert all(Path(path).exists() for path in charts.reserved)
def test_failed_reservation_never_calls_plot_or_deletes_existing_artifact(charts, tmp_path, monkeypatch):
existing = tmp_path / "prior.png"
existing.write_bytes(b"prior image")
failure = OSError("no writable temp directory")
monkeypatch.setattr(tempfile, "mkstemp", Mock(side_effect=failure))
plot = Mock()
with pytest.raises(OSError) as error:
charts._plot_analysis_chart(plot)
assert error.value is failure
plot.assert_not_called()
assert charts.reserved == {}
assert existing.read_bytes() == b"prior image"