# Routines Feature — E2E Test Plan

## Test Environment Setup

- **Backend**: Must be started with `DB_ENV=e2e DATA_ENV=e2e` to use isolated test database
- **Frontend**: Running on `localhost:5173` (Vite dev server)
- **Test Framework**: Playwright
- **Config**: `frontend/playwright.config.ts`

---

## Pre-Test Global Setup (global-setup.ts additions)

```typescript
// Add to global setup to seed a child with assigned routines
export async function seedTestRoutines() {
  const parentEmail = E2E_EMAIL;
  const childName = "Test Child";

  // Create 2-3 test routines in DB for parent
  // Assign them to test child
  // Returns { routine1Id, routine2Id, routine3Id, childId }
}
```

---

## Test Suite 1: Routine Creation & Management (Parent Mode)

**File**: `frontend/tests/e2e/mode_parent/routines/routine-creation.spec.ts`

### Test 1.1: Create Routine with Items

```
Given: Parent is logged in at /parent/tasks/routines
When: Click "Create New Routine"
Then: Navigate to /parent/tasks/routines/create
  - Form shows: Name, Points, Image fields
  - Items section shows "Add Item" form
When: Fill form:
  - Name: "Morning Routine"
  - Points: 50
  - Image: upload file
  - Add items: "Make Bed", "Get Dressed", "Eat Breakfast"
Then: Click "Create Routine"
Then: Routine saved and appears in list at /parent/tasks/routines
  - Name, points, image all visible
```

### Test 1.2: Edit Routine

```
Given: Routine "Morning Routine" exists
When: Click on routine row and "Edit" or navigate to /parent/tasks/routines/r1/edit
Then: Form pre-populated with current name, points, image, items
When: Change name to "Early Morning Routine", add new item "Shower"
Then: Save changes
Then: Routine updated in list
```

### Test 1.3: Delete Routine

```
Given: Routine exists
When: Click "Delete" from kebab menu
Then: Confirmation dialog appears
When: Confirm deletion
Then: Routine removed from list
Then: SSE event `routine_modified` with operation=DELETE received
```

---

## Test Suite 2: Routine Assignment to Children

**File**: `frontend/tests/e2e/mode_parent/routines/routine-assignment.spec.ts`

### Test 2.1: Assign Routine to Child

```
Given: Parent viewing child profile page
When: Click "Assign Routines"
Then: Navigate to /parent/:childId/assign-routines
  - ItemList with checkboxes showing all unassigned routines
When: Select "Morning Routine" and "Evening Routine"
Then: Click "Save"
Then: Redirect to child profile
Then: Child now has assigned routines (verify via API /api/child/:id)
Then: SSE event `child_routines_set` received
```

### Test 2.2: Verify Routine Appears in Child Mode

```
Given: Routine assigned to child
When: Switch to child mode (/child/:id) as that child
Then: Routines section visible between Chores and Rewards
Then: "Morning Routine" visible in ScrollingList
Then: Routine image and points visible
```

---

## Test Suite 3: Routine Completion Workflow

**File**: `frontend/tests/e2e/mode_parent/routines/routine-completion.spec.ts`

### Test 3.1: Child Completes Routine

```
Given: Child mode at /child/:id
When: Click on "Morning Routine" in Routines list
Then: RoutineConfirmDialog shows:
  - Routine image and name
  - All items listed (first 3 + "+X more")
  - "Confirm" and "Cancel" buttons
When: Click "Confirm"
Then: Dialog closes
Then: Routine status changes to "PENDING"
Then: SSE event `child_routine_confirmation` with status=PENDING received
Then: Parent receives push notification "Routine Pending"
```

### Test 3.2: Parent Approves Completed Routine

```
Given: Routine pending approval (child completed it)
When: Parent views notifications (/parent/notifications)
Then: Pending routine confirmation card appears
When: Click "Approve"
Then: Routine marked as approved
Then: Child receives points (50 in example)
Then: SSE event `child_routine_confirmation` with status=APPROVED received
Then: Child's routine shows "COMPLETED" stamp
```

### Test 3.3: Parent Rejects Routine Completion

```
Given: Routine pending approval
When: Parent clicks "Reject"
Then: Routine status returns to available
Then: No points awarded
Then: SSE event `child_routine_confirmation` with status=REJECTED received
Then: Child can try again
```

---

## Test Suite 4: Routine Scheduling

**File**: `frontend/tests/e2e/mode_parent/routines/routine-scheduling.spec.ts`

### Test 4.1: Schedule Routine for Specific Days

```
Given: Routine assigned to child
When: Parent opens child profile kebab menu → "Set Schedule" for routine
Then: ScheduleModal opens with entityType already set to "routine"
When: Mode = "Specific Days", select Monday, Wednesday, Friday
  - Set default deadline: 9:00 AM
  - Toggle deadline for Wednesday to 8:00 AM
Then: Save
Then: Routine only appears on those days in child view
Then: SSE event `routine_schedule_modified` with operation=SET received
```

### Test 4.2: Schedule Routine for Interval

```
Given: Routine assigned to child
When: Parent opens ScheduleModal for routine
When: Mode = "Every X Days", set interval to 3 days
  - Anchor date: today
  - Deadline: 10:00 AM
Then: Save
Then: Routine appears every 3 days
Then: Deadline enforced
```

### Test 4.3: Pause/Resume Schedule

```
Given: Routine has schedule
When: Parent toggles "Enabled" in ScheduleModal
Then: Routine becomes "Paused" (appears every day, no schedule restriction)
When: Toggle "Enabled" again
Then: Schedule re-enabled
Then: SSE event received
```

---

## Test Suite 5: Routine Points & Overrides

**File**: `frontend/tests/e2e/mode_parent/routines/routine-overrides.spec.ts`

### Test 5.1: Set Per-Child Point Override

```
Given: Routine worth 50 points assigned to child
When: Parent kebab menu → "Edit Points"
Then: Dialog/form to set custom points appears
When: Set to 75 points
Then: Save
Then: Routine now shows 75 points in child view
Then: When child completes routine, 75 points awarded
Then: SSE event `child_override_set` received
```

### Test 5.2: Remove Point Override

```
Given: Routine has custom points override (75)
When: Parent removes override via UI
Then: Routine reverts to default points (50)
Then: SSE event `child_override_deleted` received
```

---

## Test Suite 6: Routine Deadline Extension

**File**: `frontend/tests/e2e/mode_parent/routines/routine-extension.spec.ts`

### Test 6.1: Extend Routine Deadline

```
Given: Routine has 9:00 AM deadline
When: Current time is 9:15 AM (routine expired)
When: Child view shows "TOO LATE" stamp on routine
When: Parent opens routine kebab → "Extend Time"
When: Parent confirms extension
Then: Routine "TOO LATE" stamp disappears
Then: Child can submit routine again
Then: SSE event `routine_time_extended` received
```

---

## Test Suite 7: Real-Time SSE Events

**File**: `frontend/tests/e2e/mode_parent/routines/routine-sse-events.spec.ts`

### Test 7.1: Parent Sees Real-Time Routine Confirmations

```
Given: Parent and child have browsers open to their respective dashboards
When: Child completes routine (triggers confirm-routine endpoint)
Then: Parent sees pending routine notification appear without page refresh
Then: SSE event stream shows child_routine_confirmation event
```

### Test 7.2: Schedule Changes Propagate in Real-Time

```
Given: Parent editing routine schedule
When: Parent saves new schedule
Then: Child's routine list updates in real-time to reflect new schedule
Then: Expiry timers recalculate
```

---

## Test Suite 8: Edge Cases & Validation

**File**: `frontend/tests/e2e/mode_parent/routines/routine-edge-cases.spec.ts`

### Test 8.1: Cannot Mark Routine Complete Multiple Times Today

```
Given: Child completed routine and it was approved today
When: Child tries to click "Done" again on same routine
Then: Dialog shows "Already completed today" or similar message
Then: Cannot proceed
```

### Test 8.2: Cannot Assign Same Routine Twice

```
Given: "Morning Routine" already assigned to child
When: Parent tries to assign same routine again via RoutineAssignView
Then: Checkbox already checked or error shown
Then: Cannot create duplicate assignment
```

### Test 8.3: Cascade Delete

```
Given: Routine with items, schedules, overrides, and extensions created
When: Delete routine from parent library
Then: All related items deleted
Then: All child assignments removed
Then: All schedules removed
Then: All extensions removed
Then: All overrides removed
```

---

## Test Suite 9: Routine vs. Task Parity

**File**: `frontend/tests/e2e/mode_parent/routines/routine-task-parity.spec.ts`

### Test 9.1: ScheduleModal Works for Both Tasks and Routines

```
Given: ScheduleModal open with entityType prop
When: entityType = "task": schedule chore
Then: Endpoint called: PUT /child/:id/task/:id/schedule
When: entityType = "routine": schedule routine
Then: Endpoint called: PUT /child/:id/routine/:id/schedule
```

### Test 9.2: Override System Works Same for Both

```
Given: Both chores and routines assigned to child
When: Set custom points for chore and routine
Then: Both reflect custom values
Then: Both use same child_override table with entity_type differentiation
```

---

## Test Suite 10: Error Handling

**File**: `frontend/tests/e2e/mode_parent/routines/routine-errors.spec.ts`

### Test 10.1: Network Error on Routine Save

```
When: Network error during routine save
Then: Error message displayed
Then: User can retry
```

### Test 10.2: Authorization Error

```
Given: Parent trying to access another parent's routine
When: Fetch routine details
Then: 403 Forbidden response
Then: Appropriate error shown
```

### Test 10.3: Invalid Routine Data

```
When: Create routine with invalid points (0 or negative)
Then: Form validation error shown
When: Create routine with no items
Then: "Must have at least one item" error shown
```

---

## Test Execution Guide

### Run All Routine E2E Tests

```bash
npx playwright test e2e/mode_parent/routines --project=chromium-parent-routines
```

### Run Specific Test Suite

```bash
npx playwright test e2e/mode_parent/routines/routine-creation.spec.ts
```

### Run with Visual Mode

```bash
npx playwright test e2e/mode_parent/routines --headed --project=chromium-parent-routines
```

---

## Known Limitations & TODOs

1. **Per-child routine management section in ParentView** — May not have full kebab menu yet. Verify the UI shows assigned routines with Edit/Delete/Schedule options.
2. **RoutineDetailView** — Plan called for a dedicated detail view; currently using inline dialog. Consider creating full detail route if needed.
3. **Routine reordering** — Not implemented (order field exists in schema but no drag UI).
4. **Per-item completion tracking** — Deliberately out of scope (items don't track individual completion).

---

## Database Cleanup Between Tests

Use `global-setup.ts` to:

1. Clear e2e test database before all tests run
2. Seed parent + child + routines
3. Clear after final test completes

---

## Performance Considerations

- Routines ScrollingList may render many items → verify performance with 50+ routines
- SSE event propagation latency → measure event-to-UI update time
- Routine schedule calculations (especially interval mode) → verify no lag on schedule changes
