# Console Implementation - Remaining Work

## ✅ Completed

1. **Test Plan JSON** (`plan.json`) - All phases/tests/steps defined
2. **Console UI** (`console/index.html`, `console.js`, `console.css`) - Full Textual-style interface
3. **TestEventsPlugin** - Android plugin to receive ADB broadcasts
4. **Event System** - Library functions emit events (`ui_prompt`, `capture_*`, `verdict_*`)
5. **Helper Functions** - `set_test_context()`, `step_start()`, `step_pass()`, `step_warn()`, `step_fail()`
6. **Example Wiring** - Test 0 and partial Test 1 in `test-phase1.sh` demonstrate pattern
7. **Plugin Registration** - `TestEventsPlugin` added to `capacitor.plugins.json`
8. **Documentation** - Usage guide created

## 🔧 Critical Fix Applied

✅ **TestEventsPlugin Registration** - Added to `capacitor.plugins.json` (required for plugin to work)

## 📋 Remaining Work

### 1. Complete Wiring of test-phase1.sh

**Status**: Partially complete (Test 0 done, Test 1 started, Tests 2-4 not wired)

**Pattern to apply**:
```bash
# At test start
set_test_context "phase1" "phase1_testX" ""

# For each step
set_test_context "phase1" "phase1_testX" "p1_tX_sY"
step_start "p1_tX_sY" "Step description"
# ... do work ...
step_pass "p1_tX_sY" "Step completed"
```

**Tests to wire**:
- ✅ Test 0: Daily Rollover Verification (complete)
- ⚠️ Test 1: Force-Stop Recovery (partially done, needs completion)
- ❌ Test 2: Schedule Update Verification (not wired)
- ❌ Test 3: Recovery Timeout (not wired)
- ❌ Test 4: Invalid Data Handling (not wired)

**Step IDs from plan.json**:
- Test 1: `p1_t1_s1` through `p1_t1_s5`
- Test 2: `p1_t2_s1` through `p1_t2_s4`
- Test 3: `p1_t3_s1` through `p1_t3_s2`
- Test 4: `p1_t4_s1` through `p1_t4_s4`

### 2. Wire test-phase2.sh

**Status**: Not started

**Tests to wire**:
- Test 1: Force Stop – Alarms Cleared (`phase2_test1`)
- Test 2: Force Stop – Alarms Intact (`phase2_test2`)
- Test 3: First Launch / No Schedules (`phase2_test3`)

**Step IDs from plan.json**:
- Test 1: `p2_t1_s1` through `p2_t1_s5`
- Test 2: `p2_t2_s1` through `p2_t2_s5`
- Test 3: `p2_t3_s1` through `p2_t3_s4`

### 3. Wire test-phase3.sh

**Status**: Not started

**Tests to wire**:
- Test 1: Boot with Future Alarms (`phase3_test1`)
- Test 2: Boot with Past Alarms (`phase3_test2`)
- Test 3: Boot with No Schedules (`phase3_test3`)
- Test 4: Silent Boot Recovery (`phase3_test4`)

**Step IDs from plan.json**:
- Test 1: `p3_t1_s1` through `p3_t1_s5`
- Test 2: `p3_t2_s1` through `p3_t2_s5`
- Test 3: `p3_t3_s1` through `p3_t3_s4`
- Test 4: `p3_t4_s1` through `p3_t4_s4`

### 4. Testing & Validation

**Steps**:
1. Build Android app: `cd test-apps/android-test-app && ./gradlew assembleDebug`
2. Install on emulator/device
3. Navigate to console: Open app → should redirect to `/console/`
4. Verify console loads: Check browser console for errors
5. Test Phase A (UI-only):
   - Select a test
   - Manually mark steps as done/fail
   - Verify state persists
6. Test Phase B (Live events):
   - Set `export DNP_UI_EVENTS=1`
   - Run `./test-phase1.sh 0` (just test 0)
   - Verify events appear in console
   - Verify step status updates automatically

## 🎯 Quick Start: Wiring a Test Function

Here's a complete example for wiring a test:

```bash
test_example() {
  section "TEST: Example Test"
  
  # Set test context (no step yet)
  set_test_context "phase1" "phase1_example" ""
  
  # Step 1: Setup
  set_test_context "phase1" "phase1_example" "p1_ex_s1"
  step_start "p1_ex_s1" "Setting up test"
  capture_alarms "example_initial"
  step_pass "p1_ex_s1" "Setup complete"
  
  # Step 2: Execute
  set_test_context "phase1" "phase1_example" "p1_ex_s2"
  step_start "p1_ex_s2" "Executing test"
  # ... do work ...
  if [ success ]; then
    step_pass "p1_ex_s2" "Execution complete"
  else
    step_fail "p1_ex_s2" "Execution failed"
  fi
  
  # Step 3: Verify
  set_test_context "phase1" "phase1_example" "p1_ex_s3"
  step_start "p1_ex_s3" "Verifying results"
  # ... verify ...
  step_pass "p1_ex_s3" "Verification passed"
  
  # Verdict (automatically emits event)
  set_test_context "phase1" "phase1_example" "p1_ex_s4"
  verdict_pass "example_test" "Test passed"
}
```

## 📝 Notes

- **Step IDs must match plan.json** - Check `console/plan.json` for exact step IDs
- **Context must be set before step events** - Call `set_test_context()` before `step_start()`
- **Verdict functions auto-emit events** - No need to manually emit verdict events
- **Evidence capture auto-emits events** - `capture_alarms()`, `capture_logcat()`, `capture_screenshot()` emit events automatically
- **Operator prompts auto-emit events** - `ui_prompt()` emits `operator_required` events automatically

## 🔍 Verification Checklist

After wiring tests, verify:

- [ ] All test functions have `set_test_context()` at start
- [ ] All major steps have `step_start()` and `step_pass/fail/warn()`
- [ ] Step IDs match `plan.json` exactly
- [ ] Verdict functions are called (they emit events automatically)
- [ ] Evidence capture functions are called (they emit events automatically)
- [ ] Scripts run without errors
- [ ] Console receives events when `DNP_UI_EVENTS=1` is set

## 🚀 Priority Order

1. **Complete test-phase1.sh** (highest priority - most used)
2. **Wire test-phase2.sh** (medium priority)
3. **Wire test-phase3.sh** (medium priority)
4. **Test integration** (validate everything works)

## 📚 Reference

- **Usage Guide**: `docs/CONSOLE-USAGE.md`
- **Test Plan**: `app/src/main/assets/public/console/plan.json`
- **Event Functions**: `alarm-test-lib.sh` (functions: `emit_event`, `set_test_context`, `step_*`)
