store-v230-template-schema.test.mjs 10.3 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const here = path.dirname(fileURLToPath(import.meta.url))
const fixturesDir = path.join(here, '..', 'fixtures')

function loadJson(name) {
	return JSON.parse(fs.readFileSync(path.join(fixturesDir, name), 'utf8'))
}

function loadBareArray(name) {
	const source = fs.readFileSync(path.join(fixturesDir, name), 'utf8')
	return Function(`return (\n${source}\n)`)()
}

function collectFields(list = []) {
	const result = []
	function walk(field) {
		result.push(field)
		for (const child of field.children || []) {
			if (Array.isArray(child)) child.forEach(walk)
			else walk(child)
		}
	}
	list.forEach(walk)
	return result
}

function findDeep(list, key) {
	return collectFields(list).find(field => field.key === key)
}

function withoutBrandIds(value) {
	if (Array.isArray(value)) return value.map(withoutBrandIds)
	if (!value || typeof value !== 'object') return value
	return Object.fromEntries(
		Object.entries(value)
			.filter(([key]) => key !== 'brandId' && key !== 'filterBrandId')
			.map(([key, item]) => [key, withoutBrandIds(item)])
	)
}

const daily = loadJson('schema-store-daily-v2.3.0.json')
const production = loadJson('schema-store-daily-v2.3.0.prod.json')
const dailyJsSource = fs.readFileSync(path.join(fixturesDir, 'schema-store-daily-v2.3.0.js'), 'utf8')

describe('store daily template V2.3.0 schema', () => {
	it('IT-V230-001: metadata and exact F2-101 onward shape', () => {
		assert.equal(daily.version, '2.3.0')
		assert.equal(daily.formCode, 'store_daily_report')
		assert.equal(daily.title, '门店日报 V2.3.0')
		assert.deepEqual(
			daily.list.map(field => field.key),
			['F2-101', 'F2-102', 'F2-103', 'F2-104', 'F2-105', 'F2-106', 'F2-107']
		)
		assert.equal(collectFields(daily.list).length, 25)
		assert.equal(findDeep(daily.list, 'F2-100'), undefined)
		assert.equal(findDeep(daily.list, 'F2-107')?.children?.[0]?.length, 18)
	})

	it('IT-V230-001b: daily and production JS/JSON declare duplicate customer phone validation', () => {
		const schemas = [
			daily.list,
			production.list,
			loadBareArray('schema-store-daily-v2.3.0.js'),
			loadBareArray('schema-store-daily-v2.3.0.prod.js')
		]
		for (const list of schemas) {
			const group = findDeep(list, 'F2-107')
			assert.deepEqual(group?.validateDuplicates, ['F2-107-2'])
			assert.ok(group?.children?.[0]?.some(child => child.key === 'F2-107-2'))
		}
	})

	it('IT-V230-002: selector, phone, date, car and VIN mappings', () => {
		assert.equal(findDeep(daily.list, 'F2-101')?.type, 'multipleSelect')
		assert.equal(findDeep(daily.list, 'F2-101')?.optionsKey, 'storeStaffList')
		assert.equal(findDeep(daily.list, 'F2-107-2')?.type, 'phoneInput')
		assert.deepEqual(findDeep(daily.list, 'F2-107-4')?.rules, {
			maxDate: 'today',
			format: 'date'
		})
		assert.deepEqual(findDeep(daily.list, 'F2-107-4')?.baseSceneCode, [
			100101, 100102, 100103, 110101, 110102, 110201, 110202
		])
		assert.equal(findDeep(daily.list, 'F2-107-8')?.type, 'carBrandSelect')
		assert.equal(findDeep(daily.list, 'F2-107-8')?.rules?.level, 4)
		assert.equal(findDeep(daily.list, 'F2-107-9')?.prefixComponent, 'scan')
		assert.equal(findDeep(daily.list, 'F2-107-9')?.required, false)
		assert.equal(findDeep(daily.list, 'F2-107-9')?.rules?.maxLength, 20)
	})

	it('IT-V230-002b: all latest fixtures keep VIN optional with template maxLength only', () => {
		const schemas = [
			daily.list,
			production.list,
			loadBareArray('schema-store-daily-v2.3.0.js'),
			loadBareArray('schema-store-daily-v2.3.0.prod.js')
		]
		for (const list of schemas) {
			const vin = findDeep(list, 'F2-107-9')
			assert.equal(vin?.prefixComponent, 'scan')
			assert.equal(vin?.required, false)
			assert.deepEqual(vin?.rules, { maxLength: 20 })
		}
	})

	it('IT-V230-003: unsupported source Rule metadata is not emitted', () => {
		const monthly = findDeep(daily.list, 'F2-102')
		assert.equal(Object.hasOwn(monthly, 'sourceDefaultRule'), false)
		assert.equal(Object.hasOwn(monthly, 'value'), false)

		const owner = findDeep(daily.list, 'F2-107-6')
		assert.equal(owner?.type, 'select')
		assert.equal(owner?.optionsKey, 'storeStaffList')
		assert.equal(Object.hasOwn(owner, 'sourceDefaultRule'), false)

		const tags = findDeep(daily.list, 'F2-107-7')
		assert.equal(tags?.type, 'multipleSelect')
		assert.ok(tags?.options?.every(option => !Object.hasOwn(option, 'sourceRule')))
		assert.equal(findDeep(daily.list, 'F2-107-12')?.optionsKey, 'options')
	})

	it('IT-V230-003b: customer tags carry declarative environment-aware option visibility', () => {
		const dailyTags = findDeep(daily.list, 'F2-107-7')
		const productionTags = findDeep(production.list, 'F2-107-7')
		const findOption = (field, label) => field.options.find(option => option.label === label)
		const lightScenes = [100101, 100102, 100103]
		const dailyBrands = [3454, 3503, 3458, 3574, 3502]
		const productionBrands = [2695, 2741, 2698, 2797, 2740]

		assert.equal(dailyTags?.optionsKey, 'options')
		assert.equal(productionTags?.optionsKey, 'options')
		assert.deepEqual(findOption(dailyTags, '已触客'), {
			label: '已触客',
			value: '1'
		})
		for (const label of ['已拉群', '首保维保客户', '事故车客户']) {
			const tagIds = {
				'已拉群': '2',
				'首保维保客户': '4',
				'事故车客户': '5'
			}
			assert.deepEqual(findOption(dailyTags, label), {
				label,
				value: tagIds[label],
				baseSceneCode: lightScenes,
				brandId: dailyBrands
			})
			assert.deepEqual(findOption(productionTags, label)?.brandId, productionBrands)
		}
		assert.deepEqual(findOption(dailyTags, '已交付引荐'), {
			label: '已交付引荐',
			value: '3',
			baseSceneCode: lightScenes,
			filterBrandId: dailyBrands,
			matchEffect: 'exclude'
		})
		assert.deepEqual(
			findOption(productionTags, '已交付引荐')?.filterBrandId,
			productionBrands
		)
		assert.equal(findOption(productionTags, '已交付引荐')?.matchEffect, 'exclude')
		assert.equal(Object.hasOwn(dailyTags, 'fiveRealmBrandIds'), false)
		assert.equal(Object.hasOwn(dailyTags, 'optionShowRules'), false)
	})

	it('IT-V230-003c: customer source uses numeric values without ids', () => {
		const boutiqueScenes = [100101, 100102, 100103]
		const warrantyAndEquityScenes = [110101, 110102, 110201, 110202]
		const expected = [
			{
				label: '销售端引流-售前',
				value: '0',
				baseSceneCode: boutiqueScenes
			},
			{
				label: '交付端引流-售前',
				value: '3',
				baseSceneCode: boutiqueScenes
			},
			{
				label: '门店引流-售前',
				value: '0',
				baseSceneCode: warrantyAndEquityScenes
			},
			{
				label: '门店引流-售后',
				value: '1'
			}
		]
		assert.deepEqual(findDeep(daily.list, 'F2-107-3')?.options, expected)
		assert.deepEqual(findDeep(production.list, 'F2-107-3')?.options, expected)
		assert.ok(!expected.some(option => Object.hasOwn(option, 'id')))
		assert.ok(!expected.some(option => option.value === '2'))
	})

	it('IT-V230-004: subform childrenShowRules retain every Excel dependency', () => {
		const subform = findDeep(daily.list, 'F2-107')
		const rules = subform?.childrenShowRules
		assert.deepEqual(Object.keys(rules), [
			'F2-107-4',
			'F2-107-12',
			'F2-107-13',
			'F2-107-14',
			'F2-107-15',
			'F2-107-16',
			'F2-107-17',
			'F2-107-18'
		])
		assert.deepEqual(rules['F2-107-14']?.keys, ['F2-107-13'])
		assert.deepEqual(
			rules['F2-107-4']?.keys,
			['$sceneCodes', '$brandIds', 'F2-107-3']
		)
		for (const sceneCode of [100101, 100102, 100103, 110101, 110102, 110201, 110202]) {
			assert.match(rules['F2-107-4']?.result, new RegExp(`val1\\.includes\\('${sceneCode}'\\)`))
		}
		for (const brandId of [3454, 3503, 3458, 3574, 3502]) {
			assert.match(rules['F2-107-4']?.result, new RegExp(`val2\\.includes\\('${brandId}'\\)`))
		}
		for (const brandId of [2695, 2741, 2698, 2797, 2740]) {
			assert.match(
				findDeep(production.list, 'F2-107')?.childrenShowRules?.['F2-107-4']?.result,
				new RegExp(`val2\\.includes\\('${brandId}'\\)`)
			)
			assert.doesNotMatch(rules['F2-107-4']?.result, new RegExp(`val2\\.includes\\('${brandId}'\\)`))
		}
		assert.match(rules['F2-107-12']?.result, /includes\('3'\)/)
		assert.deepEqual(
			Object.fromEntries(
				['F2-107-12', 'F2-107-13', 'F2-107-15', 'F2-107-17', 'F2-107-18']
					.map(key => [key, rules[key]?.result])
			),
			{
				'F2-107-12': "val1 && Array.isArray(val1) && (val1.includes('1') || val1.includes('3'))",
				'F2-107-13': "val1 && Array.isArray(val1) && val1.includes('3')",
				'F2-107-15': "val1 && Array.isArray(val1) && val1.includes('1')",
				'F2-107-17': "!Array.isArray(val1) || (val1 && !val1.includes('1'))",
				'F2-107-18': "val1 && Array.isArray(val1) && val1.includes('1')"
			}
		)
		assert.deepEqual(subform?.childrenRules?.['F2-107-4'], {
			maxDate: 'new Date()',
			format: 'date'
		})
	})

	it('IT-V230-005: daily and production differ only by brand IDs and generated brand expressions', () => {
		const withoutEnvRule = value => {
			const copy = structuredClone(value)
			const subform = findDeep(copy.list, 'F2-107')
			if (subform?.childrenShowRules?.['F2-107-4']) {
				subform.childrenShowRules['F2-107-4'].result = '<environment-specific>'
			}
			return withoutBrandIds(copy)
		}
		assert.deepEqual(withoutEnvRule(production), withoutEnvRule(daily))
		assert.deepEqual(findDeep(daily.list, 'F2-103')?.brandId, [3454, 3503, 3458, 3574, 3502, 3067])
		assert.deepEqual(findDeep(production.list, 'F2-103')?.brandId, [2695, 2741, 2698, 2797, 2740, 2188])
		assert.deepEqual(findDeep(daily.list, 'F2-102')?.filterBrandId, [3454, 3503, 3458, 3574, 3502])
		assert.deepEqual(findDeep(production.list, 'F2-102')?.filterBrandId, [2695, 2741, 2698, 2797, 2740])
	})

	it('IT-V230-006: both config-center JS fixtures are parseable and match JSON lists', () => {
		assert.deepEqual(loadBareArray('schema-store-daily-v2.3.0.js'), daily.list)
		assert.deepEqual(loadBareArray('schema-store-daily-v2.3.0.prod.js'), production.list)
	})

	it('IT-V230-007: child visibility expressions use legacy-compatible double-quoted JS strings', () => {
		const resultLines = dailyJsSource
			.split(/\r?\n/)
			.filter(line => line.trimStart().startsWith('result:'))
		assert.equal(resultLines.length, 8)
		assert.ok(resultLines.every(line => /^\s*result: "/.test(line)))
		assert.ok(resultLines.every(line => !line.includes("\\'")))
	})
})