import assert from 'node:assert/strict'; import test from 'node:test'; import { loadFrontendModule } from './helpers/frontendModule.js'; async function harness(result) { const requests = []; const api = await loadFrontendModule( new URL( '../../src/pages/DemoReservations/reservationsApi.js', import.meta.url, ), { '../../helpers/websiteApi': { requestWebsite: async (...args) => { requests.push(args); if (result instanceof Error) throw result; return result; }, }, }, ); return { ...api, requests }; } const response = (extra = {}) => ({ page: 1, page_size: 10, total: 1, items: [ { id: 4, name: '张晨', contact: '+86 13800000001', companyName: '示例科技', position: '', industry: '', interest: '企业 AI 运营与治理', requirement: '第一行\n第二行', }, ], ...extra, }); test('uses backend pagination and preserves all business fields and line breaks', async () => { const data = response({ page: 2, page_size: 20, total: 21 }); const api = await harness(data); assert.equal( await api.fetchDemoReservations({ page: 2, pageSize: 20 }), data, ); const [method, path] = api.requests[0]; assert.equal(method, 'get'); const url = new URL(path, 'https://example.com'); assert.equal(url.pathname, '/api/website/demo-requests'); assert.equal(url.searchParams.get('p'), '2'); assert.equal(url.searchParams.get('page_size'), '20'); assert.equal(url.searchParams.has('pageNum'), false); assert.equal(url.searchParams.has('pageSize'), false); }); test('encodes three trimmed fuzzy AND filters without losing literal plus and wildcard characters', async () => { const api = await harness(response()); await api.fetchDemoReservations({ name: ' 张 ', contact: ' +86 138%_!& ', companyName: ' 测试科技 ', }); const params = new URL(api.requests[0][1], 'https://example.com') .searchParams; assert.equal(params.get('name'), '张'); assert.equal(params.get('contact'), '+86 138%_!&'); assert.equal(params.get('companyName'), '测试科技'); assert.equal(params.get('p'), '1'); assert.equal(params.get('page_size'), '10'); }); test('returns empty matches and out-of-range pages with the actual backend total', async () => { for (const total of [0, 23]) { const data = response({ items: [], total, page: 5 }); const api = await harness(data); assert.equal(await api.fetchDemoReservations({ page: 5 }), data); } }); test('fails visibly on malformed responses or backend errors', async () => { for (const value of [ null, {}, response({ items: null }), response({ total: -1 }), response({ page: 0 }), ]) { const api = await harness(value); await assert.rejects(api.fetchDemoReservations(), /响应格式/); } const api = await harness(new Error('没有平台管理员权限')); await assert.rejects(api.fetchDemoReservations(), /没有平台管理员权限/); });