90 lines
3.8 KiB
JavaScript
90 lines
3.8 KiB
JavaScript
'use strict';
|
|
|
|
// 主线 3e9af71 的所有旧场景都没有设置营业时段。只从旧版期望构造新增字段,实际结果原样比较。
|
|
const assert = require('node:assert/strict');
|
|
|
|
function ownerConfig(config) {
|
|
const { version, revision, ...rest } = config;
|
|
assert.equal(version, 1);
|
|
assert.ok(Number.isInteger(revision) && Object.hasOwn(rest, 'profitShareHistory'));
|
|
assert.ok(!Object.hasOwn(rest, 'businessHours'), '此期望必须来自营业时间合入前的旧版');
|
|
// 数据库 JSON 以字符串逐字比较:按主线的序列化顺序将新增键插在 revision 后。
|
|
return { version, revision, businessHours: null, ...rest };
|
|
}
|
|
|
|
function writeReceipt(body) {
|
|
return { ...body, shopSettings: ownerConfig(body.shopSettings) };
|
|
}
|
|
|
|
function expectedBody(method, endpoint, role, status, body) {
|
|
if (role !== 'owner' || status !== 200) return body;
|
|
if (method === 'GET' && endpoint === '/api/shop-settings') return ownerConfig(body);
|
|
if (
|
|
(method === 'GET' && endpoint === '/api/state') ||
|
|
(method === 'POST' && ['/api/shop-settings', '/api/printing'].includes(endpoint))
|
|
)
|
|
return writeReceipt(body);
|
|
return body;
|
|
}
|
|
|
|
// 黄金和页面用 SQLite 的 columns/rows 转储,逐笔对照用具名行并已解码 operations.result。
|
|
function expectedTables(tables) {
|
|
const result = { ...tables };
|
|
for (const name of ['meta', 'audit', 'operations']) {
|
|
const source = tables[name];
|
|
if (!source) continue;
|
|
const columns = source.columns;
|
|
const rows = (columns ? source.rows : source).map(row => {
|
|
const get = key => (columns ? row[columns.indexOf(key)] : row[key]);
|
|
const replace = (key, value) =>
|
|
columns ? row.toSpliced(columns.indexOf(key), 1, value) : { ...row, [key]: value };
|
|
if (name === 'meta' && get('key') === 'shop-settings-v1')
|
|
return replace('value', JSON.stringify(ownerConfig(JSON.parse(get('value')))));
|
|
if (name === 'audit' && get('entity') === 'shop-settings' && get('action') === '更新店铺配置') {
|
|
const detail = JSON.parse(get('detail'));
|
|
return replace(
|
|
'detail',
|
|
JSON.stringify({ ...detail, before: ownerConfig(detail.before), after: ownerConfig(detail.after) }),
|
|
);
|
|
}
|
|
if (name === 'operations') {
|
|
const stored = get('result');
|
|
const body = typeof stored === 'string' ? JSON.parse(stored) : stored;
|
|
// 这两种店主写入回执共用同一结构,操作表本身只存幂等键、摘要和返回值。
|
|
if (body?.ok === true && Object.hasOwn(body, 'shopSettings') && Object.hasOwn(body, 'printing')) {
|
|
const receipt = writeReceipt(body);
|
|
return replace('result', typeof stored === 'string' ? JSON.stringify(receipt) : receipt);
|
|
}
|
|
}
|
|
return row;
|
|
});
|
|
result[name] = columns ? { ...source, rows } : rows;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function expectedTranscript(entries) {
|
|
return entries.map(entry =>
|
|
Object.hasOwn(entry, 'body')
|
|
? { ...entry, body: expectedBody(entry.method, entry.path, entry.actor, entry.status, entry.body) }
|
|
: entry,
|
|
);
|
|
}
|
|
|
|
function expectedWriteResult(result) {
|
|
const steps = {};
|
|
for (const [label, step] of Object.entries(result.steps)) {
|
|
const requests = step.requests.map(request => ({
|
|
...request,
|
|
...(Object.hasOwn(request, 'response')
|
|
? { response: expectedBody('POST', request.endpoint, request.role, request.status, request.response) }
|
|
: {}),
|
|
...(request.database ? { database: expectedTables(request.database) } : {}),
|
|
}));
|
|
steps[label] = { ...step, requests, before: expectedTables(step.before), after: expectedTables(step.after) };
|
|
}
|
|
return { ...result, steps, final: expectedTables(result.final) };
|
|
}
|
|
|
|
module.exports = { expectedTranscript, expectedTables, expectedWriteResult };
|