Files
Ydisks-Xianyu-Helper/frontend/services/api.test.ts
T
Christ 16d63dc7c9 完善系统可靠性、登录风控与跨数据库回归
可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。

登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。

安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。

自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。

前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。

测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。

验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
2026-07-14 21:20:22 +08:00

643 lines
22 KiB
TypeScript

import { afterEach, expect, test, vi } from 'vitest';
import {
addAccount,
cancelPasswordLogin,
deleteItemPublishBatch,
checkPasswordLoginStatus,
completeQRVerification,
createNotificationChannel,
getAccountDetails,
getAutomationIssues,
getItems,
getItemPublishBatches,
getNotificationChannels,
getOrders,
getOrderAnalytics,
getReplyRules,
getShippingRules,
getSystemSettings,
getValidOrders,
logout,
importOrders,
passwordLogin,
resolveAutomationRun,
resolveDeferredAutomationTask,
syncOrders,
updateReplyRule,
deleteReplyRule,
updateAccountCookie,
updateAccountLoginInfo,
updateAccountSettings,
updateItem,
updateNotificationChannel,
updateSystemSettings,
updateShippingRule,
} from './api';
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
test('updateSystemSettings uses one atomic bulk request', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateSystemSettings({ theme_color: 'blue', renewal_log_retention_days: 15 });
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('/system-settings', expect.objectContaining({ method: 'PUT', credentials: 'include' }));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ theme_color: 'blue', renewal_log_retention_days: 15 });
});
test('getItemPublishBatches unwraps persisted batch list', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ batches: [{ id: 'batch-1', status: 'running' }] }));
vi.stubGlobal('fetch', fetchMock);
await expect(getItemPublishBatches(10)).resolves.toEqual([{ id: 'batch-1', status: 'running' }]);
expect(fetchMock).toHaveBeenCalledWith('/items/publish-batches?limit=10', expect.objectContaining({ credentials: 'include' }));
});
test('automation issue APIs expose and resolve quarantined work', async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(jsonResponse({ runs: [{ id: 1 }], pending_tasks: [{ id: 2 }] }))
.mockImplementation(() => Promise.resolve(jsonResponse({ success: true })));
vi.stubGlobal('fetch', fetchMock);
await expect(getAutomationIssues()).resolves.toEqual({ runs: [{ id: 1 }], pending_tasks: [{ id: 2 }] });
await resolveAutomationRun(1, 'continue');
await resolveDeferredAutomationTask(2, 'retry');
expect(fetchMock.mock.calls[1][0]).toBe('/automation-runs/1/resolve');
expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({ resolution: 'continue' });
expect(fetchMock.mock.calls[2][0]).toBe('/automation-pending-tasks/2/resolve');
});
test('order multipart requests use the shared authenticated form request path', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await syncOrders('acc1', 'pending_ship');
await importOrders(new FormData());
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/orders/refresh', expect.objectContaining({ method: 'POST', credentials: 'include', body: expect.any(FormData) }));
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/orders/import', expect.objectContaining({ method: 'POST', credentials: 'include', body: expect.any(FormData) }));
});
test('legacy notification channel aliases are normalized for the editor', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse([{ id: 1, name: '旧飞书', type: 'lark', config: 'not-json', enabled: true }])));
const result = await getNotificationChannels();
expect(result.data?.[0]).toMatchObject({ type: 'feishu', config: {} });
});
const jsonResponse = (body: unknown) => new Response(JSON.stringify(body), {
status: 200,
headers: { 'content-type': 'application/json' },
});
test('getOrders normalizes backend order fields', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
orders: [{ order_id: 'o1', order_status: 'shipped', quantity: '2' }],
total: 1,
}));
vi.stubGlobal('fetch', fetchMock);
const result = await getOrders(undefined, 'all', 1, 20, ' buyer ');
expect(result.data[0]).toMatchObject({ id: 'o1', status: 'shipped', quantity: 2 });
expect(result.total).toBe(1);
expect(fetchMock).toHaveBeenCalledWith('/api/orders?page=1&page_size=20&search=buyer', expect.objectContaining({ method: 'GET' }));
});
test('getOrders maps unsupported backend statuses to unknown', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({
data: [{ order_id: 'o-unknown', order_status: 'legacy_status' }],
})));
const result = await getOrders();
expect(result.data[0].status).toBe('unknown');
});
test('getValidOrders accepts wrapped responses', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
orders: [{ order_id: 'o2', order_status: 'completed', quantity: '3' }],
}));
vi.stubGlobal('fetch', fetchMock);
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-480);
const result = await getValidOrders({ start_date: '2026-01-01', end_date: '2026-01-02' });
expect(result).toEqual({
orders: [expect.objectContaining({ id: 'o2', status: 'completed', quantity: 3 })],
total: 1,
truncated: false,
});
expect(fetchMock.mock.calls[0][0]).toContain('timezone_offset_minutes=480');
});
test('getOrderAnalytics sends the browser timezone offset', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ revenue_stats: {}, daily_stats: [], status_stats: [], city_stats: [] }));
vi.stubGlobal('fetch', fetchMock);
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-330);
await getOrderAnalytics({ start_date: '2026-01-01', end_date: '2026-01-02' });
expect(fetchMock.mock.calls[0][0]).toContain('timezone_offset_minutes=330');
});
test('paid orders are normalized to pending shipment', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ data: [{ order_id: 'o-paid', order_status: 'paid' }] })));
const result = await getOrders();
expect(result.data[0].status).toBe('pending_ship');
});
test('completeQRVerification sends only the immutable target account', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, account_id: 'acc1' }));
vi.stubGlobal('fetch', fetchMock);
await completeQRVerification('session-1', 'acc1');
expect(fetchMock).toHaveBeenCalledWith('/qr-login/complete-verification/session-1', expect.objectContaining({
method: 'POST',
body: JSON.stringify({ target_account_id: 'acc1' }),
}));
});
test('deleteItemPublishBatch removes an abandoned preview', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await deleteItemPublishBatch('preview-1');
expect(fetchMock).toHaveBeenCalledWith('/items/publish-batches/preview-1', expect.objectContaining({
method: 'DELETE',
credentials: 'include',
}));
});
test('getItems normalizes multi-spec flags from backend values', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse([{
cookie_id: 'cookie-1',
item_id: 'item-1',
item_title: '普通商品',
is_multi_spec: '0',
multi_quantity_delivery: 0,
}, {
cookie_id: 'cookie-1',
item_id: 'item-2',
item_title: '多规格商品',
is_multi_spec: '1',
multi_quantity_delivery: 1,
}])));
const items = await getItems();
expect(items[0]).toMatchObject({
id: 'cookie-1-item-1',
is_multi_spec: false,
is_multi_qty_ship: false,
multi_quantity_delivery: false,
});
expect(items[1]).toMatchObject({
id: 'cookie-1-item-2',
is_multi_spec: true,
is_multi_qty_ship: true,
multi_quantity_delivery: true,
});
});
test('getSystemSettings normalizes numeric renewal retention', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({
ai_model: 'qwen-plus',
renewal_log_retention_days: 'invalid',
})));
const settings = await getSystemSettings();
expect(settings.ai_model).toBe('qwen-plus');
expect(settings.renewal_log_retention_days).toBe(10);
});
test('logout calls backend session invalidation route', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await logout();
expect(fetchMock).toHaveBeenCalledWith('/logout', expect.objectContaining({
method: 'POST',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({});
});
test('account cookie APIs include login_method when provided', async () => {
const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(jsonResponse({ success: true })));
vi.stubGlobal('fetch', fetchMock);
await addAccount('acc1', 'unb=acc1', 'qr_scan');
expect(fetchMock).toHaveBeenCalledWith('/cookies', expect.objectContaining({
method: 'POST',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
id: 'acc1',
value: 'unb=acc1',
login_method: 'qr_scan',
});
await updateAccountCookie('acc1', 'unb=acc1; x=1', 'qr_scan');
expect(fetchMock).toHaveBeenCalledWith('/cookies/acc1', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({
id: 'acc1',
value: 'unb=acc1; x=1',
login_method: 'qr_scan',
});
});
test('account editor settings use one aggregate request', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateAccountSettings('acc1', {
remark: 'main', auto_confirm: false, pause_duration: 5,
username: 'user', show_browser: true, channel_ids: [1, 2],
});
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('/cookies/acc1/settings', expect.objectContaining({ method: 'PUT' }));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
remark: 'main', auto_confirm: false, pause_duration: 5,
username: 'user', show_browser: true, channel_ids: [1, 2],
});
});
test('getAccountDetails normalizes show_browser and never exposes password', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse([{
id: 'acc1',
enabled: true,
auto_confirm: true,
remark: '主账号',
pause_duration: 0,
paused_until: 1780000000,
paused: true,
username: 'login-user',
show_browser: '1',
login_password: 'should-not-leak',
}])));
const accounts = await getAccountDetails();
expect(accounts[0]).toMatchObject({
id: 'acc1',
username: 'login-user',
show_browser: true,
login_password: '',
paused_until: 1780000000,
paused: true,
});
});
test('updateAccountLoginInfo sends exactly provided fields', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateAccountLoginInfo('acc1', { username: 'login-user', show_browser: false });
expect(fetchMock).toHaveBeenCalledWith('/cookies/acc1/login-info', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
username: 'login-user',
show_browser: false,
});
});
test('updateAccountLoginInfo can request explicit password clearing', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateAccountLoginInfo('acc1', { username: 'login-user', clear_password: true, show_browser: false });
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
username: 'login-user',
clear_password: true,
show_browser: false,
});
});
test('updateItem sends only the fields selected by the editor', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateItem('acc1', 'item1', { item_title: '改名商品' });
expect(fetchMock).toHaveBeenCalledWith('/items/acc1/item1', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
item_title: '改名商品',
});
});
test('password login service uses upstream-compatible routes', async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(jsonResponse({ success: true, session_id: 'sid', status: 'processing' }))
.mockResolvedValueOnce(jsonResponse({ status: 'success', account_id: 'acc1', cookie_count: 2 }))
.mockResolvedValueOnce(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await passwordLogin({ account_id: 'acc1', account: 'u', password: 'p' });
expect(fetchMock).toHaveBeenNthCalledWith(1, '/password-login', expect.objectContaining({
method: 'POST',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
account_id: 'acc1',
account: 'u',
password: 'p',
});
const status = await checkPasswordLoginStatus('sid');
expect(status.status).toBe('success');
expect(fetchMock).toHaveBeenNthCalledWith(2, '/password-login/check/sid', expect.objectContaining({ method: 'GET' }));
await cancelPasswordLogin('sid');
expect(fetchMock).toHaveBeenNthCalledWith(3, '/password-login/cancel/sid', expect.objectContaining({ method: 'DELETE' }));
});
test('getShippingRules exposes buyer reviewed gift rules as automation rules', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse([{
id: 12,
cookie_id: 'cookie-1',
item_id: 'item-1',
item_title: '测试商品',
name: '评价后发送赠品 - 测试商品',
trigger_type: 'buyer_reviewed',
enabled: true,
priority: 90,
config_json: '{}',
actions: [{
id: 33,
action_type: 'send_card',
card_id: 7,
card_name: '赠品库存',
delivery_count: 1,
config_json: '{"spec_name":"套餐","spec_value":"赠品"}',
enabled: true,
sort_order: 1,
}],
}])));
const rules = await getShippingRules();
expect(rules[0]).toMatchObject({
id: '12',
trigger_type: 'buyer_reviewed',
card_group_id: 7,
card_group_name: '赠品库存',
});
expect(rules[0].variants[0]).toMatchObject({
spec_name: '套餐',
spec_value: '赠品',
card_id: 7,
});
});
test('getReplyRules labels keyword matching according to engine contains behavior', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([{
id: 42,
keyword: '发货',
reply: '马上安排',
type: 'image',
image_url: 'https://img.example/reply.png',
}]));
vi.stubGlobal('fetch', fetchMock);
const rules = await getReplyRules('acc1');
expect(fetchMock).toHaveBeenCalledWith('/keywords-with-type/acc1', expect.objectContaining({ method: 'GET' }));
expect(rules[0]).toMatchObject({
id: '42',
keyword: '发货',
reply_content: '马上安排',
match_type: 'fuzzy',
type: 'image',
image_url: 'https://img.example/reply.png',
});
});
test('updateReplyRule preserves keyword image metadata when saving text edits', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateReplyRule({ id: '42', keyword: '发货', reply_content: '稍后安排', item_id: 'item-1' }, 'acc1');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('/keywords-with-type/acc1/42', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
keyword: '发货', reply: '稍后安排', item_id: 'item-1', type: 'text', image_url: '',
});
});
test('updateReplyRule clears stale content when switching reply type', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateReplyRule({ id: '42', keyword: '发货', type: 'image', image_url: 'https://img.example/new.png' }, 'acc1');
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
keyword: '发货', reply: '', item_id: '', type: 'image', image_url: 'https://img.example/new.png',
});
});
test('deleteReplyRule deletes one stable keyword row instead of replacing the list', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await deleteReplyRule('42', 'acc1');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('/keywords-with-type/acc1/42', expect.objectContaining({
method: 'DELETE',
credentials: 'include',
}));
});
test('createNotificationChannel persists email recipient as to_email config', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await createNotificationChannel({
name: '邮件通知',
type: 'email',
config: {
smtp_server: 'smtp.example.com',
smtp_port: 587,
smtp_user: 'from@example.com',
smtp_password: 'secret',
to_email: 'to@example.com',
},
});
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.type).toBe('email');
expect(JSON.parse(body.config)).toMatchObject({
to_email: 'to@example.com',
});
expect(JSON.parse(body.config)).not.toHaveProperty('from');
});
test('createNotificationChannel allows email channel to rely on system SMTP settings', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await createNotificationChannel({
name: '邮件通知',
type: 'email',
config: {
to_email: 'to@example.com',
},
});
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.type).toBe('email');
expect(JSON.parse(body.config)).toEqual({
to_email: 'to@example.com',
});
});
test('updateNotificationChannel supports partial enabled updates', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await updateNotificationChannel('7', { enabled: false });
expect(fetchMock).toHaveBeenCalledWith('/notification-channels/7', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
enabled: false,
});
});
test('updateShippingRule posts buyer reviewed gift payload to automation-rules', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 1 }));
vi.stubGlobal('fetch', fetchMock);
await updateShippingRule({
cookie_id: 'cookie-1',
item_id: 'item-1',
trigger_type: 'buyer_reviewed',
enabled: true,
variants: [{
spec_name: '',
spec_value: '',
card_id: 7,
delivery_count: 1,
enabled: true,
}],
});
expect(fetchMock).toHaveBeenCalledWith('/automation-rules', expect.objectContaining({
method: 'POST',
credentials: 'include',
}));
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body).toMatchObject({
cookie_id: 'cookie-1',
item_id: 'item-1',
name: '评价后发送赠品 - item-1',
trigger_type: 'buyer_reviewed',
});
expect(body.actions).toEqual([
expect.objectContaining({
action_type: 'send_card',
card_id: 7,
sort_order: 1,
}),
]);
});
test('updateShippingRule posts every matching card action before confirm shipment', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 3 }));
vi.stubGlobal('fetch', fetchMock);
await updateShippingRule({
cookie_id: 'cookie-1',
item_id: 'item-1',
trigger_type: 'order_paid',
enabled: true,
variants: [
{
spec_name: '套餐',
spec_value: '30天',
card_id: 8,
delivery_count: 1,
enabled: true,
},
{
spec_name: '套餐',
spec_value: '30天',
card_id: 9,
delivery_count: 2,
enabled: true,
delay_override: true,
delay_seconds: 0,
},
],
});
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.trigger_type).toBe('order_paid');
expect(body.actions).toEqual([
expect.objectContaining({
action_type: 'send_card',
card_id: 8,
sort_order: 1,
}),
expect.objectContaining({
action_type: 'send_card',
card_id: 9,
delivery_count: 2,
sort_order: 2,
}),
expect.objectContaining({
action_type: 'confirm_shipment',
sort_order: 3,
}),
]);
expect(JSON.parse(body.actions[0].config_json)).toEqual({ spec_name: '套餐', spec_value: '30天', delay_override: false });
expect(JSON.parse(body.actions[1].config_json)).toEqual({ spec_name: '套餐', spec_value: '30天', delay_override: true });
expect(body.actions[1].delay_seconds).toBe(0);
});
test('updateShippingRule preserves text actions while editing card variants', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 4 }));
vi.stubGlobal('fetch', fetchMock);
await updateShippingRule({
id: '4',
cookie_id: 'cookie-1',
item_id: 'item-1',
trigger_type: 'order_paid',
variants: [{ spec_name: '', spec_value: '', card_id: 8, delivery_count: 1, enabled: true }],
actions: [{ action_type: 'send_text', message_template: '发货提示', enabled: true, sort_order: 2 }],
});
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.actions.map((action: { action_type: string }) => action.action_type)).toEqual([
'send_card',
'send_text',
'confirm_shipment',
]);
expect(body.actions[1].message_template).toBe('发货提示');
});
test('updateShippingRule posts review request text action without card requirement', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 2 }));
vi.stubGlobal('fetch', fetchMock);
await updateShippingRule({
cookie_id: 'cookie-1',
item_id: 'item-1',
trigger_type: 'review_missing_timeout',
enabled: true,
config_json: '{"after_shipped_hours":48,"max_attempts":2}',
actions: [{
action_type: 'send_text',
message_template: '亲,方便的话麻烦给个评价~',
enabled: true,
sort_order: 1,
}],
});
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.trigger_type).toBe('review_missing_timeout');
expect(body.actions).toEqual([
expect.objectContaining({
action_type: 'send_text',
card_id: 0,
message_template: '亲,方便的话麻烦给个评价~',
}),
]);
});