冻结十六组逐笔对照的真实步骤与写入动作
This commit is contained in:
1 parent
716ae7e8ee
commit
5fbcf45911
3 files changed
+382
-31
No files matched your search
@@ -0,0 +1,96 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
|
||||
// 清单来自冻结提交 54c0587 的场景和步骤。运行时独立于被测场景和运行产物。
|
||||
const frozen = JSON.parse(fs.readFileSync(new URL('../writes/frozen-steps.json', import.meta.url), 'utf8'));
|
||||
export const fullGroups = Object.keys(frozen);
|
||||
assert.equal(fullGroups.length, 16, '冻结组数');
|
||||
assert.equal(Object.values(frozen).reduce((total, steps) => total + Object.keys(steps).length, 0), 210, '冻结步骤数');
|
||||
// 这三个现有的单跑入口复用 funds 的连续子场景。
|
||||
const fundSteps = Object.entries(frozen.funds);
|
||||
const standalone = {
|
||||
'funds-legacy-pools': Object.fromEntries(fundSteps.slice(0, 2)),
|
||||
'funds-core': Object.fromEntries(fundSteps.slice(0, 45)),
|
||||
'funds-transfer': Object.fromEntries(fundSteps.slice(45)),
|
||||
};
|
||||
export const contracts = { ...frozen, ...standalone };
|
||||
|
||||
export function assertSequence(actual, expected, name) {
|
||||
for (let i = 0; i < Math.max(expected.length, actual.length); i++) {
|
||||
if (actual[i] !== expected[i]) {
|
||||
assert.fail(`${name}第 ${i + 1} 项应为“${expected[i] ?? '结束'}”,实际为“${actual[i] ?? '结束'}”`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 普通写入冻结动作和返回状态。专测报价的步骤保留报价命中,其他步骤允许页面发起附带报价。
|
||||
export function assertStepActions(group, side, routes) {
|
||||
const observed = new Map();
|
||||
for (const { template, action, [side]: hits } of routes.actions) {
|
||||
for (const hit of hits) {
|
||||
if (hit.group !== group) continue;
|
||||
const token = [template, action, hit.status].filter(value => value !== '').join(' ');
|
||||
if (!observed.has(hit.step)) observed.set(hit.step, new Set());
|
||||
observed.get(hit.step).add(token);
|
||||
}
|
||||
}
|
||||
for (const [step, required] of Object.entries(contracts[group])) {
|
||||
const actual = observed.get(step) ?? new Set();
|
||||
for (const token of required.filter(action => action.startsWith('/api/'))) {
|
||||
assert.ok(actual.has(token), `${group} ${side}「${step}」未观测到 ${token}`);
|
||||
}
|
||||
for (const token of actual) {
|
||||
if (!token.split(' ')[0].endsWith('/quote')) {
|
||||
assert.ok(required.includes(token), `${group} ${side}「${step}」发生了未冻结的动作 ${token}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 没有浏览器 POST 的七步也留下独立证据:表单拦截确有点击,预览确有 GET,越权操作确有 403。
|
||||
export async function observeStep(group, label, fn, pages, session) {
|
||||
const expected = contracts[group][label]?.[0];
|
||||
if (!expected || expected.startsWith('/api/')) return fn();
|
||||
|
||||
const [kind, field, value] = expected.split(' ');
|
||||
if (kind === 'session') {
|
||||
const start = session.entries.length;
|
||||
const result = await fn();
|
||||
const observed = session.entries.slice(start).map(r => ['session', r.actor, r.method, r.path, r.status].join(' '));
|
||||
assert.ok(observed.includes(expected), label + ' 未观测到 ' + expected);
|
||||
return result;
|
||||
}
|
||||
const owner = pages.get('owner');
|
||||
if (kind === 'GET') {
|
||||
const observed = [];
|
||||
const collect = r => observed.push([r.request().method(), new URL(r.url()).pathname, r.status()].join(' '));
|
||||
owner.on('response', collect);
|
||||
try {
|
||||
const result = await fn();
|
||||
assert.ok(observed.includes(expected), label + ' 未观测到 ' + expected);
|
||||
return result;
|
||||
} finally {
|
||||
owner.off('response', collect);
|
||||
}
|
||||
}
|
||||
assert.equal(kind, 'issue-input', '未知的冻结步骤证据 ' + expected);
|
||||
// 在校验事件之前采集用户真实点击时的原始输入,拒绝开单时本来就不会产生业务请求。
|
||||
const attempt = await owner.evaluateHandle(field => {
|
||||
let submitted;
|
||||
const onClick = event => {
|
||||
if (!event.isTrusted || !event.target.closest('#order-form button[type=submit], button[data-action=issue]')) return;
|
||||
const input = document.querySelector(`#order-form [name="${field}"], #order-form [data-field="${field}"] input`);
|
||||
submitted = input.value;
|
||||
};
|
||||
document.addEventListener('click', onClick, true);
|
||||
return { read: () => submitted, stop: () => document.removeEventListener('click', onClick, true) };
|
||||
}, field);
|
||||
try {
|
||||
const result = await fn();
|
||||
assert.equal(await attempt.evaluate(a => a.read()), value, label + ' 提交时的原始输入');
|
||||
return result;
|
||||
} finally {
|
||||
await attempt.evaluate(a => a.stop());
|
||||
await attempt.dispose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user