group-row-snapshot.test.mjs 5.21 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const {
	getRowFieldsSnapshot,
	buildGroupRowViewMaps
} = await import('../form-engine/group-row-snapshot.js')
const { loadSchemaFixture } = await import('./schemaFixture.mjs')
const negotiationSchema = loadSchemaFixture('negotiation')
const storeSchema = loadSchemaFixture('store')

describe('group-row-snapshot', () => {
	it('IT-GROUP-SNAPSHOT-001: snapshot isolates row field values', async () => {
		const { cloneRowTemplate } = await import(
			'../form-engine/group-row-adapter.js'
		)
		const { setRowFieldInCache } = await import(
			'../form-engine/group-rows-cache.js'
		)
		const group = negotiationSchema.list.find(item => item.key === 'F3-1')
		const template = cloneRowTemplate(group)
		let cache = {
			'F3-1': [cloneRowTemplate(group), cloneRowTemplate(group)]
		}
		cache = setRowFieldInCache(cache, 'F3-1', 0, 'F3-1-4', '客户A', template)
		cache = setRowFieldInCache(cache, 'F3-1', 1, 'F3-1-4', '客户B', template)

		const row0 = getRowFieldsSnapshot(cache, 'F3-1', 0)
		const row1 = getRowFieldsSnapshot(cache, 'F3-1', 1)
		assert.equal(row0.find(f => f.key === 'F3-1-4')?.value, '客户A')
		assert.equal(row1.find(f => f.key === 'F3-1-4')?.value, '客户B')

		row0.find(f => f.key === 'F3-1-4').value = '篡改'
		const row1Again = getRowFieldsSnapshot(cache, 'F3-1', 1)
		assert.equal(row1Again.find(f => f.key === 'F3-1-4')?.value, '客户B')
	})

	it('IT-GROUP-SNAPSHOT-002: buildGroupRowViewMaps returns totals and visible map', () => {
		const cache = { 'F3-1': [[], []] }
		const values = { 'F3-1': [{}, {}] }
		const { groupRowFieldsMap, groupRowTotalMap, groupRowVisibleMap } =
			buildGroupRowViewMaps([{ key: 'F3-1' }], cache, { 'F3-1': 0 }, values)
		assert.equal(groupRowTotalMap['F3-1'], 2)
		assert.ok(Array.isArray(groupRowFieldsMap['F3-1']))
		assert.ok(Array.isArray(groupRowVisibleMap['F3-1']))
	})

	it('IT-GROUP-SNAPSHOT-004: visible map attaches _wxRowKey', () => {
		const group = negotiationSchema.list.find(item => item.key === 'F3-1')
		const cache = { 'F3-1': [[{ key: 'F3-1-4', type: 'input', value: 'x' }]] }
		const { groupRowVisibleMap } = buildGroupRowViewMaps(
			[group],
			cache,
			{ 'F3-1': 0 },
			{ 'F3-1': [{}] }
		)
		const row = groupRowVisibleMap['F3-1']
		assert.ok(row.length >= 1)
		assert.match(row[0]._wxRowKey, /^F3-1_0_/)
	})

	it('IT-GROUP-ISOLATE-001: F3-1 and F3-2 visible maps do not share customer name', async () => {
		const { cloneRowTemplate } = await import(
			'../form-engine/group-row-adapter.js'
		)
		const { setRowFieldInCache } = await import(
			'../form-engine/group-rows-cache.js'
		)
		const group1 = negotiationSchema.list.find(item => item.key === 'F3-1')
		const group2 = negotiationSchema.list.find(item => item.key === 'F3-2')
		const template1 = cloneRowTemplate(group1)
		const template2 = cloneRowTemplate(group2)
		let cache = {
			'F3-1': [cloneRowTemplate(group1)],
			'F3-2': [cloneRowTemplate(group2)]
		}
		cache = setRowFieldInCache(cache, 'F3-1', 0, 'F3-1-4', '客户甲', template1)
		cache = setRowFieldInCache(cache, 'F3-2', 0, 'F3-2-4', '客户乙', template2)

		const { groupRowVisibleMap } = buildGroupRowViewMaps(
			[group1, group2],
			cache,
			{ 'F3-1': 0, 'F3-2': 0 },
			{
				'F3-1': [{ 'F3-1-4': '客户甲' }],
				'F3-2': [{ 'F3-2-4': '客户乙' }]
			}
		)
		const name1 = groupRowVisibleMap['F3-1'].find(f => f.key === 'F3-1-4')?.value
		const name2 = groupRowVisibleMap['F3-2'].find(f => f.key === 'F3-2-4')?.value
		assert.equal(name1, '客户甲')
		assert.equal(name2, '客户乙')
	})

	it('IT-GROUP-SNAPSHOT-005: scene spec hides subform columns in visible map', async () => {
		const { cloneRowTemplate } = await import('../form-engine/group-row-adapter.js')
		const group = storeSchema.list.find(item => item.key === 'F2-11')
		const template = cloneRowTemplate(group)
		const cache = { 'F2-11': [cloneRowTemplate(group)] }
		const warrantyCtx = {
			baseSceneCode: 110201,
			brandIds: [3067, 2161],
			sceneList: [{ baseSceneCode: 110201 }]
		}
		const { groupRowVisibleMap } = buildGroupRowViewMaps(
			[group],
			cache,
			{ 'F2-11': 0 },
			{ 'F2-11': [{}] },
			{
				ctx: warrantyCtx,
				values: { 'F2-11': [{}] },
				list: storeSchema.list
			}
		)
		const keys = (groupRowVisibleMap['F2-11'] || []).map(field => field.key)
		assert.ok(keys.includes('F2-11-3'))
		assert.ok(!keys.includes('F2-11-8'))
	})

	it('IT-GROUP-SNAPSHOT-003: setRowFieldInCache seeds row from template not empty array', async () => {
		const { cloneRowTemplate } = await import(
			'../form-engine/group-row-adapter.js'
		)
		const { setRowFieldInCache, syncValuesFromGroupCache } = await import(
			'../form-engine/group-rows-cache.js'
		)
		const group = negotiationSchema.list.find(item => item.key === 'F3-1')
		const template = cloneRowTemplate(group)
		const cache = setRowFieldInCache(
			{ 'F3-1': [] },
			'F3-1',
			0,
			'F3-1-4',
			'测试',
			template
		)
		const row = cache['F3-1'][0] || []
		assert.ok(row.length > 0)
		assert.equal(row.find(f => f.key === 'F3-1-4')?.value, '测试')
		const values = syncValuesFromGroupCache({}, 'F3-1', cache['F3-1'])
		assert.equal(values['F3-1'][0]['F3-1-4'], '测试')
	})
})