Files
Ydisks-Xianyu-Helper/frontend/request.test.ts
T
ChristandClaude Opus 4.8 980de41b53 chore: 重构配套改动 + 包文档 + 构建产物清理
automation 重构期间的配套修改:
- browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go
- db:cookies/orders_ext/extras/sessions/users/password/repository 等调整
- xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua
- server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers
- frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage
- auth、notify、webui、go.mod 等

本次会话补充:
- cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档
- .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库)
- internal/webui/static/index.html 重新构建

全量 go build/vet/test + race + 前端 test/build 通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 21:41:03 +08:00

57 lines
2.0 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from 'vitest';
import { get, post, postForm } from './request';
afterEach(() => vi.unstubAllGlobals());
describe('request helpers', () => {
test('encodes query parameters and includes credentials', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'content-type': 'application/json' },
}));
vi.stubGlobal('fetch', fetchMock);
await expect(get('/items', { page: 2, keyword: 'a b', ignored: undefined })).resolves.toEqual({ ok: true });
expect(fetchMock).toHaveBeenCalledWith('/items?page=2&keyword=a+b', expect.objectContaining({
method: 'GET',
credentials: 'include',
}));
});
test('serializes JSON bodies', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ success: true }), {
status: 200,
headers: { 'content-type': 'application/json' },
}));
vi.stubGlobal('fetch', fetchMock);
await post('/login', { username: 'admin' });
expect(fetchMock).toHaveBeenCalledWith('/login', expect.objectContaining({
method: 'POST',
body: JSON.stringify({ username: 'admin' }),
headers: { 'Content-Type': 'application/json' },
}));
});
test('surfaces backend errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ detail: 'bad request' }), {
status: 400,
headers: { 'content-type': 'application/json' },
})));
await expect(get('/bad')).rejects.toThrow('bad request');
});
test('posts FormData without forcing a content type', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response('ok', { status: 200 }));
vi.stubGlobal('fetch', fetchMock);
const form = new FormData();
form.set('name', 'value');
await expect(postForm('/upload', form)).resolves.toBe('ok');
expect(fetchMock).toHaveBeenCalledWith('/upload', {
method: 'POST',
credentials: 'include',
body: form,
});
});
});