# Phase 3 User Test (Copilot CLI orchestrator)

Optional, interactive. Lets the user test the code before the PR. The prompt MUST say
that yes **removes the worktree and checks the branch out locally**, or the side effect
is implicit. Pickers follow `$HOME/.claude/multi-agent-refs/picker-contract.md`.

0. **Test Gap Report (advisory)**  -  before the local-checkout prompt run `node $HOME/.claude/scripts/test-gap-scan.mjs --base "$BASE_BRANCH" --stack <ios|android|python|node>` to surface public symbols added in this branch that have no paired test. Heuristic, deterministic, sub-second, never gates by default. iOS `*View.swift` / Android `@Composable` symbols default to `important` severity; other public API additions to `suggestion`. Optional gating via `prefs.testGap.blockingThreshold` (loops back to Phase 2 rework when the important+blocking count exceeds the threshold). Disabled when `prefs.testGap.enabled = false`.
1. **Ask** with a native `AskUserQuestion` picker (never a typed y/N prompt). The options MUST make the local-checkout side effect explicit  -  testing removes the worktree and checks the branch out into the main repo:
   - `question`: "Check out locally to test now?" (rendered in `outputLanguage`)
   - `header`: "Test" (English, <=12 chars)
   - `options`:
     - `{ label: "Test now", description: "Removes the worktree and checks the branch out into the main repo for Xcode / manual test" }`
     - `{ label: "Skip", description: "Stay in the worktree and go to Phase 4" }`
   - **Skip** → go to Phase 4
   - **Test now** → continue:
2. **Hand the branch over** (automatic). Make the WIP commit yourself, so it passes the same hooks as any commit (Standard path item 3 of Phase 4 Step 2 folds it back with Phase 2's checkpoints). The script then holds the run artefacts and the project root's current branch, removes the worktree and checks the branch out. `{worktree-path}` and `{jiraId}` come from the state (`worktreePath`, `jiraId`):
   ```bash
   git -C "{worktree-path}" add -A
   git -C "{worktree-path}" diff --cached --quiet || git -C "{worktree-path}" commit -m "WIP: {jiraId}  -  changes for user test"
   bash $HOME/.claude/scripts/user-test-handoff.sh checkout --task "{jiraId}" --worktree "{worktree-path}" --root "{project-root}" --branch "{branch-name}"
   ```
   A refused commit stops here with nothing moved. Script exits: 3 the project root has uncommitted changes; 5 the worktree or a submodule holds uncommitted, unpushed or stashed work (a submodule with no remote needs one added); both move nothing. 6 a handoff is already held, or git refused part way: run `restore` below (if it answers 7, nothing had moved). 1 the worktree is not on the branch.
3. **Give test instructions**:
   ```
   🧪 Switched to branch: {branch-name}

   To test:
     • SourceTree → check the commits and diff
     • Xcode → open the app's `.xcworkspace` → Cmd+B → Cmd+U
     • Manual test on the Simulator

   ✅ "tamam" → proceeds to Phase 4
   ❌ "fix: ..." → worktree is recreated, returns to Phase 2
   ```
4. **Pause and wait**: Wait for the user's response. Either answer starts by handing the branch back, which returns the project root to its branch, re-adds the worktree and returns the held artefacts:
   ```bash
   bash $HOME/.claude/scripts/user-test-handoff.sh restore --task "{jiraId}" --worktree "{worktree-path}" --root "{project-root}" --branch "{branch-name}"
   ```
   Exit 3: the user edited files on the branch in the project root. Ask them to `git stash`, run `restore` again, then `git -C "{worktree-path}" stash pop`; a commit made there instead would stop Phase 4 from folding the checkpoints. Exit 6: the project root could not leave the branch (an untracked file in the way) or the worktree could not be re-added; the message names the cause, the hold is kept, and `restore` runs again once it is cleared. Exit 7: nothing was held, so the worktree was never removed.
5. **If a fix is needed**: return to Phase 2 in the restored worktree.
6. Log: `🧪 Phase 3: Review (user test)  -  {result}`

> ⚠️ Never remove the worktree by hand: `user-test-handoff.sh` is the only path, and it refuses whenever work would be lost.
