AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
test(backend/experts): seed a fixture roster in tests, never the real one (#14630)
Reinier van der Leer887e549ba305
diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.pyindex 36b658c2f9..f84f900d6c 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py@@ -177,6 +177,56 @@ async def absorb_a_stale_event_loop(server: SpinTestServer):raise+# Read at import, before any test can monkeypatch seed.ROSTER.+LIVE_ROSTER_NAMES = frozenset(entry["name"] for entry in seed.ROSTER)++[email protected](autouse=True)+def refuse_to_seed_the_live_roster(monkeypatch):+ """Fail a test that writes a roster template under its shipped name.++ ``seed._upsert_template`` resolves a template by name, so seeding the real+ names against a shared database adopts the live roster rows and rewrites+ them and every hire made from them — four sessions have done it by+ accident. Tests that need the seed take ``fixture_roster``.+ """+ upsert = seed._upsert_template++ async def guarded(entry: seed.RosterEntry) -> prisma.models.Expert:+ if entry["name"] in LIVE_ROSTER_NAMES:+ pytest.fail(+ f"seeding '{entry['name']}' would rewrite the live roster template "+ "and its hires; seed through the fixture_roster fixture"+ )+ return await upsert(entry)++ monkeypatch.setattr(seed, "_upsert_template", guarded)+++async def fixture_roster(monkeypatch):+ """Point ``seed_roster`` at the shipped roster under non-colliding names.++ Only ``name`` differs from ``seed.ROSTER``, so a test still checks the+ personas, preloads and copy we ship. Returns shipped name -> its entry.+ """+ suffix = uuid.uuid4().hex[:8]++ def rename(entry: seed.RosterEntry) -> seed.RosterEntry:+ return {**entry, "name": f"{entry['name']} {suffix}"}++ roster = {entry["name"]: rename(entry) for entry in seed.ROSTER}+ monkeypatch.setattr(seed, "ROSTER", list(roster.values()))+ yield roster+ seeded = await prisma.models.Expert.prisma().find_many(+ where={+ "isTemplate": True,+ "name": {"in": [entry["name"] for entry in roster.values()]},+ }+ )+ _seeded_template_ids.extend(template.id for template in seeded)++@pytest.fixtureasync def test_user():return await _create_seed_user()@@ -344,6 +394,7 @@ async def _load_roster_store_assets() -> dict[str, str]:async def _hire_roster_and_assert_preloads(+ roster: dict[str, seed.RosterEntry],hire_user: User,templates: dict[str, prisma.models.Expert],expected: dict[str, str],@@ -354,7 +405,7 @@ async def _hire_roster_and_assert_preloads()results: dict[str, HireResult] = {}with patch.object(scheduling, "get_scheduler_client", return_value=scheduler):- for entry in seed.ROSTER:+ for persona, entry in roster.items():result = await experts_db.hire_expert(hire_user.id, templates[entry["name"]].id, None)@@ -362,7 +413,7 @@ async def _hire_roster_and_assert_preloads(assert {w.store_listing_version_id for w in result.expert.workflows} == {expected[p["slug"]] for p in entry["preloads"]}- results[entry["name"]] = result+ results[persona] = resultreturn results@@ -2293,12 +2344,12 @@ async def test_hire_from_template_with_samples_stores_plain_voice(@pytest.mark.asyncio(loop_scope="session")async def test_seed_roster_exposes_two_voice_samples_per_template(- server: SpinTestServer,+ server: SpinTestServer, fixture_roster: dict[str, seed.RosterEntry]):await _load_roster_store_assets()ids = await seed.seed_roster()seeded = {t.name: t for t in await experts_db.list_templates() if t.id in ids}- for entry in seed.ROSTER:+ for entry in fixture_roster.values():template = seeded[entry["name"]]assert len(template.voice_samples) == 2assert template.voice_preferences == entry["voice_preferences"]@@ -3123,15 +3174,17 @@ async def test_enforce_budget_pauses_blocks_and_resumes(@pytest.mark.asyncio(loop_scope="session")-async def test_seed_roster_round_trip(server: SpinTestServer):+async def test_seed_roster_round_trip(+ server: SpinTestServer, fixture_roster: dict[str, seed.RosterEntry]+):await _load_roster_store_assets()first_ids = await seed.seed_roster()- assert len(first_ids) == len(seed.ROSTER)+ assert len(first_ids) == len(fixture_roster)templates = await experts_db.list_templates()seeded = {t.name: t for t in templates if t.id in first_ids}- assert {e["name"] for e in seed.ROSTER} == set(seeded)- for entry in seed.ROSTER:+ assert {e["name"] for e in fixture_roster.values()} == set(seeded)+ for entry in fixture_roster.values():template = seeded[entry["name"]]assert template.is_templateassert template.role == entry["role"]@@ -3142,7 +3195,7 @@ async def test_seed_roster_round_trip(server: SpinTestServer):templates_after = await experts_db.list_templates()seeded_after = [t for t in templates_after if t.id in second_ids]- assert len(seeded_after) == len(seed.ROSTER)+ assert len(seeded_after) == len(fixture_roster)@pytest.mark.asyncio(loop_scope="session")@@ -3262,6 +3315,8 @@ def test_roster_day_one_is_marias_two_rows_and_hidden_for_the_rest():async def test_upsert_template_refuses_a_fourth_day_one_row_before_writing():maria = next(entry for entry in seed.ROSTER if entry["name"] == "Maria")too_many = maria.copy()+ # Not the shipped "Maria": refuse_to_seed_the_live_roster guards that name.+ too_many["name"] = f"Maria {uuid.uuid4().hex[:8]}"# One past the cap, however many rows the roster currently gives her —# deriving the list from len(maria["day_one"]) + 1 made this test pass# silently (and then fail on a MagicMock await) the moment her row count@@ -3278,7 +3333,7 @@ async def test_upsert_template_refuses_a_fourth_day_one_row_before_writing():@pytest.mark.asyncio(loop_scope="session")async def test_roster_preloads_resolve_and_hire_installs_cleanly(- server: SpinTestServer,+ server: SpinTestServer, fixture_roster: dict[str, seed.RosterEntry]):"""Launch acceptance gate against the real checked-in store assets: everyROSTER preload slug resolves to the exact StoreListingVersion the CSV@@ -3295,7 +3350,7 @@ async def test_roster_preloads_resolve_and_hire_installs_cleanly(templates = {t.name: t for t in await experts_db.list_templates() if t.id in template_ids}- for entry in seed.ROSTER:+ for entry in fixture_roster.values():expected_versions = {expected[p["slug"]] for p in entry["preloads"]}assert len(expected_versions) == len(entry["preloads"])assert templates[entry["name"]].day_one == entry["day_one"]@@ -3306,7 +3361,9 @@ async def test_roster_preloads_resolve_and_hire_installs_cleanly(# A fresh user per run: a reused fixture user would make hire_expert# short-circuit to a previous run's copy and skip _install_preloads.hire_user = await _create_seed_user()- results = await _hire_roster_and_assert_preloads(hire_user, templates, expected)+ results = await _hire_roster_and_assert_preloads(+ fixture_roster, hire_user, templates, expected+ )frankie_crons = [w.schedule_cron for w in results["Frankie"].expert.workflows if w.schedule_cron@@ -3906,6 +3963,7 @@ async def test_rescope_moves_untouched_hires_and_spares_edited_ones("isTemplate": True,})+ _seeded_template_ids.append(template.id)untouched = await experts_db.hire_expert(test_user.id, template.id, None)edited = await experts_db.hire_expert(other_user.id, template.id, None)await prisma.models.Expert.prisma().update(@@ -3996,6 +4054,7 @@ async def test_seed_roster_rescopes_untouched_hires_and_spares_edited_ones(}monkeypatch.setattr(seed, "ROSTER", [entry])(template_id,) = await seed.seed_roster()+ _seeded_template_ids.append(template_id)untouched = await experts_db.hire_expert(test_user.id, template_id, None)edited = await experts_db.hire_expert(other_user.id, template_id, None)await prisma.models.Expert.prisma().update(@@ -5260,12 +5319,12 @@ async def test_hire_copies_the_template_categories(server: SpinTestServer, test_@pytest.mark.asyncio(loop_scope="session")async def test_seed_roster_files_every_template_under_a_canonical_category(- server: SpinTestServer,+ server: SpinTestServer, fixture_roster: dict[str, seed.RosterEntry]):await _load_roster_store_assets()ids = await seed.seed_roster()seeded = {t.name: t for t in await experts_db.list_templates() if t.id in ids}- for entry in seed.ROSTER:+ for entry in fixture_roster.values():assert seeded[entry["name"]].categories == entry["categories"]assert set(entry["categories"]) <= {c.value for c in StoreCategory}