vin-submit-flush.test.mjs 1.83 KB
import { register } from 'node:module'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

register('./shims/resolve-tilde-loader.mjs', import.meta.url)

describe('vin submit flush', async () => {
	it('IT-VIN-FLUSH-001: superseded debounced validateVin rejects cancelled without hanging', async () => {
		globalThis.wx = globalThis.wx || {
			showToast() {}
		}
		const { validateVin } = await import('~/utils/vinVaildate.js')
		const vin = 'LHGCV1650MH000001'
		const first = validateVin(vin).catch(err => err)
		await new Promise(resolve => setTimeout(resolve, 20))
		validateVin(vin).catch(() => {})
		const firstResult = await Promise.race([
			first,
			new Promise((_, reject) =>
				setTimeout(() => reject(new Error('first validateVin hung')), 2000)
			)
		])
		assert.equal(firstResult.cancelled, true)
	})

	it('IT-VIN-FLUSH-002: buildVinApiValidMapFromEntries merges API results', async () => {
		const {
			buildVinApiValidMapFromEntries,
			listVinScanCellsFromCache
		} = await import('../form-engine/vin-api-valid-tracker.js')
		const cache = {
			'F3-4': [
				[
					{
						key: 'F3-4-8',
						type: 'input',
						prefixComponent: 'scan',
						value: 'LHGCV1650MH000001'
					}
				]
			]
		}
		const entries = listVinScanCellsFromCache(cache)
		assert.equal(entries.length, 1)
		const map = await buildVinApiValidMapFromEntries(entries, async () => ({
			ok: true,
			valid: true
		}))
		assert.equal(map['F3-4__0__F3-4-8'], true)
	})

	it('IT-VIN-FLUSH-003: runVinApiValidateImmediate skips debounce', async () => {
		const { runVinApiValidateImmediate } = await import(
			'../form-engine/vin-api-valid-tracker.js'
		)
		const result = await runVinApiValidateImmediate(
			'SHORT',
			async () => ({ success: true }),
			{ silent: true }
		)
		assert.equal(result.ok, true)
		assert.equal(result.valid, true)
	})
})