field-format-rules.test.mjs 4.9 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('field-format-rules', async () => {
	const {
		validatePhoneFormat,
		validateVinSyncFormat,
		resolveVinInputMaxLength,
		isVinScanField,
		shouldCheckVinApi,
		VIN_MAX_LENGTH
	} = await import('../form-engine/field-format-rules.js')

	it('IT-PHONE-001: phone format matches createOrder phoneRex', () => {
		assert.equal(validatePhoneFormat('13800138000'), null)
		assert.equal(validatePhoneFormat('12800138000'), '请输入正确的手机号')
		assert.equal(validatePhoneFormat('1380013800'), '请输入正确的手机号')
		assert.equal(validatePhoneFormat(''), null)
	})

	it('IT-VIN-SYNC-001: vin sync format validation', () => {
		assert.equal(validateVinSyncFormat('LSGGF53W8CH045613'), null)
		assert.equal(
			validateVinSyncFormat('LSGGF53W8CH04561'),
			'请输入 17 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
		)
		assert.equal(validateVinSyncFormat('LSGGF53W8CH04561!'), '请输入正确的VIN码')
	})

	it('IT-VIN-SYNC-002: vin excludes I O Q', () => {
		assert.equal(
			validateVinSyncFormat('LSGGF53W8CH04561I'),
			'请输入 17 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
		)
		assert.equal(
			validateVinSyncFormat('LSGGF53W8CH04561O'),
			'请输入 17 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
		)
	})

	it('IT-VIN-SYNC-003: isVinScanField and shouldCheckVinApi', () => {
		assert.equal(isVinScanField({ prefixComponent: 'scan' }), true)
		assert.equal(isVinScanField({ type: 'input' }), false)
		assert.equal(shouldCheckVinApi('LSGGF53W8CH045613'), true)
		assert.equal(shouldCheckVinApi('SHORT'), false)
		assert.equal(shouldCheckVinApi('LSGGF53W8CH04561'), false)
	})

	it('IT-VIN-SYNC-004: vin must be exactly 17 chars while input max length stays configurable', () => {
		const twenty = 'A'.repeat(20)
		const twentyOne = 'A'.repeat(21)
		assert.equal(
			validateVinSyncFormat(twenty),
			'请输入 17 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
		)
		assert.match(validateVinSyncFormat(twentyOne), /不能超过20位/)
		assert.equal(
			resolveVinInputMaxLength({
				prefixComponent: 'scan',
				rules: { maxLength: 20 }
			}),
			20
		)
		assert.equal(resolveVinInputMaxLength({ prefixComponent: 'scan' }), VIN_MAX_LENGTH)
	})
})

describe('vin-api-valid-tracker', async () => {
	const {
		buildVinErrorKey,
		shouldRunVinApiValidate,
		runVinApiValidate
	} = await import('../form-engine/vin-api-valid-tracker.js')

	it('IT-VIN-API-001: buildVinErrorKey for subform row', () => {
		assert.equal(
			buildVinErrorKey({ groupKey: 'F3-1', rowIndex: 0, childKey: 'F3-1-7' }),
			'F3-1__0__F3-1-7'
		)
	})

	it('IT-VIN-API-002: runVinApiValidate parses API valid flag', async () => {
		assert.equal(shouldRunVinApiValidate('LSGGF53W8CH045613'), true)
		const ok = await runVinApiValidate('LSGGF53W8CH045613', async () => ({
			data: { data: { valid: true } }
		}))
		assert.equal(ok.ok, true)
		assert.equal(ok.valid, true)
		const fail = await runVinApiValidate('LSGGF53W8CH045613', async () => {
			throw new Error('fail')
		})
		assert.equal(fail.ok, false)
		assert.equal(fail.valid, false)
	})
})

describe('validateForm phone and template-driven VIN', async () => {
	const { validateForm } = await import('../form-engine/validator.js')
	const { PHONE_FORMAT_MSG } = await import(
		'../form-engine/field-format-rules.js'
	)

	it('IT-PHONE-002: validateForm phoneInput uses boutique message', () => {
		const phoneChild = {
			key: 'F3-1-5',
			type: 'phoneInput',
			label: '客户手机号',
			required: true,
			brandId: []
		}
		const list = [
			{
				key: 'G1',
				type: 'subform',
				label: '组',
				origin_children: [phoneChild],
				children: [[phoneChild]]
			}
		]
		const values = { G1: [{ 'F3-1-5': '12800138000' }] }
		const groupRowsCache = {
			G1: [
				[
					{
						key: 'F3-1-5',
						type: 'phoneInput',
						label: '客户手机号',
						required: true,
						value: '12800138000'
					}
				]
			]
		}
		const result = validateForm(list, {}, values, groupRowsCache)
		assert.equal(result.ok, false)
		assert.equal(result.errorMap['G1__0__F3-1-5'], PHONE_FORMAT_MSG)
	})

	it('IT-VIN-VALID-001: VIN API result does not override template validation', () => {
		const vinChild = {
			key: 'F3-1-7',
			type: 'input',
			label: '车架号',
			required: true,
			prefixComponent: 'scan',
			brandId: []
		}
		const list = [
			{
				key: 'G1',
				type: 'subform',
				label: '组',
				origin_children: [vinChild],
				children: [[vinChild]]
			}
		]
		const vin = 'SHORT'
		const values = { G1: [{ 'F3-1-7': vin }] }
		const groupRowsCache = {
			G1: [
				[
					{
						key: 'F3-1-7',
						type: 'input',
						label: '车架号',
						required: true,
						prefixComponent: 'scan',
						value: vin
					}
				]
			]
		}
		const result = validateForm(list, {}, values, groupRowsCache, {
			vinApiValidMap: { 'G1__0__F3-1-7': false }
		})
		assert.equal(result.ok, true, JSON.stringify(result.errorMap))
	})
})