# 6. Testing and Troubleshooting

## Quick test script

Save as `test_autotrain.py`:

```python
import requests

BASE = "http://localhost:8000/autotrain"


def submit_feedback():
    resp = requests.post(
        f"{BASE}/feedback",
        json={
            "query": "tim ao khoac cho troi 35 do",
            "ai_response": "Minh goi y ao khoac ni nay rat am...",
            "feedback_text": "Troi nong qua, AI con tu van ao khoac",
            "feedback_type": "bug",
            "user_id": "test-user-001"
        },
    )
    resp.raise_for_status()
    data = resp.json()
    print("feedback_id:", data["feedback_id"])
    return data["feedback_id"]


def run_all():
    resp = requests.post(f"{BASE}/run-all")
    resp.raise_for_status()
    data = resp.json()
    print("processed:", data["processed"])
    return data


def list_pending_rules():
    resp = requests.get(f"{BASE}/rules?status=pending")
    resp.raise_for_status()
    rules = resp.json()
    for rule in rules:
        print(rule["rule_id"], rule["confidence"], rule["new_rule"][:60])
    return rules


def approve_rule(rule_id):
    resp = requests.post(f"{BASE}/rules/{rule_id}/approve?reviewed_by=admin")
    resp.raise_for_status()
    print(resp.json())


def deploy_rule(rule_id):
    resp = requests.post(f"{BASE}/rules/{rule_id}/deploy?deployed_by=admin")
    resp.raise_for_status()
    data = resp.json()
    print("deploy:", data["status"])
    print("target_file:", data["target_file"])
    print("backup_file:", data["backup_file"])


def status():
    resp = requests.get(f"{BASE}/status")
    resp.raise_for_status()
    print(resp.json())


if __name__ == "__main__":
    fb_id = submit_feedback()
    run_all()
    rules = list_pending_rules()
    if rules:
        approve_rule(rules[0]["rule_id"])
        deploy_rule(rules[0]["rule_id"])
    status()
```

Run:

```bash
python test_autotrain.py
```

---

## Manual curl flow

```bash
# 1. Submit feedback
curl -X POST http://localhost:8000/autotrain/feedback \
  -H "Content-Type: application/json" \
  -d '{
    "user_id": "CNF-98453",
    "query": "tim ao khoac cho troi 35 do",
    "ai_response": "Minh goi y ao khoac ni nay rat am.",
    "feedback_text": "Troi nong qua, AI con tu van ao khoac",
    "feedback_type": "bug"
  }'

# 2. Generate rules for all pending feedback
curl -X POST http://localhost:8000/autotrain/run-all

# 3. Check pending rules
curl http://localhost:8000/autotrain/rules?status=pending

# 4. Approve rule
curl -X POST http://localhost:8000/autotrain/rules/RUL-1001/approve?reviewed_by=admin

# 5. Deploy rule
curl -X POST http://localhost:8000/autotrain/rules/RUL-1001/deploy?deployed_by=admin

# 6. Check status
curl http://localhost:8000/autotrain/status
```

---

## Expected behavior checklist

- [ ] `POST /feedback` tao feedback moi voi `status = pending`
- [ ] `POST /run-all` sinh rule va chuyen feedback thanh `processed`
- [ ] `GET /rules?status=pending` thay rule moi
- [ ] `GET /rules/{rule_id}/validate` tra ket qua validation
- [ ] `POST /rules/{rule_id}/approve` doi status sang `approved`
- [ ] `POST /rules/{rule_id}/deploy` doi status sang `deployed`
- [ ] deployment tao backup file va insert row vao `deployments`
- [ ] `GET /status` thay `deployed_rules` tang len

---

## SQLite verification

```bash
sqlite3 backend/database/autotrain.db
```

Trong `sqlite3`:

```sql
.tables
SELECT feedback_id, status FROM feedbacks;
SELECT rule_id, feedback_id, status FROM generated_rules;
SELECT rule_id, deployed_by, deployed_at FROM deployments;
```

Ky vong sau full flow:

```text
feedbacks:
FB-1001 | processed

generated_rules:
RUL-1001 | FB-1001 | deployed

deployments:
RUL-1001 | admin | <timestamp>
```

---

## Common issues

### 1. "Feedback not found" (404)

Nguyen nhan:

- `feedback_id` khong ton tai

Kiem tra:

```bash
curl http://localhost:8000/autotrain/feedback
```

---

### 2. "Rule not found or not approved" (400 on deploy)

Nguyen nhan:

- rule van dang `pending`
- rule da bi `rejected`

Fix:

```bash
curl -X POST http://localhost:8000/autotrain/rules/RUL-1001/approve
```

---

### 3. Rule generation fail validation (422)

Nguyen nhan:

- `new_rule` qua ngan
- `reasoning` rong
- `confidence` ngoai khoang `0..1`
- co toxic words

Tac dong:

- feedback se bi mark `rejected`

---

### 4. Target file not found (500)

Nguyen nhan:

- `module_name` khong duoc support
- file target khong ton tai tren disk

Hien tai module duoc support:

- `fashion_rules.json`

Va target file thuc te la:

```text
backend/worker/fashion_rules.json
```

---

### 5. JSON structure khong hop le khi deploy (500)

Nguyen nhan:

- file `fashion_rules.json` khong parse duoc
- file khong phai JSON object

Vi Auto-Train append vao key `auto_train_rules`, file nay phai con la mot JSON object hop le.

---

### 6. Duplicate feedback_id

Nguyen nhan:

- tu truyen `feedback_id` bi trung

Fix:

- bo qua field `feedback_id` de system auto-generate

---

## Debugging tips

Bat logging:

```python
import logging
logging.basicConfig(level=logging.DEBUG)
```

Them log tam thoi:

```python
logger.info("[AutoTrain] Generating rule for feedback: %s", feedback_id)
logger.info("[AutoTrain] Rule generated: %s", rule_data)
logger.info("[AutoTrain] Deploy target: %s", target_path)
```

---

## Integration test idea

```python
def test_full_pipeline(client, sample_feedback):
    fb_resp = client.post("/autotrain/feedback", json=sample_feedback)
    assert fb_resp.status_code == 200
    fb_id = fb_resp.json()["feedback_id"]

    gen_resp = client.post(f"/autotrain/generate/{fb_id}")
    assert gen_resp.status_code == 200
    rule_id = gen_resp.json()["rule_id"]

    valid_resp = client.get(f"/autotrain/rules/{rule_id}/validate")
    assert valid_resp.json()["ok"] is True

    app_resp = client.post(f"/autotrain/rules/{rule_id}/approve")
    assert app_resp.json()["status"] == "approved"

    dep_resp = client.post(f"/autotrain/rules/{rule_id}/deploy")
    assert dep_resp.json()["status"] == "deployed"
```

---

## Reset test data

Neu can reset DB:

```python
def cleanup_test_data():
    conn = get_db()
    cur = conn.cursor()
    cur.execute("DELETE FROM deployments")
    cur.execute("DELETE FROM generated_rules")
    cur.execute("DELETE FROM feedbacks")
    cur.execute("DELETE FROM sqlite_sequence WHERE name='feedbacks'")
    cur.execute("DELETE FROM sqlite_sequence WHERE name='generated_rules'")
    cur.execute("DELETE FROM sqlite_sequence WHERE name='deployments'")
    conn.commit()
    conn.close()
```

Hoac xoa DB file roi init lai:

```bash
python -c "from api.autotrain.autotrain_route import init_db; init_db()"
```

---

## Performance notes

Hien tai:

- SQLite writes van serialized
- `run-all` la synchronous endpoint
- status endpoint chua cache

Neu muon scale:

- doi sang PostgreSQL
- dua generation vao background queue
- them cache cho counters / status

---

## Support

Neu flow van hong:

1. Kiem tra `backend/database/autotrain.db`
2. Kiem tra `backend/worker/fashion_rules.json`
3. Bat DEBUG logging
4. So sanh response API voi docs trong folder `28-autotrain`
