diff --git a/backend/app/services/clips.py b/backend/app/services/clips.py index e48d9c8..8123283 100644 --- a/backend/app/services/clips.py +++ b/backend/app/services/clips.py @@ -125,6 +125,9 @@ def update_clip( expected_version: int | None = None, ) -> Clip: """Apply a partial update. With expected_version, a concurrent change raises ClipConflict.""" + # Early check so a stale client gets the conflict, not a follow-up validation error + if expected_version is not None and clip.version != expected_version: + raise ClipConflict(clip) kind = changes.get("kind", clip.kind) title = changes.get("title", clip.title) content = changes.get("content", clip.content) diff --git a/backend/tests/test_projects.py b/backend/tests/test_projects.py index 9f7f850..4226b93 100644 --- a/backend/tests/test_projects.py +++ b/backend/tests/test_projects.py @@ -141,6 +141,19 @@ def test_delete_project_keeps_clips(anna): assert current["updated_at"] == clip["updated_at"] +def test_stale_editor_gets_conflict_after_project_deletion(anna): + project = create_project(anna) + clip = create_clip(anna, project_id=project["id"]) + anna.delete(f"/api/projects/{project['id']}") + + response = anna.patch( + f"/api/clips/{clip['id']}", + json={"content": "edit", "project_id": project["id"], "version": clip["version"]}, + ) + assert response.status_code == 409 + assert response.json()["current"]["project_id"] is None + + def test_requires_login(client): assert client.get("/api/projects").status_code == 401