store-template-schema.test.mjs 5.58 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { loadSchemaFixtureV211 } from './schemaFixture.mjs'

const storeSchema = loadSchemaFixtureV211('store')

function findFieldByKey(list, key) {
	return list.find(item => item.key === key)
}

/** 递归按 key 查找字段(含 subform / group 子项) */
function findFieldByKeyDeep(list, key) {
	for (const field of list || []) {
		if (field?.key === key) return field
		const children = field?.children
		if (!Array.isArray(children)) continue
		for (const child of children) {
			if (Array.isArray(child)) {
				const found = findFieldByKeyDeep(child, key)
				if (found) return found
			} else if (child?.key === key) {
				return child
			}
		}
	}
	return null
}

function collectAllKeys(list = []) {
	const keys = []
	function walk(field) {
		if (!field?.key) return
		keys.push(field.key)
		const children = field.children
		if (!Array.isArray(children)) return
		for (const child of children) {
			if (Array.isArray(child)) {
				child.forEach(walk)
			} else {
				walk(child)
			}
		}
	}
	list.forEach(walk)
	return keys
}

describe('store daily template V2.1.1 schema', () => {
	it('IT-STORE-TPL-001: fixture metadata and F2-1/F2-2 staff count wiring', () => {
		assert.equal(storeSchema.version, '2.1.1')
		assert.equal(storeSchema.formCode, 'store_daily_report')
		assert.equal(storeSchema.list.length, 55)

		const f21 = findFieldByKey(storeSchema.list, 'F2-1')
		assert.equal(f21?.type, 'multipleSelect')
		assert.equal(f21?.optionsKey, 'storeStaffList')
		assert.deepEqual(f21?.completeFun, {
			key: 'F2-2',
			result: '(list.length)'
		})

		const f22 = findFieldByKey(storeSchema.list, 'F2-2')
		assert.equal(f22?.type, 'numberInput')
		assert.equal(f22?.value, 0)
		assert.deepEqual(f22?.disabledFun, {
			keys: ['F2-1'],
			result: 'val1 != null'
		})
	})

	it('IT-STORE-TPL-002: divider fields F2-91/F2-92/F2-94', () => {
		const f291 = findFieldByKey(storeSchema.list, 'F2-91')
		assert.equal(f291?.type, 'divider')
		assert.equal(f291?.isYB, true)
		assert.deepEqual(f291?.ybCode, [110201, 110202])
		assert.deepEqual(f291?.qyCode, [110101, 110102])

		const f292 = findFieldByKey(storeSchema.list, 'F2-92')
		assert.equal(f292?.isQY, true)

		const f294 = findFieldByKey(storeSchema.list, 'F2-94')
		assert.equal(f294?.label, '延保/权益')
	})

	it('IT-STORE-TPL-003: F2-11 other-brand subform has filterBrandId', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-11')
		assert.equal(subform?.type, 'subform')
		assert.deepEqual(subform?.filterBrandId, [
			3454, 3503, 3458, 3574, 3502, 3067
		])
		assert.deepEqual(subform?.baseSceneCode, [
			110101, 110102, 110201, 110202
		])

		const orderChild = subform?.children?.[0]?.find(item => item.key === 'F2-11-1')
		assert.equal(orderChild?.optionsKey, '4sStaffList')
	})

	it('IT-STORE-TPL-004: removed F2-30/F2-36 not in schema', () => {
		const keys = collectAllKeys(storeSchema.list)
		assert.ok(!keys.includes('F2-30'))
		assert.ok(!keys.includes('F2-36'))
	})

	it('IT-STORE-TPL-005: brand-split metrics F2-38/F2-44 exist', () => {
		const f238 = findFieldByKey(storeSchema.list, 'F2-38')
		assert.equal(f238?.label, '问界店总交付数')
		assert.deepEqual(f238?.brandId, [3454])
		assert.deepEqual(f238?.baseSceneCode, [110201, 110202])

		const f244 = findFieldByKey(storeSchema.list, 'F2-44')
		assert.equal(f244?.label, '其他品牌店总交付数')
		assert.deepEqual(f244?.baseSceneCode, [
			110101, 110102, 110201, 110202
		])
		assert.deepEqual(f244?.filterBrandId, [
			3454, 3503, 3458, 3574, 3502, 3067
		])
	})

	it('IT-STORE-TPL-006: F2-14 other-brand after-sales subform equity children', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-14')
		assert.equal(subform?.type, 'subform')
		const row = subform?.children?.[0] || []
		for (const key of ['F2-14-5', 'F2-14-6', 'F2-14-8']) {
			const child = row.find(item => item.key === key)
			assert.deepEqual(child?.baseSceneCode, [110101, 110102], key)
		}
	})

	it('IT-STORE-TPL-007: F2-14-12 manufacturer warranty referral metric', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-14')
		const row = subform?.children?.[0] || []
		const child = row.find(item => item.key === 'F2-14-12')
		assert.equal(child?.label, '今日厂家权益有效转介绍')
		assert.deepEqual(child?.baseSceneCode, [110201, 110202])
		assert.deepEqual(subform?.childrenRules?.['F2-14-12'], {
			min: 0,
			integer: true
		})
	})

	it('IT-STORE-TPL-008: F2-57/F2-58 lantu-or-other-brand use empty brandId', () => {
		const fiveRealmFilter = [3454, 3503, 3458, 3574, 3502]
		for (const key of ['F2-57', 'F2-58']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.deepEqual(field?.brandId, [], key)
			assert.deepEqual(field?.filterBrandId, fiveRealmFilter, key)
		}
	})

	it('IT-STORE-TPL-009: F2-37 lantu-or-other-brand boutique metric', () => {
		const field = findFieldByKey(storeSchema.list, 'F2-37')
		assert.equal(field?.label, '其他品牌店总交付数')
		assert.deepEqual(field?.brandId, [])
		assert.deepEqual(field?.filterBrandId, [3454, 3503, 3458, 3574, 3502])
		assert.deepEqual(field?.baseSceneCode, [100101, 100102, 100103])
	})

	it('IT-STORE-TPL-010: Excel 后置显示文字元/% fields have unit', () => {
		const yuanKeys = [
			'F2-11-12',
			'F2-14-11',
			'F2-26-4',
			'F2-27-6',
			'F2-28-6',
			'F2-57-6',
			'F2-58-4'
		]
		for (const key of yuanKeys) {
			const field = findFieldByKeyDeep(storeSchema.list, key)
			assert.equal(field?.unit, '元', key)
		}
		for (const key of ['F2-34', 'F2-35']) {
			const field = findFieldByKeyDeep(storeSchema.list, key)
			assert.equal(field?.unit, '%', key)
		}
	})
})