import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { confirmedNetSummary, normalizedGrossKg, selectWeightPallet, workspaceWeightMatchesBaseline, } from './inbound-weight-ocr.mjs' test('selects a stable complete pallet snapshot without status or availability', () => { const selected = selectWeightPallet([ { palletId: '20', palletCode: 'P-20', palletType: 'STANDARD', capacityCbm: 2, tareWeightKg: 18.5 }, { palletId: '3', palletCode: 'P-03', palletType: 'STANDARD', capacityCbm: 1.5, tareWeightKg: 12 }, ]) assert.equal(selected.palletId, '3') assert.equal(selectWeightPallet([]), null) assert.equal(selectWeightPallet([ { palletId: '3', palletCode: 'P-03', palletType: 'STANDARD', capacityCbm: 1.5 }, ]), null) }) test('normalizes gross weight and summarizes confirmed net weight only', () => { assert.equal(normalizedGrossKg(50.125, 'LB'), 22.736) assert.equal(normalizedGrossKg(30, 'KG'), 30) assert.equal(normalizedGrossKg(30, 'UNKNOWN'), null) assert.deepEqual(confirmedNetSummary({ weightResults: [ { recognitionStatus: 'CONFIRMED', netWeightKg: 11.5 }, { recognitionStatus: 'PENDING_CONFIRM', netWeightKg: 99 }, { recognitionStatus: 'CONFIRMED', netWeightKg: 2.125 }, ] }), { count: 2, complete: true, total: 13.625 }) }) test('cleanup restores a non-empty carton fallback when there were no confirmed images', () => { const initial = { actualGrossWeightKg: 54.4, weightResults: [] } assert.deepEqual(confirmedNetSummary(initial), { count: 0, complete: true, total: 0 }) assert.equal(workspaceWeightMatchesBaseline({ actualGrossWeightKg: 54.4 }, 54.4), true) assert.equal(workspaceWeightMatchesBaseline({ actualGrossWeightKg: null }, 54.4), false) assert.equal(workspaceWeightMatchesBaseline({ actualGrossWeightKg: null }, null), true) }) test('HTTP scenario uses pallet option and net weight contracts without legacy final weight', () => { const source = readFileSync(new URL('inbound-weight-ocr.mjs', import.meta.url), 'utf8') assert.match(source, /\/weight-pallet-options\?tenantId=/) assert.ok(source.indexOf('const palletOptionsResponse') < source.lastIndexOf('const uploadPath')) assert.match(source, /form\.append\('file'/) assert.doesNotMatch(source, /form\.append\('palletId'/) assert.match(source, /palletId:\s*id\(pallet\.palletId\)/) assert.match(source, /grossWeightKg/) assert.match(source, /netWeightKg/) assert.match(source, /palletTareWeightKg/) assert.doesNotMatch(source, /finalWeightKg/) assert.doesNotMatch(source, /pallet\.(?:status|available)/) assert.doesNotMatch(source, /platform:pallet:list/) assert.match(source, /verifyDeleted\(context, target, original, baselineWorkspaceWeight\)/) assert.match(source, /workspaceWeightMatchesBaseline\(cleanup\.workspace, baselineWorkspaceWeight\)/) assert.doesNotMatch(source, /verifyDeleted\([^\n]+baselineConfirmedNet/) })