/** * 台账列的归型 —— **最坏的失败点是「人签完字之后」**。 * * 实测踩过:模型用 `""` 表示「原文没给目标价」(业务判断完全正确),草稿层照单全收; * 到 `releaseWrite` 往真表里写时 PG 掀桌 —— * `invalid input syntax for type double precision: ""` —— 而那已经是人签完字之后, * 整单落成 `blocked`。前面全对,最后一步炸,这种失败最伤人。 * * 所以归型放在写进草稿那一刻:归得了就归,归不了当场拒(理由喂回下一拍)。 */ import { describe, expect, it } from 'vitest'; import { coerceToColumn, distinctReadings, localStamp, missingReadings, rowLabel, rowReadings, rowKeyList, tallyReadings, toolCallReadings, touchedRows } from '../wiring/engines.ts'; describe('空值 = 这一列没有值(不写)', () => { it('空串 / 全空白 / null / undefined 一律不写 —— 「原文没写的不要补」的数据面落点', () => { for (const v of ['', ' ', null, undefined]) { expect(coerceToColumn(v, 'number').kind, JSON.stringify(v)).toBe('empty'); expect(coerceToColumn(v, 'text').kind, JSON.stringify(v)).toBe('empty'); } }); it('0 与 false 是**有值**,不能被当成空 —— 这两个值最容易被顺手丢掉', () => { expect(coerceToColumn(0, 'number')).toEqual({ kind: 'value', value: 0 }); expect(coerceToColumn('0', 'integer')).toEqual({ kind: 'value', value: 0 }); expect(coerceToColumn(false, 'boolean')).toEqual({ kind: 'value', value: false }); expect(coerceToColumn('false', 'boolean')).toEqual({ kind: 'value', value: false }); }); }); describe('数值列', () => { it('数字字符串转成数字 —— 模型给 "12000" 是常态', () => { expect(coerceToColumn('12000', 'integer')).toEqual({ kind: 'value', value: 12000 }); expect(coerceToColumn('8.4', 'number')).toEqual({ kind: 'value', value: 8.4 }); }); it('不是数的当场拒 —— 拒在写入,不拖到放行', () => { expect(coerceToColumn('待定', 'number').kind).toBe('reject'); expect(coerceToColumn({ a: 1 }, 'number').kind).toBe('reject'); expect(coerceToColumn(Number.NaN, 'number').kind).toBe('reject'); }); }); describe('布尔列', () => { it('"true"/"false" 转真布尔', () => { expect(coerceToColumn('true', 'boolean')).toEqual({ kind: 'value', value: true }); }); it('模棱两可的字样当场拒 —— "是"/"1" 各人理解不同,不替它猜', () => { expect(coerceToColumn('是', 'boolean').kind).toBe('reject'); expect(coerceToColumn(1, 'boolean').kind).toBe('reject'); }); }); describe('文本与 json 列', () => { it('text 收字符串;对象序列化 —— 比把 [object Object] 写进库强', () => { expect(coerceToColumn('8周', 'text')).toEqual({ kind: 'value', value: '8周' }); expect(coerceToColumn({ a: 1 }, 'text')).toEqual({ kind: 'value', value: '{"a":1}' }); expect(coerceToColumn(12, 'text')).toEqual({ kind: 'value', value: '12' }); }); it('json 列什么都装得下,不归型', () => { expect(coerceToColumn({ rows: [1, 2] }, 'json')).toEqual({ kind: 'value', value: { rows: [1, 2] } }); }); it('日期列按文本走(PG 自己解析格式)', () => { expect(coerceToColumn('2026-07-30', 'date')).toEqual({ kind: 'value', value: '2026-07-30' }); }); }); describe('回归:那一单为什么会 blocked', () => { it('NE555P 没给目标价 → 这一列不写(列留 NULL),而不是把 "" 塞进 numeric', () => { const patch = { pn: 'NE555P', eau: '5000', target_price: '', lead_time: '6周' }; const types: Record = { pn: 'text', eau: 'integer', target_price: 'number', lead_time: 'text' }; const out: Record = {}; for (const [k, v] of Object.entries(patch)) { const c = coerceToColumn(v, types[k] ?? 'text'); if (c.kind === 'value') out[k] = c.value; } expect(out).toEqual({ pn: 'NE555P', eau: 5000, lead_time: '6周' }); expect('target_price' in out).toBe(false); }); }); /** * 行数读数 —— **判据的另一边不能也由活动自报**。 * * 实测的一次「对账过关而账是错的」:活动把条目数记成 9,而受理记录里躺着 10 行 * (一条 PN 抄漏了后缀,补写时成了新的一行);三类条数与派单数同样自报同错, * 于是 `2 = 2` 顺利过关,人签完字,下游拿到一份多一行的交付物。 */ describe('行数读数:域_对象_rows', () => { it('按 mine: 那一份数行,名字换成 S-FEEL 认得的下划线', () => { expect( rowReadings({ 'mine:quote_intake.record': [{}, {}, {}], 'mine:quote_intake.verdict': [{}], }), ).toEqual({ quote_intake_record_rows: 3, quote_intake_verdict_rows: 1 }); }); it('不是 mine: 的键一概不数 —— 别单放行过的正式记录不算本单的行', () => { expect(rowReadings({ 'quote_intake.record': [{}, {}] })).toEqual({}); }); /* * **一行没写也是一个读数:0**(2026-08-01,全 A 类那条路逼出来的)。 * * 从前只数「写过的表」:一单没有 C 类,替代评估请求那张表一行都没写, * 于是 `quote_intake_alt_request_rows` 整个键不存在;而判据是 * `quote_intake_alt_request_rows = type_c_total`(0 = 0 本该成立)—— * 未绑定的名字参与比较求值为假,这条判据**永远过不去**,模型连撞五拍熔断, * 而数据本身完全合规。「没写过」与「写了 0 行」在业务上是同一件事。 */ it('声明过但一行没写的表读数是 0 —— 零条也是一个数,判据得算得下去', () => { const declared = [ { domain: 'quote_intake', object: 'record' }, { domain: 'quote_intake', object: 'alt_request' }, ]; expect(rowReadings({ 'mine:quote_intake.record': [{}, {}] }, declared)).toEqual({ quote_intake_record_rows: 2, quote_intake_alt_request_rows: 0, }); }); it('没给声明就还是只数写过的 —— 铺 0 的名单来自活动的台账声明', () => { expect(rowReadings({})).toEqual({}); }); }); /** * 时刻摆到屏上要**本地的**。 * * 直接切 ISO 串那八个字符,屏上写着 `07-31 20:16` 而人这边是 `08-01 04:16` —— * 差八小时的时刻比没有时刻更坏:它看起来像一个可信的事实(实测:来件那一行就这么错过一次)。 */ describe('本地时刻', () => { it('UTC 串按本地时区落到「月-日 时:分」', () => { const iso = '2026-07-31T20:16:00.000Z'; const at = new Date(iso); const two = (n: number): string => String(n).padStart(2, '0'); expect(localStamp(iso)).toBe(`${two(at.getMonth() + 1)}-${two(at.getDate())} ${two(at.getHours())}:${two(at.getMinutes())}`); }); it('不是时刻就留空 —— 宁可没有,不摆一个假的', () => { expect(localStamp('这不是时刻')).toBe(''); }); it('归零到分 —— 秒不进报告(同一单的两份件不该看起来不一样)', () => { expect(localStamp('2026-08-01T04:16:59.999Z')).toBe(localStamp('2026-08-01T04:16:01.000Z')); }); }); /** * 主键名单 —— **行数对不上时,「是哪几行」只有基座数得出来**。 * * 判据比的是两张表的行数;模型看得见「10 ≠ 9」,却不知道多的是哪一行。 * 名单一摆就是一句话能办的事:多的那个 PN 就在名单里对着。 * 摆的是主键不是整行 —— 认一行只需要它的主键,整行铺出来会把拒回理由撑爆。 */ describe('主键名单:域_对象_keys', () => { const DECL = [ { domain: 'quote_intake', object: 'record', fields: [ { name: 'rfq_ref', primaryKey: true }, { name: 'pn', primaryKey: true }, { name: 'eau', primaryKey: false }, ], }, { domain: 'quote_intake', object: 'alt_request', fields: [{ name: 'pn', primaryKey: true }] }, ]; /* * **名单要能直接照抄进 `drop` 的 keys** —— 所以摆 JSON,不摆 `A/B` 拼串。 * * 实测栽过:名单写作 `RFQ MD14.1/XTSD04GLGEAQG-I`,模型得自己猜第一段是哪一列; * 列名它猜对了,值却把空格写成了横杠(`RFQ-MD14.1` 是更常见的写法)—— * 连撤三次都撤不到,而那一行就在名单里。主键是认行的钥匙,一个字符都不能差。 */ it('主键摆成 JSON —— 列名带着值,模型原样粘进 drop 就行', () => { expect( rowKeyList({ 'mine:quote_intake.record': [{ rfq_ref: 'RFQ MD14.1', pn: 'LM358DR', eau: 30000 }] }, DECL), ).toEqual({ quote_intake_record_keys: '{"rfq_ref":"RFQ MD14.1","pn":"LM358DR"}' }); }); it('值里的空格原样留着 —— 转写一次就撤不到那一行了', () => { const out = rowKeyList({ 'mine:quote_intake.alt_request': [{ pn: 'UMW AO6604' }] }, DECL); expect(out['quote_intake_alt_request_keys']).toBe('{"pn":"UMW AO6604"}'); }); it('一行没写的表不出名单 —— 空名单比没有更像坏了', () => { expect(rowKeyList({ 'mine:quote_intake.record': [] }, DECL)).toEqual({}); expect(rowKeyList({}, DECL)).toEqual({}); }); it('**封顶 12 行**,超出的如实说还有几行 —— 再多就是把台账抄了一遍', () => { const rows = Array.from({ length: 15 }, (_, i) => ({ pn: `P${i}` })); const out = rowKeyList({ 'mine:quote_intake.alt_request': rows }, DECL); expect(out['quote_intake_alt_request_keys']).toContain('{"pn":"P0"}'); expect(out['quote_intake_alt_request_keys']).toContain('(还有 3 行)'); expect(out['quote_intake_alt_request_keys']).not.toContain('"P12"'); }); it('主键那一格空着就不摆那一格 —— 摆一个 "" 会让模型照抄一个撤不到的键', () => { expect(rowKeyList({ 'mine:quote_intake.record': [{ rfq_ref: '', pn: 'X' }] }, DECL)).toEqual({ quote_intake_record_keys: '{"pn":"X"}', }); }); it('整行主键都空 → 那一行不进名单,一行都没有就不出这个读数', () => { expect(rowKeyList({ 'mine:quote_intake.alt_request': [{ pn: '' }] }, DECL)).toEqual({}); }); it('没有主键声明的表不出名单 —— 认不出「一行是谁」就别硬凑', () => { const noKey = [{ domain: 'd', object: 'o', fields: [{ name: 'x', primaryKey: false }] }]; expect(rowKeyList({ 'mine:d.o': [{ x: 1 }] }, noKey)).toEqual({}); }); }); /** * 缺必填读数 —— **让机检判据读得到基座数出来的数**。 * * A1 的「必填齐(PN / 年用量)」是 L1 机检,从前读的却是活动自己写的 `missing_key_total`: * 人在卡上补了年用量、基座已经写回草稿层,而那个汇总列还要模型自己重算 —— * 它漏算一次这条判据就永远绿不了(连跑十轮里三轮死在这,每次连拒五拍熔断)。 * **活动自报的数不是门,是一句「改哪个数能过」的提示。** */ describe('缺必填读数:域_对象_missing', () => { const DECL = [ { domain: 'quote_intake', object: 'record', fields: [ { name: 'pn', primaryKey: true, required: false }, { name: 'eau', primaryKey: false, required: true, label: '年用量' }, // 选填但缺了要问一句 —— 它进另一组读数 `_gaps` { name: 'brand', primaryKey: false, expect: true, label: '制造商' }, ], }, ]; it('缺必填的行数由基座现数,并说清缺在哪几行、缺什么', () => { const out = missingReadings( { 'mine:quote_intake.record': [{ pn: 'A', eau: 30000 }, { pn: 'B', eau: '' }, { pn: 'C' }] }, DECL, ); expect(out['quote_intake_record_missing']).toBe(2); // 缺口摆**中文列名**:这个数会进拒回与卡面,给人看的地方不摆列名 expect(out['quote_intake_record_missing_keys']).toBe('B 缺年用量;C 缺年用量'); }); it('**补齐了就归零** —— 人在卡上填回来那一刻,判据自己就绿了', () => { const out = missingReadings({ 'mine:quote_intake.record': [{ pn: 'A', eau: 30000 }] }, DECL); expect(out['quote_intake_record_missing']).toBe(0); expect('quote_intake_record_missing_keys' in out).toBe(false); }); it('一行没写也是 0 —— 零条也是一个数(与 `_rows` 铺 0 同一口径)', () => { // 两组都铺 0:必填与选填各算各的 expect(missingReadings({}, DECL)).toEqual({ quote_intake_record_missing: 0, quote_intake_record_gaps: 0 }); }); /* * **选填缺口另算一组**(2026-08-02 用户实测:「补了目标价为啥 L4 还在那乱判」)。 * 必填缺一条都不许放行;选填缺不拦单,但那一问不能省 —— 两件事两个数。 * 从前选填那个数由活动自报,人补齐了它也不会变,于是 L4 照旧裁「不满足」。 */ it('选填缺口进 `_gaps`,与必填的 `_missing` 各算各的', () => { const rows = [{ pn: 'A', eau: 1, brand: '' }, { pn: 'B', eau: '', brand: 'X' }]; const out = missingReadings({ 'mine:quote_intake.record': rows }, DECL); expect(out['quote_intake_record_missing']).toBe(1); expect(out['quote_intake_record_gaps']).toBe(1); expect(out['quote_intake_record_gaps_keys']).toBe('A 缺制造商'); }); it('**人补齐那一格,这个数当场归零** —— 模型改不动它,所以裁得准', () => { const out = missingReadings({ 'mine:quote_intake.record': [{ pn: 'A', eau: 1, brand: 'X' }] }, DECL); expect(out['quote_intake_record_gaps']).toBe(0); expect('quote_intake_record_gaps_keys' in out).toBe(false); }); it('没有一列标 required 的表不出这个读数 —— 没声明就不该假装数得出来', () => { const noReq = [{ domain: 'd', object: 'o', fields: [{ name: 'x', primaryKey: true }] }]; expect(missingReadings({ 'mine:d.o': [{}] }, noReq)).toEqual({}); }); it('空白串按缺算 —— 与写入归型那一侧的空值口径一致', () => { const out = missingReadings({ 'mine:quote_intake.record': [{ pn: 'A', eau: ' ' }] }, DECL); expect(out['quote_intake_record_missing']).toBe(1); }); }); /** * 按值现数 —— **手工修改的全部意义就是纠正 AI 的判断**。 * * 三分类条数从前是活动自己写的 `type_a_total` 之流,而分布条、放行时的去向谓词、 * 验收判据读的全是它们。于是人在放行页把九条料全改成 A:类徽变了, * 分布条还是 5/3/1、去向还是「去选型」—— 逐行数据变了,汇总数没人重算。 * **改了类标而流向不跟着变,那个兜底功能就是假的。** */ describe('按值现数:域_对象_列_值', () => { const DECL = [ { domain: 'quote_intake', object: 'record', fields: [{ name: 'pn' }, { name: 'type_tag', tally: ['A', 'B', 'C'] }], }, ]; it('照取值域逐个数,值消毒成小写', () => { const rows = [{ type_tag: 'A' }, { type_tag: 'A' }, { type_tag: 'B' }, { type_tag: 'C' }]; expect(tallyReadings({ 'mine:quote_intake.record': rows }, DECL)).toEqual({ quote_intake_record_type_tag_a: 2, quote_intake_record_type_tag_b: 1, quote_intake_record_type_tag_c: 1, }); }); it('**人全改成 A → C 类归零**,去向谓词 `…_type_tag_c > 0` 当场变假', () => { const rows = [{ type_tag: 'A' }, { type_tag: 'A' }, { type_tag: 'A' }]; const out = tallyReadings({ 'mine:quote_intake.record': rows }, DECL); expect(out['quote_intake_record_type_tag_a']).toBe(3); expect(out['quote_intake_record_type_tag_c']).toBe(0); }); it('没出现的值也铺 0 —— 零条也是一个数,判据才算得下去', () => { expect(tallyReadings({ 'mine:quote_intake.record': [] }, DECL)).toEqual({ quote_intake_record_type_tag_a: 0, quote_intake_record_type_tag_b: 0, quote_intake_record_type_tag_c: 0, }); }); it('没声明取值域的列不出读数 —— 数不出取值域就别假装数得全', () => { expect(tallyReadings({ 'mine:d.o': [{ x: 'A' }] }, [{ domain: 'd', object: 'o', fields: [{ name: 'x' }] }])).toEqual({}); }); it('值两边的空白不算数上的差别', () => { expect( tallyReadings({ 'mine:quote_intake.record': [{ type_tag: ' A ' }] }, DECL)['quote_intake_record_type_tag_a'], ).toBe(1); }); }); /** * **工具调用读数** —— 「机检只读基座数」这条纪律第一次覆盖到工具(2026-08-03)。 * * 起因:A2 判稳偶发压根不调行情工具就把料判成「稳」转 A,而自报的那一列写得漂亮, * 去向就此走岔(约十次一次)。工具出参本来就入痕,「调没调」是基座数得出来的事实。 */ describe('工具调用读数', () => { const declared = [{ name: 'crm.history-price-read' }, { name: 'mail.send' }]; it('声明过的工具一律先铺 0 —— 没调过与调了 0 次在业务上是同一件事', () => { expect(toolCallReadings([], declared)).toEqual({ tool_crm_history_price_read_calls: 0, tool_mail_send_calls: 0, }); }); it('数成功返回的那几次,名字里的点号横杠一律换成下划线(S-FEEL 不认)', () => { const trail = [ { kind: 'result', title: 'crm.history-price-read 返回' }, { kind: 'result', title: 'crm.history-price-read 返回(3 行)' }, { kind: 'thought', title: 'crm.history-price-read 返回' }, ]; expect(toolCallReadings(trail, declared).tool_crm_history_price_read_calls).toBe(2); }); it('**调失败不算查过** —— 调不通等于没拿到行情,不该拿来当依据', () => { const trail = [{ kind: 'result', title: 'crm.history-price-read 失败:端点不可达' }]; expect(toolCallReadings(trail, declared).tool_crm_history_price_read_calls).toBe(0); }); it('没声明的工具不数 —— 声明面就是授权面,不在名单里的本来也数不着', () => { const trail = [{ kind: 'result', title: 'erp.write 返回' }]; expect(Object.keys(toolCallReadings(trail, declared))).toEqual([ 'tool_crm_history_price_read_calls', 'tool_mail_send_calls', ]); }); }); /** * 不同值计数 `域_对象_distinct_<列>` —— 覆盖闭合判据的落点(2026-08-24 柴浩实测: * 人在覆盖范围确认卡上加了一家客户,计划行数照样 > 0,「盖没盖住每一家」没有数说得出来)。 */ describe('distinctReadings —— 这一列有几个不同的值,基座数', () => { const DECL = [ { domain: 'crm_coverage', object: 'plan', fields: [{ name: 'customer_code', distinct: true }, { name: 'contact_code' }], }, ]; it('计划行数 ≠ 客户数:一家客户几位关键人就几行,distinct 数的是客户', () => { const rows = [ { customer_code: 'CU-0142', contact_code: 'CU-0142-01' }, { customer_code: 'CU-0142', contact_code: 'CU-0142-02' }, { customer_code: 'cu-1111', contact_code: 'cu-1111-00' }, ]; expect(distinctReadings({ 'mine:crm_coverage.plan': rows }, DECL)).toEqual({ crm_coverage_plan_distinct_customer_code: 2, }); }); it('没标 distinct 的列不出读数;标了的零行也铺 0 —— 未绑定的名字参与比较求值为假', () => { const out = distinctReadings({ 'mine:crm_coverage.plan': [] }, DECL); expect(out).toEqual({ crm_coverage_plan_distinct_customer_code: 0 }); }); it('空值不算一种 —— 「没写」不是一个取值', () => { const rows = [{ customer_code: 'CU-0142' }, { customer_code: '' }, { customer_code: ' ' }, {}]; expect(distinctReadings({ 'mine:crm_coverage.plan': rows }, DECL)).toEqual({ crm_coverage_plan_distinct_customer_code: 1, }); }); }); /** * `touched:域.对象` —— 落库预览的数据源(2026-08-24 柴浩实测:绑 mine: 的话 * Itelma 既有的客户类型/行业整片空白,像是数据丢了)。 */ describe('touchedRows —— 本轮碰过的行,以叠加后全貌摆出来', () => { const merged = [ { customer_code: 'CU-0142', name: 'Itelma', cust_type: 'Tier1 OEM', industry: '车规电子' }, { customer_code: 'CU-0087', name: '深圳精密', cust_type: '模组厂' }, { customer_code: 'CU-2039', name: '测试11111' }, ]; it('按草稿键挑行:碰过的行带着正式现值的全部列,没碰的行不出现', () => { const out = touchedRows(merged, [{ customer_code: 'CU-0142' }, { customer_code: 'CU-2039' }]); expect(out).toHaveLength(2); // 关键在这一列:草稿里没写过 cust_type,叠加行里必须有 —— 这正是回显的来源 expect((out[0] as Record)['cust_type']).toBe('Tier1 OEM'); expect((out[1] as Record)['name']).toBe('测试11111'); }); it('草稿新增的行(正式表没有)也在 —— 叠加视图本就含它;键对不上的丢弃不报错', () => { const withNew = [...merged, { customer_code: 'CU-NEW', contact_code: 'CU-NEW-01', name: '新人' }]; const out = touchedRows(withNew, [ { customer_code: 'CU-NEW', contact_code: 'CU-NEW-01' }, { customer_code: '查无此人', contact_code: 'X' }, ]); expect(out).toHaveLength(1); expect((out[0] as Record)['name']).toBe('新人'); }); it('数值键与字符串键按字面等值比(pg 回来的是 number,草稿键常是 string)', () => { const out = touchedRows([{ seq: 3, name: '三号' }], [{ seq: '3' }]); expect(out).toHaveLength(1); }); }); /** * 摆给人看的行标识去掉单标识(2026-08-25 用户:「COV-…/CU-0142-01 只保留斜杠右边」)。 */ describe('rowLabel —— 主键去掉单号那一段', () => { it('单据表:round_ref + contact_code → 只留 contact_code', () => { expect(rowLabel({ round_ref: 'COV-20260825002227', contact_code: 'CU-0142-01' }, 'round_ref')).toBe('CU-0142-01'); }); it('主数据表没有单标识:全部保留', () => { expect(rowLabel({ customer_code: 'CU-0142', contact_code: 'CU-0142-01' }, undefined)).toBe('CU-0142/CU-0142-01'); }); it('主键只有单号:去完一段都不剩就退回整段,不摆空串', () => { expect(rowLabel({ round_ref: 'COV-1' }, 'round_ref')).toBe('COV-1'); }); it('缺项名单走同一把尺 —— 红带 / 提交闸 / 拒回都不再带单号', () => { const out = missingReadings({ 'mine:crm_coverage.receipt': [{ round_ref: 'COV-1', contact_code: 'CU-0142-01' }] }, [ { domain: 'crm_coverage', object: 'receipt', caseKey: 'round_ref', fields: [ { name: 'round_ref', primaryKey: true }, { name: 'contact_code', primaryKey: true }, { name: 'completion_status', primaryKey: false, required: true, label: '完成状态' }, ], }, ]); expect(out['crm_coverage_receipt_missing_keys']).toBe('CU-0142-01 缺完成状态'); }); });