Files
C.E.L_Slide_test2/tests/test_user_overrides_io.py
kyeongmin 4da22adb43
Some checks failed
Multi-MDX Regression (IMP-91) / multi-mdx-regression (push) Failing after 20s
feat(#90): IMP-56 u1-u19 catch-up before final close (post-u20 push fix)
u1: text_overrides axis in user_overrides_io
u2: structure_overrides axis in user_overrides_io
u3: vite allowlist for new endpoints
u4: text_override_resolver
u5: Step 12 text_overrides apply in phase_z2_pipeline
u6: structure_override_resolver
u7: text_path_stamper
u8: SlideCanvas text-edit capture
u9: SlideCanvas structure-edit overlay
u10: userOverridesApi service extension
u11: designAgent types extension
u12: slidePlanUtils restore
u13: user_overrides endpoint tests
u14: user_overrides restore tests
u15: pipeline fallback tests
u16: edit-mode state + gating tests
u17: slide_base print mode CSS
u18: /api/connect endpoint (vite)
u19: /api/export endpoint (vite)

Recovery scope: 29 files (12 modified + 17 new). u20 already pushed in
9439575; this commit lands u1-u19 that were authored but not committed
before #90 was externally closed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 06:12:13 +09:00

349 lines
12 KiB
Python

"""IMP-52 (#80) u8 — backend tests for ``src.user_overrides_io``.
Covers the persisted axes called out in the Stage 2 plan
(IMP-51 #79 u1 extended this to 5 axes by adding ``image_overrides``;
IMP-45 #74 u1 extended to 6 axes by adding ``slide_css``;
IMP-55 #93 u1 extended to 7 axes by adding ``manual_section_assignment``;
IMP-56 #90 u1 extended to 8 axes by adding ``text_overrides``;
IMP-56 #90 u2 extended to 9 axes by adding ``structure_overrides`` —
scope LOCKED to ``{slot_order, hidden_slots}``; frame swap stays on the
existing ``frames`` axis):
1. Round-trip ``save`` → ``load`` (9 KNOWN_AXES + foreign top-level keys).
2. Unknown-key passthrough (foreign axes preserved across partial merges).
3. Missing / corrupt / non-object behavior (graceful ``{}`` + stderr warning).
4. Invalid keys (``InvalidOverrideKey`` raised on traversal / separators /
leading dot / empty).
All tests inject ``root=tmp_path`` so they never touch the real
``data/user_overrides/`` directory.
"""
from __future__ import annotations
import json
import pytest
from src.user_overrides_io import (
DEFAULT_OVERRIDES_ROOT,
InvalidOverrideKey,
KNOWN_AXES,
load,
override_path,
save,
validate_key,
)
# -- key validation ---------------------------------------------------------
def test_validate_key_accepts_typical_mdx_stems():
for key in ("01", "03", "03__DX_master", "sample.v2", "a-b_c.1"):
assert validate_key(key) == key
@pytest.mark.parametrize(
"bad_key",
[
"",
"..",
"../escape",
"sub/dir",
"sub\\dir",
".hidden",
"-leading-dash",
".",
"name with space",
"name?",
],
)
def test_validate_key_rejects_unsafe(bad_key):
with pytest.raises(InvalidOverrideKey):
validate_key(bad_key)
def test_validate_key_rejects_non_string():
for bad in (None, 123, b"bytes", ["list"], {"d": 1}):
with pytest.raises(InvalidOverrideKey):
validate_key(bad) # type: ignore[arg-type]
# -- override_path ----------------------------------------------------------
def test_override_path_uses_default_root_when_unspecified():
p = override_path("sample")
assert p.parent == DEFAULT_OVERRIDES_ROOT
assert p.name == "sample.json"
def test_override_path_honors_explicit_root(tmp_path):
p = override_path("sample", root=tmp_path)
assert p == tmp_path / "sample.json"
# -- load: missing / corrupt / non-object -----------------------------------
def test_load_missing_file_returns_empty_dict(tmp_path):
assert load("nope", root=tmp_path) == {}
def test_load_corrupt_json_warns_and_returns_empty(tmp_path, capsys):
path = override_path("corrupt", root=tmp_path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("{ this is not valid json", encoding="utf-8")
result = load("corrupt", root=tmp_path)
assert result == {}
captured = capsys.readouterr()
assert "failed to read" in captured.err
assert str(path) in captured.err
def test_load_non_object_json_warns_and_returns_empty(tmp_path, capsys):
path = override_path("list_root", root=tmp_path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("[1, 2, 3]", encoding="utf-8")
result = load("list_root", root=tmp_path)
assert result == {}
captured = capsys.readouterr()
assert "not a JSON object" in captured.err
# -- save: round-trip + partial-merge + foreign-key preserve ----------------
def _full_payload() -> dict:
return {
"layout": "sidebar-right",
"zone_geometries": {
"zone-top": {"x": 40.0, "y": 50.0, "w": 1200.0, "h": 120.0},
},
"zone_sections": {"zone-top": ["03-1", "03-2"]},
"frames": {"03-1+03-2": "frame_two_way_compare"},
"image_overrides": {
"img-1": {"x": 10.0, "y": 20.0, "w": 30.0, "h": 25.0},
},
"slide_css": "<style>.slide .frame-process-product .label { font-size: 14px; }</style>",
"manual_section_assignment": True,
"text_overrides": {"zone-top": {"slot_title.0": "edited headline"}},
"structure_overrides": {
"zone-top": {
"slot_order": ["slot_title", "slot_body"],
"hidden_slots": ["slot_caption"],
}
},
}
def test_known_axes_includes_image_overrides():
"""IMP-51 #79 u1 — ``image_overrides`` is a known axis (now 9 total)."""
assert "image_overrides" in KNOWN_AXES
assert len(KNOWN_AXES) == 9
def test_known_axes_includes_slide_css():
"""IMP-45 #74 u1 — ``slide_css`` is a known axis (9 total)."""
assert "slide_css" in KNOWN_AXES
assert len(KNOWN_AXES) == 9
def test_known_axes_includes_manual_section_assignment():
"""IMP-55 #93 u1 — bool intent marker is a known axis (9 total)."""
assert "manual_section_assignment" in KNOWN_AXES
assert len(KNOWN_AXES) == 9
def test_known_axes_includes_text_overrides():
"""IMP-56 #90 u1 — ``text_overrides`` is a known axis (9 total)."""
assert "text_overrides" in KNOWN_AXES
assert len(KNOWN_AXES) == 9
def test_known_axes_includes_structure_overrides():
"""IMP-56 #90 u2 — ``structure_overrides`` is a known axis (9 total).
Scope is locked to ``{slot_order, hidden_slots}`` only; frame swap
stays on the existing ``frames`` axis (verified at resolver u6 + Step
12 apply u7). The IO layer itself only enforces the axis name + the
foreign-key preservation invariant.
"""
assert "structure_overrides" in KNOWN_AXES
assert len(KNOWN_AXES) == 9
def test_save_then_load_round_trip(tmp_path):
key = "03"
payload = _full_payload()
written = save(key, payload, root=tmp_path)
assert written.exists()
assert written == tmp_path / "03.json"
loaded = load(key, root=tmp_path)
for axis in KNOWN_AXES:
assert loaded[axis] == payload[axis], f"axis {axis!r} did not round-trip"
def test_save_partial_payload_preserves_other_axes(tmp_path):
key = "03"
save(key, _full_payload(), root=tmp_path)
save(key, {"layout": "two-column"}, root=tmp_path)
loaded = load(key, root=tmp_path)
assert loaded["layout"] == "two-column"
assert loaded["zone_geometries"] == _full_payload()["zone_geometries"]
assert loaded["zone_sections"] == _full_payload()["zone_sections"]
assert loaded["frames"] == _full_payload()["frames"]
assert loaded["image_overrides"] == _full_payload()["image_overrides"]
assert loaded["slide_css"] == _full_payload()["slide_css"]
assert loaded["manual_section_assignment"] is True
assert loaded["text_overrides"] == _full_payload()["text_overrides"]
assert loaded["structure_overrides"] == _full_payload()["structure_overrides"]
def test_save_partial_image_overrides_preserves_other_axes(tmp_path):
"""IMP-51 #79 u1 — partial ``image_overrides`` write preserves siblings."""
key = "03"
save(key, _full_payload(), root=tmp_path)
save(
key,
{"image_overrides": {"img-9": {"x": 5.0, "y": 5.0, "w": 50.0, "h": 50.0}}},
root=tmp_path,
)
loaded = load(key, root=tmp_path)
assert loaded["image_overrides"] == {
"img-9": {"x": 5.0, "y": 5.0, "w": 50.0, "h": 50.0}
}
assert loaded["layout"] == _full_payload()["layout"]
assert loaded["zone_geometries"] == _full_payload()["zone_geometries"]
assert loaded["zone_sections"] == _full_payload()["zone_sections"]
assert loaded["frames"] == _full_payload()["frames"]
assert loaded["slide_css"] == _full_payload()["slide_css"]
assert loaded["manual_section_assignment"] is True
assert loaded["text_overrides"] == _full_payload()["text_overrides"]
assert loaded["structure_overrides"] == _full_payload()["structure_overrides"]
def test_save_axis_replaces_not_deep_merges(tmp_path):
key = "03"
save(key, {"frames": {"03-1": "frame_a", "03-2": "frame_b"}}, root=tmp_path)
save(key, {"frames": {"03-3": "frame_c"}}, root=tmp_path)
loaded = load(key, root=tmp_path)
assert loaded["frames"] == {"03-3": "frame_c"}
def test_save_none_clears_axis(tmp_path):
key = "03"
save(key, _full_payload(), root=tmp_path)
save(key, {"layout": None}, root=tmp_path)
loaded = load(key, root=tmp_path)
assert "layout" not in loaded
assert loaded["zone_geometries"] == _full_payload()["zone_geometries"]
assert loaded["frames"] == _full_payload()["frames"]
def test_save_preserves_foreign_top_level_keys(tmp_path):
"""Forward-compat: axes outside KNOWN_AXES (zone_sizes, schema_version,
...) must survive a partial merge on a known axis. (IMP-51 #79 u1
promoted ``image_overrides`` to a known axis, so it is no longer
exercised here as a foreign key.)"""
key = "03"
path = override_path(key, root=tmp_path)
path.parent.mkdir(parents=True, exist_ok=True)
pre_seed = {
"layout": "single-column",
"zone_sizes": {"zone-top": "tall"},
"schema_version": "experimental-1",
}
path.write_text(json.dumps(pre_seed), encoding="utf-8")
save(key, {"layout": "sidebar-right"}, root=tmp_path)
loaded = load(key, root=tmp_path)
assert loaded["layout"] == "sidebar-right"
assert loaded["zone_sizes"] == pre_seed["zone_sizes"]
assert loaded["schema_version"] == pre_seed["schema_version"]
def test_save_manual_section_assignment_round_trips_both_booleans(tmp_path):
"""IMP-55 #93 u1 — bool axis round-trips true/false and clears on None.
Asserts the bool is preserved literally (not coerced to int / string) so
the backend pipeline (u9) can branch on ``is True`` without false-positive
matches from truthy-but-not-True values seeded by older callers.
"""
key = "03"
save(key, {"manual_section_assignment": True}, root=tmp_path)
assert load(key, root=tmp_path)["manual_section_assignment"] is True
save(key, {"manual_section_assignment": False}, root=tmp_path)
assert load(key, root=tmp_path)["manual_section_assignment"] is False
save(key, {"manual_section_assignment": None}, root=tmp_path)
assert "manual_section_assignment" not in load(key, root=tmp_path)
def test_save_creates_parent_directory(tmp_path):
nested = tmp_path / "deep" / "nest"
assert not nested.exists()
save("03", {"layout": "two-column"}, root=nested)
assert (nested / "03.json").exists()
def test_save_writes_pretty_sorted_json_for_diffability(tmp_path):
key = "03"
save(key, _full_payload(), root=tmp_path)
raw = (tmp_path / "03.json").read_text(encoding="utf-8")
# sort_keys=True → KNOWN_AXES come out alphabetically. ``structure_overrides``
# (IMP-56 #90 u2) sorts between ``slide_css`` and ``text_overrides``.
pos_frames = raw.index('"frames"')
pos_image_overrides = raw.index('"image_overrides"')
pos_layout = raw.index('"layout"')
pos_manual = raw.index('"manual_section_assignment"')
pos_slide_css = raw.index('"slide_css"')
pos_structure_overrides = raw.index('"structure_overrides"')
pos_text_overrides = raw.index('"text_overrides"')
pos_zg = raw.index('"zone_geometries"')
pos_zs = raw.index('"zone_sections"')
assert (
pos_frames
< pos_image_overrides
< pos_layout
< pos_manual
< pos_slide_css
< pos_structure_overrides
< pos_text_overrides
< pos_zg
< pos_zs
)
def test_save_leaves_no_tmp_file_on_success(tmp_path):
save("03", _full_payload(), root=tmp_path)
leftovers = [p for p in tmp_path.iterdir() if p.name != "03.json"]
assert leftovers == [], f"tmp files leaked: {leftovers!r}"
def test_save_rejects_non_dict_partial(tmp_path):
with pytest.raises(TypeError):
save("03", ["not", "a", "dict"], root=tmp_path) # type: ignore[arg-type]
# -- save / load propagate key validation -----------------------------------
@pytest.mark.parametrize("bad_key", ["", "..", "sub/dir", ".hidden"])
def test_save_rejects_invalid_key(tmp_path, bad_key):
with pytest.raises(InvalidOverrideKey):
save(bad_key, {"layout": "two-column"}, root=tmp_path)
@pytest.mark.parametrize("bad_key", ["", "..", "sub/dir", ".hidden"])
def test_load_rejects_invalid_key(tmp_path, bad_key):
with pytest.raises(InvalidOverrideKey):
load(bad_key, root=tmp_path)