vin-ocr-bridge.test.mjs 3.9 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const {
	openVinOcr,
	handleReWrite,
	normalizeVin,
	clearPending,
	getPending,
	OCR_ROUTE
} = require('../services/vinOcrBridge.js')
const {
	resolveVinFieldContext,
	inferGroupKeyFromChildKey
} = require('../services/vin-ocr-context.js')

describe('vinOcrBridge', () => {
	it('IT-VIN-OCR-001: normalizeVin trims and uppercases', () => {
		assert.equal(normalizeVin('  lsggf53w8ch045613  '), 'LSGGF53W8CH045613')
	})

	it('IT-VIN-OCR-002: handleReWrite invokes onResult and clears pending', () => {
		clearPending()
		let result = null
		openVinOcr({
			childKey: 'F3-1-7',
			groupKey: 'F3-1',
			rowIndex: 0,
			onResult(vin) {
				result = vin
			}
		})
		assert.ok(getPending())
		const handled = handleReWrite('standNo', 'lsggf53w8ch045613')
		assert.equal(handled, true)
		assert.equal(result, 'LSGGF53W8CH045613')
		assert.equal(getPending(), null)
	})

	it('IT-VIN-OCR-003: handleReWrite returns false without pending', () => {
		clearPending()
		assert.equal(handleReWrite('standNo', 'ABC'), false)
	})

	it('IT-VIN-OCR-004: handleReWrite ignores non-standNo type', () => {
		clearPending()
		openVinOcr({
			childKey: 'x',
			onResult() {}
		})
		assert.equal(handleReWrite('plateNumber', '粤A12345'), false)
		assert.ok(getPending())
		clearPending()
	})

	it('IT-VIN-OCR-005: OCR_ROUTE uses cross-subpackage path', () => {
		assert.match(OCR_ROUTE, /^tabWorkOrder\.ocr/)
	})
})

describe('vin OCR group field write', async () => {
	const { createRequire } = await import('node:module')
	const require = createRequire(import.meta.url)
	const { loadSchemaFixture } = await import('./schemaFixture.mjs')
	const negotiationSchema = loadSchemaFixture('negotiation')
	const { setRowFieldInCache, syncValuesFromGroupCache } = await import(
		'../form-engine/group-rows-cache.js'
	)
	const { cloneRowTemplate } = await import(
		'../form-engine/group-row-adapter.js'
	)
	const group = negotiationSchema.list.find(item => item.key === 'F3-1')
	const template = cloneRowTemplate(group)

	it('IT-VIN-OCR-006: seeded row syncs VIN into values[groupKey] array', () => {
		const vin = 'LSGGF53W8CH045613'
		const cache = setRowFieldInCache(
			{ 'F3-1': [[]] },
			'F3-1',
			0,
			'F3-1-7',
			vin,
			template
		)
		const values = syncValuesFromGroupCache({}, 'F3-1', cache['F3-1'])
		const vinField = (cache['F3-1']?.[0] || []).find(f => f.key === 'F3-1-7')
		assert.equal(vinField?.value, vin)
		assert.equal(values['F3-1']?.[0]?.['F3-1-7'], vin)
	})

	it('IT-VIN-OCR-007: setRowFieldInCache seeds empty row from template', () => {
		const next = setRowFieldInCache(
			{ 'F3-1': [[]] },
			'F3-1',
			0,
			'F3-1-7',
			'ABC123',
			template
		)
		const vinField = (next['F3-1']?.[0] || []).find(f => f.key === 'F3-1-7')
		assert.equal(vinField?.value, 'ABC123')
	})
})

describe('resolveVinFieldContext', () => {
	it('IT-VIN-OCR-008: infers groupKey from childKey F3-1-7', () => {
		assert.equal(inferGroupKeyFromChildKey('F3-1-7'), 'F3-1')
	})

	it('IT-VIN-OCR-009: undefined rowIndex defaults to active index 0', () => {
		const resolved = resolveVinFieldContext(
			{ childKey: 'F3-1-7', groupKey: 'F3-1', rowIndex: undefined },
			{ 'F3-1': 0 }
		)
		assert.equal(resolved.isGroupField, true)
		assert.equal(resolved.groupKey, 'F3-1')
		assert.equal(resolved.rowIndex, 0)
	})

	it('IT-VIN-OCR-010: missing groupKey and rowIndex inferred for group child', () => {
		const resolved = resolveVinFieldContext(
			{ childKey: 'F3-1-7', rowIndex: undefined },
			{ 'F3-1': 1 }
		)
		assert.equal(resolved.groupKey, 'F3-1')
		assert.equal(resolved.rowIndex, 1)
	})

	it('IT-VIN-OCR-011: negative rowIndex falls back to groupActiveIndex', () => {
		const resolved = resolveVinFieldContext(
			{ childKey: 'F3-2-7', groupKey: 'F3-2', rowIndex: -1 },
			{ 'F3-2': 2 }
		)
		assert.equal(resolved.rowIndex, 2)
	})
})