合入主线登录和营业时段策略,对齐旧界面与 React 访问流程

This commit is contained in:
黄健铭 committed 2026-12-05 21:46:55 -08:00
commit 42395de23a
113 files changed
+1915 -558

No files matched your search

+14 -2
View File
@@ -33,7 +33,7 @@ function harness(fetcher: (url: string, init: RequestInit, attempt: number) => P
const client = new LedgerClient(env);
client.setUser('user-1');
const headers = (i: number) => calls[i].headers as Record<string, string>;
return { client, saved, calls, delays, headers, key: 'mm-pending-user-1' };
return { client, env, saved, calls, delays, headers, key: 'mm-pending-user-1' };
}
describe('读请求', () => {
@@ -50,13 +50,25 @@ describe('读请求', () => {
expect(down.calls).toHaveLength(3);
expect(down.delays).toEqual([400, 1200]);
});
it('使用中的账本收到营业时间拒绝立即退回登录页,登录表单本身显示错误', async () => {
const h = harness(() => httpError(403, '非营业时间', { code: 'BUSINESS_HOURS' }));
let locked = 0;
h.env.onBusinessHoursDenied = () => { locked++; };
await expect(h.client.api('/api/login', { member: 'assistant', password: '合成密码' })).rejects.toMatchObject({
status: 403, loginDiagnostic: { method: 'POST', path: '/api/login', status: 403 },
});
expect(locked).toBe(0);
await expect(h.client.api('/api/session')).rejects.toMatchObject({ code: 'BUSINESS_HOURS' });
await expect(h.client.api('/api/stock', { revision: 1 })).rejects.toMatchObject({ code: 'BUSINESS_HOURS' });
expect(locked).toBe(2);
});
it('明确业务拒绝不重试,保留服务端文字;登录类接口不带 X-Ledger-User', async () => {
for (const status of [400, 401, 403, 404, 409, 500]) {
const h = harness(() => httpError(status, '具体业务错误'));
await expect(h.client.api('/api/state')).rejects.toMatchObject({ status, message: '具体业务错误', confirmed: true });
expect(h.calls).toHaveLength(1);
}
const h = harness(() => ok({ user: null }));
const h = harness(url => ok({ user: url === '/api/login' ? { id: 'owner' } : null }));
await h.client.api('/api/session');
await h.client.api('/api/login', { member: 'owner', password: 'x' });
expect(h.headers(0)['X-Ledger-User']).toBeUndefined();
+30 -3
View File
@@ -16,7 +16,7 @@ const show = (session: SessionInfo, client: ReturnType<typeof fakeClient>['clien
describe('登录页', () => {
it('演示模式:每位成员一个进入按钮,点击后 POST /api/demo-login', async () => {
const { client, calls } = fakeClient({ '/api/demo-login': () => ({ user: {} }) });
const { client, calls } = fakeClient({ '/api/demo-login': () => ({ user: { id: "signed-in" } }) });
const onLoggedIn = vi.fn();
show(
{ ...base, mode: 'demo', loginOptions: [{ member: 'owner', label: '麦麦' }, { member: 'assistant', label: '娜娜' }] },
@@ -35,7 +35,7 @@ describe('登录页', () => {
const { client, calls } = fakeClient({
'/api/login': () => {
if (++attempts === 1) throw new Error('账号或密码不正确');
return { user: {} };
return { user: { id: "signed-in" } };
},
});
const onLoggedIn = vi.fn();
@@ -52,8 +52,35 @@ describe('登录页', () => {
await waitFor(() => expect(onLoggedIn).toHaveBeenCalledTimes(1));
expect(calls.at(-1)?.body).toEqual({ member: 'owner', password: 'right-password-1' });
});
it('登录回执缺少用户时展示可复制的诊断;剪贴板拒绝后提示手动复制', async () => {
const { client, calls } = fakeClient({ '/api/demo-login': () => ({ error: 'private-response-content' }) });
const onLoggedIn = vi.fn();
show({ ...base, mode: 'demo', loginOptions: [{ member: 'assistant', label: '阿一' }] }, client, onLoggedIn);
fireEvent.click(screen.getByRole('button', { name: /以阿一身份进入演示/ }));
const alert = await screen.findByRole('alert');
expect(alert).toHaveTextContent('进入演示失败');
expect(alert).toHaveTextContent('已收到服务器响应');
expect(alert).toHaveTextContent('POST /api/demo-login');
expect(alert).toHaveTextContent('HTTP 状态:200');
expect(alert).not.toHaveTextContent('private-response-content');
expect(calls).toHaveLength(1);
expect(onLoggedIn).not.toHaveBeenCalled();
const original = Object.getOwnPropertyDescriptor(navigator, 'clipboard');
const writeText = vi.fn().mockRejectedValueOnce(new Error('denied')).mockResolvedValueOnce(undefined);
Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText } });
try {
fireEvent.click(screen.getByRole('button', { name: '复制诊断信息' }));
fireEvent.click(await screen.findByRole('button', { name: '请长按或选中上方文字复制' }));
expect(await screen.findByRole('button', { name: '已复制' })).toBeInTheDocument();
expect(writeText.mock.calls[1][0]).toContain('HTTP 状态:200');
expect(writeText.mock.calls[1][0]).not.toMatch(/private-response-content|阿一|assistant/);
} finally {
if (original) Object.defineProperty(navigator, 'clipboard', original);
else Reflect.deleteProperty(navigator, 'clipboard');
}
});
it('试用空库:初始化表单校验密码长度,提交店铺与店主资料', async () => {
const { client, calls } = fakeClient({ '/api/setup': () => ({ user: {} }) });
const { client, calls } = fakeClient({ '/api/setup': () => ({ user: { id: "signed-in" } }) });
const onLoggedIn = vi.fn();
show({ ...base, setup: true }, client, onLoggedIn);
fireEvent.change(screen.getByLabelText('店铺名称'), { target: { value: '新店' } });
+72
View File
@@ -0,0 +1,72 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, expect, it, vi } from 'vitest';
import { ShopForm } from '../src/pages/Settings/Shop';
import { fakeClient } from './helpers';
afterEach(cleanup);
it('营业时间使用原始时间字符串,保存关店设置后才通知页面', async () => {
const shop = {
revision: 1,
storeName: '测试店铺',
receiptSubtitle: '',
contact: '',
businessHours: { open: '09:00', close: '18:00' },
creditCardRatePermille: 6,
profitSharePercent: 30,
profitShareEffectiveMonth: '2026-12',
feeDefaults: { certificate: '4', packaging: '3', shipping: '6', packagingPayer: 'owner' },
members: [{ member: 'owner', label: '店主' }],
};
const { client, calls } = fakeClient({ '/api/shop-settings': () => ({ shopSettings: shop }) });
client.setUser('owner');
const changed = vi.fn(),
notice = vi.fn();
const view = render(
<ShopForm
client={client}
shop={shop}
today="2026-11-15"
members={shop.members}
team
busy={false}
dirty={false}
onDirty={vi.fn()}
onBusy={vi.fn()}
onSaved={vi.fn()}
notice={notice}
/>,
);
window.addEventListener('ledger-business-hours-changed', changed);
try {
const open = () => screen.getByLabelText('开始时间');
const close = () => screen.getByLabelText('结束时间');
const submit = () => fireEvent.submit(view.container.querySelector('form')!);
expect(open()).toHaveValue('09:00');
expect(close()).toHaveValue('18:00');
submit();
await waitFor(() => expect(notice).toHaveBeenCalledWith('店铺配置已保存'));
expect(calls[0].body).not.toHaveProperty('businessHours');
expect(changed).not.toHaveBeenCalled();
fireEvent.change(open(), { target: { value: '18:00' } });
submit();
expect(await screen.findByText('开始和结束时间不能相同')).toBeInTheDocument();
fireEvent.change(close(), { target: { value: '' } });
submit();
expect(await screen.findByText('请完整填写开始和结束时间')).toBeInTheDocument();
expect(calls).toHaveLength(1);
fireEvent.change(close(), { target: { value: '02:00' } });
submit();
await waitFor(() => expect(changed).toHaveBeenCalledTimes(1));
expect(calls[1].body).toHaveProperty('businessHours', { open: '18:00', close: '02:00' });
fireEvent.change(open(), { target: { value: '' } });
fireEvent.change(close(), { target: { value: '' } });
submit();
await waitFor(() => expect(changed).toHaveBeenCalledTimes(2));
expect(calls[2].body).toHaveProperty('businessHours', null);
} finally {
window.removeEventListener('ledger-business-hours-changed', changed);
}
});