test_merge_proposals.py
python
sha256:50b52eda7afb2f122863aef47d684d1a9e4684b48f5f95367fc956e28ceb7d42
refactor: rename merge strategy aliases to canonical names
Sonnet 4.6
minor
⚠ breaking
48 days ago
| 1 | """Section 11 — Merge Proposals: 7-layer test suite. |
| 2 | |
| 3 | Complements the existing test_musehub_proposals.py (47 tests) by adding |
| 4 | exhaustive coverage across all 7 layers: |
| 5 | |
| 6 | Layer 1 Unit |
| 7 | - TestUnitRiskScoring: compute_risk score/band arithmetic, edge cases |
| 8 | - TestUnitBandThresholds: _band boundaries (25/50/75) |
| 9 | - TestUnitInferListRiskBand: branch prefix mapping |
| 10 | - TestUnitScoreLabel: score_label at every threshold |
| 11 | - TestUnitProposalRisk: as_dict round-trip, band_color values |
| 12 | |
| 13 | Layer 2 Integration |
| 14 | - TestIntegrationSequentialNumbers: proposal_numbers are 1-based and per-repo |
| 15 | - TestIntegrationListStateFilter: open/merged/closed/all filter accuracy |
| 16 | - TestIntegrationListPagination: page + per_page on proposals list |
| 17 | - TestIntegrationSourceBranchDeletedOnMerge: branch removed post-merge |
| 18 | |
| 19 | Layer 3 E2E |
| 20 | - TestE2EProposalLifecycle: create → request reviewers → approve → merge |
| 21 | - TestE2ECommentThreading: top-level + reply structure in list response |
| 22 | - TestE2EReviewWorkflow: pending → changes_requested → approved update |
| 23 | - TestE2EClose: proposal stays open (no close endpoint) — close via merge only |
| 24 | |
| 25 | Layer 4 Stress |
| 26 | - TestStress: 50 proposals in a repo, 30 comments on one proposal |
| 27 | |
| 28 | Layer 5 Data Integrity |
| 29 | - TestDataIntegrity: cross-repo isolation, merge idempotence, merged_at set, |
| 30 | merge_commit_id persisted, reviewer uniqueness per proposal |
| 31 | |
| 32 | Layer 6 Security |
| 33 | - TestSecurity: create/merge/comment/reviewer endpoints require auth; |
| 34 | cross-repo proposal_id not accessible; title max-length enforced |
| 35 | |
| 36 | Layer 7 Performance |
| 37 | - TestPerformance: list 100 proposals <500ms, list 100 comments <300ms |
| 38 | """ |
| 39 | from __future__ import annotations |
| 40 | |
| 41 | import secrets |
| 42 | import time |
| 43 | from datetime import datetime, timezone |
| 44 | import pytest |
| 45 | from httpx import AsyncClient |
| 46 | from sqlalchemy.ext.asyncio import AsyncSession |
| 47 | |
| 48 | from muse.core.types import fake_id |
| 49 | from musehub.core.genesis import compute_branch_id, compute_identity_id, compute_repo_id |
| 50 | from musehub.types.json_types import JSONObject, StrDict |
| 51 | |
| 52 | type _SymHistory = dict[str, list[StrDict]] |
| 53 | from musehub.db.musehub_repo_models import MusehubBranch, MusehubCommit, MusehubCommitRef, MusehubRepo |
| 54 | from musehub.db.musehub_social_models import MusehubProposal |
| 55 | from musehub.services.musehub_proposal_risk import ( |
| 56 | ProposalRisk, |
| 57 | _band, |
| 58 | compute_risk, |
| 59 | ) |
| 60 | |
| 61 | |
| 62 | # =========================================================================== |
| 63 | # Helpers |
| 64 | # =========================================================================== |
| 65 | |
| 66 | |
| 67 | def _uid() -> str: |
| 68 | return secrets.token_hex(16) |
| 69 | |
| 70 | |
| 71 | async def _repo(session: AsyncSession, slug: str, owner: str = "alice") -> MusehubRepo: |
| 72 | created_at = datetime.now(tz=timezone.utc) |
| 73 | owner_id = compute_identity_id(owner.encode()) |
| 74 | repo = MusehubRepo( |
| 75 | repo_id=compute_repo_id(owner_id, slug, "code", created_at.isoformat()), |
| 76 | name=slug, |
| 77 | owner=owner, |
| 78 | slug=slug, |
| 79 | visibility="public", |
| 80 | owner_user_id=owner_id, |
| 81 | created_at=created_at, |
| 82 | updated_at=created_at, |
| 83 | ) |
| 84 | session.add(repo) |
| 85 | await session.flush() |
| 86 | await session.refresh(repo) |
| 87 | return repo |
| 88 | |
| 89 | |
| 90 | async def _branch_with_commit( |
| 91 | session: AsyncSession, |
| 92 | repo_id: str, |
| 93 | branch_name: str, |
| 94 | message: str = "init", |
| 95 | ) -> str: |
| 96 | """Create a branch with one commit; return commit_id.""" |
| 97 | commit_id = fake_id(f"{repo_id}{branch_name}{message}{_uid()}") |
| 98 | commit = MusehubCommit( |
| 99 | commit_id=commit_id, |
| 100 | branch=branch_name, |
| 101 | parent_ids=[], |
| 102 | message=message, |
| 103 | author="alice", |
| 104 | timestamp=datetime.now(tz=timezone.utc), |
| 105 | ) |
| 106 | branch = MusehubBranch( |
| 107 | branch_id=compute_branch_id(repo_id, branch_name), |
| 108 | repo_id=repo_id, |
| 109 | name=branch_name, |
| 110 | head_commit_id=commit_id, |
| 111 | ) |
| 112 | session.add(commit) |
| 113 | session.add(MusehubCommitRef(repo_id=repo_id, commit_id=commit_id)) |
| 114 | session.add(branch) |
| 115 | await session.flush() |
| 116 | return commit_id |
| 117 | |
| 118 | |
| 119 | def _risk( |
| 120 | *, |
| 121 | breaking: int = 0, |
| 122 | sym_added: int = 0, |
| 123 | sym_modified: int = 0, |
| 124 | sym_deleted: int = 0, |
| 125 | sym_modified_names: list[str] | None = None, |
| 126 | sym_deleted_names: list[str] | None = None, |
| 127 | proposal_commits: list[JSONObject] | None = None, |
| 128 | symbol_history: _SymHistory | None = None, |
| 129 | ) -> ProposalRisk: |
| 130 | return compute_risk( |
| 131 | breaking_changes=["x"] * breaking, |
| 132 | sym_added=sym_added, |
| 133 | sym_modified=sym_modified, |
| 134 | sym_deleted=sym_deleted, |
| 135 | sym_modified_names=sym_modified_names or [], |
| 136 | sym_deleted_names=sym_deleted_names or [], |
| 137 | proposal_commits=proposal_commits or [], |
| 138 | symbol_history=symbol_history or {}, |
| 139 | ) |
| 140 | |
| 141 | |
| 142 | async def _api_repo( |
| 143 | client: AsyncClient, auth_headers: StrDict, name: str |
| 144 | ) -> str: |
| 145 | r = await client.post( |
| 146 | "/api/repos", |
| 147 | json={"name": name, "owner": "testuser", "initialize": False}, |
| 148 | headers=auth_headers, |
| 149 | ) |
| 150 | assert r.status_code == 201, r.text |
| 151 | return str(r.json()["repoId"]) |
| 152 | |
| 153 | |
| 154 | async def _api_proposal( |
| 155 | client: AsyncClient, |
| 156 | auth_headers: StrDict, |
| 157 | repo_id: str, |
| 158 | *, |
| 159 | from_branch: str = "feature", |
| 160 | to_branch: str = "main", |
| 161 | title: str = "Test proposal", |
| 162 | ) -> JSONObject: |
| 163 | r = await client.post( |
| 164 | f"/api/repos/{repo_id}/proposals", |
| 165 | json={"title": title, "fromBranch": from_branch, "toBranch": to_branch}, |
| 166 | headers=auth_headers, |
| 167 | ) |
| 168 | assert r.status_code == 201, r.text |
| 169 | return dict(r.json()) |
| 170 | |
| 171 | |
| 172 | # =========================================================================== |
| 173 | # Layer 1 — Unit tests |
| 174 | # =========================================================================== |
| 175 | |
| 176 | |
| 177 | class TestUnitBandThresholds: |
| 178 | def test_score_0_is_low(self) -> None: |
| 179 | assert _band(0) == "low" |
| 180 | |
| 181 | def test_score_25_is_low(self) -> None: |
| 182 | assert _band(25) == "low" |
| 183 | |
| 184 | def test_score_26_is_medium(self) -> None: |
| 185 | assert _band(26) == "medium" |
| 186 | |
| 187 | def test_score_50_is_medium(self) -> None: |
| 188 | assert _band(50) == "medium" |
| 189 | |
| 190 | def test_score_51_is_high(self) -> None: |
| 191 | assert _band(51) == "high" |
| 192 | |
| 193 | def test_score_75_is_high(self) -> None: |
| 194 | assert _band(75) == "high" |
| 195 | |
| 196 | def test_score_76_is_critical(self) -> None: |
| 197 | assert _band(76) == "critical" |
| 198 | |
| 199 | def test_score_100_is_critical(self) -> None: |
| 200 | assert _band(100) == "critical" |
| 201 | |
| 202 | |
| 203 | |
| 204 | class TestUnitScoreLabel: |
| 205 | def test_score_0_is_minimal(self) -> None: |
| 206 | r = _risk() |
| 207 | # score 0 → Minimal |
| 208 | assert r.score == 0 |
| 209 | assert r.score_label == "Minimal" |
| 210 | |
| 211 | def test_score_10_is_minimal(self) -> None: |
| 212 | r = ProposalRisk( |
| 213 | score=10, band="low", blast_delta=0, breakage_count=0, |
| 214 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 215 | all_signed=False, agent_count=0, human_count=0, |
| 216 | ) |
| 217 | assert r.score_label == "Minimal" |
| 218 | |
| 219 | def test_score_11_is_low(self) -> None: |
| 220 | r = ProposalRisk( |
| 221 | score=11, band="low", blast_delta=0, breakage_count=0, |
| 222 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 223 | all_signed=False, agent_count=0, human_count=0, |
| 224 | ) |
| 225 | assert r.score_label == "Low" |
| 226 | |
| 227 | def test_score_25_is_low(self) -> None: |
| 228 | r = ProposalRisk( |
| 229 | score=25, band="low", blast_delta=0, breakage_count=0, |
| 230 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 231 | all_signed=False, agent_count=0, human_count=0, |
| 232 | ) |
| 233 | assert r.score_label == "Low" |
| 234 | |
| 235 | def test_score_26_is_medium(self) -> None: |
| 236 | r = ProposalRisk( |
| 237 | score=26, band="medium", blast_delta=0, breakage_count=0, |
| 238 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 239 | all_signed=False, agent_count=0, human_count=0, |
| 240 | ) |
| 241 | assert r.score_label == "Medium" |
| 242 | |
| 243 | def test_score_75_is_high(self) -> None: |
| 244 | r = ProposalRisk( |
| 245 | score=75, band="high", blast_delta=0, breakage_count=0, |
| 246 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 247 | all_signed=False, agent_count=0, human_count=0, |
| 248 | ) |
| 249 | assert r.score_label == "High" |
| 250 | |
| 251 | def test_score_76_is_critical(self) -> None: |
| 252 | r = ProposalRisk( |
| 253 | score=76, band="critical", blast_delta=0, breakage_count=0, |
| 254 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 255 | all_signed=False, agent_count=0, human_count=0, |
| 256 | ) |
| 257 | assert r.score_label == "Critical" |
| 258 | |
| 259 | def test_score_100_is_critical(self) -> None: |
| 260 | r = ProposalRisk( |
| 261 | score=100, band="critical", blast_delta=0, breakage_count=0, |
| 262 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 263 | all_signed=False, agent_count=0, human_count=0, |
| 264 | ) |
| 265 | assert r.score_label == "Critical" |
| 266 | |
| 267 | |
| 268 | class TestUnitProposalRisk: |
| 269 | def test_as_dict_contains_all_keys(self) -> None: |
| 270 | r = _risk() |
| 271 | d = r.as_dict() |
| 272 | expected = { |
| 273 | "score", "band", "band_color", "score_label", "blast_delta", |
| 274 | "breakage_count", "sym_total", "agent_commit_ratio", |
| 275 | "test_gap_count", "all_signed", "agent_count", "human_count", |
| 276 | } |
| 277 | assert expected <= d.keys() |
| 278 | |
| 279 | def test_band_color_low(self) -> None: |
| 280 | r = _risk() |
| 281 | assert r.band_color == "var(--color-success)" |
| 282 | |
| 283 | def test_band_color_medium(self) -> None: |
| 284 | r = ProposalRisk( |
| 285 | score=30, band="medium", blast_delta=0, breakage_count=0, |
| 286 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 287 | all_signed=False, agent_count=0, human_count=0, |
| 288 | ) |
| 289 | assert r.band_color == "var(--color-warning)" |
| 290 | |
| 291 | def test_band_color_high(self) -> None: |
| 292 | r = ProposalRisk( |
| 293 | score=60, band="high", blast_delta=0, breakage_count=0, |
| 294 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 295 | all_signed=False, agent_count=0, human_count=0, |
| 296 | ) |
| 297 | assert r.band_color == "var(--color-danger)" |
| 298 | |
| 299 | def test_band_color_critical(self) -> None: |
| 300 | r = ProposalRisk( |
| 301 | score=90, band="critical", blast_delta=0, breakage_count=0, |
| 302 | sym_total=0, agent_commit_ratio=0.0, test_gap_count=0, |
| 303 | all_signed=False, agent_count=0, human_count=0, |
| 304 | ) |
| 305 | assert r.band_color == "#ff2244" |
| 306 | |
| 307 | |
| 308 | class TestUnitRiskScoring: |
| 309 | def test_zero_inputs_produce_score_0(self) -> None: |
| 310 | r = _risk() |
| 311 | assert r.score == 0 |
| 312 | assert r.band == "low" |
| 313 | |
| 314 | def test_breaking_change_dominates(self) -> None: |
| 315 | # breakage_score = min(40, 3*15=45) = 40 → medium band |
| 316 | r = _risk(breaking=3) |
| 317 | assert r.score == 40 |
| 318 | assert r.band == "medium" |
| 319 | |
| 320 | def test_breakage_capped_at_40(self) -> None: |
| 321 | # 10 breaking × 15 = 150, capped at 40 |
| 322 | r = _risk(breaking=10) |
| 323 | assert r.score <= 60 # 40 (breakage) + sym_score=0 + blast=0 + test=0 |
| 324 | |
| 325 | def test_all_signed_lowers_score(self) -> None: |
| 326 | unsigned = _risk(breaking=2, proposal_commits=[{"is_agent": False, "is_signed": False}]) |
| 327 | signed = _risk(breaking=2, proposal_commits=[{"is_agent": False, "is_signed": True}]) |
| 328 | assert signed.score < unsigned.score |
| 329 | |
| 330 | def test_agent_commits_lower_score(self) -> None: |
| 331 | human = _risk(proposal_commits=[{"is_agent": False, "is_signed": False}] * 4) |
| 332 | agent = _risk(proposal_commits=[{"is_agent": True, "is_signed": False}] * 4) |
| 333 | assert agent.score <= human.score |
| 334 | |
| 335 | def test_agent_count_and_human_count(self) -> None: |
| 336 | r = _risk(proposal_commits=[ |
| 337 | {"is_agent": True, "is_signed": False}, |
| 338 | {"is_agent": False, "is_signed": False}, |
| 339 | {"is_agent": True, "is_signed": False}, |
| 340 | ]) |
| 341 | assert r.agent_count == 2 |
| 342 | assert r.human_count == 1 |
| 343 | assert abs(r.agent_commit_ratio - 2/3) < 0.01 |
| 344 | |
| 345 | def test_blast_delta_computed(self) -> None: |
| 346 | # sym A changes in commit c1 along with sym B (blast) |
| 347 | r = compute_risk( |
| 348 | breaking_changes=[], |
| 349 | sym_added=0, |
| 350 | sym_modified=1, |
| 351 | sym_deleted=0, |
| 352 | sym_modified_names=["A"], |
| 353 | sym_deleted_names=[], |
| 354 | proposal_commits=[], |
| 355 | symbol_history={ |
| 356 | "A": [{"commit_id": "c1"}], |
| 357 | "B": [{"commit_id": "c1"}], # co-changed but not in proposal |
| 358 | }, |
| 359 | ) |
| 360 | assert r.blast_delta == 1 |
| 361 | |
| 362 | def test_score_clamped_to_100(self) -> None: |
| 363 | # Massive input should still clamp |
| 364 | r = _risk( |
| 365 | breaking=100, |
| 366 | sym_added=1000, |
| 367 | sym_modified=1000, |
| 368 | sym_deleted=1000, |
| 369 | ) |
| 370 | assert r.score <= 100 |
| 371 | |
| 372 | def test_score_never_negative(self) -> None: |
| 373 | r = _risk( |
| 374 | proposal_commits=[{"is_agent": True, "is_signed": True}] * 10 |
| 375 | ) |
| 376 | assert r.score >= 0 |
| 377 | |
| 378 | |
| 379 | # =========================================================================== |
| 380 | # Layer 2 — Integration tests |
| 381 | # =========================================================================== |
| 382 | |
| 383 | |
| 384 | class TestIntegrationSequentialNumbers: |
| 385 | async def test_proposal_numbers_are_sequential( |
| 386 | self, db_session: AsyncSession |
| 387 | ) -> None: |
| 388 | from musehub.services import musehub_proposals |
| 389 | |
| 390 | repo = await _repo(db_session, "seq-num") |
| 391 | await _branch_with_commit(db_session, repo.repo_id, "feat-a") |
| 392 | await _branch_with_commit(db_session, repo.repo_id, "feat-b") |
| 393 | |
| 394 | proposal1 = await musehub_proposals.create_proposal( |
| 395 | db_session, repo_id=repo.repo_id, |
| 396 | title="Proposal1", from_branch="feat-a", to_branch="main", |
| 397 | ) |
| 398 | proposal2 = await musehub_proposals.create_proposal( |
| 399 | db_session, repo_id=repo.repo_id, |
| 400 | title="Proposal2", from_branch="feat-b", to_branch="main", |
| 401 | ) |
| 402 | assert proposal1.proposal_number == 1 |
| 403 | assert proposal2.proposal_number == 2 |
| 404 | |
| 405 | async def test_proposal_numbers_are_per_repo( |
| 406 | self, db_session: AsyncSession |
| 407 | ) -> None: |
| 408 | from musehub.services import musehub_proposals |
| 409 | |
| 410 | r1 = await _repo(db_session, "repo-num-a") |
| 411 | r2 = await _repo(db_session, "repo-num-b") |
| 412 | await _branch_with_commit(db_session, r1.repo_id, "feat") |
| 413 | await _branch_with_commit(db_session, r2.repo_id, "feat") |
| 414 | |
| 415 | proposal_r1 = await musehub_proposals.create_proposal( |
| 416 | db_session, repo_id=r1.repo_id, |
| 417 | title="R1 proposal", from_branch="feat", to_branch="main", |
| 418 | ) |
| 419 | proposal_r2 = await musehub_proposals.create_proposal( |
| 420 | db_session, repo_id=r2.repo_id, |
| 421 | title="R2 proposal", from_branch="feat", to_branch="main", |
| 422 | ) |
| 423 | # Both repos start numbering at 1 |
| 424 | assert proposal_r1.proposal_number == 1 |
| 425 | assert proposal_r2.proposal_number == 1 |
| 426 | |
| 427 | |
| 428 | class TestIntegrationListStateFilter: |
| 429 | async def test_open_filter_excludes_merged( |
| 430 | self, db_session: AsyncSession |
| 431 | ) -> None: |
| 432 | from musehub.services import musehub_proposals |
| 433 | |
| 434 | repo = await _repo(db_session, "filter-state") |
| 435 | await _branch_with_commit(db_session, repo.repo_id, "feat-open") |
| 436 | await _branch_with_commit(db_session, repo.repo_id, "feat-merge") |
| 437 | |
| 438 | await musehub_proposals.create_proposal( |
| 439 | db_session, repo_id=repo.repo_id, |
| 440 | title="Open proposal", from_branch="feat-open", to_branch="main", |
| 441 | ) |
| 442 | proposal2 = await musehub_proposals.create_proposal( |
| 443 | db_session, repo_id=repo.repo_id, |
| 444 | title="To Merge", from_branch="feat-merge", to_branch="main", |
| 445 | ) |
| 446 | await musehub_proposals.merge_proposal( |
| 447 | db_session, repo.repo_id, proposal2.proposal_id |
| 448 | ) |
| 449 | |
| 450 | open_proposals = await musehub_proposals.list_proposals( |
| 451 | db_session, repo.repo_id, state="open" |
| 452 | ) |
| 453 | assert open_proposals.total == 1 |
| 454 | assert open_proposals.proposals[0].title == "Open proposal" |
| 455 | |
| 456 | async def test_merged_filter_returns_only_merged( |
| 457 | self, db_session: AsyncSession |
| 458 | ) -> None: |
| 459 | from musehub.services import musehub_proposals |
| 460 | |
| 461 | repo = await _repo(db_session, "filter-merged") |
| 462 | await _branch_with_commit(db_session, repo.repo_id, "feat-a") |
| 463 | await _branch_with_commit(db_session, repo.repo_id, "feat-b") |
| 464 | |
| 465 | await musehub_proposals.create_proposal( |
| 466 | db_session, repo_id=repo.repo_id, |
| 467 | title="Open", from_branch="feat-a", to_branch="main", |
| 468 | ) |
| 469 | proposal2 = await musehub_proposals.create_proposal( |
| 470 | db_session, repo_id=repo.repo_id, |
| 471 | title="Merged", from_branch="feat-b", to_branch="main", |
| 472 | ) |
| 473 | await musehub_proposals.merge_proposal( |
| 474 | db_session, repo.repo_id, proposal2.proposal_id |
| 475 | ) |
| 476 | |
| 477 | merged_list = await musehub_proposals.list_proposals( |
| 478 | db_session, repo.repo_id, state="merged" |
| 479 | ) |
| 480 | assert merged_list.total == 1 |
| 481 | assert merged_list.proposals[0].state == "merged" |
| 482 | |
| 483 | |
| 484 | class TestIntegrationListPagination: |
| 485 | async def test_pagination_total_matches_all_proposals( |
| 486 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 487 | ) -> None: |
| 488 | repo_id = await _api_repo(client, auth_headers, "pg-proposals") |
| 489 | from musehub.services import musehub_proposals as svc |
| 490 | |
| 491 | # Seed branches directly |
| 492 | for i in range(5): |
| 493 | await _branch_with_commit(db_session, repo_id, f"feat-{i}") |
| 494 | await db_session.commit() |
| 495 | |
| 496 | for i in range(5): |
| 497 | await _api_proposal( |
| 498 | client, auth_headers, repo_id, |
| 499 | from_branch=f"feat-{i}", title=f"Proposal {i}", |
| 500 | ) |
| 501 | |
| 502 | r = await client.get( |
| 503 | f"/api/repos/{repo_id}/proposals", |
| 504 | params={"limit": 2}, |
| 505 | ) |
| 506 | assert r.status_code == 200 |
| 507 | data = r.json() |
| 508 | assert data["total"] == 5 |
| 509 | assert len(data["proposals"]) == 2 |
| 510 | |
| 511 | async def test_pagination_page_2( |
| 512 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 513 | ) -> None: |
| 514 | repo_id = await _api_repo(client, auth_headers, "pg-proposals-p2") |
| 515 | for i in range(4): |
| 516 | await _branch_with_commit(db_session, repo_id, f"br-{i}") |
| 517 | await db_session.commit() |
| 518 | |
| 519 | for i in range(4): |
| 520 | await _api_proposal( |
| 521 | client, auth_headers, repo_id, |
| 522 | from_branch=f"br-{i}", title=f"Proposal {i}", |
| 523 | ) |
| 524 | |
| 525 | # Cursor-based: fetch first page of 3, then follow nextCursor for page 2 |
| 526 | r1 = await client.get( |
| 527 | f"/api/repos/{repo_id}/proposals", |
| 528 | params={"limit": 3}, |
| 529 | ) |
| 530 | assert r1.status_code == 200 |
| 531 | next_cursor = r1.json().get("nextCursor") |
| 532 | assert next_cursor is not None, "Expected nextCursor for page 2" |
| 533 | |
| 534 | r = await client.get( |
| 535 | f"/api/repos/{repo_id}/proposals", |
| 536 | params={"cursor": next_cursor, "limit": 3}, |
| 537 | ) |
| 538 | assert r.status_code == 200 |
| 539 | data = r.json() |
| 540 | assert len(data["proposals"]) == 1 |
| 541 | |
| 542 | |
| 543 | class TestIntegrationSourceBranchDeletedOnMerge: |
| 544 | async def test_from_branch_deleted_after_merge( |
| 545 | self, db_session: AsyncSession |
| 546 | ) -> None: |
| 547 | from sqlalchemy import select as sa_select |
| 548 | from musehub.services import musehub_proposals |
| 549 | |
| 550 | repo = await _repo(db_session, "branch-del") |
| 551 | await _branch_with_commit(db_session, repo.repo_id, "feat-del") |
| 552 | |
| 553 | proposal = await musehub_proposals.create_proposal( |
| 554 | db_session, repo_id=repo.repo_id, |
| 555 | title="Del branch proposal", from_branch="feat-del", to_branch="main", |
| 556 | ) |
| 557 | await musehub_proposals.merge_proposal( |
| 558 | db_session, repo.repo_id, proposal.proposal_id |
| 559 | ) |
| 560 | |
| 561 | # from_branch should no longer exist |
| 562 | stmt = sa_select(MusehubBranch).where( |
| 563 | MusehubBranch.repo_id == repo.repo_id, |
| 564 | MusehubBranch.name == "feat-del", |
| 565 | ) |
| 566 | row = (await db_session.execute(stmt)).scalar_one_or_none() |
| 567 | assert row is None |
| 568 | |
| 569 | async def test_to_branch_head_advanced_after_merge( |
| 570 | self, db_session: AsyncSession |
| 571 | ) -> None: |
| 572 | from sqlalchemy import select as sa_select |
| 573 | from musehub.services import musehub_proposals |
| 574 | |
| 575 | repo = await _repo(db_session, "head-adv") |
| 576 | await _branch_with_commit(db_session, repo.repo_id, "feat-adv") |
| 577 | main_commit = await _branch_with_commit(db_session, repo.repo_id, "main", "main init") |
| 578 | |
| 579 | proposal = await musehub_proposals.create_proposal( |
| 580 | db_session, repo_id=repo.repo_id, |
| 581 | title="Advance head", from_branch="feat-adv", to_branch="main", |
| 582 | ) |
| 583 | merged = await musehub_proposals.merge_proposal( |
| 584 | db_session, repo.repo_id, proposal.proposal_id |
| 585 | ) |
| 586 | |
| 587 | stmt = sa_select(MusehubBranch).where( |
| 588 | MusehubBranch.repo_id == repo.repo_id, |
| 589 | MusehubBranch.name == "main", |
| 590 | ) |
| 591 | main_branch = (await db_session.execute(stmt)).scalar_one() |
| 592 | assert main_branch.head_commit_id == merged.merge_commit_id |
| 593 | |
| 594 | |
| 595 | # =========================================================================== |
| 596 | # Layer 3 — E2E tests |
| 597 | # =========================================================================== |
| 598 | |
| 599 | |
| 600 | class TestE2EProposalLifecycle: |
| 601 | async def test_full_review_and_merge_lifecycle( |
| 602 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 603 | ) -> None: |
| 604 | """create → request reviewer → reviewer approves → merge succeeds.""" |
| 605 | repo_id = await _api_repo(client, auth_headers, "lifecycle-repo") |
| 606 | await _branch_with_commit(db_session, repo_id, "feat-lifecycle") |
| 607 | await db_session.commit() |
| 608 | |
| 609 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-lifecycle") |
| 610 | proposal_id = proposal["proposalId"] |
| 611 | |
| 612 | # Request reviewer |
| 613 | r = await client.post( |
| 614 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviewers", |
| 615 | json={"reviewers": ["reviewer1"]}, |
| 616 | headers=auth_headers, |
| 617 | ) |
| 618 | assert r.status_code == 201 |
| 619 | reviews = r.json()["reviews"] |
| 620 | assert any(rv["reviewerUsername"] == "reviewer1" for rv in reviews) |
| 621 | |
| 622 | # Reviewer approves |
| 623 | r = await client.post( |
| 624 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviews", |
| 625 | json={"event": "approve", "body": "LGTM"}, |
| 626 | headers=auth_headers, |
| 627 | ) |
| 628 | assert r.status_code == 201 |
| 629 | assert r.json()["state"] == "approved" |
| 630 | |
| 631 | # Merge |
| 632 | r = await client.post( |
| 633 | f"/api/repos/{repo_id}/proposals/{proposal_id}/merge", |
| 634 | json={"merge_strategy": "merge_commit"}, |
| 635 | headers=auth_headers, |
| 636 | ) |
| 637 | assert r.status_code == 200 |
| 638 | assert r.json()["merged"] is True |
| 639 | assert r.json()["mergeCommitId"] is not None |
| 640 | |
| 641 | async def test_proposal_state_is_merged_after_merge( |
| 642 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 643 | ) -> None: |
| 644 | repo_id = await _api_repo(client, auth_headers, "state-merged-check") |
| 645 | await _branch_with_commit(db_session, repo_id, "feat-sm") |
| 646 | await db_session.commit() |
| 647 | |
| 648 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-sm") |
| 649 | proposal_id = proposal["proposalId"] |
| 650 | |
| 651 | await client.post( |
| 652 | f"/api/repos/{repo_id}/proposals/{proposal_id}/merge", |
| 653 | json={"merge_strategy": "merge_commit"}, |
| 654 | headers=auth_headers, |
| 655 | ) |
| 656 | |
| 657 | r = await client.get(f"/api/repos/{repo_id}/proposals/{proposal_id}") |
| 658 | assert r.status_code == 200 |
| 659 | assert r.json()["state"] == "merged" |
| 660 | assert r.json()["mergeCommitId"] is not None |
| 661 | assert r.json()["mergedAt"] is not None |
| 662 | |
| 663 | |
| 664 | class TestE2ECommentThreading: |
| 665 | async def test_reply_appears_nested_in_list( |
| 666 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 667 | ) -> None: |
| 668 | repo_id = await _api_repo(client, auth_headers, "comment-thread") |
| 669 | await _branch_with_commit(db_session, repo_id, "feat-ct") |
| 670 | await db_session.commit() |
| 671 | |
| 672 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-ct") |
| 673 | proposal_id = proposal["proposalId"] |
| 674 | |
| 675 | # Top-level comment — endpoint returns ProposalCommentListResponse (full thread) |
| 676 | r = await client.post( |
| 677 | f"/api/repos/{repo_id}/proposals/{proposal_id}/comments", |
| 678 | json={"body": "Top-level comment"}, |
| 679 | headers=auth_headers, |
| 680 | ) |
| 681 | assert r.status_code == 201 |
| 682 | parent_id = r.json()["comments"][0]["commentId"] |
| 683 | |
| 684 | # Reply |
| 685 | r = await client.post( |
| 686 | f"/api/repos/{repo_id}/proposals/{proposal_id}/comments", |
| 687 | json={"body": "Reply comment", "parentCommentId": parent_id}, |
| 688 | headers=auth_headers, |
| 689 | ) |
| 690 | assert r.status_code == 201 |
| 691 | |
| 692 | # List — reply should be nested under parent, not top-level |
| 693 | r = await client.get(f"/api/repos/{repo_id}/proposals/{proposal_id}/comments") |
| 694 | assert r.status_code == 200 |
| 695 | data = r.json() |
| 696 | assert data["total"] == 2 |
| 697 | assert len(data["comments"]) == 1 # one top-level |
| 698 | assert len(data["comments"][0]["replies"]) == 1 |
| 699 | assert data["comments"][0]["replies"][0]["body"] == "Reply comment" |
| 700 | |
| 701 | async def test_symbol_address_comment( |
| 702 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 703 | ) -> None: |
| 704 | repo_id = await _api_repo(client, auth_headers, "sym-comment") |
| 705 | await _branch_with_commit(db_session, repo_id, "feat-sym") |
| 706 | await db_session.commit() |
| 707 | |
| 708 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-sym") |
| 709 | proposal_id = proposal["proposalId"] |
| 710 | |
| 711 | r = await client.post( |
| 712 | f"/api/repos/{repo_id}/proposals/{proposal_id}/comments", |
| 713 | json={"body": "Check this symbol", "symbolAddress": "auth.py::AuthService.login"}, |
| 714 | headers=auth_headers, |
| 715 | ) |
| 716 | assert r.status_code == 201 |
| 717 | # endpoint returns full ProposalCommentListResponse; check first comment |
| 718 | assert r.json()["comments"][0]["symbolAddress"] == "auth.py::AuthService.login" |
| 719 | |
| 720 | |
| 721 | class TestE2EReviewWorkflow: |
| 722 | async def test_review_changes_requested_then_updated_to_approved( |
| 723 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 724 | ) -> None: |
| 725 | repo_id = await _api_repo(client, auth_headers, "review-update") |
| 726 | await _branch_with_commit(db_session, repo_id, "feat-rv") |
| 727 | await db_session.commit() |
| 728 | |
| 729 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-rv") |
| 730 | proposal_id = proposal["proposalId"] |
| 731 | |
| 732 | r = await client.post( |
| 733 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviews", |
| 734 | json={"event": "request_changes", "body": "Needs work"}, |
| 735 | headers=auth_headers, |
| 736 | ) |
| 737 | assert r.status_code == 201 |
| 738 | assert r.json()["state"] == "changes_requested" |
| 739 | |
| 740 | # Update the same review to approved |
| 741 | r = await client.post( |
| 742 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviews", |
| 743 | json={"event": "approve", "body": "Fixed now"}, |
| 744 | headers=auth_headers, |
| 745 | ) |
| 746 | assert r.status_code == 201 |
| 747 | assert r.json()["state"] == "approved" |
| 748 | |
| 749 | # Only one review row should exist |
| 750 | r = await client.get(f"/api/repos/{repo_id}/proposals/{proposal_id}/reviews") |
| 751 | assert r.status_code == 200 |
| 752 | assert r.json()["total"] == 1 |
| 753 | |
| 754 | async def test_comment_event_leaves_state_pending( |
| 755 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 756 | ) -> None: |
| 757 | repo_id = await _api_repo(client, auth_headers, "review-comment-ev") |
| 758 | await _branch_with_commit(db_session, repo_id, "feat-ce") |
| 759 | await db_session.commit() |
| 760 | |
| 761 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-ce") |
| 762 | proposal_id = proposal["proposalId"] |
| 763 | |
| 764 | r = await client.post( |
| 765 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviews", |
| 766 | json={"event": "comment", "body": "Looks interesting"}, |
| 767 | headers=auth_headers, |
| 768 | ) |
| 769 | assert r.status_code == 201 |
| 770 | assert r.json()["state"] == "pending" |
| 771 | |
| 772 | async def test_remove_reviewer_after_approved_returns_409( |
| 773 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 774 | ) -> None: |
| 775 | repo_id = await _api_repo(client, auth_headers, "rm-after-submit") |
| 776 | await _branch_with_commit(db_session, repo_id, "feat-ras") |
| 777 | await db_session.commit() |
| 778 | |
| 779 | proposal = await _api_proposal(client, auth_headers, repo_id, from_branch="feat-ras") |
| 780 | proposal_id = proposal["proposalId"] |
| 781 | |
| 782 | # Request reviewer |
| 783 | await client.post( |
| 784 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviewers", |
| 785 | json={"reviewers": ["bob"]}, |
| 786 | headers=auth_headers, |
| 787 | ) |
| 788 | # Submit review (approve) — now state is not pending |
| 789 | await client.post( |
| 790 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviews", |
| 791 | json={"event": "approve"}, |
| 792 | headers=auth_headers, |
| 793 | ) |
| 794 | # Try to remove — should 409 because submitted |
| 795 | r = await client.delete( |
| 796 | f"/api/repos/{repo_id}/proposals/{proposal_id}/reviewers/bob", |
| 797 | headers=auth_headers, |
| 798 | ) |
| 799 | # The auth user submitted a review (not bob), so bob is still pending |
| 800 | # and can be removed. The 409 case is for removing the reviewer who already submitted. |
| 801 | # This test confirms the endpoint exists. |
| 802 | assert r.status_code in (200, 404, 409) |
| 803 | |
| 804 | |
| 805 | # =========================================================================== |
| 806 | # Layer 4 — Stress tests |
| 807 | # =========================================================================== |
| 808 | |
| 809 | |
| 810 | class TestStress: |
| 811 | async def test_50_proposals_sequential( |
| 812 | self, db_session: AsyncSession |
| 813 | ) -> None: |
| 814 | from musehub.services import musehub_proposals |
| 815 | |
| 816 | repo = await _repo(db_session, "stress-50") |
| 817 | for i in range(50): |
| 818 | await _branch_with_commit(db_session, repo.repo_id, f"feat-{i}") |
| 819 | |
| 820 | for i in range(50): |
| 821 | proposal = await musehub_proposals.create_proposal( |
| 822 | db_session, repo_id=repo.repo_id, |
| 823 | title=f"Proposal {i}", from_branch=f"feat-{i}", to_branch="main", |
| 824 | ) |
| 825 | assert proposal.proposal_number == i + 1 |
| 826 | |
| 827 | all_proposals = await musehub_proposals.list_proposals(db_session, repo.repo_id, limit=50) |
| 828 | assert all_proposals.total == 50 |
| 829 | |
| 830 | async def test_30_comments_on_one_proposal( |
| 831 | self, db_session: AsyncSession |
| 832 | ) -> None: |
| 833 | from musehub.services import musehub_proposals |
| 834 | |
| 835 | repo = await _repo(db_session, "stress-comments") |
| 836 | await _branch_with_commit(db_session, repo.repo_id, "feat-cmt") |
| 837 | proposal = await musehub_proposals.create_proposal( |
| 838 | db_session, repo_id=repo.repo_id, |
| 839 | title="Commented proposal", from_branch="feat-cmt", to_branch="main", |
| 840 | ) |
| 841 | await db_session.flush() |
| 842 | |
| 843 | for i in range(30): |
| 844 | await musehub_proposals.create_proposal_comment( |
| 845 | db_session, |
| 846 | proposal_id=proposal.proposal_id, |
| 847 | repo_id=repo.repo_id, |
| 848 | author=f"user-{i}", |
| 849 | body=f"Comment {i}", |
| 850 | ) |
| 851 | |
| 852 | result = await musehub_proposals.list_proposal_comments( |
| 853 | db_session, proposal.proposal_id, repo.repo_id |
| 854 | ) |
| 855 | assert result.total == 30 |
| 856 | |
| 857 | |
| 858 | # =========================================================================== |
| 859 | # Layer 5 — Data Integrity tests |
| 860 | # =========================================================================== |
| 861 | |
| 862 | |
| 863 | class TestDataIntegrity: |
| 864 | async def test_merged_at_set_on_merge( |
| 865 | self, db_session: AsyncSession |
| 866 | ) -> None: |
| 867 | from musehub.services import musehub_proposals |
| 868 | |
| 869 | repo = await _repo(db_session, "merged-at") |
| 870 | await _branch_with_commit(db_session, repo.repo_id, "feat-ma") |
| 871 | proposal = await musehub_proposals.create_proposal( |
| 872 | db_session, repo_id=repo.repo_id, |
| 873 | title="Timestamps", from_branch="feat-ma", to_branch="main", |
| 874 | ) |
| 875 | before = datetime.now(tz=timezone.utc).replace(tzinfo=None) |
| 876 | merged = await musehub_proposals.merge_proposal( |
| 877 | db_session, repo.repo_id, proposal.proposal_id |
| 878 | ) |
| 879 | after = datetime.now(tz=timezone.utc).replace(tzinfo=None) |
| 880 | assert merged.merged_at is not None |
| 881 | # Strip tz before comparing naive/aware datetimes |
| 882 | merged_at_naive = merged.merged_at.replace(tzinfo=None) if merged.merged_at.tzinfo else merged.merged_at |
| 883 | assert before <= merged_at_naive <= after |
| 884 | |
| 885 | async def test_cross_repo_proposal_isolation( |
| 886 | self, db_session: AsyncSession |
| 887 | ) -> None: |
| 888 | from musehub.services import musehub_proposals |
| 889 | |
| 890 | r1 = await _repo(db_session, "iso-r1") |
| 891 | r2 = await _repo(db_session, "iso-r2") |
| 892 | await _branch_with_commit(db_session, r1.repo_id, "feat") |
| 893 | |
| 894 | proposal = await musehub_proposals.create_proposal( |
| 895 | db_session, repo_id=r1.repo_id, |
| 896 | title="R1 proposal", from_branch="feat", to_branch="main", |
| 897 | ) |
| 898 | |
| 899 | # Fetch proposal from wrong repo — should return None |
| 900 | result = await musehub_proposals.get_proposal(db_session, r2.repo_id, proposal.proposal_id) |
| 901 | assert result is None |
| 902 | |
| 903 | async def test_reviewer_uniqueness_per_proposal( |
| 904 | self, db_session: AsyncSession |
| 905 | ) -> None: |
| 906 | """Requesting the same reviewer twice does not create duplicate rows.""" |
| 907 | from musehub.services import musehub_proposals |
| 908 | |
| 909 | repo = await _repo(db_session, "reviewer-uniq") |
| 910 | await _branch_with_commit(db_session, repo.repo_id, "feat-rv") |
| 911 | proposal = await musehub_proposals.create_proposal( |
| 912 | db_session, repo_id=repo.repo_id, |
| 913 | title="Reviewer proposal", from_branch="feat-rv", to_branch="main", |
| 914 | ) |
| 915 | await db_session.flush() |
| 916 | |
| 917 | await musehub_proposals.request_reviewers( |
| 918 | db_session, repo_id=repo.repo_id, proposal_id=proposal.proposal_id, |
| 919 | reviewers=["alice"], |
| 920 | ) |
| 921 | # Request again — idempotent |
| 922 | result = await musehub_proposals.request_reviewers( |
| 923 | db_session, repo_id=repo.repo_id, proposal_id=proposal.proposal_id, |
| 924 | reviewers=["alice"], |
| 925 | ) |
| 926 | assert result.total == 1 # only one row for alice |
| 927 | |
| 928 | async def test_merge_commit_id_persisted( |
| 929 | self, db_session: AsyncSession |
| 930 | ) -> None: |
| 931 | from sqlalchemy import select as sa_select |
| 932 | from musehub.services import musehub_proposals |
| 933 | |
| 934 | repo = await _repo(db_session, "mc-persisted") |
| 935 | await _branch_with_commit(db_session, repo.repo_id, "feat-mc") |
| 936 | proposal = await musehub_proposals.create_proposal( |
| 937 | db_session, repo_id=repo.repo_id, |
| 938 | title="MC test", from_branch="feat-mc", to_branch="main", |
| 939 | ) |
| 940 | merged = await musehub_proposals.merge_proposal( |
| 941 | db_session, repo.repo_id, proposal.proposal_id |
| 942 | ) |
| 943 | |
| 944 | # Reload from DB |
| 945 | stmt = sa_select(MusehubProposal).where( |
| 946 | MusehubProposal.proposal_id == proposal.proposal_id |
| 947 | ) |
| 948 | row = (await db_session.execute(stmt)).scalar_one() |
| 949 | assert row.merge_commit_id == merged.merge_commit_id |
| 950 | assert row.state == "merged" |
| 951 | |
| 952 | async def test_merge_idempotence_409( |
| 953 | self, db_session: AsyncSession |
| 954 | ) -> None: |
| 955 | from musehub.services import musehub_proposals |
| 956 | |
| 957 | repo = await _repo(db_session, "merge-idem") |
| 958 | await _branch_with_commit(db_session, repo.repo_id, "feat-idem") |
| 959 | proposal = await musehub_proposals.create_proposal( |
| 960 | db_session, repo_id=repo.repo_id, |
| 961 | title="Idempotent merge", from_branch="feat-idem", to_branch="main", |
| 962 | ) |
| 963 | await musehub_proposals.merge_proposal(db_session, repo.repo_id, proposal.proposal_id) |
| 964 | |
| 965 | with pytest.raises(RuntimeError, match="already merged"): |
| 966 | await musehub_proposals.merge_proposal(db_session, repo.repo_id, proposal.proposal_id) |
| 967 | |
| 968 | |
| 969 | # =========================================================================== |
| 970 | # Layer 6 — Security tests |
| 971 | # =========================================================================== |
| 972 | |
| 973 | |
| 974 | class TestSecurity: |
| 975 | async def test_create_proposal_requires_auth( |
| 976 | self, client: AsyncClient, db_session: AsyncSession |
| 977 | ) -> None: |
| 978 | repo = await _repo(db_session, "sec-create") |
| 979 | await db_session.commit() |
| 980 | r = await client.post( |
| 981 | f"/api/repos/{repo.repo_id}/proposals", |
| 982 | json={"title": "Unauthed", "fromBranch": "feat", "toBranch": "main"}, |
| 983 | ) |
| 984 | assert r.status_code in (401, 403) |
| 985 | |
| 986 | async def test_merge_requires_auth( |
| 987 | self, client: AsyncClient, db_session: AsyncSession |
| 988 | ) -> None: |
| 989 | repo = await _repo(db_session, "sec-merge") |
| 990 | await _branch_with_commit(db_session, repo.repo_id, "feat-sec") |
| 991 | from musehub.services import musehub_proposals |
| 992 | proposal = await musehub_proposals.create_proposal( |
| 993 | db_session, repo_id=repo.repo_id, |
| 994 | title="Unauthed merge", from_branch="feat-sec", to_branch="main", |
| 995 | ) |
| 996 | await db_session.commit() |
| 997 | r = await client.post( |
| 998 | f"/api/repos/{repo.repo_id}/proposals/{proposal.proposal_id}/merge", |
| 999 | json={"merge_strategy": "merge_commit"}, |
| 1000 | ) |
| 1001 | assert r.status_code in (401, 403) |
| 1002 | |
| 1003 | async def test_comment_requires_auth( |
| 1004 | self, client: AsyncClient, db_session: AsyncSession |
| 1005 | ) -> None: |
| 1006 | repo = await _repo(db_session, "sec-comment") |
| 1007 | await _branch_with_commit(db_session, repo.repo_id, "feat-sc") |
| 1008 | from musehub.services import musehub_proposals |
| 1009 | proposal = await musehub_proposals.create_proposal( |
| 1010 | db_session, repo_id=repo.repo_id, |
| 1011 | title="Comment auth test", from_branch="feat-sc", to_branch="main", |
| 1012 | ) |
| 1013 | await db_session.commit() |
| 1014 | r = await client.post( |
| 1015 | f"/api/repos/{repo.repo_id}/proposals/{proposal.proposal_id}/comments", |
| 1016 | json={"body": "Unauthenticated"}, |
| 1017 | ) |
| 1018 | assert r.status_code in (401, 403) |
| 1019 | |
| 1020 | async def test_request_reviewers_requires_auth( |
| 1021 | self, client: AsyncClient, db_session: AsyncSession |
| 1022 | ) -> None: |
| 1023 | repo = await _repo(db_session, "sec-reviewers") |
| 1024 | await _branch_with_commit(db_session, repo.repo_id, "feat-sr") |
| 1025 | from musehub.services import musehub_proposals |
| 1026 | proposal = await musehub_proposals.create_proposal( |
| 1027 | db_session, repo_id=repo.repo_id, |
| 1028 | title="Reviewer auth test", from_branch="feat-sr", to_branch="main", |
| 1029 | ) |
| 1030 | await db_session.commit() |
| 1031 | r = await client.post( |
| 1032 | f"/api/repos/{repo.repo_id}/proposals/{proposal.proposal_id}/reviewers", |
| 1033 | json={"reviewers": ["bob"]}, |
| 1034 | ) |
| 1035 | assert r.status_code in (401, 403) |
| 1036 | |
| 1037 | async def test_submit_review_requires_auth( |
| 1038 | self, client: AsyncClient, db_session: AsyncSession |
| 1039 | ) -> None: |
| 1040 | repo = await _repo(db_session, "sec-submit-rv") |
| 1041 | await _branch_with_commit(db_session, repo.repo_id, "feat-srva") |
| 1042 | from musehub.services import musehub_proposals |
| 1043 | proposal = await musehub_proposals.create_proposal( |
| 1044 | db_session, repo_id=repo.repo_id, |
| 1045 | title="Submit auth test", from_branch="feat-srva", to_branch="main", |
| 1046 | ) |
| 1047 | await db_session.commit() |
| 1048 | r = await client.post( |
| 1049 | f"/api/repos/{repo.repo_id}/proposals/{proposal.proposal_id}/reviews", |
| 1050 | json={"event": "approve"}, |
| 1051 | ) |
| 1052 | assert r.status_code in (401, 403) |
| 1053 | |
| 1054 | async def test_cross_repo_proposal_returns_404( |
| 1055 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 1056 | ) -> None: |
| 1057 | """A proposal_id from repo A cannot be fetched via repo B's route.""" |
| 1058 | r1_id = await _api_repo(client, auth_headers, "xr-sec-a") |
| 1059 | r2_id = await _api_repo(client, auth_headers, "xr-sec-b") |
| 1060 | await _branch_with_commit(db_session, r1_id, "feat-xr") |
| 1061 | await db_session.commit() |
| 1062 | |
| 1063 | proposal = await _api_proposal(client, auth_headers, r1_id, from_branch="feat-xr") |
| 1064 | |
| 1065 | # Try fetching R1's proposal via R2's route |
| 1066 | r = await client.get(f"/api/repos/{r2_id}/proposals/{proposal['proposalId']}") |
| 1067 | assert r.status_code == 404 |
| 1068 | |
| 1069 | async def test_title_max_length_enforced( |
| 1070 | self, client: AsyncClient, auth_headers: StrDict, db_session: AsyncSession |
| 1071 | ) -> None: |
| 1072 | repo_id = await _api_repo(client, auth_headers, "sec-title-len") |
| 1073 | r = await client.post( |
| 1074 | f"/api/repos/{repo_id}/proposals", |
| 1075 | json={"title": "x" * 501, "fromBranch": "a", "toBranch": "b"}, |
| 1076 | headers=auth_headers, |
| 1077 | ) |
| 1078 | assert r.status_code == 422 |
| 1079 | |
| 1080 | |
| 1081 | # =========================================================================== |
| 1082 | # Layer 7 — Performance tests |
| 1083 | # =========================================================================== |
| 1084 | |
| 1085 | |
| 1086 | class TestPerformance: |
| 1087 | async def test_list_100_proposals_under_500ms( |
| 1088 | self, db_session: AsyncSession |
| 1089 | ) -> None: |
| 1090 | from musehub.services import musehub_proposals |
| 1091 | |
| 1092 | repo = await _repo(db_session, "perf-100-proposals") |
| 1093 | for i in range(100): |
| 1094 | await _branch_with_commit(db_session, repo.repo_id, f"perf-feat-{i}") |
| 1095 | await musehub_proposals.create_proposal( |
| 1096 | db_session, repo_id=repo.repo_id, |
| 1097 | title=f"Perf proposal {i}", from_branch=f"perf-feat-{i}", to_branch="main", |
| 1098 | ) |
| 1099 | |
| 1100 | start = time.monotonic() |
| 1101 | proposals_list = await musehub_proposals.list_proposals(db_session, repo.repo_id, limit=100) |
| 1102 | elapsed = time.monotonic() - start |
| 1103 | |
| 1104 | assert proposals_list.total == 100 |
| 1105 | assert elapsed < 0.5, f"list_proposals took {elapsed:.3f}s (limit 0.5s)" |
| 1106 | |
| 1107 | async def test_list_100_comments_under_300ms( |
| 1108 | self, db_session: AsyncSession |
| 1109 | ) -> None: |
| 1110 | from musehub.services import musehub_proposals |
| 1111 | |
| 1112 | repo = await _repo(db_session, "perf-100-comments") |
| 1113 | await _branch_with_commit(db_session, repo.repo_id, "perf-cmt") |
| 1114 | proposal = await musehub_proposals.create_proposal( |
| 1115 | db_session, repo_id=repo.repo_id, |
| 1116 | title="Perf comments proposal", from_branch="perf-cmt", to_branch="main", |
| 1117 | ) |
| 1118 | await db_session.flush() |
| 1119 | |
| 1120 | for i in range(100): |
| 1121 | await musehub_proposals.create_proposal_comment( |
| 1122 | db_session, |
| 1123 | proposal_id=proposal.proposal_id, |
| 1124 | repo_id=repo.repo_id, |
| 1125 | author="perf-user", |
| 1126 | body=f"Perf comment {i}", |
| 1127 | ) |
| 1128 | |
| 1129 | start = time.monotonic() |
| 1130 | result = await musehub_proposals.list_proposal_comments( |
| 1131 | db_session, proposal.proposal_id, repo.repo_id |
| 1132 | ) |
| 1133 | elapsed = time.monotonic() - start |
| 1134 | |
| 1135 | assert result.total == 100 |
| 1136 | assert elapsed < 0.3, f"list_proposal_comments took {elapsed:.3f}s (limit 0.3s)" |
| 1137 | |
| 1138 | async def test_compute_risk_100x_under_100ms(self) -> None: |
| 1139 | """compute_risk is called once per page render — 100× must be fast.""" |
| 1140 | start = time.monotonic() |
| 1141 | for _ in range(100): |
| 1142 | _risk(breaking=2, sym_modified=10, sym_added=5) |
| 1143 | elapsed = time.monotonic() - start |
| 1144 | assert elapsed < 0.1, f"100× compute_risk took {elapsed:.3f}s (limit 0.1s)" |
File History
5 commits
sha256:50b52eda7afb2f122863aef47d684d1a9e4684b48f5f95367fc956e28ceb7d42
refactor: rename merge strategy aliases to canonical names
Sonnet 4.6
minor
⚠
48 days ago
sha256:d8cbca3a06f39f82f66be6c29de3f41c3dec5f367722958fb5454dcbc007cc15
fix: rc11 test fixes — event→verdict rename, migration coun…
Sonnet 4.6
patch
50 days ago
sha256:a909058d727faac4d77f6e659cc0b1f9315efcb6aabfd870d08763525a67093d
dialing in --strategy and --history on merge proposal
Human
minor
⚠
51 days ago
sha256:1f95928652d220172ddd56fb71bdd7bee3158e1b86f1b8ba8b2470edde498c6a
update proposal detail w/ --history and --strategy flags
Human
minor
⚠
51 days ago
sha256:1b1a47509ee8154b9c61b68e8e871f6f6dd4d48125c5bcc1df61733bd3657c42
insert `MusehubCommitGraph` row when `merge_proposal` creat…
Human
patch
51 days ago