# Testing & Regression Standard

## 1. Core Principle

Every meaningful behavior change must have verification.

AI-generated code must never be considered correct merely because:

- PHP syntax is valid
- Application boots
- Endpoint returns 200
- Code looks correct

Business behavior must be verified.

---

## 2. Test Pyramid

Prefer:

```text
Feature Tests
    ↓
Unit Tests
    ↓
Integration/Database Tests
```

Use the appropriate level for the behavior being tested.

---

## 3. Feature Tests

Feature tests should cover:

- API endpoints
- Authentication
- Authorization
- Validation
- Business workflows
- Status transitions
- Database changes

---

## 4. Unit Tests

Use unit tests for isolated business logic such as:

- Calculations
- Rules
- DTO transformations
- Status transition rules
- Commission calculations
- Domain services

---

## 5. Regression Tests

Whenever fixing a bug:

1. Reproduce the bug
2. Write a regression test
3. Implement the fix
4. Verify the test passes

Do not fix a recurring bug without protecting it with a test when practical.

---

## 6. Existing Tests Are Contracts

Do not delete or weaken existing tests simply because implementation changes.

If a test fails after a change:

Determine whether:

```text
Code is wrong
OR
Test represents outdated behavior
```

Only change the test when business requirements genuinely changed.

---

## 7. Database Testing

Database-dependent tests should verify:

- Record creation
- Relationships
- Constraints
- Status changes
- Transactions
- Rollback behavior
- Authorization effects

---

## 8. API Testing

Verify:

- HTTP status
- Response structure
- Success flag
- Message
- Data structure
- Validation errors
- Authorization errors
- Resource formatting

---

## 9. Permission Testing

For protected operations test:

```text
Authorized user → allowed
Unauthorized user → denied
Missing permission → denied
Wrong role → denied
```

---

## 10. Workflow Testing

For workflow/state machines test valid and invalid transitions.

Example:

```text
pending → approved
pending → rejected
approved → pending   ❌
```

Invalid transitions must be rejected.

---

## 11. Test Before Refactoring

Before large refactoring:

- Run existing test suite
- Record baseline result
- Refactor
- Run tests again

The goal is:

```text
Behavior unchanged
Architecture improved
Tests remain green
```

---

## 12. AI Validation Requirement

AI must report exactly what was executed.

Good:

```text
Executed:
php artisan test --filter=TaskTest

Result:
12 passed
```

Bad:

```text
Tests should pass.
```

Never claim unexecuted validation.

---

## 13. Final Test Checklist

- [ ] Existing relevant tests executed
- [ ] New behavior tested
- [ ] Regression scenario tested
- [ ] Authorization tested
- [ ] Validation tested
- [ ] Database behavior tested
- [ ] Failure scenarios tested
- [ ] Full suite considered
