car-brand-picker-bridge.test.mjs 3.52 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
import { readFileSync } from 'node:fs'

const require = createRequire(import.meta.url)
const bridge = require('../services/carBrandPickerBridge.js')

describe('carBrandPickerBridge', () => {
	it('IT-CAR-BRIDGE-001: completeCarPick invokes onResult', () => {
		bridge.clearPending()
		let result = null
		bridge.openCarBrandPick({
			merchantId: 10086,
			useMerchantBrandMock: true,
			onResult(carInfo) {
				result = carInfo
			}
		})
		assert.equal(bridge.getPending()?.merchantId, 10086)
		const ok = bridge.completeCarPick({
			vehicleBrandId: 1,
			vehicleBrandName: '问界',
			vehicleSubBrandId: 2,
			vehicleSubBrandName: 'AITO',
			vehicleSeriesId: 3,
			vehicleSeriesName: 'M7',
			vehicleShapeId: 4,
			vehicleShapeName: 'Max'
		})
		assert.equal(ok, true)
		assert.equal(result.vehicleShapeId, 4)
		assert.equal(result.displayText, '问界/AITO/M7/Max')
		assert.equal(bridge.getPending(), null)
	})

	it('IT-CAR-BRIDGE-003: carries normalized current three-level selection to series navigation', () => {
		const manualThreeLevels = {
			vehicleBrandId: 1,
			vehicleBrandName: '鸿蒙智行',
			vehicleSubBrandId: 2,
			vehicleSubBrandName: 'AITO 问界',
			vehicleSeriesId: 3,
			vehicleSeriesName: '问界M5'
		}
		bridge.clearPending()
		bridge.openCarBrandPick({
			merchantId: 10086,
			currentValue: manualThreeLevels,
			onResult() {}
		})
		assert.equal(bridge.getPending().currentValue.vehicleSeriesId, 3)
		const nav = bridge.getSeriesNavParams({}, [], '', bridge.getPending().currentValue)
		assert.equal(nav.currentValue.vehicleSeriesName, '问界M5')
	})

	it('IT-CAR-BRIDGE-004: snapshots JSON and cascade current values with zero IDs', () => {
		const jsonValue = JSON.stringify({
			vehicleBrandId: 0,
			vehicleBrandName: '零号品牌',
			vehicleSubBrandId: 0,
			vehicleSubBrandName: '零号子品牌',
			vehicleSeriesId: 0,
			vehicleSeriesName: '零号车系'
		})
		bridge.clearPending()
		bridge.openCarBrandPick({ merchantId: 1, currentValue: jsonValue, onResult() {} })
		assert.equal(bridge.getPending().currentValue.vehicleSeriesId, 0)
		assert.equal(bridge.getPending().currentValue.displayText, '零号品牌/零号子品牌/零号车系')

		const cascade = [
			{ id: '1', name: '岚图' },
			{ id: '2', name: '岚图汽车' },
			{ id: '3', name: 'FREE' }
		]
		bridge.openCarBrandPick({ merchantId: 2, currentValue: cascade, onResult() {} })
		const nav = bridge.getSeriesNavParams({}, [], '', cascade)
		cascade[2].name = '外部篡改'
		assert.equal(bridge.getPending().merchantId, 2)
		assert.equal(bridge.getPending().currentValue.vehicleSeriesName, 'FREE')
		assert.equal(nav.currentValue.displayText, '岚图/岚图汽车/FREE')
	})

	it('IT-CAR-BRIDGE-005: negotiation page declares currentValue outside the engine branch', () => {
		const source = readFileSync(
			new URL('../pages/negotiationDetail/negotiationDetail.js', import.meta.url),
			'utf8'
		)
		const start = source.indexOf('openCarBrandForField({ childKey, groupKey, rowIndex } = {})')
		const end = source.indexOf('\n\t/** 品牌选择回调', start)
		const method = source.slice(start, end)
		assert.match(method, /if \(this\.data\.formDisabled \|\| !childKey\) return\s*\n\t\tlet currentValue\s*\n\t\tif \(this\.engine\)/)
		assert.match(method, /useMerchantBrandMock: false,\s*\n\t\t\tcurrentValue,/)
	})

	it('IT-CAR-BRIDGE-002: route uses packageDynamicForm prefix', () => {
		assert.match(bridge.PICK_ROUTE, /^packageDynamicForm\./)
	})
})