阶段十:完成注释门禁最终验收

This commit is contained in:
Christ committed 2026-08-15 05:52:50 +08:00
1 parent d1a165f897
commit f30a1f68f8
29 files changed
+509 -7253

No files matched your search

+21 -21
View File
@@ -1,14 +1,14 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
import { get, post, postForm } from './request';
afterEach(() => vi.unstubAllGlobals());
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' },
}));
})); /* fetchMock 表示fetchMock。 */
vi.stubGlobal('fetch', fetchMock);
await expect(get('/items', { page: 2, keyword: 'a b', ignored: undefined })).resolves.toEqual({ ok: true });
@@ -16,13 +16,13 @@ describe('request helpers', () => {
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' },
}));
})); /* fetchMock 表示fetchMock。 */
vi.stubGlobal('fetch', fetchMock);
await post('/login', { username: 'admin' });
@@ -31,7 +31,7 @@ describe('request helpers', () => {
body: JSON.stringify({ username: 'admin' }),
headers: { 'Content-Type': 'application/json' },
}));
});
} /* 回调函数负责当前业务流程。 */);
test('surfaces unified backend errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ code: 'bad_request', message: 'bad request' }), {
@@ -39,11 +39,11 @@ describe('request helpers', () => {
headers: { 'content-type': 'application/json' },
})));
await expect(get('/bad')).rejects.toThrow('bad request');
});
} /* 回调函数负责当前业务流程。 */);
test('notifies the app when an authenticated request returns 401', async () => {
const events = new EventTarget();
const listener = vi.fn();
const events = new EventTarget(); /* events 表示events。 */
const listener = vi.fn(); /* listener 表示listener。 */
events.addEventListener('auth:logout', listener);
vi.stubGlobal('window', events);
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response('{}', {
@@ -53,11 +53,11 @@ describe('request helpers', () => {
await expect(get('/private')).rejects.toThrow();
expect(listener).toHaveBeenCalledOnce();
});
} /* 回调函数负责当前业务流程。 */);
test('does not notify logout for a failed login request', async () => {
const events = new EventTarget();
const listener = vi.fn();
const events = new EventTarget(); /* events 表示events。 */
const listener = vi.fn(); /* listener 表示listener。 */
events.addEventListener('auth:logout', listener);
vi.stubGlobal('window', events);
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response('{}', {
@@ -67,12 +67,12 @@ describe('request helpers', () => {
await expect(post('/login', {}, { skipAuthLogout: true })).rejects.toThrow();
expect(listener).not.toHaveBeenCalled();
});
} /* 回调函数负责当前业务流程。 */);
test('posts FormData without forcing a content type', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response('ok', { status: 200 }));
const fetchMock = vi.fn().mockResolvedValue(new Response('ok', { status: 200 })); /* fetchMock 表示fetchMock。 */
vi.stubGlobal('fetch', fetchMock);
const form = new FormData();
const form = new FormData(); /* form 表示form。 */
form.set('name', 'value');
await expect(postForm('/upload', form)).resolves.toBe('ok');
expect(fetchMock).toHaveBeenCalledWith('/upload', expect.objectContaining({
@@ -80,18 +80,18 @@ describe('request helpers', () => {
credentials: 'include',
body: form,
}));
});
} /* 回调函数负责当前业务流程。 */);
test('aborts requests at the configured timeout', async () => {
vi.useFakeTimers();
const fetchMock = vi.fn((_url: string, init: RequestInit) => new Promise<Response>((_resolve, reject) => {
init.signal?.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')), { once: true });
}));
init.signal?.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')) /* 回调函数负责当前业务流程。 */, { once: true });
} /* 回调函数负责当前业务流程。 */) /* 回调函数负责当前业务流程。 */); /* fetchMock 表示fetchMock。 */
vi.stubGlobal('fetch', fetchMock);
const pending = get('/slow', undefined, { timeoutMs: 50 });
const rejection = expect(pending).rejects.toThrow('请求超时');
const pending = get('/slow', undefined, { timeoutMs: 50 }); /* pending 表示pending。 */
const rejection = expect(pending).rejects.toThrow('请求超时'); /* rejection 表示rejection。 */
await vi.advanceTimersByTimeAsync(50);
await rejection;
vi.useRealTimers();
});
});
} /* 回调函数负责当前业务流程。 */);
} /* 回调函数负责当前业务流程。 */);