// Generated from lib/receiving-validation.ts; engine source SHA-256: 591e304e8a2f6157423415de27a89c54d490657ae3d76e698ef18161b807814a
// Synthetic component evaluation only. Run: node receiving-validation-reproduce.mjs
import { isDeepStrictEqual } from 'node:util';
export const RECEIVING_ENGINE_VERSION = '1.0.0';
export const MAX_DEMO_CARTONS = 1000000;
const fieldNames = {
    productA: 'Product A quantity',
    productB: 'Product B quantity',
    printedTotal: 'Printed total',
};
function parseCartons(raw, field) {
    const value = raw.trim();
    let code;
    let reason = '';
    if (value === '') {
        code = 'missing';
        reason = 'is missing. Check the sample source before entering a value.';
    }
    else if (/^-\d+(?:\.\d+)?$/.test(value)) {
        code = 'negative';
        reason = 'cannot be negative.';
    }
    else if (/^\d+\.\d+$/.test(value)) {
        code = 'non_integer';
        reason = 'must be a whole carton count; decimal notation is not accepted.';
    }
    else if (!/^\d+$/.test(value)) {
        code = 'invalid';
        reason = 'must contain digits only (no units, signs, formulas or scientific notation).';
    }
    else if (!Number.isSafeInteger(Number(value)) || Number(value) > MAX_DEMO_CARTONS) {
        code = 'out_of_range';
        reason = `exceeds this demo’s limit of ${MAX_DEMO_CARTONS.toLocaleString('en-US')} cartons per field.`;
    }
    return code
        ? { value: null, issue: { field, code, message: `${fieldNames[field]} ${reason}` } }
        : { value: Number(value) };
}
/** Validate structured synthetic counts only; no extraction or document authenticity check. */
export function validateReceiving(input) {
    const a = parseCartons(input.productA, 'productA');
    const b = parseCartons(input.productB, 'productB');
    const total = parseCartons(input.printedTotal, 'printedTotal');
    const issues = [a.issue, b.issue, total.issue].filter((issue) => Boolean(issue));
    const lineTotal = a.value === null || b.value === null ? null : a.value + b.value;
    const discrepancy = lineTotal === null || total.value === null ? null : lineTotal - total.value;
    if (discrepancy !== null && discrepancy !== 0) {
        issues.push({ field: 'printedTotal', code: 'total_mismatch', message: `Line quantities total ${lineTotal} cartons, but the printed total is ${total.value}. A reviewer must resolve the ${Math.abs(discrepancy)}-carton difference.` });
    }
    return { status: issues.length === 0 ? 'pass' : 'review_required', lineTotal, printedTotal: total.value, discrepancy, issues };
}
/** Bind approval to the exact editable values, including formatting. */
export function receivingSnapshotKey(input) {
    return JSON.stringify([input.productA, input.productB, input.printedTotal]);
}
/** A local demo gate, not authentication or an auditable production approval. */
export function buildApprovedReceivingCsv(input, approvedSnapshot) {
    const result = validateReceiving(input);
    if (result.status !== 'pass' || approvedSnapshot !== receivingSnapshotKey(input))
        return null;
    // Only fixed synthetic labels and validated numeric values enter the export.
    return [
        'document_id,item,cartons,record_type',
        `DEMO-014,Product A,${Number(input.productA.trim())},synthetic_approved_sample`,
        `DEMO-014,Product B,${Number(input.productB.trim())},synthetic_approved_sample`,
        `DEMO-014,Printed total,${result.printedTotal},synthetic_approved_sample`,
        '',
    ].join('\r\n');
}

const cases = [
  {
    "id": "match",
    "name": "Totals match",
    "input": {
      "productA": "60",
      "productB": "40",
      "printedTotal": "100"
    },
    "expected": {
      "status": "pass",
      "lineTotal": 100,
      "printedTotal": 100,
      "discrepancy": 0,
      "issues": []
    }
  },
  {
    "id": "mismatch",
    "name": "Quantity mismatch",
    "input": {
      "productA": "60",
      "productB": "40",
      "printedTotal": "98"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": 100,
      "printedTotal": 98,
      "discrepancy": 2,
      "issues": [
        {
          "field": "printedTotal",
          "code": "total_mismatch"
        }
      ]
    }
  },
  {
    "id": "quantity",
    "name": "Missing quantity",
    "input": {
      "productA": "60",
      "productB": "",
      "printedTotal": "100"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": 100,
      "discrepancy": null,
      "issues": [
        {
          "field": "productB",
          "code": "missing"
        }
      ]
    }
  },
  {
    "id": "total",
    "name": "Missing total",
    "input": {
      "productA": "60",
      "productB": "40",
      "printedTotal": ""
    },
    "expected": {
      "status": "review_required",
      "lineTotal": 100,
      "printedTotal": null,
      "discrepancy": null,
      "issues": [
        {
          "field": "printedTotal",
          "code": "missing"
        }
      ]
    }
  },
  {
    "id": "negative",
    "name": "Negative carton quantity",
    "input": {
      "productA": "-5",
      "productB": "40",
      "printedTotal": "35"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": 35,
      "discrepancy": null,
      "issues": [
        {
          "field": "productA",
          "code": "negative"
        }
      ]
    }
  },
  {
    "id": "decimal",
    "name": "Fractional carton quantity",
    "input": {
      "productA": "60",
      "productB": "40.5",
      "printedTotal": "100"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": 100,
      "discrepancy": null,
      "issues": [
        {
          "field": "productB",
          "code": "non_integer"
        }
      ]
    }
  },
  {
    "id": "malformed",
    "name": "Quantity includes text",
    "input": {
      "productA": "60 cartons",
      "productB": "40",
      "printedTotal": "100"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": 100,
      "discrepancy": null,
      "issues": [
        {
          "field": "productA",
          "code": "invalid"
        }
      ]
    }
  },
  {
    "id": "whitespace",
    "name": "Surrounding whitespace",
    "input": {
      "productA": " 60 ",
      "productB": "40",
      "printedTotal": " 100 "
    },
    "expected": {
      "status": "pass",
      "lineTotal": 100,
      "printedTotal": 100,
      "discrepancy": 0,
      "issues": []
    }
  },
  {
    "id": "zero",
    "name": "Explicit zero is present",
    "input": {
      "productA": "0",
      "productB": "40",
      "printedTotal": "40"
    },
    "expected": {
      "status": "pass",
      "lineTotal": 40,
      "printedTotal": 40,
      "discrepancy": 0,
      "issues": []
    }
  },
  {
    "id": "exponent",
    "name": "Scientific notation blocked",
    "input": {
      "productA": "6e1",
      "productB": "40",
      "printedTotal": "100"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": 100,
      "discrepancy": null,
      "issues": [
        {
          "field": "productA",
          "code": "invalid"
        }
      ]
    }
  },
  {
    "id": "limit",
    "name": "Quantity exceeds demo limit",
    "input": {
      "productA": "1000001",
      "productB": "0",
      "printedTotal": "1000000"
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": 1000000,
      "discrepancy": null,
      "issues": [
        {
          "field": "productA",
          "code": "out_of_range"
        }
      ]
    }
  },
  {
    "id": "all-missing",
    "name": "All counts missing",
    "input": {
      "productA": "",
      "productB": " ",
      "printedTotal": ""
    },
    "expected": {
      "status": "review_required",
      "lineTotal": null,
      "printedTotal": null,
      "discrepancy": null,
      "issues": [
        {
          "field": "productA",
          "code": "missing"
        },
        {
          "field": "productB",
          "code": "missing"
        },
        {
          "field": "printedTotal",
          "code": "missing"
        }
      ]
    }
  }
];
const results = cases.map(fixture => {
  const result = validateReceiving(fixture.input);
  const actual = { ...result, issues: result.issues.map(({ field, code }) => ({ field, code })) };
  return { ...fixture, actual, passed: isDeepStrictEqual(actual, fixture.expected) };
});
const summary = {
  total: results.length,
  passed: results.filter(result => result.passed).length,
  failed: results.filter(result => !result.passed).length,
  expectedPassCases: results.filter(result => result.expected.status === 'pass').length,
  expectedReviewCases: results.filter(result => result.expected.status === 'review_required').length,
};
console.log(JSON.stringify({ dataset: {"id":"synthetic-receiving-cartons","version":"1.0.0","kind":"handwritten_synthetic_fixtures","caseCount":12,"description":"Two fixed synthetic product lines and a printed carton total. Expected statuses, totals, discrepancies and issue codes are handwritten independently of the validator."}, engineVersion: RECEIVING_ENGINE_VERSION, engineSourceSha256: '591e304e8a2f6157423415de27a89c54d490657ae3d76e698ef18161b807814a', evaluatedAt: new Date().toISOString(), summary, results, limitations: ["These 12 authored synthetic cases test a deterministic validation component, not AI extraction accuracy, real documents or customer outcomes.","A fixture pass means the actual status, totals, discrepancy and issue field/code pairs match its handwritten expectation. Review-required cases are expected successful test outcomes.","Coverage is limited to two whole-carton quantities and one printed total. Item identity, units, document authenticity, physical delivery, duplicates, taxes and purchase-order matching are not checked.","The local approval checkbox is a demonstration, not an authenticated identity, durable audit trail or production authorization control.","No model, upload, API, WMS/ERP integration, throughput benchmark, latency measurement, ROI estimate or production SLA is included.","The handwritten examples are not a representative or held-out customer data set; passing them does not establish general production reliability."] }, null, 2));
if (summary.failed > 0) process.exitCode = 1;
