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
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
"""API surface.

Covers the things that were wrong or absent in the previous system: that
endpoints refuse unauthenticated callers, that one user cannot reach another
user's work, that CORS headers actually come from the app, and that the
pipeline catalogue the web app builds its UI from is served correctly.
"""

from __future__ import annotations

import uuid

import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine

from app.core.auth import current_user
from app.db.models import Base, User, UserRole, UserStatus
from app.db.session import get_session
from app.main import app
from app.pipelines.registry import all_pipelines


@pytest_asyncio.fixture
async def api_engine():
    eng = create_async_engine("sqlite+aiosqlite:///:memory:", future=True)
    async with eng.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    yield eng
    await eng.dispose()


@pytest_asyncio.fixture
async def api_db(api_engine) -> AsyncSession:
    factory = async_sessionmaker(api_engine, class_=AsyncSession, expire_on_commit=False)
    async with factory() as session:
        yield session


@pytest_asyncio.fixture
async def alice(api_db) -> User:
    # Approved with every tool. What a grant withholds is covered in
    # test_approval.py; these tests are about everything else.
    p = User(id=uuid.uuid4(), email="alice@example.com", role=UserRole.user,
             status=UserStatus.active,
             allowed_task_types=[pl.key for pl in all_pipelines()])
    api_db.add(p)
    await api_db.commit()
    return p


@pytest_asyncio.fixture
async def bob(api_db) -> User:
    p = User(id=uuid.uuid4(), email="bob@example.com", role=UserRole.user,
             status=UserStatus.active,
             allowed_task_types=[pl.key for pl in all_pipelines()])
    api_db.add(p)
    await api_db.commit()
    return p


def _client(api_db, as_user: User | None) -> AsyncClient:
    async def _session_override():
        yield api_db

    app.dependency_overrides[get_session] = _session_override
    if as_user is not None:
        app.dependency_overrides[current_user] = lambda: as_user
    else:
        app.dependency_overrides.pop(current_user, None)

    return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")


@pytest_asyncio.fixture(autouse=True)
async def _reset_overrides():
    yield
    app.dependency_overrides.clear()


# ---------------------------------------------------------------------- meta


async def test_health_needs_no_auth(api_db):
    async with _client(api_db, None) as c:
        r = await c.get("/health")
    assert r.status_code == 200
    assert r.json()["status"] == "ok"


async def test_cors_headers_come_from_the_app(api_db):
    """The old backend shipped none of these - only the HF proxy added them."""
    async with _client(api_db, None) as c:
        r = await c.options(
            "/v1/pipelines",
            headers={
                "Origin": "http://localhost:5173",
                "Access-Control-Request-Method": "GET",
            },
        )
    assert r.headers.get("access-control-allow-origin") == "http://localhost:5173"


# ----------------------------------------------------------------- pipelines


async def test_pipeline_catalogue_is_public_and_complete(api_db):
    async with _client(api_db, None) as c:
        r = await c.get("/v1/pipelines")
    assert r.status_code == 200
    items = r.json()["items"]
    assert {p["key"] for p in items} == {
        "book", "course", "training_bag", "arabic_training_bag", "arabic_slides", "english_slides",
        "instructional_design", "level", "motion_video",
        "static_infographic", "interactive_infographic",
        "references", "appendix",
        "case_study", "training_activities", "applied_tools",
        "pre_post_test", "analytical_case_study",
        "text_image", "simulation_game", "interactive_activities", "dna_microlearning",
        "book_practical", "book_narrative", "novel",
        "book_v3",
    }
    book = next(p for p in items if p["key"] == "book")
    assert len(book["steps"]) == 14
    # The form comes from the chosen route, not from the step.
    routes = {v["key"]: v for v in book["variants"]}
    assert set(routes) == {"from_scratch", "from_text", "from_file"}
    assert routes["from_scratch"]["input_schema"]["properties"]["topic"]
    assert routes["from_file"]["accepts_uploads"] is True


async def test_task_types_carry_the_commercial_fields_the_grid_shows(api_db, alice):
    """The create grid reads /v1/task-types, not /v1/pipelines - it needs the
    Arabic name, the category, the outputs and the cost hint as well as the
    routes. A pipeline missing a DEFAULTS entry silently lands in 'أخرى'."""
    async with _client(api_db, alice) as c:
        r = await c.get("/v1/task-types")
    assert r.status_code == 200
    items = r.json()["items"]
    assert {t["id"] for t in items} == {
        "book", "course", "training_bag", "arabic_training_bag", "arabic_slides", "english_slides",
        "instructional_design", "level", "motion_video",
        "static_infographic", "interactive_infographic",
        "references", "appendix",
        "case_study", "training_activities", "applied_tools",
        "pre_post_test", "analytical_case_study",
        "text_image", "simulation_game", "interactive_activities", "dna_microlearning",
        "book_practical", "book_narrative", "novel",
        "book_v3",
    }

    for item in items:
        assert item["nameAr"], f"{item['id']} has no Arabic name"
        assert item["category"] != "أخرى", f"{item['id']} is missing a DEFAULTS entry"
        assert item["outputs"] and item["estimatedSeconds"] and item["costHint"]
        assert item["variants"], f"{item['id']} publishes no entry route"

    # The create screen is split by company: كونتيفا the interactive work,
    # رؤية the rest, and the sources under both.
    brands = {t["id"]: t["brands"] for t in items}
    assert brands["references"] == ["contiva", "roya"]
    for key in ("case_study", "text_image", "simulation_game", "interactive_activities",
                "dna_microlearning", "interactive_infographic", "motion_video",
                "static_infographic"):
        assert brands[key] == ["contiva"], key
    for key in ("book", "course", "arabic_training_bag", "level", "arabic_slides"):
        assert brands[key] == ["roya"], key
    assert all(set(b) <= {"contiva", "roya"} and b for b in brands.values())

    level = next(t for t in items if t["id"] == "level")
    assert level["nameAr"] == "مستوى تدريبي"
    assert {v["id"] for v in level["variants"]} >= {"units_supplied", "outline_generated"}
    supplied = next(v for v in level["variants"] if v["id"] == "units_supplied")
    assert supplied["inputSchema"]["properties"]["units"]["type"] == "array"
    assert len(level["stages"]) == 7


async def test_task_types_refuses_an_anonymous_caller(api_db):
    async with _client(api_db, None) as c:
        r = await c.get("/v1/task-types")
    assert r.status_code == 401


async def test_unknown_pipeline_is_404(api_db):
    async with _client(api_db, None) as c:
        r = await c.get("/v1/pipelines/does-not-exist")
    assert r.status_code == 404


# ------------------------------------------------------------------ authgate


@pytest.mark.parametrize(
    "method,path",
    [
        ("get", "/v1/me"),
        ("get", "/v1/projects"),
        ("post", "/v1/projects"),
        ("get", "/v1/admin/overview"),
        ("get", "/v1/admin/users"),
    ],
)
async def test_protected_endpoints_reject_anonymous_callers(api_db, method, path):
    async with _client(api_db, None) as c:
        r = await c.request(method.upper(), path, json={} if method == "post" else None)
    assert r.status_code in (401, 403), f"{path} let an anonymous caller through"


# ------------------------------------------------------------------ projects


async def test_create_and_list_a_project(api_db, alice):
    async with _client(api_db, alice) as c:
        created = await c.post(
            "/v1/projects",
            json={"kind": "book", "title": "كتاب القيادة", "description": "تجربة"},
        )
        assert created.status_code == 201
        body = created.json()
        assert body["title"] == "كتاب القيادة"
        assert body["owner_id"] == str(alice.id)

        listed = await c.get("/v1/projects")
    assert listed.json()["total"] == 1


async def test_project_kind_must_be_a_known_pipeline(api_db, alice):
    async with _client(api_db, alice) as c:
        r = await c.post("/v1/projects", json={"kind": "podcast", "title": "x"})
    assert r.status_code == 422
    assert "not a known project type" in r.json()["detail"]


async def test_one_user_cannot_read_another_users_project(api_db, alice, bob):
    async with _client(api_db, alice) as c:
        created = await c.post("/v1/projects", json={"kind": "book", "title": "alice's"})
    project_id = created.json()["id"]

    async with _client(api_db, bob) as c:
        r = await c.get(f"/v1/projects/{project_id}")

    # 404 rather than 403 - bob should not learn that this id exists
    assert r.status_code == 404


async def test_starting_a_run_returns_202_and_queues_it(api_db, alice, monkeypatch):
    queued: list[uuid.UUID] = []

    async def fake_enqueue(run_id):
        queued.append(run_id)

    monkeypatch.setattr("app.api.v1.routes.enqueue_run", fake_enqueue)

    async with _client(api_db, alice) as c:
        project = (await c.post("/v1/projects", json={"kind": "book", "title": "t"})).json()
        r = await c.post(
            f"/v1/projects/{project['id']}/runs",
            json={"params": {"brief": {"book_title": "x", "target_audience": "y"}}},
        )

    assert r.status_code == 202
    assert r.json()["status"] == "queued"
    assert len(queued) == 1


async def test_a_project_cannot_have_two_runs_at_once(api_db, alice, monkeypatch):
    monkeypatch.setattr("app.api.v1.routes.enqueue_run", lambda run_id: _noop())

    async def _noop():
        return None

    async with _client(api_db, alice) as c:
        project = (await c.post("/v1/projects", json={"kind": "book", "title": "t"})).json()
        first = await c.post(f"/v1/projects/{project['id']}/runs", json={})
        second = await c.post(f"/v1/projects/{project['id']}/runs", json={})

    assert first.status_code == 202
    assert second.status_code == 409


# --------------------------------------------------------------------- admin


async def test_admin_endpoints_reject_a_normal_user(api_db, alice):
    async with _client(api_db, alice) as c:
        r = await c.get("/v1/admin/overview")
    assert r.status_code == 403


async def test_admin_overview_reports_zeros_on_an_empty_system(api_db):
    admin = User(id=uuid.uuid4(), email="admin@test.local", role=UserRole.admin,
                    status=UserStatus.active)
    api_db.add(admin)
    await api_db.commit()

    async with _client(api_db, admin) as c:
        r = await c.get("/v1/admin/overview")

    assert r.status_code == 200
    body = r.json()
    assert body["users_total"] == 1
    assert body["cost_all_time_usd"] == 0.0


async def test_admin_cannot_demote_themselves(api_db):
    admin = User(id=uuid.uuid4(), email="admin2@test.local", role=UserRole.admin,
                    status=UserStatus.active)
    api_db.add(admin)
    await api_db.commit()

    async with _client(api_db, admin) as c:
        r = await c.patch(f"/v1/admin/users/{admin.id}", json={"role": "user"})

    assert r.status_code == 409


async def test_run_detail_does_not_reach_for_a_lazy_relationship(api_db, alice, monkeypatch):
    """RunDetailOut declares `steps`, and Run has a relationship of the same
    name. Validating the ORM object against the schema made Pydantic load it
    lazily from a thread with no greenlet - a 500 on every run screen."""
    from app.db.models import Project, Run, RunStatus, StepRun, StepStatus

    project = Project(owner_id=alice.id, kind="book", title="كتاب")
    api_db.add(project)
    await api_db.commit()

    run = Run(project_id=project.id, pipeline_key="book", params={},
              created_by=alice.id, status=RunStatus.running, current_step="book.units")
    api_db.add(run)
    await api_db.flush()
    api_db.add_all([
        StepRun(run_id=run.id, step_key="book.units", position=0,
                status=StepStatus.succeeded, output={"units": []}),
        StepRun(run_id=run.id, step_key="book.outline", position=1,
                status=StepStatus.pending),
    ])
    await api_db.commit()

    async with _client(api_db, alice) as c:
        r = await c.get(f"/v1/runs/{run.id}")

    assert r.status_code == 200, r.text[:300]
    body = r.json()
    assert body["status"] == "running"
    assert [s["step_key"] for s in body["steps"]] == ["book.units", "book.outline"]
    # has_output is computed per step, not read off the relationship.
    assert body["steps"][0]["has_output"] is True
    assert body["steps"][1]["has_output"] is False


# ------------------------------------------------ the brief is checked at the door

# Every variant carries an input model, the catalogue publishes it as JSON
# Schema, and the web app builds and enforces the form from it. The server was
# not checking it at all - params went straight into the run. A value the form
# would have refused was accepted, the blueprint step ran and was paid for, the
# user approved it, and the run died two steps later on a Pydantic error about
# a field they were never offered a chance to correct.


async def test_a_brief_that_breaks_its_own_form_is_refused_before_anything_runs(
    api_db, alice, monkeypatch
):
    queued: list[uuid.UUID] = []

    async def fake_enqueue(run_id):
        queued.append(run_id)

    monkeypatch.setattr("app.api.v1.routes.enqueue_run", fake_enqueue)

    async with _client(api_db, alice) as c:
        project = (
            await c.post("/v1/projects", json={"kind": "training_bag", "title": "t"})
        ).json()
        r = await c.post(
            f"/v1/projects/{project['id']}/runs",
            json={
                "params": {
                    "variant": "from_text",
                    # The published form declares a minimum length.
                    "brief": {"blueprint_text": "too short"},
                }
            },
        )

    assert r.status_code == 422, r.text
    body = r.json()["detail"]
    assert "blueprint_text" in str(body), body
    assert queued == [], "the run was queued despite an invalid brief"


async def test_an_unknown_route_is_refused(api_db, alice):
    """The entry step branches on this and falls through to a default, so a
    typo quietly runs something other than what was asked for."""
    async with _client(api_db, alice) as c:
        project = (
            await c.post("/v1/projects", json={"kind": "training_bag", "title": "t"})
        ).json()
        r = await c.post(
            f"/v1/projects/{project['id']}/runs",
            json={"params": {"variant": "from_txt", "brief": {}}},
        )

    assert r.status_code == 422
    assert "from_text" in r.json()["detail"], "the message should list the real routes"


async def test_a_valid_brief_still_starts(api_db, alice, monkeypatch):
    """The guard must not become a second, stricter form."""
    monkeypatch.setattr("app.api.v1.routes.enqueue_run", lambda run_id: _noop())

    async def _noop():
        return None

    async with _client(api_db, alice) as c:
        project = (
            await c.post("/v1/projects", json={"kind": "training_bag", "title": "t"})
        ).json()
        r = await c.post(
            f"/v1/projects/{project['id']}/runs",
            json={
                "params": {
                    "variant": "from_text",
                    "brief": {
                        "blueprint_text": "Program Title: X\n" + "y" * 80,
                        "question_count": 10,
                        "activities_per_day": 2,
                    },
                }
            },
        )

    assert r.status_code == 202, r.text


def test_health_says_which_commit_it_is_running():
    """«Did the deploy land?» had no answer, so it was guessed at from a
    marker that turned out to belong to an earlier commit - and a run was
    resumed against code that was still building, failing with the very
    error the deploy was meant to fix.

    The deploy writes the SHA into `app/BUILD.txt`; this reads it back.
    """
    from app.main import _build

    got = _build()
    assert got and got != "unknown"