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

const staffSchema = loadSchemaFixtureV211('staff')

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

function findChildByKey(group, key) {
	return (group?.children || []).find(child => child.key === key)
}

/** 递归收集 schema 内全部 key */
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('staff daily template V2.1.1 schema', () => {
	it('IT-STAFF-TPL-001: fixture metadata and F1-1 attendance radio', () => {
		assert.equal(staffSchema.version, '2.1.1')
		assert.equal(staffSchema.formCode, 'staff_daily_report')
		assert.equal(staffSchema.list.length, 13)

		const f11 = findFieldByKey(staffSchema.list, 'F1-1')
		assert.equal(f11?.type, 'radio')
		assert.equal(f11?.value, '在岗')
		assert.deepEqual(
			f11?.options?.map(o => o.value),
			['在岗', '调休']
		)
	})

	it('IT-STAFF-TPL-002: F1-2/F1-3 on-duty show rules', () => {
		for (const key of ['F1-2', 'F1-3']) {
			const field = findFieldByKey(staffSchema.list, key)
			assert.equal(field?.rules?.keys?.[0], 'F1-1', key)
			assert.match(String(field?.rules?.result), /在岗/, key)
		}
	})

	it('IT-STAFF-TPL-003: F1-12/F1-13 five-realm boutique top-level fields', () => {
		for (const key of ['F1-12', 'F1-13']) {
			const field = findFieldByKey(staffSchema.list, key)
			assert.equal(field?.type, 'numberInput', key)
			assert.deepEqual(
				field?.brandId,
				[3454, 3503, 3458, 3574, 3502],
				key
			)
			assert.deepEqual(field?.baseSceneCode, [100101, 100102, 100103], key)
			assert.equal(field?.rules?.keys?.[0], 'F1-1', key)
			assert.match(String(field?.rules?.result), /在岗/, key)
		}
	})

	it('IT-STAFF-TPL-004: F1-5问界 group children after V2.1.1 trim', () => {
		const group = findFieldByKey(staffSchema.list, 'F1-5')
		assert.equal(group?.type, 'group')
		assert.deepEqual(group?.brandId, [3454])
		assert.deepEqual(group?.baseSceneCode, [100101, 100102, 100103, 110201, 110202])

		const childKeys = (group?.children || []).map(c => c.key).sort()
		assert.deepEqual(childKeys, ['F1-5-3', 'F1-5-4', 'F1-5-5', 'F1-5-6'])

		assert.deepEqual(findChildByKey(group, 'F1-5-3')?.baseSceneCode, [
			100101, 100102, 100103
		])
		assert.deepEqual(findChildByKey(group, 'F1-5-4')?.baseSceneCode, [
			110201, 110202
		])
	})

	it('IT-STAFF-TPL-005: removed F1-5-1/F1-5-2 not in schema', () => {
		const keys = collectAllKeys(staffSchema.list)
		assert.ok(!keys.includes('F1-5-1'))
		assert.ok(!keys.includes('F1-5-2'))
		assert.ok(!keys.includes('F1-10-1'))
		assert.ok(!keys.includes('F1-10-2'))
	})

	it('IT-STAFF-TPL-006: F1-11 other-brand group has twelve children', () => {
		const group = findFieldByKey(staffSchema.list, 'F1-11')
		assert.equal(group?.type, 'group')
		assert.deepEqual(group?.filterBrandId, [
			3454, 3503, 3458, 3574, 3502, 3067
		])

		const keys = (group?.children || []).map(c => c.key).sort()
		assert.equal(keys.length, 12)
		assert.ok(keys.includes('F1-11-8'))
		assert.ok(keys.includes('F1-11-9'))
		assert.ok(keys.includes('F1-11-10'))

		const f118 = group.children.find(c => c.key === 'F1-11-8')
		assert.deepEqual(f118?.baseSceneCode, [110101, 110102])
	})

	it('IT-STAFF-TPL-007: F1-5 childrenRules cover active children', () => {
		const group = findFieldByKey(staffSchema.list, 'F1-5')
		const rules = group?.childrenRules || {}
		for (const key of ['F1-5-3', 'F1-5-4', 'F1-5-5', 'F1-5-6']) {
			assert.equal(rules[key]?.integer, true, key)
			assert.equal(rules[key]?.min, 0, key)
		}
	})

	it('IT-STAFF-TPL-008: F1-10岚图 group is warranty-only', () => {
		const group = findFieldByKey(staffSchema.list, 'F1-10')
		assert.deepEqual(group?.baseSceneCode, [110201, 110202])
		const childKeys = (group?.children || []).map(c => c.key)
		assert.deepEqual(childKeys, ['F1-10-3', 'F1-10-4', 'F1-10-5'])
	})

	it('IT-STAFF-TPL-009: F1-11-1 lantu-or-other-brand uses empty brandId', () => {
		const group = findFieldByKey(staffSchema.list, 'F1-11')
		const child = findChildByKey(group, 'F1-11-1')
		assert.deepEqual(child?.brandId, [])
		assert.deepEqual(child?.filterBrandId, [3454, 3503, 3458, 3574, 3502])
		assert.deepEqual(child?.baseSceneCode, [100101, 100102, 100103])
	})

	it('IT-STAFF-TPL-010: F1-3 refund amount has unit 元 from Excel rule', () => {
		const field = findFieldByKey(staffSchema.list, 'F1-3')
		assert.equal(field?.label, '延保退单金额')
		assert.equal(field?.unit, '元')
		assert.equal(field?.rules?.decimal, true)
	})
})