<!-- LEGACY_READ -->
<!--
// LEGACY TABLE WARNING:
// product_fitment is deprecated and must not be used for
// fitment evaluation, confidence scoring, or reconciliation.
// Use fitment_resolved instead.
-->
# Phase 2 Normalize Fitment Tests (Turn14)

## Purpose
Lock Phase 2 normalization behavior with deterministic, CLI-only tests that:
- Catch join/key mismatches.
- Prevent silent exits.
- Ensure `product_fitment` is only populated on true success.

Scope and guardrails:
- CLI execution only.
- Real DB (no mocks).
- Writes allowed only to `product_fitment` and `turn14_items.fitment_normalized`.
- No runtime code, no schema changes, no AI/heuristics.

## TEST-01 — Happy Path (Turn14, Canonical Demo Item)
Command:
```
php fitment/normalize_fitment.php --distributor=turn14 --item_id=4386648
```

Expected:
- Exit code: `0`
- Console: no `[PHASE2 ERROR]`
- `turn14_items.fitment_normalized = 1`
- ≥ 1 row written to `product_fitment`

SQL assertion:
```
SELECT COUNT(*) AS cnt
FROM product_fitment
WHERE internal_product_id = 18579;
```

Pass condition:
```
cnt > 0
```

## TEST-02 — Missing Turn14 Item
Command:
```
php fitment/normalize_fitment.php --distributor=turn14 --item_id=DOES_NOT_EXIST
```

Expected console output:
```
[PHASE2 ERROR] Turn14 item not found or missing part_number
```

Assertions:
- Exit code: `1`
- No rows written to `product_fitment`
- No flags flipped

## TEST-03 — Missing Required Arguments
Command:
```
php fitment/normalize_fitment.php --distributor=turn14
```

Expected console output:
```
[PHASE2 ERROR] Missing required arguments
```

Assertions:
- Exit code: `1`
- No DB writes

## TEST-04 — No Raw Fitment Rows for part_number
Setup:
Use a real Turn14 item that has no raw fitment, or temporarily point to an item
with a known non-matching `part_number`.

Expected console output:
```
[PHASE2 ERROR] No raw fitment rows found for part_number
```

Assertions:
- Exit code: `1`
- `turn14_items.fitment_normalized` remains `0`
- `product_fitment` unchanged

## TEST-05 — Zero Rows Written Guard
Condition:
If raw rows are found but normalization logic produces zero authoritative rows.

Expected console output:
```
[PHASE2 ERROR] Normalization produced zero authoritative rows
```

Assertions:
- Exit code: `1`
- `fitment_normalized` must NOT flip to `1`
- Runtime remains safe

## Recommended Execution Order
```
php fitment/normalize_fitment.php --distributor=turn14 --item_id=4386648 || exit 1
php fitment/normalize_fitment.php --distributor=turn14 --item_id=DOES_NOT_EXIST && exit 1
php fitment/normalize_fitment.php --distributor=turn14 || exit 1
```
