# Lead review: a reproducible local dry run

This educational example takes fictional Google Ads and Meta Ads source labels through normalization, deduplication, quality checks, a simulated human review and a simulated delivery queue. Classification uses explicit JavaScript rules. There is no LLM, advertising API connection, CRM connection, personal data or client result in this package.

## Run it

Use Node.js 20 or later. Download and extract the ZIP, open a terminal in its folder, then run:

```sh
node run.mjs
node run.mjs --verify
```

The first command writes `observed-results.json` beside the script and prints the same JSON. The second compares the full output against `expected-results.json` and runs eight edge checks. No package installation or network access is needed. Tested on Node.js v24.15.0 on October 17, 2026; this is a local execution test, not an n8n import test.

## Input contract

`source-fixture.json` has three parts:

| Part | Fields and meaning |
| --- | --- |
| `events` | `eventId`, `leadId`, `source`, `market`, `crmStatus`, `consent`, `formComplete`, `intent` |
| `humanReview.approvedLeadIds` | Fictional reviewer decisions supplied as input. They do not come from the classification function. |
| `deliverySimulation` | Which synthetic lead fails once and the maximum number of attempts. |

All IDs are anonymous fixture references. They are not reusable cross-platform identity keys. A real integration needs an agreed identity mapping and persistent deduplication storage across runs.

## Rules and decisions

1. Trim strings; normalize source/status to lowercase and market/lead ID to uppercase. Boolean flags must be the boolean `true`; a string such as `"true"` does not pass. Output uses an allowlist and omits unknown input fields.
2. Drop an identical normalized event with the same event ID. Stop on a reused event ID with a different payload. Group the remaining events by the fixture's lead ID. Never silently choose a winner between conflicting statuses.
3. Hold a record if consent is missing, or if its only status is `disqualified` or `customer`. In this exercise, a consent flag is a routing rule; it does not establish a legal basis for processing real data.
4. Route conflicting or unknown statuses, unrecognized sources, markets outside US/CA, conflicting markets, incomplete forms and general questions to `needs_review`. A `new` status also needs review. Only a consistent `qualified` record with complete data, a requested demo and passing flags becomes `ready_for_review`.
5. Read the separate reviewer decisions. Reject an approval for any record outside `ready_for_review`. No approval means no delivery attempts. In a real process, a reviewer would correct flagged source data before approving it; that resolution is outside this fixture.
6. Send approved IDs to the local delivery simulation. Reuse one idempotency key for retries. `M-002` deliberately fails on attempt 1, enters `retry_queued`, then receives a `simulated_receipt` on attempt 2. A single-attempt configuration leaves it failed.

The script has no durable queue, delay, remote endpoint or remote idempotency guarantee. All simulation state is recreated on each run.

## Expected result

| Measure | Observed count |
| --- | ---: |
| Input events | 12 |
| Unique events | 11 |
| Removed duplicate events | 1 |
| Unique leads | 10 |
| Ready for human review | 2 |
| Needs review | 6 |
| Held | 2 |
| Fixture approvals | 2 |
| Simulated receipts | 2 |
| Simulated retry transitions | 1 |
| Real external deliveries | 0 |

Inspect `G-007` for `unknown_status`, `G-009` for `conflicting_status`, and the delivery log for the two attempts on `M-002`. `expected-results.json` and the included `observed-results.json` contain the full decisions and simulation log. These are fixture outcomes, not lead-quality or sales benchmarks.

`--verify` checks the expected output plus: conflicting event payload rejection, unsafe approval rejection, no approval/no attempts, omission of extra input data, retry exhaustion, conflicting markets, unknown status and conflicting status.

## Map the pattern to n8n later

An implementation can start with Manual Trigger, then use Code nodes for normalization, grouping and rule evaluation. Keep a separate review checkpoint and delivery adapter. n8n's normal item structure is an array of objects with a `json` field; map the fixture fields explicitly rather than assuming a Google or Meta payload has this shape.

This package is a Node.js example, not an importable n8n workflow. Its filesystem CLI and Node imports are not drop-in Code node code. The n8n version, source permissions, webhook verification, actual payloads, reviewer access, persistent queue, retry timing and CRM response handling still need implementation and testing. A CRM acknowledgment should be stored separately from a sales outcome.

Official documentation checked October 17, 2026:

- [Manual Trigger](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.manualworkflowtrigger/): manual execution before adding automatic triggers.
- [Code node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.code/): JavaScript processing and execution modes.
- [n8n data structure](https://docs.n8n.io/data/data-structure/): item arrays and `json` fields.

## Package integrity

`SHA256SUMS.txt` lists the SHA-256 digest of each packaged payload file. The ZIP contains the README, script, fixture, expected result, observed result and checksum manifest. The manifest does not hash itself. A fresh local run should reproduce the observed result exactly.
