---
name: coverage-check
description: Gate 2 Phase 2d — Apply action items từ review-report (Phase 2c), generate coverage matrix (requirement→TC mapping), tạo final testcase set hoàn chỉnh, và trigger automation-testing để sinh Playwright scripts. Chạy sau Phase 2c APPROVED.
keywords: gate2, phase2d, coverage, matrix, final testcase, playwright, scripts, traceability
---

# Coverage Check & Final Output — Gate 2 Phase 2d

> **GATE 2 PHASE 2d: Chỉ chạy sau Phase 2c (Review & Optimize) đã được APPROVED.**
>
> Phase này là giai đoạn cuối của Gate 2: apply tất cả sửa đổi, generate coverage matrix, xuất final TC set, và sinh Playwright scripts.

---

## Conditions to Enter Phase 2d

- ✅ Phase 2c APPROVED — `test-plan/review-report.md` đã được TESTER approve
- ✅ Files tồn tại: `test-plan/test-cases/`, `test-plan/review-report.md`, `test-plan/test-analysis.md`

---

## Checklist trước khi output (bắt buộc)

Trước khi xuất final TC set, kiểm tra:

- [ ] Đã xóa duplicate TCs (từ review-report.md Section 1)
- [ ] Đã sửa TC có vấn đề (từ review-report.md Section 2)
- [ ] Đã tách/gộp TCs cần optimize (từ review-report.md Section 3)
- [ ] Đã thêm TC còn thiếu (từ review-report.md Section 4)
- [ ] Đã bổ sung Accessibility/Compatibility cases (từ review-report.md Section 6)
- [ ] 100% requirements trong scope được cover bởi ít nhất 1 TC

---

## Step 1: Apply Action Items từ Phase 2c

Đọc `test-plan/review-report.md` và apply từng action item từ **Section 7 — Danh sách cần sửa**:

| Loại action | Cách thực hiện |
|-------------|---------------|
| Xóa duplicate | Remove TC file hoặc xóa row khỏi testcase table |
| Sửa TC có vấn đề | Edit cột Scenario/Steps/Expected Result/Preconditions cụ thể |
| Tách TC dài | Split thành 2 TC riêng với TC_ID mới |
| Gộp TC | Merge nội dung, giữ TC_ID của TC đầu tiên |
| Thêm TC mới | Tạo TC mới với TC_ID tiếp theo trong chuỗi |

Ghi lại những gì đã apply:

```markdown
## Action Items Applied

| # | Action | TC_ID | Vấn đề | Priority | Status |
|---|--------|-------|--------|---------|--------|
| 1 | Sửa Expected Result | AD10_005 | Expected Result mơ hồ | High | ✅ Done |
| 2 | Thêm Precondition | AD10_007 | Thiếu precondition | High | ✅ Done |
| 3 | Thêm TC mới | AD10_012 | Thiếu screen reader case | Medium | ✅ Done |
| 4 | Xóa duplicate | AD10_003 | Duplicate với AD10_001 | High | ✅ Done |
```

---

## Step 2: Generate Coverage Matrix

So sánh requirements từ `test-plan/test-analysis.md` với TC set hiện tại:

**Format coverage matrix:**

```markdown
## Coverage Matrix — [Feature Name]

| # | Requirement/Acceptance Criteria | Có TC cover? | TC IDs | % |
|---|--------------------------------|-------------|--------|---|
| AC1 | User đăng nhập thành công với credentials hợp lệ | ✅ | AD10_001 | 100% |
| AC2 | Hiển thị error khi credentials sai | ✅ | AD10_002 | 100% |
| AC3 | Validate required fields | ✅ | AD10_003, AD10_004 | 100% |
| BR1 | Account bị lock sau 5 lần sai | ✅ | AD10_008 | 100% |
| WCAG | Keyboard navigation qua form | ✅ | AD10_006 | 100% |
| COMPAT | Hoạt động trên Firefox | ⚠️ | AD10_013 (pending) | 0% |

**Summary:**
- Total requirements/AC: [N]
- Covered: [N] ([N]%)
- Not covered: [N] ([N]%)
```

**Nếu coverage < 100%** → tạo TC mới để cover trước khi tiếp tục.

---

## Step 3: Export Final Testcase Set

Consolidate toàn bộ TCs (sau khi apply actions) vào **1 file duy nhất**:

Lưu `test-plan/test-cases/final-testcases.md`:

Ngôn ngữ output: auto-detect theo ngôn ngữ của ticket/testcase input — xem `custom/rules/output-language.md` và `custom/skills/test-skills/rules/qa-writing-standards.md` (input tiếng Việt → output tiếng Việt; ngược lại mặc định tiếng Anh; tiêu đề cột bảng vẫn giữ tiếng Anh).

```markdown
# Final Test Cases — [Feature Name]

**Version:** 2.0 (after Phase 2d) | **Date:** [YYYY-MM-DD] | **Status:** Final

| TC_ID | Scenario | Preconditions | Test Steps | Test Data | Expected Result | Severity | Type |
|-------|----------|---------------|------------|-----------|----------------|----------|------|
| AD10_001 | Đăng nhập thành công | App chạy. User tồn tại. | 1. Mở /login 2. Nhập email hợp lệ 3. Nhập password hợp lệ 4. Click "Đăng nhập" | user@example.com / Password123! | Redirect tới /dashboard. Hiển thị "Chào [Tên user]" ở header. | Critical | Auto |
| AD10_002 | Error khi sai password | App chạy. User tồn tại. | 1. Mở /login 2. Nhập email hợp lệ 3. Nhập password SAI 4. Click "Đăng nhập" | user@example.com / WrongPass | Error "Sai email hoặc mật khẩu" hiển thị. URL vẫn /login. | High | Auto |
| ... | ... | ... | ... | ... | ... | ... | ... |
```

**Cột Type:**
- `Auto` — test case này sẽ có Playwright script
- `Manual` — test case này cần TESTER test thủ công (UI phức tạp, OTP, physical device)

---

## Step 4: Generate Playwright Scripts (Incremental)

Với tất cả TCs có `Type = Auto` trong `final-testcases.md`:

**Bước 4a — Kiểm tra scripts hiện có:**
```
Glob: test-plan/test-scripts/*.spec.ts
```
Phân loại mỗi TC_ID:
- `[NEW]` — chưa có `.spec.ts` tương ứng → cần sinh
- `[UPDATED]` — đã có `.spec.ts` nhưng TC nằm trong danh sách thay đổi (từ Section 7 review-report.md) → cần regenerate
- `[SKIP]` — đã có `.spec.ts` và TC không thay đổi → giữ nguyên

**Bước 4b — Invoke `automation-testing` skill** chỉ cho TCs `[NEW]` và `[UPDATED]`:

```
Sinh Playwright scripts cho:
[NEW]     AD10_005 — Scenario: ...
[UPDATED] AD10_002 — Scenario: ... (TC content changed: Expected Result đã sửa)
Output path: test-plan/test-scripts/
```

Scripts được lưu tại `test-plan/test-scripts/[TC_ID].spec.ts`:

```
test-plan/test-scripts/
├── AD10_001.spec.ts   ← [SKIP] unchanged
├── AD10_002.spec.ts   ← [UPDATED] regenerated
├── AD10_003.spec.ts   ← [SKIP] unchanged
├── AD10_004.spec.ts   ← [SKIP] unchanged
├── AD10_005.spec.ts   ← [NEW] created
└── ...
```

**Traceability mapping:**

```markdown
## Script Traceability

| TC_ID | Script File | Status | Ghi chú |
|-------|-------------|--------|---------|
| AD10_001 | test-plan/test-scripts/AD10_001.spec.ts | ✅ SKIP — unchanged | |
| AD10_002 | test-plan/test-scripts/AD10_002.spec.ts | ✅ UPDATED | Expected Result sửa |
| AD10_005 | test-plan/test-scripts/AD10_005.spec.ts | ✅ NEW | |
| AD10_011 | — | 🔧 Manual (OTP required) | |
```

---

## Gate 2d Display (Final)

```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⏸️ GATE 2: TEST PLAN READY

Test cases: [N] total ([N] Auto / [N] Manual)
Coverage:   [N/N] requirements covered ([N]%)
Scripts:    [N] Playwright specs ([N] new / [N] updated / [N] skipped unchanged)

Issues resolved:
  Duplicates removed:  [N]
  TCs fixed:           [N]
  TCs added:           [N]
  TCs split/merged:    [N]

→ Review: test-plan/
│   ├── test-analysis.md        (Gate 1)
│   ├── checklist.md            (Phase 2a)
│   ├── test-plan.md            (Phase 2b)
│   ├── review-report.md        (Phase 2c)
│   ├── test-cases/final-testcases.md  (Phase 2d final)
│   └── test-scripts/*.spec.ts  (Playwright)
→ Type APPROVED to proceed to Gate 3 (Execution)
→ Or provide feedback to update
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```

Sau APPROVED → **tiến sang Gate 3**.

---

## Mandatory Rules

- ❌ **KHÔNG** generate scripts trước khi coverage = 100%
- ❌ **KHÔNG** bỏ qua bất kỳ action item nào từ review-report.md
- ✅ **PHẢI** apply TẤT CẢ changes từ Phase 2c trước khi generate coverage matrix
- ✅ **PHẢI** đạt 100% AC coverage trước khi phase kết thúc
- ✅ **PHẢI** có traceability mapping (TC_ID → script file)
- ✅ Scripts **PHẢI** dùng selector từ DOM thật (thông qua automation-testing skill Phase 3)
