fix: make Search watch admission recoverable (Closes #1295)
This commit is contained in:
1 parent
4fe07bcd52
commit
6c12731215
7 files changed
+213
-15
No files matched your search
@@ -838,6 +838,37 @@ process.stdout.write(JSON.stringify({{calls,status:final.status,number:final.det
|
||||
}
|
||||
|
||||
|
||||
def test_search_preview_preserves_authoritative_watch_and_offers_following_repair():
|
||||
script = f"""
|
||||
const createSearchPreview = require({json.dumps(str(SEARCH_PREVIEW))});
|
||||
(async () => {{
|
||||
const states = [];
|
||||
const detail = {{repository:'stackchain/api',number:42,kind:'issue',state:'open',claimable:true,watching:false}};
|
||||
const preview = createSearchPreview({{
|
||||
fetchJson:async()=>detail,
|
||||
watch:async()=>({{watching:true,following_synced:false,error:'Watching in Gitea, but Following could not sync. Retry this action.'}}),
|
||||
onState:state=>states.push(state),
|
||||
}});
|
||||
await preview.open(detail);
|
||||
await preview.setWatching(true);
|
||||
const final=states.at(-1);
|
||||
process.stdout.write(JSON.stringify({{
|
||||
status:final.status,
|
||||
watching:final.detail.watching,
|
||||
message:globalThis.searchPreviewWatchStatus(final),
|
||||
}}));
|
||||
}})().catch(error=>{{console.error(error);process.exit(1);}});
|
||||
"""
|
||||
result = subprocess.run(["node", "-e", script], capture_output=True, text=True)
|
||||
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert json.loads(result.stdout) == {
|
||||
"status": "watch-partial",
|
||||
"watching": True,
|
||||
"message": "Watching in Gitea, but Following could not sync. Tap Stop watching, then Watch issue to repair.",
|
||||
}
|
||||
|
||||
|
||||
def test_mobile_search_preview_exposes_touch_safe_watch_action():
|
||||
html = dashboard_bundle_text()
|
||||
css = (FRONTEND / "dashboard.css").read_text()
|
||||
|
||||
@@ -53,3 +53,125 @@ async def test_confirmed_watch_updates_account_following_collection(monkeypatch,
|
||||
"updated_at": "2026-08-23T03:00:00Z",
|
||||
"url": "https://forge.example/stackchain/api/issues/42",
|
||||
}]}
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_full_following_collection_rejects_watch_before_gitea_mutation(monkeypatch, tmp_path):
|
||||
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"b" * 32, limit=1)
|
||||
store.set_watching("timmy", {
|
||||
"repository": "stackchain/api", "number": 7, "title": "Already followed",
|
||||
"state": "open", "updated_at": "2026-08-23T02:00:00Z",
|
||||
"url": "https://forge.example/stackchain/api/issues/7",
|
||||
}, True)
|
||||
calls = []
|
||||
|
||||
async def preview(repository, kind, number):
|
||||
return {
|
||||
"repository": repository, "kind": kind, "number": number,
|
||||
"title": "Another issue", "state": "open",
|
||||
"updated_at": "2026-08-23T03:00:00Z",
|
||||
"url": "https://forge.example/stackchain/api/issues/42", "claimable": True,
|
||||
}
|
||||
|
||||
async def set_subscription(*args):
|
||||
calls.append(args)
|
||||
return {"watching": True}
|
||||
|
||||
async def user():
|
||||
return {"login": "timmy"}
|
||||
|
||||
monkeypatch.setattr(main, "_following_store", lambda: store)
|
||||
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
|
||||
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
|
||||
monkeypatch.setattr(main, "current_user", user)
|
||||
|
||||
transport = httpx.ASGITransport(app=main.app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
response = await client.put(
|
||||
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
|
||||
)
|
||||
|
||||
assert response.status_code == 503
|
||||
assert calls == []
|
||||
assert store.get("timmy")["items"][0]["number"] == 7
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_following_write_failure_compensates_confirmed_gitea_watch(monkeypatch, tmp_path):
|
||||
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"c" * 32)
|
||||
calls = []
|
||||
|
||||
async def preview(repository, kind, number):
|
||||
return {
|
||||
"repository": repository, "kind": kind, "number": number,
|
||||
"title": "Recoverable watch", "state": "open",
|
||||
"updated_at": "2026-08-23T03:00:00Z",
|
||||
"url": "https://forge.example/stackchain/api/issues/42", "claimable": True,
|
||||
}
|
||||
|
||||
async def set_subscription(repository, number, watching):
|
||||
calls.append(watching)
|
||||
return {"watching": watching}
|
||||
|
||||
async def user():
|
||||
return {"login": "timmy"}
|
||||
|
||||
def fail_write(*args):
|
||||
raise OSError("disk unavailable")
|
||||
|
||||
monkeypatch.setattr(main, "_following_store", lambda: store)
|
||||
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
|
||||
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
|
||||
monkeypatch.setattr(main, "current_user", user)
|
||||
monkeypatch.setattr(store, "set_watching", fail_write)
|
||||
|
||||
transport = httpx.ASGITransport(app=main.app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
response = await client.put(
|
||||
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
|
||||
)
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.json() == {"error": "Watch status was not changed. Please retry."}
|
||||
assert calls == [True, False]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_uncompensated_following_write_reports_authoritative_partial_outcome(monkeypatch, tmp_path):
|
||||
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"d" * 32)
|
||||
calls = []
|
||||
|
||||
async def preview(repository, kind, number):
|
||||
return {
|
||||
"repository": repository, "kind": kind, "number": number,
|
||||
"title": "Split watch", "state": "open",
|
||||
"updated_at": "2026-08-23T03:00:00Z",
|
||||
"url": "https://forge.example/stackchain/api/issues/42", "claimable": True,
|
||||
}
|
||||
|
||||
async def set_subscription(repository, number, watching):
|
||||
calls.append(watching)
|
||||
return {"watching": True}
|
||||
|
||||
async def user():
|
||||
return {"login": "timmy"}
|
||||
|
||||
monkeypatch.setattr(main, "_following_store", lambda: store)
|
||||
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
|
||||
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
|
||||
monkeypatch.setattr(main, "current_user", user)
|
||||
monkeypatch.setattr(store, "set_watching", lambda *args: (_ for _ in ()).throw(OSError()))
|
||||
|
||||
transport = httpx.ASGITransport(app=main.app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
response = await client.put(
|
||||
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"watching": True,
|
||||
"following_synced": False,
|
||||
"error": "Watching in Gitea, but Following could not sync. Retry this action.",
|
||||
}
|
||||
assert calls == [True, False]
|
||||
@@ -784,6 +784,7 @@ async def test_work_preview_normalizes_details_and_only_allows_unassigned_open_i
|
||||
"title": "Repair queue",
|
||||
"body": "Keep mobile operators moving.",
|
||||
"state": "open",
|
||||
"updated_at": "2026-08-23T03:00:00Z",
|
||||
"html_url": "http://127.0.0.1:3000/stackchain/api/issues/42",
|
||||
"user": {"login": "alex"},
|
||||
"labels": [{"name": "P1"}, None],
|
||||
@@ -803,6 +804,7 @@ async def test_work_preview_normalizes_details_and_only_allows_unassigned_open_i
|
||||
"title": "Repair queue",
|
||||
"body": "Keep mobile operators moving.",
|
||||
"state": "open",
|
||||
"updated_at": "2026-08-23T03:00:00Z",
|
||||
"author": "alex",
|
||||
"labels": ["P1"],
|
||||
"assignees": [],
|
||||
|
||||
Reference in new issue
Block a user