/sync answered changed: False whenever the hash matched, even when the last apply left conflicts — so once the machine's own skill was removed, Sync now never linked the synced one until the bundle changed (hit on the Orus Mac with linear-metadata). A same-hash bundle with conflicts is applied again (needs_apply). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
4.6 KiB
Python
115 lines
4.6 KiB
Python
"""labsync: the lab's portable skills and agents, installed on a worker."""
|
|
|
|
import base64
|
|
import os
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
import labsync
|
|
|
|
|
|
def _b64(text):
|
|
return base64.b64encode(text.encode()).decode()
|
|
|
|
|
|
def _body(h="aaaa1111", skills=None, agents=None, execs=()):
|
|
return labsync.SyncBody(hash=h, skills=skills or {}, agents=agents or {},
|
|
executables=list(execs))
|
|
|
|
|
|
def _cfg(tmp_path):
|
|
return [tmp_path / ".claude", tmp_path / ".claude-work"]
|
|
|
|
|
|
def test_apply_links_every_config_dir(tmp_path):
|
|
root = tmp_path / "lab-sync"
|
|
body = _body(skills={"caveman": {"SKILL.md": _b64("---\nname: caveman\n---\n"),
|
|
"scripts/run.sh": _b64("#!/bin/sh\necho hi\n")}},
|
|
agents={"helper": "---\nname: helper\n---\nhi"},
|
|
execs=["caveman/scripts/run.sh"])
|
|
st = labsync.apply(body, root, _cfg(tmp_path))
|
|
assert st["skills"] == ["caveman"] and st["agents"] == ["helper"]
|
|
assert st["conflicts"] == []
|
|
for cfg in _cfg(tmp_path):
|
|
link = cfg / "skills" / "caveman"
|
|
assert link.is_symlink()
|
|
assert (link / "SKILL.md").read_text().startswith("---")
|
|
assert os.access(link / "scripts" / "run.sh", os.X_OK)
|
|
assert (cfg / "agents" / "helper.md").read_text().endswith("hi")
|
|
assert labsync.state(root)["hash"] == "aaaa1111"
|
|
|
|
|
|
def test_machine_own_skill_wins(tmp_path):
|
|
root = tmp_path / "lab-sync"
|
|
cfg = tmp_path / ".claude"
|
|
mine = cfg / "skills" / "caveman"
|
|
mine.mkdir(parents=True)
|
|
(mine / "SKILL.md").write_text("mine")
|
|
st = labsync.apply(_body(skills={"caveman": {"SKILL.md": _b64("lab")}}), root, [cfg])
|
|
assert st["conflicts"] == [str(mine)]
|
|
assert not mine.is_symlink() and (mine / "SKILL.md").read_text() == "mine"
|
|
|
|
|
|
def test_dropped_items_lose_their_links_only(tmp_path):
|
|
root = tmp_path / "lab-sync"
|
|
cfg = tmp_path / ".claude"
|
|
labsync.apply(_body("aaaa1111", skills={"a": {"SKILL.md": _b64("a")},
|
|
"b": {"SKILL.md": _b64("b")}},
|
|
agents={"x": "x"}), root, [cfg])
|
|
st = labsync.apply(_body("bbbb2222", skills={"a": {"SKILL.md": _b64("a2")}}),
|
|
root, [cfg])
|
|
assert st["skills"] == ["a"] and st["agents"] == []
|
|
assert (cfg / "skills" / "a" / "SKILL.md").read_text() == "a2"
|
|
assert not (cfg / "skills" / "b").exists()
|
|
assert not (cfg / "agents" / "x.md").exists()
|
|
# current + the one before stay on disk, nothing older.
|
|
labsync.apply(_body("cccc3333", skills={"a": {"SKILL.md": _b64("a3")}}), root, [cfg])
|
|
assert sorted(p.name for p in root.glob("v-*")) == ["v-bbbb2222", "v-cccc3333"]
|
|
|
|
|
|
def test_an_empty_bundle_unlinks_everything(tmp_path):
|
|
root = tmp_path / "lab-sync"
|
|
cfg = tmp_path / ".claude"
|
|
labsync.apply(_body("aaaa1111", skills={"a": {"SKILL.md": _b64("a")}}), root, [cfg])
|
|
st = labsync.apply(_body("e3b0c44298fc1c14"), root, [cfg])
|
|
assert st["skills"] == [] and st["hash"] == "e3b0c44298fc1c14"
|
|
assert not (cfg / "skills" / "a").exists()
|
|
assert (root / "current").resolve() == (root / "v-e3b0c44298fc1c14").resolve()
|
|
|
|
|
|
def test_bad_paths_are_refused(tmp_path):
|
|
root = tmp_path / "lab-sync"
|
|
for rel in ("../evil", "/etc/passwd", "a/../../b", ""):
|
|
with pytest.raises(HTTPException):
|
|
labsync.apply(_body(skills={"s": {"SKILL.md": _b64("s"), rel: _b64("x")}}),
|
|
root, [tmp_path / ".claude"])
|
|
with pytest.raises(HTTPException):
|
|
labsync.apply(_body(skills={"../s": {"SKILL.md": _b64("s")}}), root, [])
|
|
with pytest.raises(HTTPException):
|
|
labsync.apply(_body(skills={"s": {"README.md": _b64("no SKILL.md")}}), root, [])
|
|
assert not list(root.glob("v-*")) and not list(root.glob(".stage-*"))
|
|
|
|
|
|
def test_state_before_first_sync(tmp_path):
|
|
assert labsync.state(tmp_path / "nothing") == {"hash": None}
|
|
|
|
|
|
def test_same_hash_with_conflicts_is_applied_again(tmp_path):
|
|
"""Sync now after the machine's own skill was removed links the lab's."""
|
|
root = tmp_path / "lab-sync"
|
|
cfg = tmp_path / ".claude"
|
|
mine = cfg / "skills" / "caveman"
|
|
mine.mkdir(parents=True)
|
|
(mine / "SKILL.md").write_text("mine")
|
|
body = _body(skills={"caveman": {"SKILL.md": _b64("lab")}})
|
|
st = labsync.apply(body, root, [cfg])
|
|
assert st["conflicts"] and labsync.needs_apply(st, body.hash)
|
|
(mine / "SKILL.md").unlink()
|
|
mine.rmdir()
|
|
st = labsync.apply(body, root, [cfg])
|
|
assert st["conflicts"] == [] and mine.is_symlink()
|
|
assert (mine / "SKILL.md").read_text() == "lab"
|
|
assert not labsync.needs_apply(st, body.hash)
|
|
assert labsync.needs_apply(st, "bbbb2222")
|