vin-template-only-validation.test.mjs 3.84 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'

function validateFixedField(validateForm, field, value, options = {}) {
	return validateForm(
		[field],
		{},
		{ [field.key]: value },
		{},
		options
	)
}

function loadPageWithoutVinValidationService(relativePath) {
	const source = readFileSync(new URL(relativePath, import.meta.url), 'utf8').replace(
		/import\s*{([\s\S]*?)}\s*from\s*'~\/utils\/vinVaildate\.js'/,
		"const {$1} = require('~/utils/vinVaildate.js')"
	)
	let definition
	const fallbackModule = new Proxy(
		{},
		{
			get: () => () => ({})
		}
	)
	const requireForPage = specifier => {
		if (specifier === '~/utils/vinVaildate.js') {
			throw new Error('daily report pages must not load the VIN validation service')
		}
		return fallbackModule
	}
	new Function('require', 'Page', 'wx', source)(
		requireForPage,
		config => {
			definition = config
		},
		{}
	)
	return definition
}

function loadInputComponentWithoutVinValidationService() {
	const source = readFileSync(
		new URL('../components/form-field-input/form-field-input.js', import.meta.url),
		'utf8'
	)
	let definition
	const requireForComponent = specifier => {
		if (specifier === '~/utils/vinVaildate.js') {
			throw new Error('input component must not load the VIN validation service')
		}
		return {}
	}
	new Function('require', 'Component', 'wx', source)(
		requireForComponent,
		config => {
			definition = config
		},
		{}
	)
	return definition
}

describe('VIN template-only validation', async () => {
	const { validateForm } = await import('../form-engine/validator.js')

	it('accepts an optional store VIN when it satisfies the template maxLength rule', () => {
		const field = {
			key: 'F2-107-9',
			type: 'input',
			label: '客户车架号',
			required: false,
			prefixComponent: 'scan',
			rules: { maxLength: 20 }
		}
		const result = validateFixedField(validateForm, field, 'SHORT', {
			vinApiValidMap: { 'F2-107-9': false }
		})

		assert.equal(result.ok, true, JSON.stringify(result.errorMap))
	})

	it('rejects a store VIN longer than the template maxLength', () => {
		const field = {
			key: 'F2-107-9',
			type: 'input',
			label: '客户车架号',
			required: false,
			prefixComponent: 'scan',
			rules: { maxLength: 20 }
		}
		const result = validateFixedField(validateForm, field, 'A'.repeat(21))

		assert.equal(result.ok, false)
		assert.equal(result.errorMap['F2-107-9'], '客户车架号不能超过20字')
	})

	it('uses only the negotiation template required rule for VIN', () => {
		const field = {
			key: 'F3-1-7',
			type: 'input',
			label: '车架号',
			required: true,
			prefixComponent: 'scan',
			rules: {}
		}

		const empty = validateFixedField(validateForm, field, '')
		const filled = validateFixedField(validateForm, field, 'SHORT', {
			vinApiValidMap: { 'F3-1-7': false }
		})

		assert.equal(empty.ok, false)
		assert.equal(empty.errorMap['F3-1-7'], '车架号不能为空')
		assert.equal(filled.ok, true, JSON.stringify(filled.errorMap))
	})

	it('loads both daily report pages without the VIN validation service', () => {
		assert.ok(loadPageWithoutVinValidationService('../pages/storeDailyReport/storeDailyReport.js'))
		assert.ok(loadPageWithoutVinValidationService('../pages/negotiationDetail/negotiationDetail.js'))
	})

	it('keeps scan input length driven by the same template rule as a normal input', () => {
		const component = loadInputComponentWithoutVinValidationService()
		const context = {
			data: structuredClone(component.data),
			setData(patch) {
				Object.assign(this.data, patch)
			}
		}

		component.methods.observeField.call(context, {
			prefixComponent: 'scan',
			rules: {}
		})
		assert.equal(context.data.maxLength, 140)

		component.methods.observeField.call(context, {
			prefixComponent: 'scan',
			rules: { maxLength: 20 }
		})
		assert.equal(context.data.maxLength, 20)
	})
})