|
| 1 | +"""Tests for per-skill staging fan-out (issue #120). |
| 2 | +
|
| 3 | +Pure-stdlib (unittest), hermetic (tmpdir only), no API key, no network. |
| 4 | +Run: python -m pytest tests/test_sleep_staging_fanout.py |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import tempfile |
| 11 | +import unittest |
| 12 | + |
| 13 | +from skillopt_sleep.staging import ( |
| 14 | + SkillProposal, |
| 15 | + StagingError, |
| 16 | + proposal_filename, |
| 17 | + skill_proposal_rows, |
| 18 | + write_skill_proposals, |
| 19 | + write_staging, |
| 20 | +) |
| 21 | +from skillopt_sleep.types import SleepReport |
| 22 | + |
| 23 | + |
| 24 | +def _proposal(name="example-skill", body="# example\n", live=None, root="/tmp/live"): |
| 25 | + if live is None: |
| 26 | + live = os.path.join(root, name, "SKILL.md") |
| 27 | + return SkillProposal(name, body, live) |
| 28 | + |
| 29 | + |
| 30 | +def _report(): |
| 31 | + return SleepReport(night=1, project="/repo/example", accepted=True, |
| 32 | + gate_action="accept_new_best") |
| 33 | + |
| 34 | + |
| 35 | +class TestSkillProposalRows(unittest.TestCase): |
| 36 | + def test_one_row_per_skill_in_order(self): |
| 37 | + rows = skill_proposal_rows([_proposal("alpha"), _proposal("beta")]) |
| 38 | + self.assertEqual([r["skill_name"] for r in rows], ["alpha", "beta"]) |
| 39 | + self.assertEqual([r["proposed_file"] for r in rows], |
| 40 | + ["proposed_SKILL.alpha.md", "proposed_SKILL.beta.md"]) |
| 41 | + self.assertEqual(rows[0]["live_skill_path"], "/tmp/live/alpha/SKILL.md") |
| 42 | + |
| 43 | + def test_filenames_are_unique_per_skill(self): |
| 44 | + self.assertNotEqual(proposal_filename("alpha"), proposal_filename("beta")) |
| 45 | + |
| 46 | + def test_duplicate_skill_name_is_refused(self): |
| 47 | + with self.assertRaises(StagingError): |
| 48 | + skill_proposal_rows([_proposal("alpha"), |
| 49 | + _proposal("alpha", live="/tmp/other/SKILL.md")]) |
| 50 | + |
| 51 | + def test_two_skills_targeting_one_file_are_refused(self): |
| 52 | + shared = "/tmp/live/shared/SKILL.md" |
| 53 | + with self.assertRaises(StagingError): |
| 54 | + skill_proposal_rows([_proposal("alpha", live=shared), |
| 55 | + _proposal("beta", live=shared)]) |
| 56 | + |
| 57 | + def test_unsafe_skill_names_are_refused(self): |
| 58 | + for bad in ["", " ", ".", "..", "../escape", "a/b", "a\\b", "/abs", |
| 59 | + "~home", "bad\nname"]: |
| 60 | + with self.assertRaises(StagingError, msg=bad): |
| 61 | + skill_proposal_rows([_proposal(bad)]) |
| 62 | + |
| 63 | + def test_unsafe_live_paths_are_refused(self): |
| 64 | + for bad in ["", "relative/SKILL.md", "~/skills/a/SKILL.md", |
| 65 | + "/tmp/live/../../etc/SKILL.md", "/tmp/live/a/SKILL.txt"]: |
| 66 | + with self.assertRaises(StagingError, msg=bad): |
| 67 | + skill_proposal_rows([_proposal("alpha", live=bad)]) |
| 68 | + |
| 69 | + |
| 70 | +class TestWriteSkillProposals(unittest.TestCase): |
| 71 | + def test_writes_one_file_per_skill(self): |
| 72 | + with tempfile.TemporaryDirectory() as tmp: |
| 73 | + rows = write_skill_proposals(tmp, [ |
| 74 | + _proposal("alpha", "# alpha\n"), |
| 75 | + _proposal("beta", "# beta\n"), |
| 76 | + ]) |
| 77 | + self.assertEqual(sorted(os.listdir(tmp)), |
| 78 | + ["proposed_SKILL.alpha.md", "proposed_SKILL.beta.md"]) |
| 79 | + with open(os.path.join(tmp, rows[0]["proposed_file"]), encoding="utf-8") as f: |
| 80 | + self.assertEqual(f.read(), "# alpha\n") |
| 81 | + |
| 82 | + def test_no_proposals_writes_nothing(self): |
| 83 | + with tempfile.TemporaryDirectory() as tmp: |
| 84 | + self.assertEqual(write_skill_proposals(tmp, []), []) |
| 85 | + self.assertEqual(os.listdir(tmp), []) |
| 86 | + |
| 87 | + def test_rejected_fan_out_leaves_no_partial_files(self): |
| 88 | + with tempfile.TemporaryDirectory() as tmp: |
| 89 | + with self.assertRaises(StagingError): |
| 90 | + write_skill_proposals(tmp, [_proposal("alpha"), _proposal("../escape")]) |
| 91 | + self.assertEqual(os.listdir(tmp), []) |
| 92 | + |
| 93 | + def test_writes_leave_no_temporary_files_behind(self): |
| 94 | + with tempfile.TemporaryDirectory() as tmp: |
| 95 | + write_skill_proposals(tmp, [_proposal("alpha")]) |
| 96 | + self.assertEqual([n for n in os.listdir(tmp) if n.startswith(".tmp-")], []) |
| 97 | + |
| 98 | + def test_rewrite_replaces_content_atomically(self): |
| 99 | + with tempfile.TemporaryDirectory() as tmp: |
| 100 | + write_skill_proposals(tmp, [_proposal("alpha", "# first\n")]) |
| 101 | + write_skill_proposals(tmp, [_proposal("alpha", "# second\n")]) |
| 102 | + path = os.path.join(tmp, proposal_filename("alpha")) |
| 103 | + with open(path, encoding="utf-8") as f: |
| 104 | + self.assertEqual(f.read(), "# second\n") |
| 105 | + self.assertEqual(sorted(os.listdir(tmp)), [proposal_filename("alpha")]) |
| 106 | + |
| 107 | + |
| 108 | +class TestWriteStagingCompatibility(unittest.TestCase): |
| 109 | + def _manifest(self, out): |
| 110 | + with open(os.path.join(out, "manifest.json"), encoding="utf-8") as f: |
| 111 | + return json.load(f) |
| 112 | + |
| 113 | + def test_legacy_layout_when_multi_skill_is_unused(self): |
| 114 | + with tempfile.TemporaryDirectory() as tmp: |
| 115 | + out = write_staging( |
| 116 | + tmp, report=_report(), proposed_skill="# skill\n", |
| 117 | + proposed_memory="# memory\n", |
| 118 | + live_skill_path=os.path.join(tmp, "live", "SKILL.md"), |
| 119 | + live_memory_path=os.path.join(tmp, "live", "CLAUDE.md"), |
| 120 | + report_md="# report\n", |
| 121 | + ) |
| 122 | + self.assertEqual( |
| 123 | + sorted(os.listdir(out)), |
| 124 | + ["manifest.json", "proposed_CLAUDE.md", "proposed_SKILL.md", |
| 125 | + "report.json", "report.md"], |
| 126 | + ) |
| 127 | + manifest = self._manifest(out) |
| 128 | + self.assertNotIn("skills", manifest) |
| 129 | + self.assertTrue(manifest["has_skill"]) |
| 130 | + |
| 131 | + def test_fan_out_adds_files_and_manifest_rows(self): |
| 132 | + with tempfile.TemporaryDirectory() as tmp: |
| 133 | + live_root = os.path.join(tmp, "live") |
| 134 | + out = write_staging( |
| 135 | + tmp, report=_report(), proposed_skill=None, proposed_memory=None, |
| 136 | + live_skill_path=os.path.join(live_root, "SKILL.md"), |
| 137 | + live_memory_path=os.path.join(live_root, "CLAUDE.md"), |
| 138 | + report_md="# report\n", |
| 139 | + skill_proposals=[ |
| 140 | + _proposal("alpha", "# alpha\n", root=live_root), |
| 141 | + _proposal("beta", "# beta\n", root=live_root), |
| 142 | + ], |
| 143 | + ) |
| 144 | + self.assertEqual( |
| 145 | + sorted(os.listdir(out)), |
| 146 | + ["manifest.json", "proposed_SKILL.alpha.md", "proposed_SKILL.beta.md", |
| 147 | + "report.json", "report.md"], |
| 148 | + ) |
| 149 | + rows = self._manifest(out)["skills"] |
| 150 | + self.assertEqual([r["skill_name"] for r in rows], ["alpha", "beta"]) |
| 151 | + self.assertEqual(rows[1]["live_skill_path"], |
| 152 | + os.path.join(live_root, "beta", "SKILL.md")) |
| 153 | + |
| 154 | + def test_unsafe_fan_out_writes_no_manifest(self): |
| 155 | + with tempfile.TemporaryDirectory() as tmp: |
| 156 | + with self.assertRaises(StagingError): |
| 157 | + write_staging( |
| 158 | + tmp, report=_report(), proposed_skill=None, proposed_memory=None, |
| 159 | + live_skill_path=os.path.join(tmp, "live", "SKILL.md"), |
| 160 | + live_memory_path=os.path.join(tmp, "live", "CLAUDE.md"), |
| 161 | + report_md="# report\n", |
| 162 | + skill_proposals=[_proposal("alpha", live="relative/SKILL.md")], |
| 163 | + ) |
| 164 | + for root, _dirs, files in os.walk(tmp): |
| 165 | + self.assertNotIn("manifest.json", files, root) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + unittest.main() |
0 commit comments