signal-bot/tests/test_tool_imports.py
Krzysztof kuhy Rudnicki 9d546fef9a feat: import a folder of photos without importing it twice
tool/tagger.py did not run at all: 48957a1 renamed pack.default_dir to
default_dirs and missed the caller, so the tagger raised ImportError at
module scope through two releases. Nothing could have caught it -- tool/ is
outside --cov and mypy ran with [signal_bot, fake_signal]. Both are widened
here, and tests/test_tool_imports.py imports every script under tool/.

--import was a filename check and shutil.copy2. Pointed at a phone export
that overlaps the album it re-imported the same photo under whatever name
the exporter used that time. It now identifies files by magic bytes, strips
them through the same media_strip path !add_photo uses, and dedups on exact
bytes twice: the source hash, then -- only for what survives -- the hash
after stripping. sources.json records both, so a second run on the same
folder is a no-op. What the album refuses is moved to _skipped/<reason>/
beside the source, never deleted.

Two things that made "strip on import" impossible until now:

media_tags claims -all= zeroes the QuickTime date tags and allows them by
value. It does not; measured, it leaves CreateDate at its real value, and
every mp4 was being refused. -time:all= is what zeroes them.

And a tag allowlist cannot see the worst class at all. IMG_6924.mp4 carries
mebx timed-metadata holding per-frame face bounding boxes and a face id,
and those survive a complete tag strip because they live in mdat as opaque
samples. media_tracks.py drops the track with ffmpeg and verifies with
ffprobe that only video and audio survived. This is not the ffmpeg mistake
media_strip warns about: that was ffmpeg replacing exiftool for tags, this
is track removal with exiftool still doing every tag, and the gate is a
positive read-back rather than an exit code.

The roster was write-once. The setup form only appeared on an empty album,
Album.setup overwrote the file whole, and keys were positional (p{len+1}),
so inserting somebody renumbered everyone after them and repointed every
tags.json entry at the wrong person. Keys now come from the highest number
in use, the form merges, and a person can be added mid-session.

--untagged filters the page and never the server's list. /media/{index} and
/api/tag both index the full album, so a shorter list server-side would
show item i of the album while tagging item i of the filtered set.

Measured on the live album: 174 source files -> 1 not media, 82 already in
pack, 24 duplicate in batch, 67 imported, and re-running imports nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WBrE1b9HvFw3xvG9bJiDFt
2026-08-31 13:22:57 +02:00

67 lines
2.5 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Every script under tool/ imports, and the tagger answers --help.
This exists because ``tool/tagger.py`` shipped an unconditional ImportError
and survived two releases with it: commit 48957a1 renamed
``pack.default_dir`` to ``default_dirs`` and missed the caller. Nothing could
have caught it. ``tool/`` is outside ``--cov`` in pyproject.toml, mypy ran
with ``args: [signal_bot, fake_signal]``, and ruff lints these files but
cannot resolve a name in another module.
So this is deliberately the cheapest possible test: import each file and, for
the one with a CLI, run it. It adds no coverage burden -- ``tests/`` is not
measured -- and no module-scope ImportError can survive it.
"""
from __future__ import annotations
import importlib.util
import sys
from pathlib import Path
import pytest
from signal_bot.external import run
TOOL = Path(__file__).resolve().parent.parent / "tool"
def _modules() -> list[Path]:
"""Every Python file under tool/, so a new one is covered automatically."""
return sorted(path for path in TOOL.glob("*.py") if path.name != "__init__.py")
def test_there_is_something_to_check() -> None:
# Guards against the glob silently matching nothing and the whole file
# passing vacuously.
assert _modules()
@pytest.mark.parametrize("path", _modules(), ids=lambda path: path.name)
def test_the_module_imports(path: Path) -> None:
spec = importlib.util.spec_from_file_location(f"_tool_{path.stem}", path)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
# tool/ is a directory of scripts, not a package, and a couple of them
# import a sibling by bare name. That resolves in real use -- running
# "python tool/x.py" puts tool/ at sys.path[0] -- so the harness has to
# reproduce it rather than the scripts having to work around it.
sys.path.insert(0, str(TOOL))
try:
spec.loader.exec_module(module)
finally:
sys.path.remove(str(TOOL))
sys.modules.pop(spec.name, None)
def test_the_tagger_answers_help() -> None:
# Through signal_bot.external, like every other subprocess here: S603 is
# suppressed for exactly one file and widening it is what that entry
# exists to prevent.
result = run([sys.executable, str(TOOL / "tagger.py"), "--help"])
assert result.ok, result.stderr
for flag in ("--import", "--untagged", "--dry-run"):
assert flag in result.stdout