# Lead form: duplicate submission check

Fixtures are fictional and use example.test addresses. This is not a client case or evidence of a commercial result.

## Actually tested

In the actual local HTTP example, two rapid clicks create two leads before the fix and one after server-side idempotency. A valid submission returns 201; blank name and invalid email return 422. An injected server error returns 503 without writing; retrying the same key creates a record, and replay after success returns 200 after the fix. A network cut is explicitly simulated by browser interception before the handler, producing zero server requests or records. Each locale and phase executes eight POSTs: five stored records before and three after, because separate probes use distinct keys. Six of eight cases pass before the fix, with two failures; all eight pass after. The standalone test below repeats the handler contract without a browser; actual HTTP results and server logs are in the report JSON.

## Editable contract

- Rule owner: [name].
- Input version and expected scope: [values].
- Allowed fields, mappings and exceptions: [rules].
- Expected outcome for each case: [examples].
- Repeat-check and evidence retention procedure: [steps].

## Function and independent reproduction

Download both inventory JSON files for this locale. Save them beside model.mjs, copy the function and test code below into model.mjs, and run node model.mjs with Node.js. JSON and Markdown do not execute checks on their own.

```js
function validateLead(input) {
 const errors=[];
 if(typeof input.fullName!=='string'||input.fullName.trim().length<2||input.fullName.trim().length>100)errors.push('invalid-name');
 if(typeof input.email!=='string'||input.email.length>254||!/^\S+@\S+\.\S+$/.test(input.email))errors.push('invalid-email');
 if(typeof input.message!=='string'||input.message.trim().length<5||input.message.length>1000)errors.push('invalid-message');
 return errors;
}

function createLeadHandler(idempotent) {
 const state={records:[],requests:[]},seen=new Map(),failedOnce=new Set();
 function handle(input,idempotencyKey,injectFailure=false) {
  const errors=validateLead(input),normalised={fullName:String(input.fullName??'').trim(),email:String(input.email??'').trim().toLowerCase(),message:String(input.message??'').trim()},fingerprint=JSON.stringify(normalised);
  let result;
  if(errors.length)result={status:422,outcome:'validation-rejected',errors};
  else if(typeof idempotencyKey!=='string'||idempotencyKey.length<8||idempotencyKey.length>100)result={status:400,outcome:'invalid-idempotency-key'};
  else if(idempotent&&seen.has(idempotencyKey)) {
   const previous=seen.get(idempotencyKey);result=previous.fingerprint===fingerprint?{status:200,outcome:'replayed',recordId:previous.recordId}:{status:409,outcome:'idempotency-conflict'};
  } else if(injectFailure&&!failedOnce.has(idempotencyKey)) {failedOnce.add(idempotencyKey);result={status:503,outcome:'injected-server-failure'};}
  else {const recordId='lead-'+String(state.records.length+1).padStart(3,'0');state.records.push({recordId,...normalised,idempotencyKey});seen.set(idempotencyKey,{fingerprint,recordId});result={status:201,outcome:'created',recordId};}
  state.requests.push({sequence:state.requests.length+1,idempotencyKey,...result});return result;
 }
 return {state,handle};
}

import fs from 'node:fs';
import assert from 'node:assert/strict';
const input = JSON.parse(fs.readFileSync('en-after-inventory.json', 'utf8'));
const buggy = createLeadHandler(false);
buggy.handle(input.validLead, 'double-click-fixture');
buggy.handle(input.validLead, 'double-click-fixture');
assert.equal(buggy.state.records.length, 2);
const fixed = createLeadHandler(true);
assert.equal(fixed.handle(input.validLead, 'double-click-fixture').status, 201);
assert.equal(fixed.handle(input.validLead, 'double-click-fixture').status, 200);
assert.equal(fixed.state.records.length, 1);
assert.equal(fixed.handle(input.invalidInputs[0], 'invalid-name-fixture').status, 422);
assert.equal(fixed.handle(input.invalidInputs[1], 'invalid-email-fixture').status, 422);
const count = fixed.state.records.length;
assert.equal(fixed.handle(input.validLead, 'server-retry-fixture', true).status, 503);
assert.equal(fixed.state.records.length, count);
assert.equal(fixed.handle(input.validLead, 'server-retry-fixture', true).status, 201);
assert.equal(fixed.handle(input.validLead, 'server-retry-fixture', true).status, 200);
assert.equal(fixed.handle({...input.validLead, message:'A different message.'}, 'double-click-fixture').status, 409);
console.log('PASS');
```

## Files

- [before inventory](https://evolkov.tech/wp-content/uploads/reproleaf-guides/landing-form-testing-20261117/en-before-inventory.json)
- [before report](https://evolkov.tech/wp-content/uploads/reproleaf-guides/landing-form-testing-20261117/en-before-report.json)
- [after inventory](https://evolkov.tech/wp-content/uploads/reproleaf-guides/landing-form-testing-20261117/en-after-inventory.json)
- [after report](https://evolkov.tech/wp-content/uploads/reproleaf-guides/landing-form-testing-20261117/en-after-report.json)

Screenshots show an owned local worksheet or form with actual results. This is not the Reproleaf interface. Example data does not establish production CRM readiness, specification quality, security, legal compliance or the absence of all defects.
