241 lines
11 KiB
TypeScript
241 lines
11 KiB
TypeScript
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
||||
|
|
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
|
|||
|
|
import { MaterialPoolsPage } from '../src/pages/MaterialPools/MaterialPoolsPage';
|
|||
|
|
import { fakeClient } from './helpers';
|
|||
|
|
import type { LedgerState } from '../src/shared';
|
|||
|
|
|
|||
|
|
const today = '2026-11-28',
|
|||
|
|
state: LedgerState = { today, mode: 'demo', user: { id: 'owner', role: 'owner', member: 'owner', name: '店主' } };
|
|||
|
|
const purchase = {
|
|||
|
|
id: 'purchase-1',
|
|||
|
|
date: today,
|
|||
|
|
mg: 1250,
|
|||
|
|
amount: 5050,
|
|||
|
|
revision: 4,
|
|||
|
|
actualpayer: 'owner',
|
|||
|
|
note: '原凭据',
|
|||
|
|
};
|
|||
|
|
const pool = {
|
|||
|
|
id: today,
|
|||
|
|
date: today,
|
|||
|
|
usedMg: 0,
|
|||
|
|
totalMg: 1250,
|
|||
|
|
totalAmount: 5050,
|
|||
|
|
remainingMg: 1250,
|
|||
|
|
remainingCost: 5050,
|
|||
|
|
averageRate: 4040,
|
|||
|
|
status: 'unused',
|
|||
|
|
purchases: [purchase],
|
|||
|
|
usages: [],
|
|||
|
|
byPayer: { owner: { purchased: 5050 } },
|
|||
|
|
};
|
|||
|
|
beforeEach(() => {
|
|||
|
|
localStorage.clear();
|
|||
|
|
window.history.replaceState({}, '', '/next/material-pools');
|
|||
|
|
HTMLDialogElement.prototype.showModal = function () {
|
|||
|
|
this.setAttribute('open', '');
|
|||
|
|
};
|
|||
|
|
HTMLDialogElement.prototype.close = function () {
|
|||
|
|
this.removeAttribute('open');
|
|||
|
|
};
|
|||
|
|
});
|
|||
|
|
afterEach(() => vi.restoreAllMocks());
|
|||
|
|
it('saves the raw native form fields, clears the draft and opens the saved pool after refreshing state', async () => {
|
|||
|
|
const { client, calls } = fakeClient({
|
|||
|
|
'/api/material-pools': () => ({ rows: [pool] }),
|
|||
|
|
'/api/material-purchases': () => ({ poolId: today }),
|
|||
|
|
'/api/state': () => state,
|
|||
|
|
});
|
|||
|
|
client.setUser('owner');
|
|||
|
|
const onState = vi.fn();
|
|||
|
|
render(<MaterialPoolsPage state={state} client={client} onState={onState} />);
|
|||
|
|
await screen.findByRole('button', { name: /^查看$/ });
|
|||
|
|
fireEvent.change(screen.getByLabelText('购买克重(g)'), { target: { value: '1.250' } });
|
|||
|
|
fireEvent.change(screen.getByLabelText('购买总价(元)'), { target: { value: '50.50' } });
|
|||
|
|
fireEvent.change(screen.getByLabelText('备注(选填)'), { target: { value: ' 原凭据 ' } });
|
|||
|
|
expect(JSON.parse(localStorage.getItem('mm-material-purchase-owner')!)).toMatchObject({
|
|||
|
|
grams: '1.250',
|
|||
|
|
note: '原凭据',
|
|||
|
|
});
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: '保存购料' }));
|
|||
|
|
await screen.findByText('原凭据');
|
|||
|
|
expect(calls.filter(c => c.method === 'POST').map(c => c.body)).toEqual([
|
|||
|
|
{ date: today, grams: '1.250', total: '50.50', payer: 'owner', note: '原凭据' },
|
|||
|
|
]);
|
|||
|
|
expect(onState).toHaveBeenCalledWith(state);
|
|||
|
|
expect(localStorage.getItem('mm-material-purchase-owner')).toBeNull();
|
|||
|
|
});
|
|||
|
|
it('uses the original revision and legacy payer field for edit and delete', async () => {
|
|||
|
|
const routes = {
|
|||
|
|
'/api/material-pools': () => ({ rows: [pool] }),
|
|||
|
|
'/api/material-purchases/purchase-1': () => ({ purchase }),
|
|||
|
|
'/api/material-purchases/purchase-1/edit': () => ({ poolId: today }),
|
|||
|
|
'/api/material-purchases/purchase-1/delete': () => ({ previousPoolId: today }),
|
|||
|
|
'/api/state': () => state,
|
|||
|
|
};
|
|||
|
|
const { client, calls } = fakeClient(routes);
|
|||
|
|
client.setUser('owner');
|
|||
|
|
render(<MaterialPoolsPage state={state} client={client} />);
|
|||
|
|
fireEvent.click(await screen.findByRole('button', { name: /^查看$/ }));
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: /^修改$/ }));
|
|||
|
|
const edit = within(await screen.findByRole('dialog'));
|
|||
|
|
await edit.findByRole('button', { name: '保存修改' });
|
|||
|
|
expect(edit.getByLabelText('付款人')).toHaveValue('owner');
|
|||
|
|
fireEvent.click(edit.getByRole('button', { name: '保存修改' }));
|
|||
|
|
await screen.findByText('购料修改已保存');
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: /^删除$/ }));
|
|||
|
|
fireEvent.click(await screen.findByRole('button', { name: '确认删除购料' }));
|
|||
|
|
await waitFor(() => expect(calls.filter(c => c.method === 'POST')).toHaveLength(2));
|
|||
|
|
expect(calls.filter(c => c.method === 'POST').map(c => c.body)).toEqual([
|
|||
|
|
{ date: today, grams: '1.250', total: '50.50', payer: 'owner', note: '原凭据', revision: 4 },
|
|||
|
|
{ revision: 4 },
|
|||
|
|
]);
|
|||
|
|
});
|
|||
|
|
it('validates and confirms the rolling preview before requesting a cutover', async () => {
|
|||
|
|
const { client, calls } = fakeClient({
|
|||
|
|
'/api/material-pools': () => ({ rows: [], rolling: { enabled: false, canEnable: true } }),
|
|||
|
|
'/api/material-pools/rolling-preview': () => ({
|
|||
|
|
canEnable: true,
|
|||
|
|
date: today,
|
|||
|
|
revision: 'hash',
|
|||
|
|
opening: { mg: 1250, amount: 5050 },
|
|||
|
|
affectedOrderCount: 0,
|
|||
|
|
relatedOrders: [],
|
|||
|
|
affectedDates: [],
|
|||
|
|
summary: { affectedOrderCount: 0, goodsDelta: 0, profitDelta: 0 },
|
|||
|
|
}),
|
|||
|
|
'/api/material-pools/rolling-enable': () => ({}),
|
|||
|
|
'/api/state': () => state,
|
|||
|
|
});
|
|||
|
|
client.setUser('owner');
|
|||
|
|
render(<MaterialPoolsPage state={state} client={client} />);
|
|||
|
|
fireEvent.click(await screen.findByRole('button', { name: '核对并启用跨日结转' }));
|
|||
|
|
const check = await screen.findByRole('checkbox');
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: '确认启用跨日结转' }));
|
|||
|
|
expect(calls.filter(c => c.method === 'POST')).toHaveLength(0);
|
|||
|
|
fireEvent.click(check);
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: '确认启用跨日结转' }));
|
|||
|
|
await screen.findByText('跨日结转已启用');
|
|||
|
|
expect(calls.find(c => c.method === 'POST')!.body).toEqual({ date: today, revision: 'hash', verified: true });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('posts a historical correction only after verification and allows an owner to reject a stale request', async () => {
|
|||
|
|
const oldDay = '2026-11-27',
|
|||
|
|
original = { ...purchase, date: oldDay, payer: 'owner', ownerReviewRequired: true };
|
|||
|
|
const group = { mg: 0, amount: 0, count: 0 },
|
|||
|
|
todayPool = { ...pool, opening: group, todayPurchases: { ...group, items: [] }, todayReturns: group, returns: [] };
|
|||
|
|
const historical = { ...pool, date: oldDay, id: oldDay, purchases: [original] };
|
|||
|
|
const preview = {
|
|||
|
|
before: original,
|
|||
|
|
after: { ...original, amount: 5250 },
|
|||
|
|
affectedDates: [],
|
|||
|
|
relatedOrders: [],
|
|||
|
|
summary: { affectedOrderCount: 0, goodsDelta: 0, profitDelta: 0 },
|
|||
|
|
};
|
|||
|
|
const change = {
|
|||
|
|
id: 'review-1',
|
|||
|
|
purchase_revision: 4,
|
|||
|
|
action: 'edit',
|
|||
|
|
status: 'pending',
|
|||
|
|
stale: true,
|
|||
|
|
canApprove: false,
|
|||
|
|
requester_name: '娜娜',
|
|||
|
|
created: '2026-11-28T00:00:00Z',
|
|||
|
|
reason: '复核发票',
|
|||
|
|
preview,
|
|||
|
|
};
|
|||
|
|
const { client, calls } = fakeClient({
|
|||
|
|
'/api/material-pools': () => ({
|
|||
|
|
rows: [historical],
|
|||
|
|
current: todayPool,
|
|||
|
|
rolling: { enabled: true, cutover: today },
|
|||
|
|
}),
|
|||
|
|
'/api/material-purchases/purchase-1': () => ({ purchase: original }),
|
|||
|
|
'/api/material-purchases/purchase-1/change': () => ({}),
|
|||
|
|
'/api/material-purchase-changes': () => ({ rows: [change], counts: { approved: 0, pending: 1, rejected: 0 } }),
|
|||
|
|
'/api/material-purchase-changes/review-1': () => change,
|
|||
|
|
'/api/material-purchase-changes/review-1/reject': () => ({}),
|
|||
|
|
'/api/state': () => state,
|
|||
|
|
});
|
|||
|
|
client.setUser('owner');
|
|||
|
|
render(<MaterialPoolsPage state={state} client={client} />);
|
|||
|
|
fireEvent.click((await screen.findAllByRole('button', { name: /^查看$/ }))[1]);
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: '申请修改' }));
|
|||
|
|
const dlg = within(await screen.findByRole('dialog'));
|
|||
|
|
await dlg.findByLabelText('更正原因');
|
|||
|
|
fireEvent.change(dlg.getByLabelText('购买总价(元)'), { target: { value: '52.50' } });
|
|||
|
|
fireEvent.change(dlg.getByLabelText('更正原因'), { target: { value: '发票补录' } });
|
|||
|
|
fireEvent.click(dlg.getByRole('button', { name: '提交修改申请' }));
|
|||
|
|
expect(calls.some(c => c.method === 'POST')).toBe(false);
|
|||
|
|
fireEvent.click(dlg.getByRole('checkbox'));
|
|||
|
|
fireEvent.click(dlg.getByRole('button', { name: '提交修改申请' }));
|
|||
|
|
await screen.findByText('购料更正申请已提交');
|
|||
|
|
expect(calls.find(c => c.method === 'POST')!.body).toMatchObject({
|
|||
|
|
date: oldDay,
|
|||
|
|
total: '52.50',
|
|||
|
|
revision: 4,
|
|||
|
|
action: 'edit',
|
|||
|
|
reason: '发票补录',
|
|||
|
|
verified: true,
|
|||
|
|
});
|
|||
|
|
fireEvent.click(await screen.findByRole('button', { name: '查看影响并复核' }));
|
|||
|
|
const r = within(await screen.findByRole('dialog'));
|
|||
|
|
await r.findByRole('button', { name: '驳回申请' });
|
|||
|
|
expect(r.queryByRole('button', { name: '批准购料更正' })).toBeNull();
|
|||
|
|
fireEvent.click(r.getByRole('button', { name: '驳回申请' }));
|
|||
|
|
await r.findByText(/请填写驳回意见/);
|
|||
|
|
fireEvent.change(r.getByRole('textbox'), { target: { value: '原单已变更' } });
|
|||
|
|
fireEvent.click(r.getByRole('button', { name: '驳回申请' }));
|
|||
|
|
await screen.findByText('购料更正申请已驳回');
|
|||
|
|
expect(calls.find(c => c.url.endsWith('/reject'))?.body).toEqual({ note: '原单已变更' });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('preserves signed transfer direction and string revision in legacy settlement', async () => {
|
|||
|
|
const legacy = { ...state, user: { ...state.user, member: '' } };
|
|||
|
|
const p = {
|
|||
|
|
...pool,
|
|||
|
|
revision: 9,
|
|||
|
|
settlement: { settlementDue: -3650, pending: false, assistantInventoryCredit: 3650 },
|
|||
|
|
};
|
|||
|
|
const { client, calls } = fakeClient({
|
|||
|
|
'/api/material-pools': () => ({ rows: [p] }),
|
|||
|
|
['/api/material-pools/' + today + '/settlement']: () => ({}),
|
|||
|
|
'/api/state': () => legacy,
|
|||
|
|
});
|
|||
|
|
client.setUser('owner');
|
|||
|
|
render(<MaterialPoolsPage state={legacy} client={client} />);
|
|||
|
|
fireEvent.click(await screen.findByRole('button', { name: /^查看$/ }));
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: '登记实际交款/返款' }));
|
|||
|
|
const dlg = within(await screen.findByRole('dialog'));
|
|||
|
|
expect(dlg.getByLabelText('转款方向')).toHaveValue('toAssistant');
|
|||
|
|
fireEvent.click(dlg.getByRole('checkbox'));
|
|||
|
|
fireEvent.click(dlg.getByRole('button', { name: '保存实际交款' }));
|
|||
|
|
await screen.findByText('本批料款交款已保存');
|
|||
|
|
expect(calls.find(c => c.method === 'POST')!.body).toEqual({
|
|||
|
|
date: today,
|
|||
|
|
amount: '36.50',
|
|||
|
|
direction: 'toAssistant',
|
|||
|
|
note: '',
|
|||
|
|
verified: true,
|
|||
|
|
revision: '9',
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('retains the expanded pool across tabs and leaves a different pending operation to its owner', async () => {
|
|||
|
|
const { client, calls } = fakeClient({ '/api/material-pools': () => ({ rows: [pool] }) });
|
|||
|
|
client.setUser('owner');
|
|||
|
|
const view = render(<MaterialPoolsPage client={client} state={state} />);
|
|||
|
|
fireEvent.click(await screen.findByRole('button', { name: /^查看$/ }));
|
|||
|
|
await screen.findByText('原凭据');
|
|||
|
|
view.unmount();
|
|||
|
|
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(<MaterialPoolsPage client={client} state={state} onRecoverOther={other} />);
|
|||
|
|
await screen.findByText('原凭据');
|
|||
|
|
fireEvent.click(screen.getByRole('button', { name: '恢复原操作' }));
|
|||
|
|
expect(other).toHaveBeenCalledTimes(1);
|
|||
|
|
expect(calls.some(c => c.method === 'POST')).toBe(false);
|
|||
|
|
});
|