Skip to content

Commit 9a2b976

Browse files
MaxFreedomPollardteknium1
authored andcommitted
test(skills): add regression tests for bundled-update backup recovery
Three tests covering: a stale .bak poisoning a failed update's move/restore, an orphaned .bak misread as a user deletion, and a partially written dest blocking restore-on-failure. All three fail on current main without the fix. Refs #44942
1 parent 3581131 commit 9a2b976

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

‎tests/tools/test_skills_sync.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for tools/skills_sync.py — manifest-based skill seeding and updating."""
22

3+
import shutil
34
import json
45
from pathlib import Path
56
from unittest.mock import patch
@@ -1067,3 +1068,123 @@ def test_remove_keeps_user_modified(self, tmp_path):
10671068
assert "EDITED" in (skills_dir / "beta" / "SKILL.md").read_text()
10681069
# non-bundled local skill never considered
10691070
assert (skills_dir / "mine" / "SKILL.md").exists()
1071+
1072+
1073+
class TestUpdateBackupRecovery:
1074+
"""Regression tests for backup handling in the bundled-update path.
1075+
1076+
Covers three failure modes around ``dest.with_suffix(".bak")``:
1077+
a stale backup poisoning the next update's move/restore, an orphaned
1078+
backup (crash between move and copytree) being misread as a user
1079+
deletion, and a partially-written dest blocking restore-on-failure.
1080+
"""
1081+
1082+
def _setup(self, tmp_path, bundled_text="# Old v2 (updated)"):
1083+
"""Bundled dir with one flat skill, plus user dirs."""
1084+
bundled = tmp_path / "bundled_skills"
1085+
(bundled / "old-skill").mkdir(parents=True)
1086+
(bundled / "old-skill" / "SKILL.md").write_text(bundled_text)
1087+
skills_dir = tmp_path / "user_skills"
1088+
skills_dir.mkdir()
1089+
manifest_file = skills_dir / ".bundled_manifest"
1090+
return bundled, skills_dir, manifest_file
1091+
1092+
def _patches(self, bundled, skills_dir, manifest_file):
1093+
from contextlib import ExitStack
1094+
stack = ExitStack()
1095+
stack.enter_context(patch("tools.skills_sync._get_bundled_dir", return_value=bundled))
1096+
stack.enter_context(patch("tools.skills_sync._get_optional_dir", return_value=bundled.parent / "optional-skills"))
1097+
stack.enter_context(patch("tools.skills_sync.SKILLS_DIR", skills_dir))
1098+
stack.enter_context(patch("tools.skills_sync.MANIFEST_FILE", manifest_file))
1099+
return stack
1100+
1101+
def _seed_synced_copy(self, skills_dir, manifest_file, text="# Old v1"):
1102+
"""User copy of old-skill whose hash matches the manifest origin."""
1103+
dest = skills_dir / "old-skill"
1104+
dest.mkdir(parents=True)
1105+
(dest / "SKILL.md").write_text(text)
1106+
with patch("tools.skills_sync.MANIFEST_FILE", manifest_file):
1107+
_write_manifest({"old-skill": _dir_hash(dest)})
1108+
return dest
1109+
1110+
def test_stale_backup_does_not_poison_failed_update(self, tmp_path):
1111+
"""A leftover .bak must not nest the live copy or corrupt restore.
1112+
1113+
With a stale ``old-skill.bak`` present, ``shutil.move(dest, backup)``
1114+
moves the live copy *inside* the stale dir. If copytree then fails,
1115+
restore drags the stale junk (with the live copy nested in it) back
1116+
to dest — corrupting the skill and wedging it as "user-modified".
1117+
"""
1118+
bundled, skills_dir, manifest_file = self._setup(tmp_path)
1119+
dest = self._seed_synced_copy(skills_dir, manifest_file)
1120+
1121+
stale = skills_dir / "old-skill.bak"
1122+
stale.mkdir()
1123+
(stale / "SKILL.md").write_text("# stale junk from an earlier failure")
1124+
1125+
def _boom(src, dst, **kwargs):
1126+
raise OSError("simulated copy failure")
1127+
1128+
with self._patches(bundled, skills_dir, manifest_file), \
1129+
patch("tools.skills_sync.shutil.copytree", side_effect=_boom):
1130+
sync_skills(quiet=True)
1131+
1132+
# The live copy must survive the failed update untouched...
1133+
assert (dest / "SKILL.md").read_text() == "# Old v1"
1134+
# ...not be nested inside recycled stale-backup content.
1135+
assert not (dest / "old-skill").exists()
1136+
# And no backup directory may linger.
1137+
assert not (skills_dir / "old-skill.bak").exists()
1138+
1139+
def test_orphaned_backup_is_recovered_not_treated_as_deleted(self, tmp_path):
1140+
"""Crash between move and copytree must not lose the skill.
1141+
1142+
After such a crash, dest is gone and the user's only copy sits in
1143+
``old-skill.bak``. The sync loop's "in manifest but not on disk"
1144+
branch reads that as a deliberate user deletion and skips — the
1145+
skill silently vanishes. It must instead be recovered and updated.
1146+
"""
1147+
bundled, skills_dir, manifest_file = self._setup(tmp_path)
1148+
dest = self._seed_synced_copy(skills_dir, manifest_file)
1149+
# Simulate the crash: dest was moved aside, new copy never arrived.
1150+
shutil.move(str(dest), str(skills_dir / "old-skill.bak"))
1151+
1152+
with self._patches(bundled, skills_dir, manifest_file):
1153+
result = sync_skills(quiet=True)
1154+
1155+
# Recovered and then updated to the new bundled version in one run.
1156+
assert (dest / "SKILL.md").exists()
1157+
assert (dest / "SKILL.md").read_text() == "# Old v2 (updated)"
1158+
assert "old-skill" in result["updated"]
1159+
assert not (skills_dir / "old-skill.bak").exists()
1160+
1161+
def test_partial_copy_failure_restores_original(self, tmp_path):
1162+
"""A half-written dest must not block restore-on-failure.
1163+
1164+
If copytree dies after creating dest, the ``not dest.exists()``
1165+
guard skips the restore: the user keeps a broken partial skill,
1166+
the .bak lingers, and the partial hash wedges the skill as
1167+
"user-modified" on every later sync.
1168+
"""
1169+
bundled, skills_dir, manifest_file = self._setup(tmp_path)
1170+
dest = self._seed_synced_copy(skills_dir, manifest_file)
1171+
1172+
def _partial_then_fail(src, dst, **kwargs):
1173+
Path(dst).mkdir(parents=True, exist_ok=True)
1174+
(Path(dst) / "PARTIAL").write_text("half-written")
1175+
raise OSError("simulated failure mid-copy")
1176+
1177+
with self._patches(bundled, skills_dir, manifest_file), \
1178+
patch("tools.skills_sync.shutil.copytree", side_effect=_partial_then_fail):
1179+
sync_skills(quiet=True)
1180+
1181+
# Original content restored, partial debris and backup gone.
1182+
assert (dest / "SKILL.md").read_text() == "# Old v1"
1183+
assert not (dest / "PARTIAL").exists()
1184+
assert not (skills_dir / "old-skill.bak").exists()
1185+
1186+
# And the skill is not wedged: a later normal sync updates cleanly.
1187+
with self._patches(bundled, skills_dir, manifest_file):
1188+
result2 = sync_skills(quiet=True)
1189+
assert "old-skill" in result2["updated"]
1190+
assert result2["user_modified"] == []

0 commit comments

Comments
 (0)