356 lines
17 KiB
TypeScript
356 lines
17 KiB
TypeScript
/**
|
||
* 关联单据带出:界面上那一份即时预览算得对不对。
|
||
*
|
||
* 对面是 `backend/app/service/linkfill.py`(提交时服务端按同一份配置重算并覆盖),
|
||
* 两边必须算出同一个结果——预览一个值、提交另一个值,是这类映射最难查的错。
|
||
* 钉住四件事:
|
||
*
|
||
* 1. **主表那一格是「几张单变成一个值」**:按配的合并档合,取不到值给形状正确的空;
|
||
* 2. **明细那一格是「一单一行」**:新关联的补行、取消关联的删行、留下的行按来源刷新
|
||
* 配到的那一列,人手工改过的其它列一个字不动;
|
||
* 3. **关联没变时一行都不重排**:否则下一环提交会把人删掉的行复活;
|
||
* 4. **指向掉在地上的不写**:那一格的名字会被新字段复用,把别的单的数盖到它头上最危险;
|
||
* 5. **两个关联字段带同一张明细表**:各带各的行,后算的那一格不许删对方刚补的那一行。
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import {
|
||
ROW_LINK_KEY,
|
||
TRACE_KEY,
|
||
applyLinkFill,
|
||
autoLinkFillRules,
|
||
cleanLinkFill,
|
||
describeLinkFillWhere,
|
||
linkFillCapableFields,
|
||
linkFillCount,
|
||
linkFillFields,
|
||
linkFillIntoTable,
|
||
linkFillPicks,
|
||
linkFillProblems,
|
||
linkFillSourceFields,
|
||
linkFillTotalRules,
|
||
linkFillValues,
|
||
shapeLinkFillValue,
|
||
splitLinkTarget
|
||
} from '@/utils/linkFill'
|
||
import type { ProcessFieldOption } from '@/types'
|
||
import type { FormFieldDef, LinkFillRule } from '@/types'
|
||
|
||
function relation(rules: LinkFillRule[], extra: Partial<FormFieldDef> = {}): FormFieldDef {
|
||
return { key: 'orders', type: 'relation', label: '关联报销单', optionSource: 'process', linkFill: { rules }, ...extra }
|
||
}
|
||
|
||
function schema(rules: LinkFillRule[]): FormFieldDef[] {
|
||
return [
|
||
relation(rules),
|
||
{ key: 'src_serial', type: 'text', label: '来源单号' },
|
||
{ key: 'total', type: 'money', label: '合计' },
|
||
{ key: 'when', type: 'date', label: '发起日期' },
|
||
{
|
||
key: 'items',
|
||
type: 'table',
|
||
label: '明细',
|
||
columns: [
|
||
{ key: 'amount', type: 'money', label: '金额' },
|
||
{ key: 'note', type: 'text', label: '备注' }
|
||
]
|
||
}
|
||
]
|
||
}
|
||
|
||
const J1 = { job: 'J1', values: { 'work.serial': 'BX-001', 'data.amount': 100, 'data.day': '2026-03-01 00:00:00' } }
|
||
const J2 = { job: 'J2', values: { 'work.serial': 'BX-002', 'data.amount': '2,300.50', 'data.day': '2026-03-05 00:00:00' } }
|
||
|
||
describe('配置的形状', () => {
|
||
it('没写取哪一列或写进哪一格的规则丢掉,一条不剩等于没配', () => {
|
||
expect(cleanLinkFill({ rules: [{ to: 'total' } as unknown as LinkFillRule] })).toBeNull()
|
||
expect(cleanLinkFill({ rules: [{ pick: 'work.serial' } as unknown as LinkFillRule] })).toBeNull()
|
||
expect(cleanLinkFill({ rules: [] })).toBeNull()
|
||
})
|
||
|
||
it('跟默认一模一样的不留,认不出的合并档也丢掉', () => {
|
||
expect(cleanLinkFill({ rules: [{ pick: 'a', to: 'b', agg: 'first' }] })).toEqual({ rules: [{ pick: 'a', to: 'b' }] })
|
||
expect(cleanLinkFill({ rules: [{ pick: 'a', to: 'b', agg: 'avg' as LinkFillRule['agg'] }] }))
|
||
.toEqual({ rules: [{ pick: 'a', to: 'b' }] })
|
||
})
|
||
|
||
it('拼接的分隔符原样留着,不 trim', () => {
|
||
expect(cleanLinkFill({ rules: [{ pick: 'a', to: 'b', agg: 'join', separator: ' / ' }] }))
|
||
.toEqual({ rules: [{ pick: 'a', to: 'b', agg: 'join', separator: ' / ' }] })
|
||
})
|
||
|
||
it('来源不是流程单据就不算配过(后端 configured 同一句)', () => {
|
||
const dict = { key: 'x', type: 'select', optionSource: 'dict', dictCode: 'd', linkFill: { rules: [{ pick: 'work.serial', to: 'x' }] } } as FormFieldDef
|
||
expect(linkFillFields([dict])).toEqual([])
|
||
expect(linkFillProblems(dict, [dict])[0]).toContain('流程单据')
|
||
})
|
||
})
|
||
|
||
describe('指向怎么认', () => {
|
||
it('整串先认主表字段,认不出再拆「表.列」', () => {
|
||
const rows = schema([])
|
||
expect(splitLinkTarget('total', rows)).toEqual({ table: 'total', column: '' })
|
||
expect(splitLinkTarget('items.amount', rows)).toEqual({ table: 'items', column: 'amount' })
|
||
expect(splitLinkTarget('items.nope', rows)).toEqual({ table: '', column: '' })
|
||
expect(splitLinkTarget('gone', rows)).toEqual({ table: '', column: '' })
|
||
})
|
||
|
||
it('指向不存在的格子、两条抢同一格、数装不进文本格,都当场说', () => {
|
||
const rows = schema([{ pick: 'work.serial', to: 'gone' }])
|
||
expect(linkFillProblems(rows[0], rows)[0]).toContain('不存在的字段「gone」')
|
||
const twice = schema([{ pick: 'work.serial', to: 'total' }, { pick: 'work.title', to: 'total', agg: 'join' }])
|
||
expect(linkFillProblems(twice[0], twice).join(';')).toContain('都写进「total」')
|
||
const text = schema([{ pick: 'data.amount', to: 'src_serial', agg: 'sum' }])
|
||
expect(linkFillProblems(text[0], text).join(';')).toContain('装不下数')
|
||
})
|
||
})
|
||
|
||
describe('主表:几张单变成一个值', () => {
|
||
it('取第一个按关联顺序,拼接按配的分隔符', () => {
|
||
const rows = schema([{ pick: 'work.serial', to: 'src_serial' }])
|
||
const data: Record<string, unknown> = { orders: ['J1'] }
|
||
expect(applyLinkFill(rows, data, { orders: [J1] })).toEqual(['src_serial'])
|
||
expect(data.src_serial).toBe('BX-001')
|
||
|
||
const joined = schema([{ pick: 'work.serial', to: 'src_serial', agg: 'join', separator: ' / ' }])
|
||
const many: Record<string, unknown> = { orders: ['J1', 'J2'] }
|
||
applyLinkFill(joined, many, { orders: [J1, J2] })
|
||
expect(many.src_serial).toBe('BX-001 / BX-002')
|
||
})
|
||
|
||
it('相加按分算,带千分位的文本也认', () => {
|
||
const rows = schema([{ pick: 'data.amount', to: 'total', agg: 'sum' }])
|
||
const data: Record<string, unknown> = { orders: ['J1', 'J2'] }
|
||
applyLinkFill(rows, data, { orders: [J1, J2] })
|
||
expect(data.total).toBe(2400.5)
|
||
})
|
||
|
||
it('日期那一格收成 YYYY-MM-DD,单据本体那种带时分秒的写法也收', () => {
|
||
const rows = schema([{ pick: 'data.day', to: 'when' }])
|
||
const data: Record<string, unknown> = { orders: ['J1'] }
|
||
applyLinkFill(rows, data, { orders: [J1] })
|
||
expect(data.when).toBe('2026-03-01')
|
||
expect(shapeLinkFillValue('2026-03-01 09:30:00', 'datetime')).toBe('2026-03-01T09:30')
|
||
})
|
||
|
||
it('取消全部关联时那一格清空', () => {
|
||
const rows = schema([{ pick: 'data.amount', to: 'total', agg: 'sum' }])
|
||
const data: Record<string, unknown> = { orders: [], total: 300, [TRACE_KEY]: { orders: ['J1'] } }
|
||
applyLinkFill(rows, data, { orders: [] })
|
||
expect(data.total).toBe('')
|
||
})
|
||
|
||
it('没问回来的那一格不动:预览没到不等于值该被抹掉', () => {
|
||
const rows = schema([{ pick: 'data.amount', to: 'total', agg: 'sum' }])
|
||
const data: Record<string, unknown> = { orders: ['J1'], total: 300 }
|
||
expect(applyLinkFill(rows, data, {})).toEqual([])
|
||
expect(data.total).toBe(300)
|
||
})
|
||
})
|
||
|
||
describe('明细:一单一行', () => {
|
||
const rules: LinkFillRule[] = [{ pick: 'data.amount', to: 'items.amount' }]
|
||
|
||
it('两张单补两行,行上认得出是哪张单带来的', () => {
|
||
const data: Record<string, unknown> = { orders: ['J1', 'J2'] }
|
||
applyLinkFill(schema(rules), data, { orders: [J1, J2] })
|
||
expect((data.items as Record<string, unknown>[]).map((row) => row[ROW_LINK_KEY])).toEqual(['J1', 'J2'])
|
||
expect((data.items as Record<string, unknown>[]).map((row) => row.amount)).toEqual([100, 2300.5])
|
||
// 新行的形状与人点「+」加出来的那一行一致
|
||
expect(Object.keys((data.items as Record<string, unknown>[])[0]).sort()).toEqual([ROW_LINK_KEY, 'amount', 'note'])
|
||
})
|
||
|
||
it('关联没变时不重排:人删掉的行不复活,手工加的行一个字不动', () => {
|
||
const data: Record<string, unknown> = {
|
||
orders: ['J1', 'J2'],
|
||
[TRACE_KEY]: { orders: ['J1', 'J2'] },
|
||
items: [{ [ROW_LINK_KEY]: 'J1', amount: 100, note: '自己写的' }, { amount: 999, note: '手工加的行' }]
|
||
}
|
||
expect(applyLinkFill(schema(rules), data, { orders: [J1, J2] })).toEqual([])
|
||
expect((data.items as Record<string, unknown>[]).map((row) => row[ROW_LINK_KEY])).toEqual(['J1', undefined])
|
||
expect((data.items as Record<string, unknown>[])[1].amount).toBe(999)
|
||
})
|
||
|
||
it('关联变了才补行删行,且新行加在末尾不打乱人排的序', () => {
|
||
const data: Record<string, unknown> = {
|
||
orders: ['J1', 'J2'],
|
||
[TRACE_KEY]: { orders: ['J1'] },
|
||
items: [{ [ROW_LINK_KEY]: 'J1', amount: 100, note: '自己写的' }, { amount: 999 }]
|
||
}
|
||
applyLinkFill(schema(rules), data, { orders: [J1, J2] })
|
||
expect((data.items as Record<string, unknown>[]).map((row) => row[ROW_LINK_KEY])).toEqual(['J1', undefined, 'J2'])
|
||
expect((data.items as Record<string, unknown>[])[0].note).toBe('自己写的')
|
||
})
|
||
|
||
it('取消关联的那一行跟着删,留下的行按来源刷新配到的那一列', () => {
|
||
const data: Record<string, unknown> = {
|
||
orders: ['J2'],
|
||
[TRACE_KEY]: { orders: ['J1', 'J2'] },
|
||
items: [{ [ROW_LINK_KEY]: 'J1', amount: 100 }, { [ROW_LINK_KEY]: 'J2', amount: 200 }]
|
||
}
|
||
applyLinkFill(schema(rules), data, { orders: [J2] })
|
||
expect((data.items as Record<string, unknown>[]).map((row) => row[ROW_LINK_KEY])).toEqual(['J2'])
|
||
})
|
||
|
||
it('取不到值落一个形状正确的空,行还是那张单的', () => {
|
||
const data: Record<string, unknown> = { orders: ['J1'] }
|
||
applyLinkFill(schema([{ pick: 'data.nope', to: 'items.amount' }]), data, { orders: [J1] })
|
||
expect((data.items as Record<string, unknown>[])[0].amount).toBe('')
|
||
})
|
||
|
||
it('补行也认最多行数', () => {
|
||
const rows = schema(rules)
|
||
rows[4].maxRows = 1
|
||
const data: Record<string, unknown> = { orders: ['J1', 'J2'] }
|
||
applyLinkFill(rows, data, { orders: [J1, J2] })
|
||
expect(data.items as unknown[]).toHaveLength(1)
|
||
})
|
||
})
|
||
|
||
describe('两个关联字段带同一张明细表', () => {
|
||
// 「挂两类单子」在真实表单上就是两格(报销单一格、采购单一格,字段名根本不是一套),
|
||
// 而明细表只有一张。少了这一道兜底,后算的那一格会把前一格刚补的那几行删干净。
|
||
const ORDERS: FormFieldDef = {
|
||
key: 'orders', type: 'relation', label: '关联报销单', optionSource: 'process',
|
||
linkFill: { rules: [{ pick: 'data.amount', to: 'items.amount' }] }
|
||
}
|
||
const BUYS: FormFieldDef = {
|
||
key: 'buys', type: 'picker', label: '关联采购单', optionSource: 'process',
|
||
linkFill: { rules: [{ pick: 'data.cost', to: 'items.amount' }] }
|
||
}
|
||
const FIELDS: FormFieldDef[] = [
|
||
ORDERS, BUYS,
|
||
{ key: 'items', type: 'table', label: '明细', columns: [{ key: 'amount', type: 'money', label: '金额' }] }
|
||
]
|
||
const ORDER = { job: 'J1', values: { 'data.amount': 100 } }
|
||
const BUY = { job: 'J9', values: { 'data.cost': 500 } }
|
||
const links = (data: Record<string, unknown>) =>
|
||
(data.items as Record<string, unknown>[]).map((row) => row[ROW_LINK_KEY])
|
||
|
||
it('各带一行,后算的那一格不许删前一格刚补的那一行', () => {
|
||
const data: Record<string, unknown> = { orders: ['J1'], buys: ['J9'] }
|
||
applyLinkFill(FIELDS, data, { orders: [ORDER], buys: [BUY] })
|
||
expect(links(data)).toEqual(['J1', 'J9'])
|
||
expect((data.items as Record<string, unknown>[]).map((row) => row.amount)).toEqual([100, 500])
|
||
})
|
||
|
||
it('只撤一格的关联:对方带来的那一行原样留着', () => {
|
||
const data: Record<string, unknown> = {
|
||
orders: [],
|
||
buys: ['J9'],
|
||
[TRACE_KEY]: { orders: ['J1'], buys: ['J9'] },
|
||
items: [{ [ROW_LINK_KEY]: 'J1', amount: 100 }, { [ROW_LINK_KEY]: 'J9', amount: 500 }]
|
||
}
|
||
applyLinkFill(FIELDS, data, { orders: [], buys: [BUY] })
|
||
expect(links(data)).toEqual(['J9'])
|
||
})
|
||
|
||
it('对方那一串值存的是单号:靠问回来的那些 job 认得出那一行是谁带来的', () => {
|
||
const data: Record<string, unknown> = {
|
||
orders: ['BX-001'],
|
||
buys: ['CG-009'],
|
||
[TRACE_KEY]: { orders: [], buys: ['CG-009'] },
|
||
items: [{ [ROW_LINK_KEY]: 'J9', amount: 500 }]
|
||
}
|
||
applyLinkFill(FIELDS, data, { orders: [ORDER], buys: [BUY] })
|
||
expect(links(data)).toEqual(['J9', 'J1'])
|
||
})
|
||
})
|
||
|
||
describe('问后端要哪几列', () => {
|
||
it('多个格子的 pick 并成一趟,重复的去掉', () => {
|
||
const rows = [
|
||
...schema([{ pick: 'work.serial', to: 'src_serial' }]),
|
||
relation([{ pick: 'work.serial', to: 'total', agg: 'sum' }, { pick: 'data.tax', to: 'when' }], { key: 'more' })
|
||
]
|
||
expect(linkFillPicks(rows)).toEqual(['work.serial', 'data.tax'])
|
||
})
|
||
|
||
it('关联的那串值去重、按关联顺序,单选那一格也认', () => {
|
||
expect(linkFillValues(relation([]), { orders: ['J2', 'J1', 'J2', ''] })).toEqual(['J2', 'J1'])
|
||
expect(linkFillValues(relation([]), { orders: 'J1' })).toEqual(['J1'])
|
||
})
|
||
|
||
it('带出留痕按关联字段各记一份', () => {
|
||
const data: Record<string, unknown> = { orders: ['J1'] }
|
||
applyLinkFill(schema([{ pick: 'work.serial', to: 'src_serial' }]), data, { orders: [J1] })
|
||
expect((data[TRACE_KEY] as Record<string, unknown>).orders).toEqual(['J1'])
|
||
})
|
||
})
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// 设计器那几句:入口上念的账(配了几条、落到哪儿、这张表被谁填、按同名自动配)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/** 三句措辞由界面递进来,工具层不碰 i18n;测试这里给一句最小说明。 */
|
||
const TEXTS = {
|
||
main: (fields: string, count: number) => `主表 ${fields}(${count})`,
|
||
rows: (table: string, columns: string) => `往「${table}」补行(${columns})`,
|
||
none: '还没落到具体格子'
|
||
}
|
||
|
||
describe('入口上念的那几笔账', () => {
|
||
it('条数只认候选来源是流程单据的那几格,总账把几格加起来', () => {
|
||
const rows = schema([{ pick: 'work.serial', to: 'src_serial' }, { pick: 'data.amount', to: 'items.amount' }])
|
||
expect(linkFillCount(rows[0])).toBe(2)
|
||
expect(linkFillTotalRules(rows)).toBe(2)
|
||
// 配得再齐,来源不是「流程单据」就不算配过带出
|
||
const dict = relation([{ pick: 'work.serial', to: 'src_serial' }], { optionSource: 'dict', dictCode: 'x' })
|
||
expect(linkFillCount(dict)).toBe(0)
|
||
})
|
||
|
||
it('挂单据的格子配没配都列,「去哪儿配」那一栏只列还没配的', () => {
|
||
const rows = [
|
||
...schema([{ pick: 'work.serial', to: 'src_serial' }]),
|
||
relation([], { key: 'buys', label: '关联采购单' })
|
||
]
|
||
expect(linkFillSourceFields(rows).map((item) => item.key)).toEqual(['orders', 'buys'])
|
||
expect(linkFillCapableFields(rows).map((item) => item.key)).toEqual(['buys'])
|
||
})
|
||
|
||
it('落点那句念的是格子的名字,不是内部键', () => {
|
||
const rows = schema([
|
||
{ pick: 'work.serial', to: 'src_serial' },
|
||
{ pick: 'data.amount', to: 'items.amount' },
|
||
{ pick: 'data.tax', to: 'items.note' },
|
||
{ pick: 'data.day', to: 'total' }
|
||
])
|
||
expect(describeLinkFillWhere(linkFillFields(rows)[0]?.linkFill ?? null, rows, TEXTS)).toBe(
|
||
'主表 来源单号、合计(2);往「明细」补行(金额、备注)'
|
||
)
|
||
// 一条都没落上时那句得说人话,不能是空
|
||
expect(describeLinkFillWhere(null, rows, TEXTS)).toBe('还没落到具体格子')
|
||
})
|
||
|
||
it('这张明细表被哪几格填:只认带进这张表的那些列,别的格子不算', () => {
|
||
const rows = schema([
|
||
{ pick: 'work.serial', to: 'src_serial' },
|
||
{ pick: 'data.amount', to: 'items.amount' }
|
||
])
|
||
rows.push(relation([{ pick: 'data.price', to: 'items.note' }], { key: 'buys', label: '关联采购单' }))
|
||
expect(linkFillIntoTable('items', rows).map((item) => [item.field.key, item.columns])).toEqual([
|
||
['orders', ['amount']],
|
||
['buys', ['note']]
|
||
])
|
||
// 没有任何格子往这张表里带 → 空清单,由界面自己说那句「还没有哪一格往里带值」
|
||
expect(linkFillIntoTable('nothing', rows)).toEqual([])
|
||
})
|
||
|
||
it('按同名自动配:名字对得上的配起来,对不上、已配过、装的是关联的都不猜', () => {
|
||
const rows = schema([])
|
||
const source: ProcessFieldOption[] = [
|
||
{ key: 'src_serial', label: '别的名字', type: 'text' },
|
||
{ key: 'x', label: '合计', type: 'money' },
|
||
{ key: 'amount', label: '金额', type: 'money' },
|
||
{ key: 'orders', label: '关联报销单', type: 'relation' },
|
||
{ key: 'items', label: '明细', type: 'table' },
|
||
{ key: 'ghost', label: '这一格表单上没有', type: 'text' }
|
||
]
|
||
// 合计那一格已经被她自己写过了:手写的最大,自动配不许抢
|
||
const rules = autoLinkFillRules(source, rows, [{ pick: 'work.title', to: 'total' }])
|
||
expect(rules).toEqual([
|
||
{ pick: 'data.src_serial', to: 'src_serial' },
|
||
{ pick: 'data.amount', to: 'items.amount' }
|
||
])
|
||
})
|
||
})
|