v211-template-visibility.test.mjs 10.7 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { loadSchemaFixtureV211 } from './schemaFixture.mjs'

const BASE_SCENE_CODE = {
	'1.1': 100101,
	'1.2': 100102,
	'1.3': 100103,
	'2.1.1': 110101,
	'2.1.2': 110102,
	'2.3.1': 110201,
	'2.3.2': 110202
}

/** 与 convert 脚本 extract_scene_codes 契约一致(边界匹配,防 2.1.1 误命中 1.1) */
function extractSceneCodes(visibility) {
	const codes = []
	for (const token of Object.keys(BASE_SCENE_CODE).sort((a, b) => b.length - a.length)) {
		const pattern = new RegExp(`(?<![\\d.])${token.replace(/\./g, '\\.')}(?![\\d.])`)
		if (pattern.test(visibility)) {
			codes.push(BASE_SCENE_CODE[token])
		}
	}
	return [...new Set(codes)].sort((a, b) => a - b)
}

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

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

const staffSchema = loadSchemaFixtureV211('staff')
const storeSchema = loadSchemaFixtureV211('store')

describe('V2.1.1 scene code extraction contract', () => {
	it('IT-V211-001: 2.1.1 visibility must not include boutique codes', () => {
		assert.deepEqual(extractSceneCodes('[场景 包含 2.1.1 or 2.1.2] 则 [显示]'), [
			110101, 110102
		])
	})

	it('IT-V211-002: combined warranty/equity visibility excludes boutique', () => {
		assert.deepEqual(
			extractSceneCodes(
				'[场景 包含 2.3.1 or 2.3.2 or 2.1.1 or 2.1.2] 则 [显示]'
			),
			[110101, 110102, 110201, 110202]
		)
	})

	it('IT-V211-003: boutique-only visibility unchanged', () => {
		assert.deepEqual(extractSceneCodes('[场景 包含 1.1 or 1.2 or 1.3] 则 [显示]'), [
			100101, 100102, 100103
		])
	})
})

describe('V2.1.1 staff template baseSceneCode', () => {
	const f111 = findFieldByKey(staffSchema.list, 'F1-11')

	it('IT-V211-STAFF-001: F1-11 equity children use QY codes only', () => {
		for (const key of ['F1-11-5', 'F1-11-6', 'F1-11-8']) {
			const child = findChildByKey(f111, key)
			assert.deepEqual(child?.baseSceneCode, [110101, 110102], key)
		}
	})

	it('IT-V211-STAFF-002: F1-11 warranty child keeps YB codes only', () => {
		const child = findChildByKey(f111, 'F1-11-7')
		assert.deepEqual(child?.baseSceneCode, [110201, 110202])
	})
})

describe('V2.1.1 store template baseSceneCode', () => {
	it('IT-V211-STORE-001: F2-11 equity children use QY codes only', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-11')
		const row = subform?.children?.[0] || []
		for (const key of ['F2-11-6', 'F2-11-7', 'F2-11-8']) {
			const child = row.find(item => item.key === key)
			assert.deepEqual(child?.baseSceneCode, [110101, 110102], key)
		}
	})

	it('IT-V211-STORE-002: F2-14 equity children use QY codes only', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-14')
		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-V211-STORE-003: other-brand combined parents exclude boutique codes', () => {
		const expected = [110101, 110102, 110201, 110202]
		for (const key of ['F2-44', 'F2-51', 'F2-8', 'F2-11', 'F2-14']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.deepEqual(field?.baseSceneCode, expected, key)
		}
	})
})

/** 与 form-engine matchesBaseSceneCode 契约一致,避免测试环境 ESM/CJS 混用 */
function matchesBaseSceneCode(field, ctx) {
	const fieldCodes = (field?.baseSceneCode || []).map(code => Number(code))
	if (!fieldCodes.length) return true
	const merchantCodes = (ctx.sceneList || []).map(item => Number(item?.baseSceneCode))
	return fieldCodes.some(code => merchantCodes.includes(code))
}

/** 与 form-engine brandSpec 契约一致 */
function matchesBrandSpec(field, ctx) {
	const brandIds = ctx.brandIds || []
	if (field.brandId?.length > 0) {
		const hasIntersection = field.brandId.some(id => brandIds.includes(id))
		if (!hasIntersection) return false
	}
	if (field.filterBrandId?.length > 0 && brandIds.length > 0) {
		const hasOtherBrand = brandIds.some(id => !field.filterBrandId.includes(id))
		if (!hasOtherBrand) return false
	}
	return true
}

function isFieldVisibleForTest(field, ctx) {
	return matchesBaseSceneCode(field, ctx) && matchesBrandSpec(field, ctx)
}

describe('V2.1.1 visibility contract under boutique-only scene', () => {
	const boutiqueCtx = {
		baseSceneCode: 100101,
		brandIds: [3454, 2161],
		sceneList: [{ baseSceneCode: 100101 }]
	}
	const equityCtx = {
		baseSceneCode: 110101,
		brandIds: [9999],
		sceneList: [{ baseSceneCode: 110101 }]
	}
	const warrantyCtx = {
		baseSceneCode: 110201,
		brandIds: [9999],
		sceneList: [{ baseSceneCode: 110201 }]
	}
	const otherBrandCtx = {
		baseSceneCode: 100101,
		brandIds: [9999],
		sceneList: [{ baseSceneCode: 100101 }]
	}

	it('IT-V211-STAFF-003: F1-11 equity/warranty children do not match boutique scene', () => {
		const f111 = findFieldByKey(staffSchema.list, 'F1-11')
		for (const key of ['F1-11-5', 'F1-11-6', 'F1-11-7', 'F1-11-8']) {
			const child = findChildByKey(f111, key)
			assert.equal(matchesBaseSceneCode(child, boutiqueCtx), false, key)
		}
		for (const key of ['F1-11-1', 'F1-11-2', 'F1-11-9']) {
			const child = findChildByKey(f111, key)
			assert.equal(matchesBaseSceneCode(child, boutiqueCtx), true, key)
		}
	})

	it('IT-V211-STORE-004: other-brand warranty/equity parents do not match boutique scene', () => {
		for (const key of ['F2-11', 'F2-14', 'F2-44', 'F2-51', 'F2-8']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.equal(matchesBaseSceneCode(field, otherBrandCtx), false, key)
		}
	})

	it('IT-V211-STORE-005: F2-11 equity row cells do not match boutique scene', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-11')
		const rowTemplate = subform?.children?.[0] || []
		for (const key of ['F2-11-6', 'F2-11-7', 'F2-11-8']) {
			const cell = rowTemplate.find(item => item.key === key)
			assert.equal(matchesBaseSceneCode(cell, otherBrandCtx), false, key)
		}
	})

	it('IT-V211-STORE-006: F2-14-12 matches warranty scene only', () => {
		const subform = findFieldByKey(storeSchema.list, 'F2-14')
		const rowTemplate = subform?.children?.[0] || []
		const cell = rowTemplate.find(item => item.key === 'F2-14-12')
		assert.equal(matchesBaseSceneCode(cell, warrantyCtx), true)
		assert.equal(matchesBaseSceneCode(cell, equityCtx), false)
		assert.equal(matchesBaseSceneCode(cell, boutiqueCtx), false)
	})

	it('IT-V211-STAFF-004: equity scene matches F1-11广联权益 children only', () => {
		const f111 = findFieldByKey(staffSchema.list, 'F1-11')
		for (const key of ['F1-11-5', 'F1-11-6', 'F1-11-8']) {
			const child = findChildByKey(f111, key)
			assert.equal(matchesBaseSceneCode(child, equityCtx), true, key)
		}
		for (const key of ['F1-11-1', 'F1-11-3', 'F1-11-7']) {
			const child = findChildByKey(f111, key)
			assert.equal(matchesBaseSceneCode(child, equityCtx), false, key)
		}
	})

	it('IT-V211-STAFF-005: F1-12 boutique fields do not match equity-only scene', () => {
		for (const key of ['F1-12', 'F1-13']) {
			const field = findFieldByKey(staffSchema.list, key)
			assert.equal(matchesBaseSceneCode(field, equityCtx), false, key)
		}
	})
})

describe('V2.1.1 store lantu-or-other-brand visibility', () => {
	const boutiqueScene = [{ baseSceneCode: 100101 }]
	const otherOnlyCtx = {
		baseSceneCode: 100101,
		brandIds: [9999],
		sceneList: boutiqueScene
	}
	const lantuOnlyCtx = {
		baseSceneCode: 100101,
		brandIds: [3067],
		sceneList: boutiqueScene
	}
	const fiveRealmOnlyCtx = {
		baseSceneCode: 100101,
		brandIds: [3454],
		sceneList: boutiqueScene
	}
	const mixedCtx = {
		baseSceneCode: 100101,
		brandIds: [3454, 9999],
		sceneList: boutiqueScene
	}

	it('IT-V211-STORE-007: F2-57/F2-58 visible for other-brand-only merchant', () => {
		for (const key of ['F2-57', 'F2-58']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.equal(isFieldVisibleForTest(field, otherOnlyCtx), true, key)
		}
	})

	it('IT-V211-STORE-008: F2-57/F2-58 visible for lantu-only merchant', () => {
		for (const key of ['F2-57', 'F2-58']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.equal(isFieldVisibleForTest(field, lantuOnlyCtx), true, key)
		}
	})

	it('IT-V211-STORE-009: F2-57/F2-58 hidden for five-realm-only merchant', () => {
		for (const key of ['F2-57', 'F2-58']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.equal(isFieldVisibleForTest(field, fiveRealmOnlyCtx), false, key)
		}
	})

	it('IT-V211-STORE-010: F2-57/F2-58 visible when five-realm plus other brand', () => {
		for (const key of ['F2-57', 'F2-58']) {
			const field = findFieldByKey(storeSchema.list, key)
			assert.equal(isFieldVisibleForTest(field, mixedCtx), true, key)
		}
	})

	it('IT-V211-STORE-011: F2-23 visible for other-brand-only merchant', () => {
		const field = findFieldByKey(storeSchema.list, 'F2-23')
		assert.equal(isFieldVisibleForTest(field, otherOnlyCtx), true)
	})

	it('IT-V211-STORE-012: F2-37 visible for lantu-or-other-brand with boutique scene', () => {
		const field = findFieldByKey(storeSchema.list, 'F2-37')
		assert.equal(isFieldVisibleForTest(field, otherOnlyCtx), true)
		assert.equal(isFieldVisibleForTest(field, lantuOnlyCtx), true)
		assert.equal(isFieldVisibleForTest(field, fiveRealmOnlyCtx), false)
	})

	it('IT-V211-STORE-013: F2-37 hidden when boutique scene missing', () => {
		const field = findFieldByKey(storeSchema.list, 'F2-37')
		const warrantyOnlyCtx = {
			baseSceneCode: 110201,
			brandIds: [9999],
			sceneList: [{ baseSceneCode: 110201 }]
		}
		assert.equal(isFieldVisibleForTest(field, warrantyOnlyCtx), false)
	})
})

describe('V2.1.1 staff lantu-or-other-brand visibility', () => {
	const boutiqueScene = [{ baseSceneCode: 100101 }]
	const otherOnlyCtx = {
		baseSceneCode: 100101,
		brandIds: [9999],
		sceneList: boutiqueScene
	}
	const lantuOnlyCtx = {
		baseSceneCode: 100101,
		brandIds: [3067],
		sceneList: boutiqueScene
	}
	const fiveRealmOnlyCtx = {
		baseSceneCode: 100101,
		brandIds: [3454],
		sceneList: boutiqueScene
	}

	it('IT-V211-STAFF-006: F1-11-1 visible for other-brand-only merchant', () => {
		const f111 = findFieldByKey(staffSchema.list, 'F1-11')
		const child = findChildByKey(f111, 'F1-11-1')
		assert.equal(isFieldVisibleForTest(child, otherOnlyCtx), true)
	})

	it('IT-V211-STAFF-007: F1-11-1 visible for lantu-only merchant', () => {
		const f111 = findFieldByKey(staffSchema.list, 'F1-11')
		const child = findChildByKey(f111, 'F1-11-1')
		assert.equal(isFieldVisibleForTest(child, lantuOnlyCtx), true)
	})

	it('IT-V211-STAFF-008: F1-11-1 hidden for five-realm-only merchant', () => {
		const f111 = findFieldByKey(staffSchema.list, 'F1-11')
		const child = findChildByKey(f111, 'F1-11-1')
		assert.equal(isFieldVisibleForTest(child, fiveRealmOnlyCtx), false)
	})
})