mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-22 18:01:58 +00:00
M3 复核修正(第 1 轮):单位带尾随空白的真报告不再被判不通过;运行输入也核对显示文字;出处面板的长整数一位不差;核对通过的字要原样显示在正文里
- 显示文字比对两边都只去首尾空白:口径卡单位写成「元 」(单位输入框不去空白、指标节点照存)时,服务端照原样接在数 后面,正文是「74,613.65元 」。以前只去正文一边,原样导出的真报告被判不通过,出处面板里两行字看起来一模一样,读者 会以为文件被改过 - 运行输入([[i:…]])不随文件附带,照旧无法核对;正文显示的字拿引用的值按服务端的写法(不分组、不接单位、可带换算) 重新渲染比对,只改显示文字(统计周期、地区这类)的判不通过 - 出处面板:DECIMAL 落成数字文本的格子,「快照里的值」照快照原文写;快照那一行的数照 JSON 原文写。超过 2^53 的订单号 不再在「通过」的条目里和一位不差的「引用的值」并排、自相矛盾 - 正文的排法核对和显示共用一套函数(unitPlan / flowsOf / checkLayout),读的人看到的数要就是核对过的那个:伪造的链接 把数字塞进不显示的链接地址时,整句按未排版的原文显示;正文里根本不显示的(分隔线里、表格行列之外的格)服务端导出时 不会出现,判不通过;紧挨着补数字、负号(隔着零宽字符、显示时藏起来的排版标记也算)或同一句里有方向控制字符的判无法 核对,两个数之间的区间连字符照常通过。正文按片段本身找核对结果:文字片段冒用数字的 id 借不到「通过」(以前会显示成 绿色下划线的可点数字)。真数据 71 份报告文档、3266 处已解析的数字和取值里,新规则一处都没有触发 - 「无法核对显示文字」的几条原因改成读者看得懂、知道怎么办的说法,不再提 BigInt、正则、ASCII - check-bundle:真导出上断言没有「无法核对显示文字」(显示规则漏了真实写法时这里先报);订单号连同快照那一行一位不差; 新样本页 formats-unit(单位带尾随空白,原样导出全部通过);经营月报上链接藏数、补一位数、整段改成分隔线、文字片段冒用 数字的 id 四种伪造各自断言
This commit is contained in:
+261
-93
@@ -11,7 +11,9 @@
|
||||
* ⑤ 报告里的每个数字和取值:引用的值等于口径卡里那个指标的值,或者快照里那一格的值(判等照搬服务端
|
||||
* engine/expressions.py 的 cell_value / same_value);正文显示的字等于按服务端的显示规则(engine/evidence.py 的
|
||||
* render_number / render_cell:千分位、小数位、单位、万 / 亿换算、百分数,文字压成一行)把那个值渲染出来的字,
|
||||
* 只去掉首尾空白。本页渲染不出来的写法判无法核对,不判通过;
|
||||
* 两边都只去掉首尾空白。本页渲染不出来的写法判无法核对,不判通过。运行输入不随文件附带,照旧无法核对,但显示的字
|
||||
* 照样拿引用的值核对,对不上判不通过。核对通过的字要原样显示在正文里(checkLayout):正文里根本不显示的判不通过,
|
||||
* 紧挨着还有数字或负号、读起来不是这个值的判无法核对;
|
||||
* ⑥ 运行信息:运行结果、运行类别、出具档位拿封存范围内已核对的事件对照。工作流名、版本、起止时间、节点名称
|
||||
* 绑不进封存,页面上写明它们取自运行记录。
|
||||
* 每项给出通过 / 不通过 / 无法核对和原因。服务端导出时不会出现的状态(被引用的工件不在清单里、报告文档标着遮罩)
|
||||
@@ -163,13 +165,17 @@
|
||||
shownZero: '按显示规则会显示为 0',
|
||||
shownTooLong: '数值位数超出可显示的范围',
|
||||
shownNotFinite: '不是有限的数值',
|
||||
shownInexact: '数值超出浏览器能精确表示的范围,文件里也找不到它的原文',
|
||||
shownInexact: '数值位数过多,本页无法精确核对',
|
||||
shownUnit: '口径卡记下的单位不是文字',
|
||||
shownShape: '值是对象或列表,本页无法按系统的显示规则重新渲染',
|
||||
shownDigits: '数字文本中含非 ASCII 数字,本页无法按系统的显示规则重新渲染',
|
||||
shownShape: '这个值是结构化数据,本页无法按系统的显示规则核对',
|
||||
shownDigits: '数字里有全角或其他文字的数字,本页无法按系统的显示规则核对',
|
||||
shownDisplay: (v) => `报告文档记下的单元格显示规则版本「${v}」无法识别`,
|
||||
shownOld: '这个浏览器不支持按系统的显示规则重新渲染(缺少 BigInt 或 Unicode 正则)',
|
||||
shownBroken: '重新渲染时出错',
|
||||
shownOld: '这个浏览器版本较旧,请用新版浏览器打开本文件',
|
||||
shownBroken: '本页按系统的显示规则处理这个值时出错',
|
||||
/** 正文的排法(checkLayout):显示文字核对过了,读起来却不是这个值 */
|
||||
shownGlued: '紧挨着的正文里还有数字或负号,读起来和核对过的不是同一个值',
|
||||
shownTurned: '这一句里有改变文字方向的控制字符,显示出来的顺序可能和核对过的不一样',
|
||||
numNotShown: '正文里没有显示这一处:导出时不会出现这种情况,文件可能被改过',
|
||||
numAria: (kind, text, result) => `${kind === 'value' ? '取值' : '数字'} ${text},${result},查看出处`,
|
||||
statusOk: (seq, what) => `与第 ${seq} 条事件(${what})一致`,
|
||||
statusMismatch: (claim, seq, what) => `文件里写的是「${claim}」,与封存范围内最后一条结束类事件(第 ${seq} 条,${what})不符`,
|
||||
@@ -199,7 +205,8 @@
|
||||
shaSubtle: 'SHA-256 由浏览器内置实现计算(crypto.subtle)',
|
||||
shaJs: 'SHA-256 由本页内置的实现计算(浏览器未提供 crypto.subtle),结果与浏览器内置实现一致',
|
||||
renderNote: '正文里带出处的数字和取值,本页按系统的显示规则(千分位、小数位、单位、万 / 亿换算、百分数,文字压成一行)'
|
||||
+ '把口径卡或快照里的值重新渲染一遍,与显示的文字逐字比对。',
|
||||
+ '把口径卡或快照里的值(运行输入按引用的值)重新渲染一遍,与显示的文字逐字比对;核对通过的字在正文里原样显示,'
|
||||
+ '紧挨着它还有数字或负号的判无法核对。',
|
||||
schema: (s) => `文件格式 ${s}`,
|
||||
}
|
||||
|
||||
@@ -674,6 +681,207 @@
|
||||
return typeof node === 'string' ? node : null
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 正文的排法:核对和显示共用
|
||||
//
|
||||
// 正文怎样排,核对时就怎样算(不碰页面,node 里也算得了):一个数字、取值在正文里显示成什么字、紧挨着它的是什么字,
|
||||
// 都按这一节的函数算;显示时(renderBlock / renderUnit)也走它们,两边不会各算各的。
|
||||
|
||||
// 行内语法:代码、链接(只显示文字,不给链接:页面不连网)、粗体、斜体、删除线,和前端 run/Markdown.tsx 的规则一致。
|
||||
// 斜体要求左边不是 *(否则 **粗体** 会被它先拆掉):前端用后行断言,这里不用(较早的 Safari 不认,构造正则时
|
||||
// 就会报错,整页核对跟着失败),匹配到以后自己看左边那个字
|
||||
const INLINE = '(`+)([^`]+?)\\1|\\[([^\\]]+)\\]\\(([^)\\s]+)[^)]*\\)|\\*\\*([^*]+?)\\*\\*|__([^_]+?)__'
|
||||
+ '|\\*([^*\\n]+?)\\*(?!\\*)|~~([^~]+?)~~'
|
||||
const BIT = { code: 1, bold: 2, strike: 4, link: 8, em: 16 }
|
||||
// 列表项的开头:行首的 -、*、+ 或者序号
|
||||
const ITEM_START = /(^|\n)[ \t]*([-*+]|\d+[.)])[ \t]+$/
|
||||
|
||||
/** 整句算一次行内样式:服务端把一句切成了好几个片段,粗体、代码可能跨片段,每个片段取自己那一截 */
|
||||
function inlineStyles(text) {
|
||||
const hide = new Uint8Array(text.length)
|
||||
const style = new Uint8Array(text.length)
|
||||
const walk = (from, to, inherited, depth) => {
|
||||
const part = text.slice(from, to)
|
||||
const re = new RegExp(INLINE, 'g')
|
||||
let last = 0
|
||||
for (let m = re.exec(part); m; m = re.exec(part)) {
|
||||
const [whole, ticks, code, linkText, , bold1, bold2, italic, strike] = m
|
||||
if (italic !== undefined && m.index > 0 && part[m.index - 1] === '*') {
|
||||
// 左边紧挨着 *:不算斜体(前端后行断言排除的情形),从下一个字接着找
|
||||
re.lastIndex = m.index + 1
|
||||
continue
|
||||
}
|
||||
for (let i = last; i < m.index; i++) style[from + i] = inherited
|
||||
const inner = code ?? linkText ?? bold1 ?? bold2 ?? italic ?? strike ?? ''
|
||||
const base = from + m.index
|
||||
const off = code !== undefined ? ticks.length : whole.indexOf(inner)
|
||||
for (let i = 0; i < whole.length; i++) hide[base + i] = 1
|
||||
for (let i = 0; i < inner.length; i++) hide[base + off + i] = 0
|
||||
const bit = code !== undefined ? BIT.code : linkText !== undefined ? BIT.link
|
||||
: bold1 !== undefined || bold2 !== undefined ? BIT.bold : italic !== undefined ? BIT.em : BIT.strike
|
||||
if (code !== undefined || linkText !== undefined || depth >= 3) {
|
||||
for (let i = 0; i < inner.length; i++) style[base + off + i] = inherited | bit
|
||||
} else {
|
||||
walk(base + off, base + off + inner.length, inherited | bit, depth + 1)
|
||||
}
|
||||
last = m.index + whole.length
|
||||
}
|
||||
for (let i = last; i < part.length; i++) style[from + i] = inherited
|
||||
}
|
||||
walk(0, text.length, 0, 0)
|
||||
return { hide, style }
|
||||
}
|
||||
|
||||
/** 列表的各项:一项从行首写着列表符号或序号的结构片段开始,后面的句子接在这一项里 */
|
||||
function listItems(units) {
|
||||
const items = []
|
||||
for (const u of units) {
|
||||
const lead = Array.isArray(u.segments) ? u.segments[0] : null
|
||||
const starts = !items.length || (isObj(lead) && lead.kind === 'structural' && ITEM_START.test(String(lead.text ?? '')))
|
||||
if (starts) items.push({ depth: Number(u.depth) || 0, units: [u] })
|
||||
else items[items.length - 1].units.push(u)
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
/** 表格:{ cells(位置 → 句子,一格可能被分成几句,按位置归拢), rows(表身各行的行号,表头是 -1,另算), cols } */
|
||||
function tableCells(units) {
|
||||
const cells = new Map()
|
||||
const rows = []
|
||||
let cols = 0
|
||||
for (const u of units) {
|
||||
const loc = isObj(u.loc) ? u.loc : { row: 0, col: 0 }
|
||||
push(cells, `${loc.row}:${loc.col}`, u)
|
||||
cols = Math.max(cols, (Number(loc.col) || 0) + 1)
|
||||
if (loc.row >= 0 && !rows.includes(loc.row)) rows.push(loc.row)
|
||||
}
|
||||
rows.sort((a, b) => a - b)
|
||||
return { cells, rows, cols }
|
||||
}
|
||||
|
||||
/**
|
||||
* 一块里连着显示的几组句子:段落、标题、引用整块一组,列表每项一组,表格每格一组(只有表头和表身各行各列上的格
|
||||
* 显示出来)。分隔线不显示句子,代码块按原文显示、不标数字:都交回空的
|
||||
*/
|
||||
function flowsOf(block) {
|
||||
const units = Array.isArray(block.units) ? block.units.filter(isObj) : []
|
||||
switch (block.type) {
|
||||
case 'hr':
|
||||
case 'code':
|
||||
return []
|
||||
case 'list':
|
||||
return listItems(units).map((item) => item.units)
|
||||
case 'table': {
|
||||
const { cells, rows, cols } = tableCells(units)
|
||||
return [-1, ...rows].flatMap((r) => Array.from({ length: cols }, (_, c) => cells.get(`${r}:${c}`) || []))
|
||||
}
|
||||
default:
|
||||
return [units]
|
||||
}
|
||||
}
|
||||
|
||||
const plans = new WeakMap()
|
||||
|
||||
/**
|
||||
* 一句怎样显示:{ segs(结构片段以外的片段), text(连起来的字), styles(行内样式), at(每个片段在 text 里的起止),
|
||||
* raw }。数字、取值片段里有字会被行内标记藏起来时(伪造的链接把它塞进不显示的链接地址、取值里本身带 * 这类),整句
|
||||
* 按未排版的原文显示(raw):核对过的字一个不少地露出来,读的人不会只看到旁边伪造的那个数。核对和显示各算一次会是
|
||||
* 同一个结果,算过的记下来
|
||||
*/
|
||||
function unitPlan(unit) {
|
||||
let plan = plans.get(unit)
|
||||
if (plan) return plan
|
||||
const segs = (Array.isArray(unit.segments) ? unit.segments : []).filter((s) => isObj(s) && s.kind !== 'structural')
|
||||
const at = []
|
||||
let text = ''
|
||||
for (const seg of segs) {
|
||||
const piece = String(seg.text ?? '')
|
||||
at.push([text.length, text.length + piece.length])
|
||||
text += piece
|
||||
}
|
||||
let styles = inlineStyles(text)
|
||||
const raw = segs.some((seg, i) => (seg.kind === 'number' || seg.kind === 'value')
|
||||
&& styles.hide.subarray(at[i][0], at[i][1]).some((x) => x === 1))
|
||||
if (raw) styles = { hide: new Uint8Array(text.length), style: new Uint8Array(text.length) }
|
||||
plan = { segs, text, styles, at, raw }
|
||||
plans.set(unit, plan)
|
||||
return plan
|
||||
}
|
||||
|
||||
// 判断紧挨着的字:数字(Python 的 \d 认的全都算,全角的、数学字母区的也是)、负号(半角、全角、小号、数学减号);
|
||||
// 看不见的字(零宽空格这类格式字符、组合附加符号)跳过,隔着它们照样算紧挨着。改变文字方向的控制字符另算:它能把
|
||||
// 后面的一位数挪到数字前面显示,按字的先后看不出来
|
||||
const DIGIT = unicode('^\\p{Nd}$') || /^[0-9\uff10-\uff19]$/
|
||||
const MINUS = /^[-\u2212\ufe63\uff0d]$/
|
||||
const INVISIBLE = unicode('^[\\p{Cf}\\p{Mn}\\p{Me}]$')
|
||||
|| /^[\u00ad\u0300-\u036f\u200b-\u200f\u202a-\u202e\u2060-\u2064\ufeff]$/
|
||||
const BIDI = /^[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]$/
|
||||
|
||||
/**
|
||||
* ⑤ 的最后一步:核对通过的数字、取值,读的人在正文里看到的就该是核对过的那串字。items 是这份报告的 片段 → 核对结果,
|
||||
* 每一项记下正文里显示出来的字(onPage)。
|
||||
* - 正文里根本不显示它(分隔线里、表格里不在各行各列上的格):伪造的字可以写在别处,读的人看不到核对过的那个数。
|
||||
* 服务端导出时不会出现(分隔线不带句子,表格的格都在行列上),判不通过;
|
||||
* - 紧挨着的字里还有数字或负号(前面补一位、后面接一位或一段小数、前面加负号;隔着看不见的字、显示时藏起来的排版
|
||||
* 标记也算紧挨着),或者同一句里有改变文字方向的控制字符:读起来的数不止核对过的这几位,判无法核对——真报告里
|
||||
* 也可能有(放行的数字、日期紧贴着引用),不能说成被改过。两个数之间的连字符是区间,不算负号。
|
||||
* 代码块按原文显示、不标数字,照旧
|
||||
*/
|
||||
function checkLayout(doc, items) {
|
||||
const numeric = (seg) => seg.kind === 'number' || seg.kind === 'value'
|
||||
for (const block of Array.isArray(doc.blocks) ? doc.blocks.filter(isObj) : []) {
|
||||
if (block.type === 'code') {
|
||||
for (const unit of Array.isArray(block.units) ? block.units.filter(isObj) : []) {
|
||||
for (const seg of unitPlan(unit).segs) if (items.has(seg)) items.get(seg).onPage = String(seg.text ?? '')
|
||||
}
|
||||
continue
|
||||
}
|
||||
for (const flow of flowsOf(block)) {
|
||||
// 这一组显示出来的字(按码点:数学字母区的数字占两个 UTF-16 单元),逐个记下属于哪个片段:藏起来的不显示,
|
||||
// 看不见的不参与判断。可点的数字自成一块,它里面的字改变不了外面的方向,只看别的片段里的方向控制字符
|
||||
const chars = []
|
||||
let turned = false
|
||||
for (const unit of flow) {
|
||||
const { segs, text, styles, at } = unitPlan(unit)
|
||||
segs.forEach((seg, i) => {
|
||||
let shown = ''
|
||||
for (let k = at[i][0]; k < at[i][1]; k++) {
|
||||
if (styles.hide[k]) continue
|
||||
let c = text[k]
|
||||
const code = text.charCodeAt(k)
|
||||
if (code >= 0xd800 && code < 0xdc00 && k + 1 < at[i][1]) c = text.slice(k, ++k + 1)
|
||||
shown += c
|
||||
if (BIDI.test(c) && !items.has(seg)) turned = true
|
||||
if (!INVISIBLE.test(c)) chars.push([c, seg])
|
||||
}
|
||||
if (items.has(seg)) items.get(seg).onPage = shown
|
||||
})
|
||||
}
|
||||
const ch = (k) => (k >= 0 && k < chars.length ? chars[k][0] : '')
|
||||
const digit = (k) => DIGIT.test(ch(k))
|
||||
const inNumber = (k) => k >= 0 && k < chars.length && numeric(chars[k][1])
|
||||
const spans = new Map()
|
||||
chars.forEach(([, seg], k) => {
|
||||
if (spans.has(seg)) spans.get(seg)[1] = k
|
||||
else spans.set(seg, [k, k])
|
||||
})
|
||||
for (const [seg, item] of items) {
|
||||
if (item.result !== PASS || !spans.has(seg)) continue
|
||||
const [first, last] = spans.get(seg)
|
||||
const before = digit(first) && (digit(first - 1) || (/^[.,]$/.test(ch(first - 1)) && digit(first - 2)))
|
||||
const sign = (digit(first) || MINUS.test(ch(first))) && MINUS.test(ch(first - 1))
|
||||
&& !digit(first - 2) && !inNumber(first - 2)
|
||||
const after = digit(last) && (digit(last + 1) || (/^[.,]$/.test(ch(last + 1)) && digit(last + 2)))
|
||||
if (turned || before || sign || after) {
|
||||
Object.assign(item, { result: NA, reason: T.numShownUnknown(turned ? T.shownTurned : T.shownGlued) })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const item of items.values()) {
|
||||
if (item.onPage === undefined && item.result === PASS) Object.assign(item, { result: FAIL, reason: T.numNotShown })
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 核对
|
||||
|
||||
function push(map, key, value) {
|
||||
@@ -897,6 +1105,8 @@
|
||||
: { result: NA, reason: T.numDocUntrusted, source: describe(seg, doc) }
|
||||
out.numbers.push({ ...check, seg, report, key: `${report.node_id}|${seg.id}` })
|
||||
}
|
||||
// 核对通过的,还要看读的人在正文里看到的是不是这串字
|
||||
if (trusted.has(report)) checkLayout(doc, new Map(out.numbers.filter((n) => n.report === report).map((n) => [n.seg, n])))
|
||||
}
|
||||
|
||||
// ⑥ 运行信息
|
||||
@@ -927,7 +1137,14 @@
|
||||
const source = describe(seg, doc)
|
||||
if (!cite) return { result: NA, reason: T.numUncited, source }
|
||||
if (cite.status !== 'resolved') return { result: NA, reason: T.numUnresolved(cite.reason), source }
|
||||
if (cite.kind === 'input') return { result: NA, reason: T.numInput, source }
|
||||
if (cite.kind === 'input') {
|
||||
// 运行输入不随文件附带,引用的值核对不了,照旧无法核对。正文显示的字照样拿引用的值按服务端的写法(render_number
|
||||
// 不分组、不接单位,可带换算)重新渲染比对:只改显示文字(统计周期、地区这类)的伪造在这里露馅,判不通过
|
||||
const info = { source, kind: cite.kind, cite, citedToken: citedToken() }
|
||||
const shown = checkShown(info, seg, () => renderNumber(cite.value, info.citedToken,
|
||||
{ fmt: 'plain', conv: convOf(cite) }))
|
||||
return shown.result === FAIL ? shown : { ...shown, result: NA, reason: T.numInput }
|
||||
}
|
||||
if (cite.kind !== 'metric' && cite.kind !== 'cell') return { result: NA, reason: T.numOther, source }
|
||||
const entry = own(doc.catalog, cite.alias)
|
||||
const info = { source, kind: cite.kind, cite }
|
||||
@@ -970,6 +1187,9 @@
|
||||
info.column = column
|
||||
info.truth = cellValue(raw, columnKind(art.json, column))
|
||||
info.truthToken = tokenAt(tokensOf(art), ['rows', loc.row, key])
|
||||
// 落成数字文本的(DECIMAL)按数比对,出处面板照快照的原文写:读成 double 后超过 2^53 的订单号会丢位,
|
||||
// 和一位不差的「引用的值」并排时自相矛盾
|
||||
if (typeof raw === 'string' && typeof info.truth === 'number') info.truthText = pyStrip(raw)
|
||||
cell = { raw, column }
|
||||
} catch (e) {
|
||||
return { ...info, result: FAIL, reason: T.numNoCell(e.message) }
|
||||
@@ -998,8 +1218,10 @@
|
||||
const exactText = (value, token) => (typeof value === 'number' ? numberText(value, token) : valueText(value))
|
||||
|
||||
/**
|
||||
* 正文显示的字和按显示规则渲染出来的字逐字比对,只去掉首尾空白。渲染不出来的判无法核对:不认识的格式不能当成核对过了。
|
||||
* 对不上的点名两样:正文显示的字、引用的值(加上按显示规则应显示的字,和引用的值写法一样时不写)
|
||||
* 正文显示的字和按显示规则渲染出来的字逐字比对,两边都只去掉首尾空白:口径卡的单位带尾随空白(「元 」)时,服务端照原样
|
||||
* 接在数后面,正文和渲染出来的都是「74,613.65元 」,只去一边的话原样导出的报告也对不上。渲染不出来的判无法核对:
|
||||
* 不认识的格式不能当成核对过了。对上了记 shownOk(出处面板据此注明一致);对不上的点名两样:正文显示的字、引用的值
|
||||
* (加上按显示规则应显示的字,和引用的值写法一样时不写)
|
||||
*/
|
||||
function checkShown(info, seg, render) {
|
||||
let expected
|
||||
@@ -1010,7 +1232,7 @@
|
||||
}
|
||||
info.expected = expected
|
||||
const shown = String(seg.text ?? '')
|
||||
if (pyStrip(shown) === expected) return { ...info, result: PASS }
|
||||
if (pyStrip(shown) === pyStrip(expected)) return { ...info, result: PASS, shownOk: true }
|
||||
const value = info.cite.value
|
||||
const cited = typeof value === 'number' ? numberText(value, info.citedToken) : value == null ? null : textOf(value)
|
||||
return { ...info, result: FAIL, reason: T.numShownDiffers(shown, cited == null ? T.nullValue : `「${cited}」`,
|
||||
@@ -1148,51 +1370,6 @@
|
||||
|
||||
// ------------------------------------------------------------------ 渲染:报告正文
|
||||
|
||||
// 行内语法:代码、链接(只显示文字,不给链接:页面不连网)、粗体、斜体、删除线,和前端 run/Markdown.tsx 的规则一致。
|
||||
// 斜体要求左边不是 *(否则 **粗体** 会被它先拆掉):前端用后行断言,这里不用(较早的 Safari 不认,构造正则时
|
||||
// 就会报错,整页核对跟着失败),匹配到以后自己看左边那个字
|
||||
const INLINE = '(`+)([^`]+?)\\1|\\[([^\\]]+)\\]\\(([^)\\s]+)[^)]*\\)|\\*\\*([^*]+?)\\*\\*|__([^_]+?)__'
|
||||
+ '|\\*([^*\\n]+?)\\*(?!\\*)|~~([^~]+?)~~'
|
||||
const BIT = { code: 1, bold: 2, strike: 4, link: 8, em: 16 }
|
||||
// 列表项的开头:行首的 -、*、+ 或者序号
|
||||
const ITEM_START = /(^|\n)[ \t]*([-*+]|\d+[.)])[ \t]+$/
|
||||
|
||||
/** 整句算一次行内样式:服务端把一句切成了好几个片段,粗体、代码可能跨片段,每个片段取自己那一截 */
|
||||
function inlineStyles(text) {
|
||||
const hide = new Uint8Array(text.length)
|
||||
const style = new Uint8Array(text.length)
|
||||
const walk = (from, to, inherited, depth) => {
|
||||
const part = text.slice(from, to)
|
||||
const re = new RegExp(INLINE, 'g')
|
||||
let last = 0
|
||||
for (let m = re.exec(part); m; m = re.exec(part)) {
|
||||
const [whole, ticks, code, linkText, , bold1, bold2, italic, strike] = m
|
||||
if (italic !== undefined && m.index > 0 && part[m.index - 1] === '*') {
|
||||
// 左边紧挨着 *:不算斜体(前端后行断言排除的情形),从下一个字接着找
|
||||
re.lastIndex = m.index + 1
|
||||
continue
|
||||
}
|
||||
for (let i = last; i < m.index; i++) style[from + i] = inherited
|
||||
const inner = code ?? linkText ?? bold1 ?? bold2 ?? italic ?? strike ?? ''
|
||||
const base = from + m.index
|
||||
const off = code !== undefined ? ticks.length : whole.indexOf(inner)
|
||||
for (let i = 0; i < whole.length; i++) hide[base + i] = 1
|
||||
for (let i = 0; i < inner.length; i++) hide[base + off + i] = 0
|
||||
const bit = code !== undefined ? BIT.code : linkText !== undefined ? BIT.link
|
||||
: bold1 !== undefined || bold2 !== undefined ? BIT.bold : italic !== undefined ? BIT.em : BIT.strike
|
||||
if (code !== undefined || linkText !== undefined || depth >= 3) {
|
||||
for (let i = 0; i < inner.length; i++) style[base + off + i] = inherited | bit
|
||||
} else {
|
||||
walk(base + off, base + off + inner.length, inherited | bit, depth + 1)
|
||||
}
|
||||
last = m.index + whole.length
|
||||
}
|
||||
for (let i = last; i < part.length; i++) style[from + i] = inherited
|
||||
}
|
||||
walk(0, text.length, 0, 0)
|
||||
return { hide, style }
|
||||
}
|
||||
|
||||
function styledSlice(text, styles, from, to) {
|
||||
const frag = document.createDocumentFragment()
|
||||
let i = from
|
||||
@@ -1217,19 +1394,15 @@
|
||||
.filter((s) => isObj(s) && s.kind !== 'structural').map((s) => String(s.text ?? '')).join('')
|
||||
|
||||
function renderUnit(unit, ctx) {
|
||||
const segs = (Array.isArray(unit.segments) ? unit.segments : []).filter((s) => isObj(s) && s.kind !== 'structural')
|
||||
const text = segs.map((s) => String(s.text ?? '')).join('')
|
||||
const styles = inlineStyles(text)
|
||||
const { segs, text, styles, at } = unitPlan(unit)
|
||||
const frag = document.createDocumentFragment()
|
||||
let pos = 0
|
||||
for (const seg of segs) {
|
||||
const from = pos
|
||||
pos += String(seg.text ?? '').length
|
||||
let body = styledSlice(text, styles, from, pos)
|
||||
segs.forEach((seg, i) => {
|
||||
let body = styledSlice(text, styles, at[i][0], at[i][1])
|
||||
if (seg.strong) body = h('strong', {}, body)
|
||||
const number = ctx.numbers.get(`${ctx.report.node_id}|${seg.id}`)
|
||||
// 按片段本身找它的核对结果,不按片段 id:伪造的文字片段写上和数字一样的 id,也借不到它的结果、显示不成可点的数字
|
||||
const number = ctx.numbers.get(seg)
|
||||
frag.append(number ? numberButton(number, body) : h('span', {}, body))
|
||||
}
|
||||
})
|
||||
return frag
|
||||
}
|
||||
|
||||
@@ -1250,16 +1423,9 @@
|
||||
case 'code':
|
||||
return h('pre', {}, h('code', {}, units.map(unitText).join('')))
|
||||
case 'list': {
|
||||
const items = []
|
||||
for (const u of units) {
|
||||
const lead = Array.isArray(u.segments) ? u.segments[0] : null
|
||||
const starts = !items.length || (isObj(lead) && lead.kind === 'structural' && ITEM_START.test(String(lead.text ?? '')))
|
||||
if (starts) items.push({ depth: Number(u.depth) || 0, units: [u] })
|
||||
else items[items.length - 1].units.push(u)
|
||||
}
|
||||
const list = h(block.ordered ? 'ol' : 'ul')
|
||||
if (block.ordered && Number.isInteger(block.start) && block.start !== 1) list.setAttribute('start', String(block.start))
|
||||
for (const item of items) {
|
||||
for (const item of listItems(units)) {
|
||||
const li = h('li', {}, item.units.map(inline))
|
||||
if (item.depth) li.style.marginLeft = `${item.depth * 14}px`
|
||||
list.append(li)
|
||||
@@ -1267,17 +1433,8 @@
|
||||
return list
|
||||
}
|
||||
case 'table': {
|
||||
// 表头 row = -1;一格可能被分成几句,按位置归拢
|
||||
const cells = new Map()
|
||||
const rowIds = []
|
||||
let cols = 0
|
||||
for (const u of units) {
|
||||
const loc = isObj(u.loc) ? u.loc : { row: 0, col: 0 }
|
||||
push(cells, `${loc.row}:${loc.col}`, u)
|
||||
cols = Math.max(cols, (Number(loc.col) || 0) + 1)
|
||||
if (loc.row >= 0 && !rowIds.includes(loc.row)) rowIds.push(loc.row)
|
||||
}
|
||||
rowIds.sort((a, b) => a - b)
|
||||
// 表头 row = -1;一格可能被分成几句,按位置归拢(tableCells)
|
||||
const { cells, rows: rowIds, cols } = tableCells(units)
|
||||
const textAt = (r, c) => (cells.get(`${r}:${c}`) || []).map(unitText).join('').replace(/[*`]/g, '').trim()
|
||||
const numeric = Array.from({ length: cols }, (_, c) => {
|
||||
const texts = rowIds.map((r) => textAt(r, c)).filter(Boolean)
|
||||
@@ -1331,16 +1488,18 @@
|
||||
const cite = isObj(number.seg.cite) ? number.seg.cite : null
|
||||
const card = number.card
|
||||
// 显示文字核对过的(引用的值对上了,按显示规则渲染得出来):一致时注明,对不上时另列应显示的字。没有出处的数、
|
||||
// 核对不了的引用不写:那些字没人重新渲染过
|
||||
// 核对不了的引用不写:那些字没人重新渲染过。一致不等于通过:运行输入的显示文字对得上,引用的值仍核对不了;紧挨着
|
||||
// 还有数字的,这处的字本身也对得上,读起来却不是这个值
|
||||
const checked = typeof number.expected === 'string'
|
||||
const same = checked && number.result === PASS
|
||||
const same = checked && number.shownOk === true
|
||||
const rows = [
|
||||
[T.dResult, [badge(number.result), number.reason ? ` ${number.reason}` : '']],
|
||||
[T.dSource, number.source],
|
||||
number.node ? [T.dNode, nodeLabel(number.node)] : null,
|
||||
card ? [T.dCaliber, [card.caliber || '—', card.caliber_version ? ` ${card.caliber_version}` : ''].join('')] : null,
|
||||
cite && cite.status === 'resolved' && 'value' in cite ? [T.dCited, exactText(cite.value, number.citedToken)] : null,
|
||||
'truth' in number ? [number.kind === 'metric' ? T.dCard : T.dCell, exactText(number.truth, number.truthToken)] : null,
|
||||
'truth' in number ? [number.kind === 'metric' ? T.dCard : T.dCell,
|
||||
number.truthText ?? exactText(number.truth, number.truthToken)] : null,
|
||||
checked && !same ? [T.dExpected, number.expected] : null,
|
||||
[T.dShown, [String(number.seg.text ?? ''), same ? h('span', { class: 'muted' }, `(${T.dShownSame})`) : null]],
|
||||
number.artifact ? [T.dArtifact, [h('code', {}, short(number.artifact)), artifactState(number.artifact)]] : null,
|
||||
@@ -1351,14 +1510,22 @@
|
||||
if (snap && typeof snap.sql === 'string' && snap.sql) parts.push(h('p', { class: 'sub' }, T.dSql), h('pre', { class: 'sql' }, snap.sql))
|
||||
const record = snap && Number.isInteger(number.row) && Array.isArray(snap.rows) ? snap.rows[number.row] : undefined
|
||||
if (Array.isArray(record) || isObj(record)) {
|
||||
// 被引用的那一行:快照随文件附带(没碰设了遮罩的列)才走到这里,整行照原样列出,引用的那一格高亮
|
||||
// 被引用的那一行:快照随文件附带(没碰设了遮罩的列)才走到这里,整行照原样列出,引用的那一格高亮。数照 JSON
|
||||
// 原文写(同上面「快照里的值」):超过 2^53 的订单号读成 double 会丢位,和一位不差的「引用的值」对不上
|
||||
let columns = (Array.isArray(snap.columns) ? snap.columns : []).map(String)
|
||||
if (isObj(record) && !columns.length) columns = Object.keys(record)
|
||||
const cellAt = (c, i) => (isObj(record) ? own(record, c) : record[i])
|
||||
const art = view.out.artifacts.find((a) => a.a.id === number.artifact)
|
||||
const tokens = art ? tokensOf(art) : null
|
||||
const cellText = (c, i) => {
|
||||
const value = isObj(record) ? own(record, c) : record[i]
|
||||
if (value == null) return '—'
|
||||
if (typeof value !== 'number') return textOf(value)
|
||||
return numberText(value, tokenAt(tokens, ['rows', number.row, isObj(record) ? c : i]))
|
||||
}
|
||||
parts.push(h('p', { class: 'sub' }, T.dRow(number.row)), h('div', { class: 'table-wrap' }, h('table', {},
|
||||
h('thead', {}, h('tr', {}, columns.map((c) => h('th', { class: c === number.column ? 'hit' : null }, c)))),
|
||||
h('tbody', {}, h('tr', {}, columns.map((c, i) => h('td', { class: c === number.column ? 'hit' : null },
|
||||
cellAt(c, i) == null ? '—' : textOf(cellAt(c, i)))))))))
|
||||
h('tbody', {}, h('tr', {}, columns.map((c, i) => h('td', { class: c === number.column ? 'hit' : null,
|
||||
'data-cell': c }, cellText(c, i))))))))
|
||||
}
|
||||
return parts
|
||||
}
|
||||
@@ -1453,11 +1620,12 @@
|
||||
root.append(h('p', { class: 'note' }, T.noReports))
|
||||
return
|
||||
}
|
||||
const numbers = new Map(out.numbers.map((n) => [n.key, n]))
|
||||
for (const item of out.docs) {
|
||||
const label = nodeLabel(item.report.node_id)
|
||||
// 报告的徽标连同正文里的数字一起算:重算了全部哈希的伪造只在 ⑤ 露馅,标题旁不能还写着通过
|
||||
const nums = out.numbers.filter((n) => n.report === item.report)
|
||||
// 正文里按片段本身找核对结果(renderUnit)
|
||||
const numbers = new Map(nums.map((n) => [n.seg, n]))
|
||||
const docResult = [item.art, item.link].some((x) => x && x.result === FAIL) || !item.art ? FAIL : item.trusted ? PASS : NA
|
||||
const result = summarize([{ result: docResult }, ...nums])
|
||||
const box = h('article', { class: 'report', 'data-report': item.report.node_id, 'data-result': result },
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
小数位与 |.N / |int、pct;流水快照里的文字取值(地区)、DECIMAL 落成的数字文本、负数、换算成万、超过 2^53 的订单号、
|
||||
ISO 日期时间(文档记下 2 版显示规则)。check-bundle 在它上面只改显示文字做伪造,tests/test_bundle_shown.py 在 node 里
|
||||
逐个格式核一遍
|
||||
- formats-unit:同经营月报,销售额的单位带尾随空白(「元 」:单位输入框不去空白,指标节点照存),正文显示「74,613.65元 」,
|
||||
原样导出照样全部通过
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -79,6 +81,9 @@ FORMATS_CARD = {"kind": "metric_set", "caliber": "经营月报口径", "caliber_
|
||||
{"id": "share", "name": "线上占比", "value": 0.346, "unit": "", "decimals": None, "format": "thousands"},
|
||||
{"id": "export", "name": "出口额", "value": 15230.4, "unit": "美元", "decimals": 1, "format": "thousands"},
|
||||
]}
|
||||
#: 同上,销售额的单位带尾随空白:服务端照原样接在数后面
|
||||
FORMATS_UNIT_CARD = {**FORMATS_CARD, "metrics": [{**FORMATS_CARD["metrics"][0], "unit": "元 "},
|
||||
*FORMATS_CARD["metrics"][1:]]}
|
||||
#: 经营流水:地区是文字,金额一格是 DECIMAL 落成的数字文本、一格是负数,订单号超过 2^53,付款时间是 ISO 日期时间
|
||||
LEDGER = {"columns": ["region", "gmv", "order_no", "paid_at"],
|
||||
"column_types": {"region": "text", "gmv": "number", "order_no": "number", "paid_at": "datetime"},
|
||||
@@ -123,10 +128,12 @@ def masked_card(crm_id: str) -> dict[str, Any]:
|
||||
|
||||
class Sample:
|
||||
"""一次虚构的已封存运行:工件(原字节)、证据目录、报告文档、事件。masked=True 时口径卡带着取自遮罩列的输入;
|
||||
regions=True 时会员库另有一次按地区汇总的查询(目录里排在 Q3)。card、table 换掉口径卡和门店流水的快照(经营月报)。"""
|
||||
regions=True 时会员库另有一次按地区汇总的查询(目录里排在 Q3)。card、table 换掉口径卡和门店流水的快照(经营月报);
|
||||
inputs 是运行输入(报告里写 [[i:字段]] 引用)。"""
|
||||
|
||||
def __init__(self, *, nan: bool = False, masked: bool = False, regions: bool = False,
|
||||
card: dict[str, Any] | None = None, table: dict[str, Any] | None = None) -> None:
|
||||
card: dict[str, Any] | None = None, table: dict[str, Any] | None = None,
|
||||
inputs: dict[str, Any] | None = None) -> None:
|
||||
self.files: dict[str, bytes] = {}
|
||||
snapshot = table or shop(nan)
|
||||
self.shop_id = self.put(snapshot)
|
||||
@@ -147,7 +154,7 @@ class Sample:
|
||||
"columns": REGIONS["columns"], "rows": len(REGIONS["rows"]), "truncated": False}
|
||||
self.entries["card"] = {"kind": "metric_set", "node_id": "card", "artifact": self.card_id}
|
||||
self.ledger = list(self.entries.values())
|
||||
self.catalog = build_catalog(nodes={"card": self.card}, ledger=self.ledger)
|
||||
self.catalog = build_catalog(nodes={"card": self.card}, ledger=self.ledger, inputs=inputs)
|
||||
|
||||
def put(self, obj: Any) -> str:
|
||||
artifact, raw = stored(obj)
|
||||
@@ -230,10 +237,12 @@ def build(sample: Sample, events: list[tuple[int, str, str | None, Any]], bound:
|
||||
exported_at=EXPORTED_AT, legacy=legacy, graph=GRAPH, source_masks=source_masks or {})
|
||||
|
||||
|
||||
def formats_bundle() -> dict[str, Any]:
|
||||
"""经营月报(formats)的页面数据:每种显示格式各引用一处。"""
|
||||
f = Sample(card=FORMATS_CARD, table=LEDGER)
|
||||
events, bound = f.events({"write": f.doc(FORMATS, "write")})
|
||||
def formats_bundle(*, card: dict[str, Any] = FORMATS_CARD, table: dict[str, Any] = LEDGER, text: str = FORMATS,
|
||||
inputs: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
"""经营月报(formats)的页面数据:每种显示格式各引用一处。tests/test_bundle_shown.py 换掉口径卡、流水、正文或者
|
||||
加上运行输入,组装别的写法。"""
|
||||
f = Sample(card=card, table=table, inputs=inputs)
|
||||
events, bound = f.events({"write": f.doc(text, "write")})
|
||||
return build(f, events, bound)
|
||||
|
||||
|
||||
@@ -282,6 +291,7 @@ def bundles() -> dict[str, dict[str, Any]]:
|
||||
out["masked-source"] = build(r, events, bound, source_masks={"crm": ["phone"]})
|
||||
|
||||
out["formats"] = formats_bundle()
|
||||
out["formats-unit"] = formats_bundle(card=FORMATS_UNIT_CARD)
|
||||
return out
|
||||
|
||||
|
||||
|
||||
@@ -464,7 +464,7 @@ def test_the_sample_pages_for_the_browser_check_are_sealed_for_real():
|
||||
rendered = pages()
|
||||
got = {name: json.loads(data_block(page)) for name, page in rendered.items()}
|
||||
assert set(got) == {"base", "two-reports", "legacy", "failed", "continued", "degraded", "missing-doc",
|
||||
"missing-card", "nan", "stray", "masked-card", "masked-source", "formats"}
|
||||
"missing-card", "nan", "stray", "masked-card", "masked-source", "formats", "formats-unit"}
|
||||
for name, bundle in got.items():
|
||||
assert browser_manifest(bundle["events"]) == bundle["meta"]["manifest_hash"], name
|
||||
|
||||
@@ -513,3 +513,6 @@ def test_the_sample_pages_for_the_browser_check_are_sealed_for_real():
|
||||
nan = got["nan"]
|
||||
assert "NaN" in next(e["canonical"] for e in nan["events"] if e["type"] == "run.started")
|
||||
assert b"NaN" in base64.b64decode(next(a["b64"] for a in nan["artifacts"] if a.get("node_id") == "fetch"))
|
||||
# 口径卡单位带尾随空白:服务端把「元 」原样接在数后面,正文里的销售额带着那个空白
|
||||
shown = {s.get("ref"): s["text"] for s in segments(got["formats-unit"])}
|
||||
assert shown["m:gmv"] == "74,613.65元 " and shown["m:gmv|万"] == "7.46万元 "
|
||||
|
||||
@@ -9,13 +9,18 @@
|
||||
日期时间按文档记下的显示规则版本;服务端渲染不出来的,浏览器也不给出一个字。本页没有移植的写法(对象、列表、非 ASCII
|
||||
数字)一律渲染不出来,判无法核对,不会给出一串不一样的字;
|
||||
- 整份核对(verify):compose_doc 真实组装的报告原样通过;只改一个数字或取值的显示文字、重算全部哈希的伪造页,
|
||||
①–④ 照样对得上,⑤ 里那一项不通过,原因点名「正文显示「X」,引用的值是「Y」」;认不出的换算判无法核对,不判通过。
|
||||
①–④ 照样对得上,⑤ 里那一项不通过,原因点名「正文显示「X」,引用的值是「Y」」;认不出的换算判无法核对,不判通过;
|
||||
- 口径卡单位前后带空白(「元 」,服务端照原样接):两边都只去首尾空白,原样导出照样通过;运行输入照旧无法核对,显示
|
||||
文字拿引用的值核对,只改显示文字的不通过;出处面板里数字文本照原文写;
|
||||
- 正文的排法:核对通过的字要原样显示——伪造的链接藏不起它(整句按原文显示),正文里不显示的判不通过,紧挨着补数字、
|
||||
负号、方向控制字符的判无法核对,区间、日期这类真报告里的写法照常通过。
|
||||
|
||||
数据全是虚构的。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import copy
|
||||
import hashlib
|
||||
import json
|
||||
import random
|
||||
@@ -66,8 +71,11 @@ async function main() {
|
||||
return {
|
||||
events: results(out.events), manifest: results(out.manifest), artifacts: results(out.artifacts),
|
||||
chain: results(out.chain), meta: results(out.meta),
|
||||
// shownOk:显示文字核对过、对上了;truthText:出处面板「快照里的值」写的字(数字文本照原文);onPage:正文里这处
|
||||
// 可点的数字上读的人看到的字(没有显示成可点的数字是 null)
|
||||
numbers: out.numbers.map((n) => ({ key: n.key, ref: n.seg.ref ?? null, text: String(n.seg.text ?? ''),
|
||||
result: n.result, reason: n.reason || '', expected: n.expected ?? null })),
|
||||
result: n.result, reason: n.reason || '', expected: n.expected ?? null, shownOk: !!n.shownOk,
|
||||
truthText: n.truthText ?? null, onPage: n.onPage ?? null })),
|
||||
}
|
||||
}
|
||||
main().then((r) => process.stdout.write(JSON.stringify(r)), (e) => {
|
||||
@@ -99,7 +107,8 @@ NUMBERS: list[Any] = [
|
||||
FORMATS = [None, "thousands", "plain", "percent_of_ratio", "bogus"]
|
||||
DECIMALS = ["null", "0", "1", "2", "4", "-2", "6", "100", "101", "-101", "true", "2.0", "\"2\""]
|
||||
CONVS = [None, "", "万", "亿", "pct", "int", ".0", ".3", ".6", ".7", "千", "PCT"]
|
||||
UNITS = ["", "元", "美元", "%", "%", "人次"]
|
||||
#: 单位照口径卡上写的原样接在后面:前后带空白(含全角空格)的也照接,服务端不去空白
|
||||
UNITS = ["", "元", "美元", "%", "%", "人次", "元 ", "元\u3000", " 元"]
|
||||
|
||||
|
||||
def py_number(value: Any, unit: str, decimals: Any, fmt: Any, conv: Any) -> str | None:
|
||||
@@ -250,12 +259,12 @@ def reseal(bundle: dict[str, Any], art: dict[str, Any], doc: dict[str, Any]) ->
|
||||
return bundle
|
||||
|
||||
|
||||
def forged(ref: str, text: str, **cite: Any) -> dict[str, Any]:
|
||||
"""经营月报样本里一个片段的显示文字改成 text(cite.value 不动,cite.rendered 跟着改,cite 另改 cite 里给的几项),
|
||||
重算全部哈希。"""
|
||||
def forged(ref: str, text: str, bundle: dict[str, Any] | None = None, **cite: Any) -> dict[str, Any]:
|
||||
"""经营月报样本(或给的 bundle)里一个片段的显示文字改成 text(cite.value 不动,cite.rendered 跟着改,cite 另改
|
||||
cite 里给的几项),重算全部哈希。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
bundle = formats_bundle()
|
||||
bundle = bundle or formats_bundle()
|
||||
art, doc = doc_of(bundle)
|
||||
seg = segment(doc, ref)
|
||||
seg["text"] = text
|
||||
@@ -263,10 +272,25 @@ def forged(ref: str, text: str, **cite: Any) -> dict[str, Any]:
|
||||
return reseal(bundle, art, doc)
|
||||
|
||||
|
||||
def rewritten(bundle: dict[str, Any], change: Any) -> dict[str, Any]:
|
||||
"""change(文档) 就地改报告文档,再重算全部哈希(同 reseal)。"""
|
||||
art, doc = doc_of(bundle)
|
||||
change(doc)
|
||||
return reseal(bundle, art, doc)
|
||||
|
||||
|
||||
def by_id(doc: dict[str, Any], seg_id: str) -> dict[str, Any]:
|
||||
return next(s for b in doc["blocks"] for u in b["units"] for s in u["segments"] if s.get("id") == seg_id)
|
||||
|
||||
|
||||
def verdicts(bundle: dict[str, Any]) -> dict[str, Any]:
|
||||
return node("verify", data=bundle, seal=bundle["meta"]["manifest_hash"])
|
||||
|
||||
|
||||
def item(got: dict[str, Any], ref: str) -> dict[str, Any]:
|
||||
return next(n for n in got["numbers"] if n["ref"] == ref)
|
||||
|
||||
|
||||
def test_every_format_in_the_monthly_sample_passes_as_exported():
|
||||
"""经营月报样本(compose_doc 真实组装:货币单位、万 / 亿、比率、负数、不分组、小数位、pct、DECIMAL 文本、
|
||||
超过 2^53 的订单号、日期时间、文字取值)原样导出:每个数字和取值都通过,显示文字核对过(expected 等于正文)。"""
|
||||
@@ -366,8 +390,9 @@ def test_a_browser_without_bigint_cannot_check_numbers_but_still_checks_the_rest
|
||||
assert all(r == "pass" for key in ("events", "manifest", "artifacts", "chain", "meta") for r in got[key])
|
||||
texts = {"v:Q1.r0.region", "v:Q1.r1.region", "v:Q1.r0.paid_at"}
|
||||
assert all(n["result"] == "pass" for n in got["numbers"] if n["ref"] in texts)
|
||||
assert all(n["result"] == "na" and n["reason"] == "无法核对显示文字:这个浏览器不支持按系统的显示规则重新渲染(缺少 BigInt 或 "
|
||||
"Unicode 正则)" for n in got["numbers"] if n["ref"] not in texts)
|
||||
# 原因写给读报告的人:说清该怎么办,不提 BigInt、正则这类实现细节
|
||||
assert all(n["result"] == "na" and n["reason"] == "无法核对显示文字:这个浏览器版本较旧,请用新版浏览器打开本文件"
|
||||
for n in got["numbers"] if n["ref"] not in texts)
|
||||
|
||||
|
||||
def test_every_sample_page_still_passes_its_display_check():
|
||||
@@ -379,3 +404,258 @@ def test_every_sample_page_still_passes_its_display_check():
|
||||
got = verdicts(bundle)
|
||||
assert not [n for n in got["numbers"] if n["reason"].startswith(("正文显示", "无法核对显示文字"))], name
|
||||
assert all(n["expected"] == n["text"] for n in got["numbers"] if n["result"] == "pass"), name
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 单位前后带空白:服务端照原样接在数后面,只去首尾空白时两边一样去
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def card_with_unit(unit: str) -> dict[str, Any]:
|
||||
"""经营月报的口径卡,销售额的单位换成 unit。"""
|
||||
from tests.bundle_samples import FORMATS_CARD
|
||||
|
||||
card = copy.deepcopy(FORMATS_CARD)
|
||||
card["metrics"][0]["unit"] = unit
|
||||
return card
|
||||
|
||||
|
||||
@pytest.mark.parametrize("unit", ["元 ", "元\u3000", " 元", "元 \u3000"])
|
||||
def test_a_unit_with_surrounding_blanks_passes_as_exported(unit):
|
||||
"""单位输入框不去空白,指标节点照存:服务端把「元 」原样接在数后面,正文显示「74,613.65元 」。只去掉首尾空白时
|
||||
两边一样去:原样导出的销售额和换算成万的那处都通过,不会被说成文件被改过。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
got = verdicts(formats_bundle(card=card_with_unit(unit)))
|
||||
for ref in ("m:gmv", "m:gmv|万"):
|
||||
hit = item(got, ref)
|
||||
assert hit["result"] == "pass" and hit["shownOk"] and hit["expected"] == hit["text"], hit
|
||||
assert all(n["result"] == "pass" for n in got["numbers"]), [n for n in got["numbers"] if n["result"] != "pass"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("unit", ["元 ", "元\u3000"])
|
||||
@pytest.mark.parametrize("ref, digits", [("m:gmv", "84,613.65"), ("m:gmv|万", "8.46万")])
|
||||
def test_a_unit_with_trailing_blanks_still_catches_a_changed_digit(unit, ref, digits):
|
||||
"""单位带尾随空白时,改了一位数字照样不通过,原因点名改过的字。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
text = f"{digits}{unit}"
|
||||
hit = item(verdicts(forged(ref, text, formats_bundle(card=card_with_unit(unit)))), ref)
|
||||
assert hit["result"] == "fail" and hit["reason"].startswith(f"正文显示「{text}」,引用的值是「74613.65」"), hit
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 运行输入:输入本身不随文件附带,显示文字照样拿引用的值核对
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
INPUTS = {"week_start": "2026-09-01", "target": 50000, "region": "华南", "rate": 0.125, "reviewed": True}
|
||||
INPUT_TEXT = ("## 周报\n\n统计周期自 [[i:week_start]] 起,目标 [[i:target]](约 [[i:target|万]]),重点地区 [[i:region]],"
|
||||
"达成率 [[i:rate|pct]],已复核:[[i:reviewed]]。")
|
||||
|
||||
|
||||
def test_input_citations_check_their_shown_text():
|
||||
"""运行输入不随文件附带,引用的值核对不了(照旧无法核对,原因不变);正文显示的字照样拿引用的值按服务端的写法
|
||||
(不分组、不接单位,可带换算)重新渲染比对:原样导出时对得上,出处面板注明一致。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
got = verdicts(formats_bundle(text=INPUT_TEXT, inputs=INPUTS))
|
||||
inputs = [n for n in got["numbers"] if n["ref"].startswith("i:")]
|
||||
assert {n["ref"]: n["text"] for n in inputs} == {
|
||||
"i:week_start": "2026-09-01", "i:target": "50000", "i:target|万": "5万", "i:region": "华南",
|
||||
"i:rate|pct": "12.5%", "i:reviewed": "是"}
|
||||
assert all(n["result"] == "na" and n["reason"] == "引用的是运行输入,运行输入未随文件附带" and n["shownOk"]
|
||||
for n in inputs), inputs
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ref, text, cited, expected", [
|
||||
("i:week_start", "2026-09-08", "2026-09-01", None), # 改了统计周期(文字取值)
|
||||
("i:region", "华东", "华南", None),
|
||||
("i:target", "60000", "50000", None),
|
||||
("i:target|万", "6万", "50000", "5万"),
|
||||
("i:rate|pct", "21.5%", "0.125", "12.5%"),
|
||||
])
|
||||
def test_changing_only_the_shown_text_of_an_input_fails(ref, text, cited, expected):
|
||||
"""只改运行输入的显示文字(引用的值不动)、重算全部哈希:不通过,原因点名正文显示的字和引用的值。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
hit = item(verdicts(forged(ref, text, formats_bundle(text=INPUT_TEXT, inputs=INPUTS))), ref)
|
||||
assert hit["result"] == "fail" and hit["reason"].startswith(f"正文显示「{text}」,引用的值是「{cited}」"), hit
|
||||
if expected:
|
||||
assert hit["reason"].endswith(f",按显示规则应显示为「{expected}」"), hit
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 出处面板:落成数字文本的值照原文写
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_numeric_text_cells_show_their_own_digits_in_the_panel():
|
||||
"""DECIMAL(20,0) 落成数字文本的订单号:按数比对时读成 double 会丢位,出处面板「快照里的值」照快照的原文写,
|
||||
和「引用的值」一位不差;45678.50 这类也照原文写。"""
|
||||
from tests.bundle_samples import LEDGER, formats_bundle
|
||||
|
||||
table = copy.deepcopy(LEDGER)
|
||||
table["rows"][0][2] = "202609071234567891"
|
||||
got = verdicts(formats_bundle(table=table))
|
||||
order = item(got, "v:Q1.r0.order_no")
|
||||
assert order["result"] == "pass" and order["text"] == "202,609,071,234,567,891", order
|
||||
assert order["truthText"] == "202609071234567891", order
|
||||
assert item(got, "v:Q1.r0.gmv")["truthText"] == "45678.50"
|
||||
# 不是数字文本的格子不另写原文:照旧按值写
|
||||
assert item(got, "v:Q1.r1.gmv")["truthText"] is None and item(got, "v:Q1.r0.region")["truthText"] is None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 正文的排法:核对通过的字要原样显示,旁边的字不能把它改读成别的数
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_every_checked_value_is_shown_on_the_page_as_checked():
|
||||
"""原样导出:每个数字和取值都显示成可点的数字,读的人看到的字就是核对过的字。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
got = verdicts(formats_bundle())
|
||||
assert all(n["onPage"] == n["text"] for n in got["numbers"]), [n for n in got["numbers"] if n["onPage"] != n["text"]]
|
||||
|
||||
|
||||
def test_inline_markup_cannot_hide_a_checked_value():
|
||||
"""伪造的链接把核对通过的数字、取值塞进链接地址(链接只显示文字、不显示地址),链接文字写成别的数:这一句改按
|
||||
未排版的原文显示,核对过的字一个不少地露出来,读的人看得到两个数。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
def change(doc: dict[str, Any]) -> None:
|
||||
by_id(doc, "s3")["text"] += "[84,613.65元](x " # 本月销售额 [84,613.65元](x 74,613.65元)(约 …
|
||||
by_id(doc, "s5")["text"] = ")" + by_id(doc, "s5")["text"]
|
||||
by_id(doc, "s36")["text"] += "[华南](x " # ),[华南](x 华东)冲减 …
|
||||
by_id(doc, "s38")["text"] = ")" + by_id(doc, "s38")["text"]
|
||||
|
||||
got = verdicts(rewritten(formats_bundle(), change))
|
||||
for ref, text in (("m:gmv", "74,613.65元"), ("v:Q1.r1.region", "华东")):
|
||||
hit = item(got, ref)
|
||||
assert hit["result"] == "pass" and hit["onPage"] == text, hit
|
||||
|
||||
|
||||
GLUED = "无法核对显示文字:紧挨着的正文里还有数字或负号,读起来和核对过的不是同一个值"
|
||||
|
||||
|
||||
def _glue_before(seg_id: str, extra: str, after: str = ""):
|
||||
def change(doc: dict[str, Any]) -> None:
|
||||
by_id(doc, seg_id)["text"] += extra
|
||||
if after:
|
||||
nxt = by_id(doc, f"s{int(seg_id[1:]) + 2}")
|
||||
nxt["text"] = after + nxt["text"]
|
||||
return change
|
||||
|
||||
|
||||
def _glue_after(seg_id: str, extra: str):
|
||||
def change(doc: dict[str, Any]) -> None:
|
||||
by_id(doc, seg_id)["text"] = extra + by_id(doc, seg_id)["text"]
|
||||
return change
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ref, change", [
|
||||
("m:gmv", _glue_before("s3", "1")), # 前面多一位:174,613.65元
|
||||
("m:gmv", _glue_before("s3", "1\u200b")), # 隔着看不见的零宽空格
|
||||
("m:gmv", _glue_before("s3", "1**", after="**")), # 隔着排版时藏起来的粗体标记
|
||||
("m:gmv", _glue_before("s3", chr(0x1D7F7))), # 数学字母区的数字 1(占两个 UTF-16 单元)
|
||||
("v:Q1.r0.gmv", _glue_after("s34", "0")), # 后面多一位:45,678.50
|
||||
("v:Q1.r0.gmv", _glue_after("s34", ".5")), # 后面接小数:45,678.5.5
|
||||
("m:conv_rate", _glue_before("s12", "-")), # 前面加负号:-2.35%
|
||||
("m:conv_rate", _glue_before("s12", "\uff0d")), # 全角负号
|
||||
("m:refund", _glue_before("s9", "-")), # 负数前面再加负号:--3,120.50元
|
||||
("v:Q1.r0.paid_at", _glue_after("s45", "5")), # 取值以数字结尾:2026-09-07 08:21:395
|
||||
])
|
||||
def test_digits_glued_to_a_checked_value_cannot_pass(ref, change):
|
||||
"""伪造者在相邻的文字片段里补一位数、一个负号(隔着零宽空格、藏起来的粗体标记也算紧挨着):显示出来的数不止
|
||||
核对过的这几位,这一项判无法核对、写明原因;别的照常通过。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
got = verdicts(rewritten(formats_bundle(), change))
|
||||
hit = item(got, ref)
|
||||
assert hit["result"] == "na" and hit["reason"] == GLUED and hit["shownOk"], hit
|
||||
assert all(n["result"] == "pass" for n in got["numbers"] if n["ref"] != ref)
|
||||
|
||||
|
||||
def test_direction_controls_next_to_checked_values_cannot_pass():
|
||||
"""文字方向控制字符(从右往左覆盖)能把后面的一位数挪到数字前面显示,按字的先后看不出紧挨着:同一句里出现在
|
||||
别的片段里,这一句的数字、取值判无法核对;别的段照常通过。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
def change(doc: dict[str, Any]) -> None:
|
||||
by_id(doc, "s5")["text"] = chr(0x202E) + by_id(doc, "s5")["text"]
|
||||
|
||||
got = verdicts(rewritten(formats_bundle(), change))
|
||||
first = {"m:gmv", "m:gmv|万", "m:annual|亿", "m:refund", "m:conv_rate", "m:growth", "m:orders", "m:avg",
|
||||
"m:avg|.3", "m:avg|int", "m:share|pct", "m:export"}
|
||||
for n in got["numbers"]:
|
||||
if n["ref"] in first:
|
||||
assert n["result"] == "na" and n["reason"] == ("无法核对显示文字:这一句里有改变文字方向的控制字符,"
|
||||
"显示出来的顺序可能和核对过的不一样"), n
|
||||
else:
|
||||
assert n["result"] == "pass", n
|
||||
|
||||
|
||||
def test_ranges_dates_and_signs_written_by_the_system_still_pass():
|
||||
"""真报告里常见的写法不算紧挨着:两个数之间的连字符(区间)、日期和数字之间隔着字或空白、负数自己的负号。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
text = ("## 区间\n\n销售额区间 [[m:gmv]]-[[m:export]],流水区间 [[v:Q1.r0.gmv]]-[[v:Q1.r1.gmv]]。"
|
||||
"截至2026年9月[[m:orders]],2026-09-07 当天客单价 [[m:avg]],环比 [[m:growth]]。")
|
||||
got = verdicts(formats_bundle(text=text))
|
||||
assert len(got["numbers"]) == 7
|
||||
assert all(n["result"] == "pass" and n["onPage"] == n["text"] for n in got["numbers"]), got["numbers"]
|
||||
|
||||
|
||||
NOT_SHOWN = "正文里没有显示这一处:导出时不会出现这种情况,文件可能被改过"
|
||||
|
||||
|
||||
def test_a_checked_value_in_a_block_the_page_does_not_show_fails():
|
||||
"""分隔线不显示句子:把一段改成分隔线,这一段里核对通过的数字、取值读的人都看不到(伪造的字可以写在别处),
|
||||
服务端导出时不会出现,判不通过;别的段照常通过。"""
|
||||
from tests.bundle_samples import formats_bundle
|
||||
|
||||
def change(doc: dict[str, Any]) -> None:
|
||||
doc["blocks"][2]["type"] = "hr"
|
||||
|
||||
got = verdicts(rewritten(formats_bundle(), change))
|
||||
second = {"v:Q1.r0.region", "v:Q1.r0.gmv", "v:Q1.r0.gmv|万", "v:Q1.r1.region", "v:Q1.r1.gmv",
|
||||
"v:Q1.r0.order_no", "v:Q1.r0.paid_at"}
|
||||
for n in got["numbers"]:
|
||||
if n["ref"] in second:
|
||||
assert n["result"] == "fail" and n["reason"] == NOT_SHOWN and n["onPage"] is None, n
|
||||
else:
|
||||
assert n["result"] == "pass" and n["onPage"] == n["text"], n
|
||||
|
||||
|
||||
def test_a_table_cell_outside_the_rows_and_columns_fails():
|
||||
"""表格只显示表头和各行各列上的格:把一格的位置改成第 1.5 列,读的人看不到它,判不通过。原样导出时表格里每一格都
|
||||
显示成可点的数字。"""
|
||||
from tests.bundle_samples import FORMATS, formats_bundle
|
||||
|
||||
bundle = formats_bundle(text=FORMATS + "\n\n[[table:Q1 cols=region,gmv rows=0-1]]")
|
||||
got = verdicts(bundle)
|
||||
assert all(n["result"] == "pass" and n["onPage"] == n["text"] for n in got["numbers"])
|
||||
moved: list[str] = []
|
||||
|
||||
def change(doc: dict[str, Any]) -> None:
|
||||
table = next(b for b in doc["blocks"] if b["type"] == "table")
|
||||
unit = next(u for u in table["units"] if u.get("loc") == {"row": 1, "col": 1})
|
||||
unit["loc"] = {"row": 1, "col": 1.5}
|
||||
moved.extend(f"write|{s['id']}" for s in unit["segments"] if s["kind"] in ("number", "value"))
|
||||
|
||||
got = verdicts(rewritten(bundle, change))
|
||||
assert len(moved) == 1
|
||||
for n in got["numbers"]:
|
||||
if n["key"] in moved:
|
||||
assert n["result"] == "fail" and n["reason"] == NOT_SHOWN, n
|
||||
else:
|
||||
assert n["result"] == "pass", n
|
||||
|
||||
|
||||
def test_reasons_for_readers_do_not_name_implementation_details():
|
||||
"""核对页是发给业务读者的文件:「无法核对显示文字」的原因说清是什么情况、该怎么办,不提 BigInt、正则、ASCII。"""
|
||||
source = VERIFIER.read_text(encoding="utf-8")
|
||||
start = source.index("const T = {")
|
||||
block = source[start:source.index("\n }\n", start)]
|
||||
for word in ("BigInt", "Unicode", "ASCII", "正则", "重新渲染时出错"):
|
||||
assert word not in block, word
|
||||
|
||||
+115
-19
@@ -8,6 +8,7 @@
|
||||
// - 导出:从服务端找一份已封存、封存核对一致、有报告文档的运行(GET /api/runs 找成功结束的,再看 /verify 和证据接口),
|
||||
// 下载 bundle.html:状态码、类型、附件名(RFC 5987)、页面本身零外链。找不到可导出的运行时照实报失败并说明原因;
|
||||
// - 断网打开:拦掉一切网络请求,从本地文件打开:各项核对没有不通过、总览通过、没有发出任何请求,数字点开看得到出处;
|
||||
// 数字与取值的显示文字都核对得了(没有「无法核对显示文字」:移植的显示规则漏了真实写法时这里先报);
|
||||
// - 去掉 crypto.subtle:页面加载前删掉它,照常全部通过,页脚写明用的是本页内置的实现;
|
||||
// - 篡改:① 一条带规范字符串的事件内容、② 一份嵌入工件的一个字节、③ 元信息里的封存哈希,各自对应的核对项不通过、
|
||||
// 总览显示不通过;另做两种「哈希全部重算」的伪造:改报告里一个数字的引用值,或者引用值不动、只改它的显示文字(数字改
|
||||
@@ -18,8 +19,9 @@
|
||||
// - 样本页:tests/bundle_samples.py 用虚构数据真实生成的几份页面(遮罩快照、两份报告、早期运行、失败结束、先失败后
|
||||
// 经继续运行跑到成功的早期运行、两条出具
|
||||
// 判定、服务端缺文件、裸 NaN、别的运行的文档、口径卡有输入取自遮罩列、设了遮罩的数据源上按地区汇总的查询、每种显示
|
||||
// 格式各引用一处的经营月报),逐份断言各项结果和原因文案;经营月报上只改文字取值、数字、换算成万的口径卡引用的显示
|
||||
// 文字并重算全部哈希,各自变红并点名;在上面篡改 ④
|
||||
// 格式各引用一处的经营月报、单位带尾随空白的经营月报),逐份断言各项结果和原因文案;经营月报上只改文字取值、数字、
|
||||
// 换算成万的口径卡引用的显示文字并重算全部哈希,各自变红并点名;核对过的字不动、改它旁边的(链接地址藏起数字、前面
|
||||
// 补一位数、整段改成分隔线、文字片段冒用数字的 id),读的人看到的数要就是核对过的那个;在上面篡改 ④
|
||||
// 引用链(换文档、拿掉报告、多一份没人引用的工件)、⑥ 运行信息(运行结果、类别、出具档位)、拿掉被引用的口径卡、把
|
||||
// 报告文档标成遮罩、把遮罩卡标成服务端的文件与 id 不一致或解析不出来(服务端对被改过、解析不了的遮罩卡就这么标)、
|
||||
// 遮罩源上随文件附带的汇总快照改一个字节、它的数字改引用值并重算全部哈希、
|
||||
@@ -146,6 +148,9 @@ const stateOf = (page) => page.evaluate(() => ({
|
||||
}, {}),
|
||||
// 正文里标成不通过的数字:读屏念的那一句
|
||||
failedButtons: [...document.querySelectorAll('button.num[data-result="fail"]')].map((b) => b.getAttribute('aria-label') ?? ''),
|
||||
// 正文里每个可点的数字:哪一处、结果、读的人看到的字
|
||||
buttons: [...document.querySelectorAll('button.num')].map((b) => ({ key: b.dataset.num, result: b.dataset.result,
|
||||
text: b.textContent })),
|
||||
sha: document.querySelector('[data-sha]')?.dataset.sha ?? null,
|
||||
seal: document.getElementById('seal-hash')?.textContent ?? '',
|
||||
overflow: document.documentElement.scrollWidth - document.documentElement.clientWidth,
|
||||
@@ -165,18 +170,30 @@ const hasItem = (s, check, result, text) => s.items.some((i) => i.check === chec
|
||||
* node、改过的片段 seg),找不到可改的返回 null
|
||||
*/
|
||||
function rewrite(data, pick, edit) {
|
||||
const embedded = new Set(data.artifacts.filter((a) => typeof a.b64 === 'string').map((a) => a.id))
|
||||
return rewriteDoc(data, (doc) => {
|
||||
let hit = null
|
||||
for (const block of doc.blocks ?? []) for (const unit of block.units ?? []) for (const seg of unit.segments ?? []) {
|
||||
const c = seg.cite
|
||||
if (!hit && c?.status === 'resolved' && ['metric', 'cell'].includes(c.kind)
|
||||
&& embedded.has(doc.catalog?.[c.alias]?.artifact) && pick(seg)) hit = seg
|
||||
}
|
||||
if (!hit) return null
|
||||
edit(hit)
|
||||
return { what: `片段 ${hit.id}(${hit.cite.kind})`, seg: hit }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* change(文档) 就地改报告文档(返回 { what, seg? } 说明改了哪里,找不到可改的返回 null),再把文档、引用它的事件、
|
||||
* 清单哈希全部重算(页面顶部的封存哈希由 tamper 的 seal 换)。返回说明,另带所在报告 node
|
||||
*/
|
||||
function rewriteDoc(data, change) {
|
||||
const docArt = data.artifacts.find((a) => a.kind === 'report_doc' && typeof a.b64 === 'string')
|
||||
if (!docArt) return null
|
||||
const doc = JSON.parse(Buffer.from(docArt.b64, 'base64').toString('utf8'))
|
||||
const embedded = new Set(data.artifacts.filter((a) => typeof a.b64 === 'string').map((a) => a.id))
|
||||
let hit = null
|
||||
for (const block of doc.blocks ?? []) for (const unit of block.units ?? []) for (const seg of unit.segments ?? []) {
|
||||
const c = seg.cite
|
||||
if (!hit && c?.status === 'resolved' && ['metric', 'cell'].includes(c.kind)
|
||||
&& embedded.has(doc.catalog?.[c.alias]?.artifact) && pick(seg)) hit = seg
|
||||
}
|
||||
if (!hit) return null
|
||||
edit(hit)
|
||||
const done = change(doc)
|
||||
if (!done) return null
|
||||
const bytes = Buffer.from(JSON.stringify(doc), 'utf8')
|
||||
const oldId = docArt.id
|
||||
const newId = sha(bytes)
|
||||
@@ -192,10 +209,12 @@ function rewrite(data, pick, edit) {
|
||||
const lines = data.events.map((e, i) => [e, i]).sort((x, y) => x[0].seq - y[0].seq || x[1] - y[1])
|
||||
.map(([e]) => `${e.seq}|${e.type}|${e.node_id ?? ''}|${e.data_sha256}`)
|
||||
data.meta.manifest_hash = sha(lines.join('\n'))
|
||||
return { what: `片段 ${hit.id}(${hit.cite.kind})`, node: data.reports.find((r) => r.doc_artifact === newId)?.node_id,
|
||||
seg: hit }
|
||||
return { ...done, node: data.reports.find((r) => r.doc_artifact === newId)?.node_id }
|
||||
}
|
||||
|
||||
/** 经营月报样本里按 id 找片段(改它两边的文字做排版上的伪造) */
|
||||
const segById = (doc, id) => doc.blocks.flatMap((b) => b.units).flatMap((u) => u.segments).find((s) => s.id === id)
|
||||
|
||||
/** 改一个数字的引用值(显示文字不动),重算全部哈希:⑤ 拿口径卡、快照对出来 */
|
||||
const forge = (data) => rewrite(data, (seg) => typeof seg.cite.value === 'number', (seg) => { seg.cite.value += 1 })
|
||||
|
||||
@@ -356,6 +375,12 @@ await section('断网打开', async () => {
|
||||
check('核对项「artifacts」没有不通过', noFail(s.checks.artifacts), s.checks.artifacts)
|
||||
check('数字与取值没有不通过的', noFail(s.checks.numbers) || s.checks.numbers === 'na',
|
||||
`${s.checks.numbers}:${JSON.stringify(s.numberStates)}`)
|
||||
// 显示文字在真数据上也要核得了:移植的显示规则漏了某种真实写法(或者真报告的排版被判成紧挨着)时,数字会大批变成
|
||||
// 「无法核对显示文字」,上面那条「没有不通过」看不出来。Chrome 有 BigInt,正常导出的都该核得了。读的是真数据:
|
||||
// 只打印条数
|
||||
const unshown = new Set(s.items.filter((i) => i.check === 'numbers' && i.result === 'na'
|
||||
&& i.text.includes('无法核对显示文字')).map((i) => i.text)).size
|
||||
check('数字与取值的显示文字都核对得了(没有「无法核对显示文字」)', unshown === 0, `${unshown} 项`)
|
||||
// 早期运行的运行结果、升级前的运行类别核对不了(无法核对),只要求没有对不上的
|
||||
check('运行信息没有与已核对的事件对不上的', s.checks.meta !== 'fail', `${s.checks.meta}:${JSON.stringify(s.meta)}`)
|
||||
check('没有发出任何网络请求', requests.length === 0, requests.slice(0, 3).join('、'))
|
||||
@@ -404,8 +429,9 @@ await section('去掉 crypto.subtle', async () => {
|
||||
// ---------------------------------------------------------------- 篡改
|
||||
|
||||
/**
|
||||
* 在一份页面上篡改后断网打开。mutate(data) 改数据、返回说明(找不到可改的返回 null);expect(state) 返回
|
||||
* [[检查名, 条件, 说明]…]。seal:同时把页面顶部的封存哈希换成改过的数据里的那一份(重算全部哈希的伪造会这么做)
|
||||
* 在一份页面上篡改后断网打开。mutate(data) 改数据、返回说明(找不到可改的返回 null);expect(state, 说明, page) 返回
|
||||
* [[检查名, 条件, 说明]…](要点开面板的可以是异步的)。seal:同时把页面顶部的封存哈希换成改过的数据里的那一份
|
||||
* (重算全部哈希的伪造会这么做)
|
||||
*/
|
||||
async function tamper(source, label, mutate, expect, { seal = false } = {}) {
|
||||
const data = structuredClone(dataOf(source))
|
||||
@@ -419,7 +445,9 @@ async function tamper(source, label, mutate, expect, { seal = false } = {}) {
|
||||
const opened = await openFile(writePage(`tampered-${label.replace(/[^\w一-鿿]+/g, '_')}.html`, page))
|
||||
try {
|
||||
const s = await stateOf(opened.page)
|
||||
for (const [name, cond, detail] of expect(s, what)) check(`篡改${label}:${name}`, cond, detail ?? `${typeof what === 'string' ? what : what.what};${describe(s)}`)
|
||||
for (const [name, cond, detail] of await expect(s, what, opened.page)) {
|
||||
check(`篡改${label}:${name}`, cond, detail ?? `${typeof what === 'string' ? what : what.what};${describe(s)}`)
|
||||
}
|
||||
} finally {
|
||||
await opened.ctx.close()
|
||||
}
|
||||
@@ -550,7 +578,7 @@ await section('样本页:各种形状', async () => {
|
||||
check('生成样本页(tests/bundle_samples.py)', false, String(e?.message ?? e))
|
||||
return
|
||||
}
|
||||
check('生成样本页(tests/bundle_samples.py)', Object.keys(samples).length === 13, Object.keys(samples).join('、'))
|
||||
check('生成样本页(tests/bundle_samples.py)', Object.keys(samples).length === 14, Object.keys(samples).join('、'))
|
||||
|
||||
await sample('base', async (s, page) => {
|
||||
check('base:总览通过,有无法核对的项时写「可核对的项全部通过」', s.overall === 'pass' && s.text === '可核对的项全部通过',
|
||||
@@ -733,13 +761,33 @@ await section('样本页:各种形状', async () => {
|
||||
detail.replace(/\s+/g, ' ').slice(0, 160))
|
||||
const order = page.locator('button.num').filter({ hasText: '202,609,071,234,567,890' })
|
||||
detail = '找不到订单号'
|
||||
let rowCell = ''
|
||||
if (await order.count()) {
|
||||
await order.first().click()
|
||||
detail = await appears(page.locator('#detail')) ? await page.locator('#detail').innerText() : '点开后没有出处面板'
|
||||
rowCell = await page.locator('#detail td[data-cell="order_no"]').textContent().catch(() => '') ?? ''
|
||||
await page.keyboard.press('Escape')
|
||||
}
|
||||
check('formats:超过 2^53 的订单号核对通过,出处面板里的值一位不差', detail.includes('202609071234567890')
|
||||
&& detail.includes('与按显示规则渲染的结果一致'), detail.replace(/\s+/g, ' ').slice(0, 160))
|
||||
// 引用的值、快照里的值、快照第 1 行那一格都按 JSON 原文写:读成 double 的 202609071234567900 一处都不能有
|
||||
check('formats:超过 2^53 的订单号核对通过,出处面板里的值(连同快照那一行)一位不差',
|
||||
detail.includes('202609071234567890') && !detail.includes('202609071234567900')
|
||||
&& rowCell === '202609071234567890' && detail.includes('与按显示规则渲染的结果一致'),
|
||||
`${detail.replace(/\s+/g, ' ').slice(0, 160)} | 那一行的格:${rowCell}`)
|
||||
})
|
||||
// 口径卡单位带尾随空白(「元 」):服务端照原样接在数后面,正文是「74,613.65元 」,两边都只去首尾空白,原样导出照样
|
||||
// 全部通过,不会被说成文件被改过
|
||||
await sample('formats-unit', async (s, page) => {
|
||||
check('formats-unit:口径卡单位带尾随空白时原样导出「全部通过」', s.overall === 'pass' && s.text === '全部通过'
|
||||
&& s.numberStates.pass === 19, `${s.overall}:${s.text};${JSON.stringify(s.numberStates)}`)
|
||||
const gmv = page.locator('button.num').filter({ hasText: /^74,613\.65元\s*$/ })
|
||||
let detail = '找不到销售额'
|
||||
if (await gmv.count()) {
|
||||
await gmv.first().click()
|
||||
detail = await appears(page.locator('#detail')) ? await page.locator('#detail').innerText() : '点开后没有出处面板'
|
||||
await page.keyboard.press('Escape')
|
||||
}
|
||||
check('formats-unit:点开销售额,注明与按显示规则渲染的结果一致,不另列「按显示规则应为」',
|
||||
detail.includes('与按显示规则渲染的结果一致') && !detail.includes('按显示规则应为'), detail.replace(/\s+/g, ' ').slice(0, 160))
|
||||
})
|
||||
|
||||
// 不执行脚本(附件预览、快速查看):封存哈希、运行 ID、工作流名照样看得到,状态写「尚未核对」,不是一直「正在核对」
|
||||
@@ -921,6 +969,54 @@ await section('样本页:篡改', async () => {
|
||||
await opened.ctx.close()
|
||||
}
|
||||
}
|
||||
// 核对过的字不动、改它旁边的(经营月报,重算全部哈希):读的人看到的数要就是核对过的那个
|
||||
const buttonOf = (s, forged) => s.buttons.find((b) => b.key === `${forged.node}|${forged.seg.id}`)
|
||||
// 伪造的链接把销售额塞进链接地址(链接只显示文字),链接文字写成 84,613.65元:这一句改按原文显示,核对过的字露出来
|
||||
await tamper(samples.formats, '用链接地址藏起核对通过的数字', (data) => rewriteDoc(data, (doc) => {
|
||||
segById(doc, 's3').text += '[84,613.65元](x '
|
||||
segById(doc, 's5').text = `)${segById(doc, 's5').text}`
|
||||
return { what: '销售额塞进链接地址', seg: segById(doc, 's4') }
|
||||
}), async (s, forged, page) => {
|
||||
const btn = buttonOf(s, forged)
|
||||
const sentence = await page.locator('.doc p').first().innerText()
|
||||
return [
|
||||
['销售额照样显示成可点的数字,字一个不少', btn?.text === '74,613.65元' && btn.result === 'pass', JSON.stringify(btn)],
|
||||
['这一句按原文显示,伪造的链接写法露在外面', sentence.includes('[84,613.65元](x 74,613.65元)'), sentence.slice(0, 80)],
|
||||
]
|
||||
}, { seal: true })
|
||||
// 前面补一位数:读起来是 174,613.65元,绿色下划线只在 74,613.65元 下面
|
||||
await tamper(samples.formats, '核对通过的数字前面补一位数', (data) => rewriteDoc(data, (doc) => {
|
||||
segById(doc, 's3').text += '1'
|
||||
return { what: '本月销售额 174,613.65元', seg: segById(doc, 's4') }
|
||||
}), (s, forged) => {
|
||||
const btn = buttonOf(s, forged)
|
||||
const item = s.items.find((i) => i.check === 'numbers' && i.result === 'na' && i.text.includes('紧挨着的正文里还有数字或负号'))
|
||||
return [
|
||||
['销售额判无法核对,不再标通过', btn?.result === 'na', JSON.stringify(btn)],
|
||||
['⑤ 写明紧挨着的正文里还有数字或负号', !!item, item?.text ?? '没有这一项'],
|
||||
['总览不再是「全部通过」', s.overall === 'pass' && s.text === '可核对的项全部通过', `${s.overall}:${s.text}`],
|
||||
]
|
||||
}, { seal: true })
|
||||
// 一段改成分隔线:分隔线不显示句子,这一段里核对通过的数字读的人看不到,导出时不会出现
|
||||
await tamper(samples.formats, '把一段改成分隔线', (data) => rewriteDoc(data, (doc) => {
|
||||
doc.blocks[2].type = 'hr'
|
||||
return { what: '第二段改成分隔线' }
|
||||
}), (s) => [
|
||||
...turnsRed('numbers', ['events', 'manifest', 'artifacts', 'chain'])(s),
|
||||
['原因写明正文里没有显示这一处', hasItem(s, 'numbers', 'fail', '正文里没有显示这一处:导出时不会出现这种情况')],
|
||||
], { seal: true })
|
||||
// 文字片段冒用销售额的 id:页面按片段本身找核对结果,冒用的文字显示不成可点的数字、借不到「通过」
|
||||
await tamper(samples.formats, '文字片段冒用核对通过的数字的 id', (data) => rewriteDoc(data, (doc) => {
|
||||
const gmv = segById(doc, 's4')
|
||||
doc.blocks[2].units.at(-1).segments.push({ kind: 'text', id: gmv.id, text: '84,613.65元', state: 'neutral' })
|
||||
return { what: '第二段末尾加一段冒用 id 的文字', seg: gmv }
|
||||
}), (s, forged) => {
|
||||
const same = s.buttons.filter((b) => b.key === `${forged.node}|${forged.seg.id}`)
|
||||
return [
|
||||
['冒用 id 的文字不显示成可点的数字', same.length === 1 && same[0].text === '74,613.65元'
|
||||
&& !s.buttons.some((b) => b.text.includes('84,613.65')), JSON.stringify(same)],
|
||||
]
|
||||
}, { seal: true })
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------- 记录页的导出按钮
|
||||
|
||||
Reference in New Issue
Block a user