import { beforeEach, it, expect, vi } from 'vitest'; import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import { PriceLocksPage } from '../src/pages/PriceLocks/PriceLocksPage'; import { fakeClient } from './helpers'; import type { LedgerState, SessionInfo } from '../src/shared'; const state: LedgerState = { today: '2026-11-28', mode: 'demo', user: { id: 'owner', role: 'owner', member: 'owner', name: '店主' }, }; const session: SessionInfo = { user: state.user, today: state.today, setup: true, mode: 'demo' }; const line = { lineId: 'line-1', name: '足金999', quantity: '1.250', goldPrice: '950.00', laborRate: '10.00', laborMode: 'piece', pieces: 2, material: { rate: '820.00', payer: 'owner', verified: true }, }; beforeEach(() => { localStorage.clear(); window.history.replaceState({}, '', '/next/price-lock-create'); HTMLDialogElement.prototype.showModal = function () { this.setAttribute('open', ''); }; HTMLDialogElement.prototype.close = function () { this.removeAttribute('open'); }; }); it('preserves the stable line identity and native string values when editing an existing two-item price lock', async () => { window.history.replaceState({}, '', '/next/price-lock-create?edit=lock-1'); const lock = { id: 'lock-1', date: state.today, status: 'pending', number: '20261128-X' }, inputs = { date: state.today, customer: '复核', salesperson: '店主', member: 'owner', lines: [line, { ...line, lineId: 'line-2', pieces: 1 }], payment: { amount: '2000.00', account: '麦麦微信', verified: true }, discount: '0', }; const { client, calls } = fakeClient({ '/api/price-locks/lock-1/edit-context': () => ({ eligible: true, lock, inputs, requiresApproval: false, revision: 7, }), '/api/material-pools': () => ({ rows: [] }), '/api/price-locks/lock-1/edit': () => ({ price_lock_id: lock.id }), '/api/state': () => state, }); client.setUser('owner'); render(); fireEvent.change(await screen.findByLabelText('修改原因'), { target: { value: '核实件数' } }); fireEvent.click(screen.getByRole('checkbox', { name: /已核实整单商品/ })); fireEvent.click(screen.getByRole('button', { name: '保存锁价修改' })); await waitFor(() => expect(calls.filter(c => c.method === 'POST')).toHaveLength(1)); expect(calls.find(c => c.method === 'POST')).toMatchObject({ url: '/api/price-locks/lock-1/edit', body: { revision: 7, verified: true, reason: '核实件数', lines: [ { lineId: 'line-1', quantity: '1.250', pieces: '2' }, { lineId: 'line-2', quantity: '1.250', pieces: '1' }, ], }, }); }); it('restores a legacy single-line lock draft into the editable multi-item form', async () => { localStorage.setItem( 'mm-price-lock-draft-owner', JSON.stringify({ date: state.today, customer: '旧单草稿', line: { name: '旧金链', quantity: '1.234', goldPrice: '700', laborMode: 'gram', laborRate: '20', pieces: '1' }, material: { rate: '630', payer: 'owner', verified: true }, payment: { account: '麦麦现金', verified: false }, }), ); const { client } = fakeClient({ '/api/material-pools': () => ({ rows: [] }) }); client.setUser('owner'); render(); expect(screen.getByLabelText('锁定克重(g)')).toHaveValue(1.234); expect(screen.getByLabelText('收款方式')).toHaveValue('现金'); expect(screen.getByLabelText('原锁进货料价(元/克)')).toHaveValue(630); fireEvent.click(screen.getByRole('button', { name: '+ 添加商品' })); expect(screen.getAllByLabelText('锁定克重(g)')).toHaveLength(2); fireEvent.click(screen.getAllByRole('button', { name: '移除商品' })[1]); expect(screen.getByLabelText('黄金商品名称(可自定义)')).toHaveValue('旧金链'); }); it('hands another page its own pending job without sending it from the lock form', async () => { const { client, calls } = fakeClient({ '/api/material-pools': () => ({ rows: [] }) }); client.setUser('owner'); localStorage.setItem( 'mm-pending-owner', JSON.stringify({ key: 'pending-order-0001', url: '/api/orders/issue', data: {}, userId: 'owner' }), ); const other = vi.fn().mockResolvedValue(undefined); render( , ); fireEvent.click(screen.getByRole('button', { name: '恢复原操作' })); expect(other).toHaveBeenCalledTimes(1); expect(calls.some(c => c.method === 'POST')).toBe(false); });