107 lines
4.5 KiB
JavaScript
107 lines
4.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
|
|
export async function exerciseOrderConflicts({
|
|
base,
|
|
day,
|
|
passwords,
|
|
session,
|
|
post,
|
|
open,
|
|
field,
|
|
legacyForm,
|
|
step,
|
|
submit,
|
|
route,
|
|
issue,
|
|
}) {
|
|
for (const conflict of ['revision', 'date']) {
|
|
const id = await issue(conflict === 'revision' ? '并发入账' : '填单跨过营业日');
|
|
const page = await open('assistant', id);
|
|
const form = await legacyForm(page, 'expense');
|
|
await field(form, 'amount').fill('5.76');
|
|
await field(form, 'note').fill('新增支出');
|
|
await field(form, 'change_reason').fill('核实后补登');
|
|
await field(form, 'verified').check();
|
|
if (conflict === 'revision') {
|
|
await post('owner', route(id) + '/expense', { date: day, amount: '1', note: '同时另记支出', payer: 'owner' });
|
|
} else {
|
|
await session.day('2026-11-21');
|
|
await post('assistant', '/api/login', { member: 'assistant', password: passwords.assistant });
|
|
await page
|
|
.context()
|
|
.addCookies([{ name: 'mm_session', value: session.cookies.assistant.slice('mm_session='.length), url: base }]);
|
|
}
|
|
await step(conflict === 'revision' ? '并发改账后拒绝过期的直接登记' : '跨过营业日后拒绝原直接登记', async () => {
|
|
const [response] = await Promise.all([
|
|
page.waitForResponse(
|
|
r => new URL(r.url()).pathname === route(id) + '/expense' && r.request().method() === 'POST',
|
|
),
|
|
form.locator('button.primary').click(),
|
|
]);
|
|
assert.equal(response.status(), conflict === 'revision' ? 409 : 403, await response.text());
|
|
await form.locator('#order-action-change-error .error').waitFor();
|
|
assert.equal(await field(form, 'amount').inputValue(), '5.76');
|
|
assert.equal(await field(form, 'verified').isChecked(), false);
|
|
if (conflict === 'revision') assert.equal(await form.locator('button.primary').isDisabled(), true);
|
|
else assert.equal(await form.getAttribute('data-order-direct'), 'false');
|
|
});
|
|
if (conflict === 'date') {
|
|
await field(form, 'verified').check();
|
|
await submit('跨过营业日保留原输入转为审批', page, form, route(id) + '/change');
|
|
}
|
|
}
|
|
|
|
// 提交已经落库、响应丢失时,复用详情内的恢复按钮及原操作号。
|
|
await session.day(day);
|
|
const id = await issue('订单记账回执中断');
|
|
const page = await open('assistant', id);
|
|
const form = await legacyForm(page, 'expense');
|
|
await field(form, 'amount').fill('2.50');
|
|
await field(form, 'note').fill('账务回执测试');
|
|
await field(form, 'change_reason').fill('核准凭据');
|
|
await field(form, 'verified').check();
|
|
const endpoint = base + route(id) + '/expense';
|
|
const attempts = [];
|
|
await page.route(endpoint, async intercepted => {
|
|
const req = intercepted.request();
|
|
attempts.push({ key: req.headers()['idempotency-key'], data: req.postDataJSON() });
|
|
const response = await intercepted.fetch();
|
|
assert.equal(response.status(), 200, await response.text());
|
|
await intercepted.abort('failed');
|
|
});
|
|
try {
|
|
await step('订单记账后连续收不到回执', async () => {
|
|
await form.locator('button.primary').click();
|
|
await page
|
|
.locator('#order-dialog')
|
|
.getByText(/连接中断,提交结果待确认/)
|
|
.first()
|
|
.waitFor();
|
|
assert.equal(attempts.length, 3);
|
|
assert.ok(attempts[0].key);
|
|
assert.deepEqual(attempts[1], attempts[0]);
|
|
assert.deepEqual(attempts[2], attempts[0]);
|
|
assert.equal(await field(form, 'amount').inputValue(), '2.50');
|
|
assert.equal(await form.locator('button.primary').isDisabled(), true, '结果待确认时仍可再次保存');
|
|
});
|
|
} finally {
|
|
await page.unroute(endpoint);
|
|
}
|
|
await step('从订单详情原样恢复同笔记账', async () => {
|
|
const retry = page.locator('#order-dialog[open]').getByRole('button', { name: '恢复原操作', exact: true });
|
|
const [response] = await Promise.all([
|
|
page.waitForResponse(r => r.url() === endpoint && r.request().method() === 'POST'),
|
|
retry.click(),
|
|
]);
|
|
assert.equal(response.status(), 200);
|
|
assert.equal(response.request().headers()['idempotency-key'], attempts[0].key);
|
|
assert.deepEqual(response.request().postDataJSON(), attempts[0].data);
|
|
await form.waitFor({ state: 'hidden' });
|
|
assert.equal(await page.locator('#order-dialog[open]').count(), 1);
|
|
assert.equal(
|
|
(await session.get('assistant', route(id))).expenses.filter(row => row.note === '账务回执测试').length,
|
|
1,
|
|
);
|
|
});
|
|
}
|