CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / TypeScript typecheck (push) Successful in 57s
CI & Build / integration (push) Successful in 1m19s
CI & Build / Python tests (push) Successful in 2m2s
CI & Build / Build & push image (push) Successful in 15s
Docstrings, section headers and assertion messages drop the record number, milestone or step that introduced the behaviour, and incident accounts are restated as the present-tense failure shape. A dead pointer to a design doc that is not in the repo is removed. Renamed to the property they check: test_the_old_shape_would_be_caught -> test_a_rank_then_scope_query_is_caught test_a_ledger_repeat_no_longer_produces_that_case_at_all -> test_a_ledger_repeat_is_counted_not_withheld test_the_header_no_longer_promises_once_per_session -> test_the_header_explains_the_seen_marker test_the_portal_case_no_longer_stamps_one_canon_onto_a_whole_file -> test_one_canon_is_not_stamped_onto_a_whole_file test_the_repos_own_prose_no_longer_defines_with -> test_the_repos_own_prose_does_not_define_with test_the_original_regression_create_note_with_content -> test_the_real_server_refuses_content_on_create_note Fixture and example ids stay: "note 5" in a supersession chain, "rule 156" in an illustrative question, `#2275` as the hex-versus-citation case, `#3244` as the example of a bare number, fixed dates in date-filter tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
178 lines
7.6 KiB
Python
178 lines
7.6 KiB
Python
"""Tests for un-merge — reversing one source out of a merged survivor.
|
|
|
|
The design question is how to subtract without stripping call sites the
|
|
survivor legitimately owns. The answer is per-source
|
|
attribution recorded AT MERGE TIME: each `merged_from` entry holds only what
|
|
that source actually added. These tests pin that rule, and the refusal that
|
|
guards the case where the attribution isn't there.
|
|
"""
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from scribe.services import snippets as s
|
|
from tests.helpers import loc
|
|
|
|
|
|
def _survivor(locations, merged_from, tags=None, owner=7):
|
|
"""A survivor note whose `data` carries locations + merge provenance."""
|
|
return SimpleNamespace(
|
|
id=1, user_id=owner, note_type="snippet", deleted_at=None,
|
|
title="f — does a thing", tags=tags or ["python", "snippet"],
|
|
body=s.compose_body(code="x = 1", language="python", locations=locations,
|
|
merged_from=merged_from),
|
|
data=s.compose_data(name="f", when_to_use="does a thing", language="python",
|
|
code="x = 1", locations=locations,
|
|
merged_from=merged_from),
|
|
)
|
|
|
|
|
|
async def _run_unmerge(survivor, *, source_alive=None, restore=1):
|
|
"""Drive unmerge_snippet with the DB stubbed; return update_note's kwargs."""
|
|
async def fake_get(_uid, sid):
|
|
if sid == survivor.id:
|
|
return survivor
|
|
return source_alive
|
|
|
|
with (
|
|
patch.object(s, "get_snippet", AsyncMock(side_effect=fake_get)),
|
|
patch("scribe.services.access.can_write_note", AsyncMock(return_value=True)),
|
|
patch("scribe.services.trash.restore_entity", AsyncMock(return_value=restore)),
|
|
patch.object(s.notes_svc, "update_note",
|
|
AsyncMock(return_value=survivor)) as upd,
|
|
):
|
|
await s.unmerge_snippet(7, 1, 2)
|
|
return upd.await_args.kwargs
|
|
|
|
|
|
# --- the subtraction ------------------------------------------------------
|
|
|
|
|
|
async def test_unmerge_strips_only_what_the_source_contributed():
|
|
"""The survivor's own location survives; the source's is removed."""
|
|
mine, theirs = loc(path="mine.py", repo="r"), loc(path="theirs.py", repo="r")
|
|
survivor = _survivor(
|
|
[mine, theirs],
|
|
[{"id": 2, "locations": [theirs], "tags": ["helper"]}],
|
|
tags=["python", "snippet", "helper", "core"],
|
|
)
|
|
kwargs = await _run_unmerge(survivor)
|
|
assert kwargs["data"]["locations"] == [mine]
|
|
assert "helper" not in kwargs["tags"]
|
|
assert "core" in kwargs["tags"]
|
|
|
|
|
|
async def test_a_location_the_survivor_also_owned_is_never_stripped():
|
|
"""The central hazard the task named. If a source brought a location the
|
|
survivor ALREADY had, merge attributes nothing to it — so reversing must
|
|
leave that call site in place."""
|
|
shared = loc(path="shared.py", repo="r")
|
|
survivor = _survivor([shared], [{"id": 2, "locations": [], "tags": ["t"]}])
|
|
kwargs = await _run_unmerge(survivor)
|
|
assert kwargs["data"]["locations"] == [shared]
|
|
|
|
|
|
async def test_the_reversed_entry_leaves_the_provenance_list():
|
|
survivor = _survivor(
|
|
[loc(path="a.py", repo="r"), loc(path="b.py", repo="r")],
|
|
[{"id": 2, "locations": [loc(path="b.py", repo="r")]}, {"id": 3, "locations": []}],
|
|
)
|
|
kwargs = await _run_unmerge(survivor)
|
|
assert s.merged_from_ids(kwargs["data"]["merged_from"]) == [3]
|
|
assert "#2" not in kwargs["body"]
|
|
# The other source's history is untouched — un-merge reverses one thing.
|
|
assert "**Merged from:** #3" in kwargs["body"]
|
|
|
|
|
|
async def test_unmerging_the_last_source_clears_the_provenance_line():
|
|
survivor = _survivor([loc(path="a.py", repo="r")], [{"id": 2, "locations": [], "tags": ["t"]}])
|
|
kwargs = await _run_unmerge(survivor)
|
|
assert "Merged from" not in kwargs["body"]
|
|
|
|
|
|
# --- restoring the source -------------------------------------------------
|
|
|
|
|
|
async def test_an_already_restored_source_is_repaired_not_refused():
|
|
"""The scenario that motivated the feature: the operator restored the source
|
|
from the trash by hand, so both records claim its call sites and nothing ever
|
|
stripped the survivor's copy. Un-merge must fix that, not reject it."""
|
|
theirs = loc(path="theirs.py", repo="r")
|
|
survivor = _survivor([loc(path="mine.py", repo="r"), theirs],
|
|
[{"id": 2, "locations": [theirs]}])
|
|
alive = SimpleNamespace(id=2, user_id=7, note_type="snippet", deleted_at=None,
|
|
title="g — x", tags=["snippet"], body="", data=None)
|
|
with patch("scribe.services.trash.restore_entity", AsyncMock()) as revive:
|
|
kwargs = await _run_unmerge(survivor, source_alive=alive)
|
|
# Nothing to revive — it's already alive — but the subtraction still happens.
|
|
revive.assert_not_called()
|
|
assert kwargs["data"]["locations"] == [loc(path="mine.py", repo="r")]
|
|
|
|
|
|
async def test_a_purged_source_is_refused_and_the_survivor_is_untouched():
|
|
"""If the source can't come back, stripping the survivor would lose the
|
|
locations entirely — no record would claim them."""
|
|
survivor = _survivor([loc(path="a.py", repo="r")], [{"id": 2, "locations": [loc(path="a.py", repo="r")]}])
|
|
|
|
async def fake_get(_uid, sid):
|
|
return survivor if sid == 1 else None
|
|
|
|
with (
|
|
patch.object(s, "get_snippet", AsyncMock(side_effect=fake_get)),
|
|
patch("scribe.services.access.can_write_note", AsyncMock(return_value=True)),
|
|
patch("scribe.services.trash.restore_entity", AsyncMock(return_value=None)),
|
|
patch.object(s.notes_svc, "update_note", AsyncMock()) as upd,
|
|
):
|
|
with pytest.raises(s.UnmergeError, match="purged"):
|
|
await s.unmerge_snippet(7, 1, 2)
|
|
upd.assert_not_called()
|
|
|
|
|
|
# --- refusals -------------------------------------------------------------
|
|
|
|
|
|
async def test_an_entry_without_attribution_is_refused_not_guessed():
|
|
"""Bare-id provenance comes from parsing the body, which can only hold ids.
|
|
Subtracting a guess could strip call sites the survivor owns — so refuse and
|
|
say what to do instead."""
|
|
survivor = _survivor([loc(path="a.py", repo="r")], [2])
|
|
|
|
async def fake_get(_uid, sid):
|
|
return survivor if sid == 1 else None
|
|
|
|
with (
|
|
patch.object(s, "get_snippet", AsyncMock(side_effect=fake_get)),
|
|
patch("scribe.services.access.can_write_note", AsyncMock(return_value=True)),
|
|
patch.object(s.notes_svc, "update_note", AsyncMock()) as upd,
|
|
):
|
|
with pytest.raises(s.UnmergeError, match="provenance"):
|
|
await s.unmerge_snippet(7, 1, 2)
|
|
upd.assert_not_called()
|
|
|
|
|
|
async def test_unmerging_something_never_absorbed_is_refused():
|
|
survivor = _survivor([loc(path="a.py", repo="r")], [{"id": 99, "locations": []}])
|
|
with (
|
|
patch.object(s, "get_snippet", AsyncMock(return_value=survivor)),
|
|
patch("scribe.services.access.can_write_note", AsyncMock(return_value=True)),
|
|
):
|
|
with pytest.raises(s.UnmergeError, match="no record of absorbing"):
|
|
await s.unmerge_snippet(7, 1, 2)
|
|
|
|
|
|
async def test_unmerge_requires_write_access():
|
|
"""Same rule as merge: a read-only share can see the record, not rearrange it."""
|
|
survivor = _survivor([loc(path="a.py", repo="r")], [{"id": 2, "locations": []}])
|
|
with (
|
|
patch.object(s, "get_snippet", AsyncMock(return_value=survivor)),
|
|
patch("scribe.services.access.can_write_note", AsyncMock(return_value=False)),
|
|
):
|
|
with pytest.raises(PermissionError):
|
|
await s.unmerge_snippet(7, 1, 2)
|
|
|
|
|
|
async def test_unmerge_on_a_missing_survivor_returns_none():
|
|
with patch.object(s, "get_snippet", AsyncMock(return_value=None)):
|
|
assert await s.unmerge_snippet(7, 1, 2) is None
|