import assert from 'node:assert/strict'; import { ui } from './funds-ui.mjs'; export async function afterSales(context, { owner, assistant, yesterday }) { const { step, pageFor } = context; const { action, field, dialog, fill, navigate, write, submit } = ui(context); const update = /\/api\/after-sales\/[^/]+\/update$/; const reviewEndpoint = choice => new RegExp('/api/after-sales/changes/[^/]+/' + choice + '$'); async function edit(page, customer) { await action(page.locator('#order-list tr').filter({ hasText: customer }), 'after-sale-edit').click(); const form = page.locator('#after-sale-edit-form'); await form.waitFor(); return form; } async function list(page, name) { if (!(await action(page, 'after-sale-changes-open').isVisible())) await page.locator('.order-review-menu > summary').click(); await action(page, 'after-sale-changes-open').click(); await action(dialog(page).locator('tr').filter({ hasText: name }), 'after-sale-change-open').click(); await dialog(page).locator('.after-sale-comparison').waitFor(); } async function committedWithoutReceipt(page, name, url, form, checkSaved) { const sent = []; const loseReceipt = async route => { const request = route.request(); sent.push({ key: request.headers()['idempotency-key'], body: request.postData() }); // 第一次请求已经落库。后续自动确认也拿不到回执,直到用户恢复原操作。 if (sent.length === 1) assert.equal((await route.fetch()).status(), 200); await route.abort('connectionreset'); }; await step(name + '已保存但回执中断', async () => { await page.route(url, loseReceipt); try { await submit(form); await action(dialog(page), 'retry').waitFor({ timeout: 20000 }); assert.equal(sent.length, 3, '首次提交及两次自动确认'); assert.ok(sent[0].key); assert.deepEqual(sent, [sent[0], sent[0], sent[0]]); } finally { await page.unroute(url, loseReceipt); } }); const result = await write('同一操作号恢复' + name, page, url, () => action(dialog(page), 'retry').click(), { after: checkSaved, }); assert.equal(result.request().headers()['idempotency-key'], sent[0].key); assert.equal(result.request().postData(), sent[0].body); } const otherAssistant = await pageFor('assistant', { fresh: true }); const otherOwner = await pageFor('owner', { fresh: true }); const names = ['独立售后已核对', '独立售后需驳回']; for (const [i, customer] of names.entries()) { await navigate(assistant, 'orders'); await action(assistant, 'new-after-sale').click(); const fresh = assistant.locator('#after-sale-form'); await fill(fresh, { project: '维修费用', customer, amount: '8.50' }); await field(fresh, 'payer').selectOption('assistant'); await write('店员新增' + customer, assistant, '/api/after-sales', () => submit(fresh)); let form = await edit(assistant, customer); if (i === 0) { await navigate(otherAssistant, 'orders'); const second = await edit(otherAssistant, customer); await field(second, 'amount').fill('9.25'); await write('另一窗口直接更正同日独立售后', otherAssistant, update, () => submit(second)); await field(form, 'amount').fill('9.80'); await write('店员冲突版本不能覆盖已修改金额', assistant, update, () => submit(form), { status: 409 }); assert.equal(await field(form, 'amount').isDisabled(), true); assert.equal(await action(form, 'close-after-sale').isEnabled(), true); await action(dialog(assistant), 'after-sale-edit').click(); form = assistant.locator('#after-sale-edit-form'); await field(form, 'amount').waitFor(); assert.equal(await field(form, 'amount').inputValue(), '9.25'); } await fill(form, { date: yesterday, amount: '12.80', reason: '核对维修收据' }); await form.getByRole('button', { name: '提交修改申请' }).waitFor(); const pending = () => dialog(assistant).locator('.after-sale-pending').waitFor(); if (i === 0) await committedWithoutReceipt(assistant, '店员跨日独立售后申请', update, form, pending); else await write('店员提交第二份跨日独立售后申请', assistant, update, () => submit(form), { after: pending }); assert.equal(await dialog(assistant).locator('[data-action=after-sale-change-open]').count(), 0); await action(dialog(assistant), 'close-after-sale').click(); } await list(assistant, names[0]); assert.equal(await dialog(assistant).locator('[data-action=after-sale-review-decision]').count(), 0); await action(dialog(assistant), 'after-sale-changes-close').click(); await navigate(owner, 'orders'); // 从原记录打开复核会先关闭编辑窗。关闭复核后回订单,返回列表则保留状态筛选。 await action(owner.locator('#order-list tr').filter({ hasText: names[0] }), 'after-sale-edit').click(); await action(dialog(owner), 'after-sale-change-open').click(); await dialog(owner).locator('.after-sale-comparison').waitFor(); await action(dialog(owner), 'after-sale-changes-close').click(); assert.equal(await owner.locator('dialog[open]').count(), 0); for (const [i, customer] of names.entries()) { await list(owner, customer); await action(dialog(owner), 'after-sale-changes-reload').click(); await action(dialog(owner).locator('tr').filter({ hasText: customer }), 'after-sale-change-open').click(); const approval = dialog(owner).locator('[data-action=after-sale-review-decision][data-kind=approve]'); await approval.click(); await action(dialog(owner), 'after-sale-review-cancel').click(); await approval.click(); const form = owner.locator('#after-sale-review-form'); assert.equal(await form.evaluate(f => f.checkValidity()), false); await fill(form, { note: '已核对维修收据' }); await field(form, 'verified').check(); if (i === 0) { await committedWithoutReceipt(owner, '店主同意独立售后修改', reviewEndpoint('approve'), form, () => dialog(owner).getByRole('heading', { name: '售后修改记录' }).waitFor(), ); } else { await navigate(otherOwner, 'orders'); await list(otherOwner, customer); await dialog(otherOwner).locator('[data-kind=reject]').click(); const reject = otherOwner.locator('#after-sale-review-form'); assert.equal(await reject.evaluate(f => f.checkValidity()), false); await field(reject, 'note').fill(' '); await submit(reject); await dialog(otherOwner).getByText('请填写驳回意见。', { exact: false }).waitFor(); await field(reject, 'note').fill('原始费用属实'); await write('另一店主窗口驳回独立售后修改', otherOwner, reviewEndpoint('reject'), () => submit(reject)); await write('旧窗口不能重复批准已驳回的独立售后', owner, reviewEndpoint('approve'), () => submit(form), { status: 409, }); assert.equal(await field(form, 'verified').isChecked(), false); assert.equal(await field(form, 'verified').isDisabled(), true); await action(dialog(owner), 'after-sale-change-open').click(); await dialog(owner).getByRole('heading', { name: '售后修改记录' }).waitFor(); await dialog(owner).getByText('驳回意见:原始费用属实').waitFor(); } await action(dialog(owner), 'after-sale-changes-close').click(); } }