# Milestone 2 — Backend testing guide

**Outcome:** Students get grade-appropriate daily tasks and submit results. Teachers can read results for assigned students only. Works without the Week 3 XP / mastery engine.

Interactive API docs (Swagger): [http://localhost:3001/docs](http://localhost:3001/docs)  
OpenAPI JSON: [http://localhost:3001/docs.json](http://localhost:3001/docs.json)

---

## Prerequisites

1. PostgreSQL DB created and schema applied (see `sql/README.md`)
2. `.env` configured (`DATABASE_URL`, JWT secrets, SMTP optional for invites)
3. Seeds + API:

```bash
npm install
npm run seed:auth
npm run seed:curriculum
npm run dev
```

API base: `http://localhost:3001`

---

## Acceptance criteria

| # | Criterion | How to verify |
|---|---|---|
| 1 | Student receives permitted, grade-appropriate tasks | `GET /student/tasks` returns tasks from membership worlds |
| 2 | Each submit creates a school-isolated result | `POST /student/results` → 201; row scoped to student school |
| 3 | Teachers see results for assigned students only | `GET /teacher/results` / `GET /teacher/students/:id/results` |

One-shot check (creates a temp student login if needed):

```bash
npm run smoke:m2
```

Expect `ACCEPTANCE_OK` at the end.

---

## Roles you need

| Role | How you get it |
|---|---|
| Owner | Seeded: `superadmin@neverland.education` / `neverland` |
| Teacher | Invite via school admin, or use an existing teacher account |
| Student | School enrols student + email → student sets password via invite link |

Students do **not** use the web dashboard. After accepting the invite they sign in on **VR / school devices** with email + password (`POST /auth/login` → `/vr/*` or `/student/*`).

### Student add → login flow

1. School admin: **Students → Add / Edit** and **give the student an email** (required)  
2. API: `POST /students` with `email` (or edit + invite via `POST /students/:id/invite`)  
3. Student opens invite email → `/invite/<token>` → sets password  
4. Account is `role=student` and linked to `students.user_id`  
5. Student / VR: `POST /auth/login` then `GET /vr/tasks` / `POST /vr/results`

Also run once (existing DBs):

```bash
npm run seed:curriculum
```

(applies `sql/migrations/004_student_login.sql` as well).

---

## Manual test flow (Swagger or curl)

### 1. Health

```bash
curl http://localhost:3001/health
```

Expect: `{ "ok": true, "service": "neverland-api", "milestone": 2 }`

### 2. Login (teacher)

```bash
curl -X POST http://localhost:3001/auth/login ^
  -H "Content-Type: application/json" ^
  -d "{\"email\":\"YOUR_TEACHER_EMAIL\",\"password\":\"YOUR_PASSWORD\"}"
```

Copy `accessToken`. In Swagger: **Authorize** → `Bearer <accessToken>`.

### 3. Catalog (any authenticated role)

```bash
curl http://localhost:3001/catalog/worlds -H "Authorization: Bearer TOKEN"
curl "http://localhost:3001/catalog/objectives?subject=math&grade=6" -H "Authorization: Bearer TOKEN"
curl "http://localhost:3001/catalog/exercises?objective=fractions" -H "Authorization: Bearer TOKEN"
```

School-linked roles only see worlds on the school membership plan.

### 4. Teacher roster + results

```bash
curl http://localhost:3001/teacher/classes -H "Authorization: Bearer TEACHER_TOKEN"
curl http://localhost:3001/teacher/students -H "Authorization: Bearer TEACHER_TOKEN"
curl "http://localhost:3001/teacher/results?limit=50" -H "Authorization: Bearer TEACHER_TOKEN"
curl http://localhost:3001/teacher/students/STUDENT_ID/results -H "Authorization: Bearer TEACHER_TOKEN"
```

### 5. Student tasks + submit

Login as a **student** JWT, then:

```bash
curl http://localhost:3001/student/tasks -H "Authorization: Bearer STUDENT_TOKEN"
```

Pick `tasks[0].id` and `tasks[0].exercise.id`, then:

```bash
curl -X POST http://localhost:3001/student/results ^
  -H "Authorization: Bearer STUDENT_TOKEN" ^
  -H "Content-Type: application/json" ^
  -d "{\"exerciseId\":\"EXERCISE_UUID\",\"dailyTaskId\":\"TASK_UUID\",\"correct\":true,\"attempts\":1,\"hintsUsed\":0,\"timeSpentSec\":40}"
```

Expect **201** and a `result` object.

### 6. Confirm teacher can see it

```bash
curl "http://localhost:3001/teacher/results?studentId=STUDENT_ID" -H "Authorization: Bearer TEACHER_TOKEN"
```

`results` should include the new row; `summary` aggregates by objective.

### 7. VR aliases (same student token)

```bash
curl http://localhost:3001/vr/worlds -H "Authorization: Bearer STUDENT_TOKEN"
curl http://localhost:3001/vr/tasks -H "Authorization: Bearer STUDENT_TOKEN"
```

`POST /vr/results` body matches `POST /student/results`.

### 8. Permission check

Teacher token on student route must fail:

```bash
curl http://localhost:3001/student/tasks -H "Authorization: Bearer TEACHER_TOKEN"
```

Expect **403**.

---

## M2 endpoint map

### Catalog (auth)
| Method | Path | Notes |
|---|---|---|
| GET | `/catalog/worlds` | Membership-filtered for school roles |
| GET | `/catalog/objectives?world=&subject=&grade=` | |
| GET | `/catalog/exercises?objectiveId=` or `?objective=` | |

### Student me (role: `student`)
| Method | Path | Notes |
|---|---|---|
| GET | `/student/tasks?date=YYYY-MM-DD` | Creates today if missing |
| POST | `/student/results` | Body: exerciseId, correct, optional attempts/hints/time/dailyTaskId |

### VR aliases
| Method | Path | Notes |
|---|---|---|
| GET | `/vr/worlds` | Same as catalog worlds |
| GET | `/vr/tasks?date=` | Student only |
| POST | `/vr/results` | Student only |

### Teacher me (role: `teacher`)
| Method | Path | Notes |
|---|---|---|
| GET | `/teacher/dashboard?classId=` | Summary for assigned class |
| GET | `/teacher/classes` | + membership worlds |
| GET | `/teacher/students?classId=&q=` | Assigned classes only |
| GET | `/teacher/students/:id` | |
| GET | `/teacher/students/:id/results` | |
| GET | `/teacher/results?classId=&studentId=&objectiveId=&from=&to=&limit=` | Results + summary |

### School directory (still M1; needed for setup)
| Area | Paths |
|---|---|
| Teachers | `GET/POST /teachers…` |
| Classes | `GET/POST/PATCH/DELETE /classes…` |
| Students | `GET/POST/PATCH/DELETE /students…` |

---

## Dashboard smoke (optional)

With API + dashboard (`neverland-dashboard` on `:3000`) running:

1. Login as the teacher
2. **Learning Objectives** — should mention catalog objectives / submitted results
3. **Results** — “Class patterns use N submitted results…”
4. **My Class → student** — exercise history includes submitted activity rows

---

## Out of scope for M2

- XP / levels engine
- Mastery engine (Mastered / Developing rules as a first-class stored status)
- PDF / CSV reports
- DB-persisted recommendations
- Multiplayer

Those belong to Week 3–4.
