import test from 'node:test' import assert from 'node:assert/strict' import { readSource } from './helpers.ts' import { emptyUserPageResult, normalizeUserPageParams, toUserListQuery, USER_PAGE_DEFAULT_SIZE, USER_PAGE_MAX_SIZE, } from '../src/api/users-dto.ts' test('test_task_041_users_dto_normal_primary_path', () => { // 正常主路径:合法分页/关键字归一为可查询参数。 const p = normalizeUserPageParams({ page: 2, pageSize: 15, keyword: ' 张 ' }) assert.equal(p.page, 2) assert.equal(p.pageSize, 15) assert.equal(p.keyword, '张') }) test('test_task_041_users_dto_normal_variant_input', () => { // 正常变体:查询序列化采用 Java 参数名(page/page_size/username)。 const q = toUserListQuery({ page: 3, pageSize: 20, keyword: 'admin' }) assert.deepEqual(q, { page: 3, page_size: 20, username: 'admin' }) }) test('test_task_041_users_dto_normal_repeated_operation_is_idempotent', () => { // 正常重复:同一输入归一结果稳定、不改输入。 const raw = { page: 1, pageSize: 15 } assert.deepEqual(normalizeUserPageParams(raw), normalizeUserPageParams(raw)) assert.deepEqual(raw, { page: 1, pageSize: 15 }) }) test('test_task_041_users_dto_boundary_empty_input', () => { // 边界空值:空对象回默认首页/页大小;空关键字视为无过滤。 const p = normalizeUserPageParams({}) assert.equal(p.page, 1) assert.equal(p.pageSize, USER_PAGE_DEFAULT_SIZE) assert.equal(p.keyword, undefined) }) test('test_task_041_users_dto_boundary_single_item', () => { // 边界单元素:单条分页结果 DTO 结构完整(items/total/page/pageSize)。 const empty = emptyUserPageResult() assert.deepEqual(empty.items, []) assert.equal(empty.total, 0) assert.equal(empty.page, 1) assert.equal(empty.pageSize, USER_PAGE_DEFAULT_SIZE) }) test('test_task_041_users_dto_boundary_limit_or_missing_field', () => { // 边界上限/越界:页码 <1 归 1;页大小超上限/非正被夹取。 assert.equal(normalizeUserPageParams({ page: 0, pageSize: 999 }).page, 1) assert.equal(normalizeUserPageParams({ page: -3, pageSize: 0 }).pageSize, USER_PAGE_DEFAULT_SIZE) assert.equal(normalizeUserPageParams({ pageSize: 999 }).pageSize, USER_PAGE_MAX_SIZE) }) test('test_task_041_users_dto_invalid_input_rejected', () => { // 异常输入:非数字/字符串关键字无效时不抛错并按缺省处理。 const p = normalizeUserPageParams({ page: Number.NaN, pageSize: Number.NaN, keyword: undefined }) assert.equal(p.page, 1) assert.equal(p.pageSize, USER_PAGE_DEFAULT_SIZE) assert.equal(p.keyword, undefined) }) test('test_task_041_users_dto_dependency_failure_returns_actionable_message', () => { // 依赖失败:DTO 纯模块不依赖 axios/http,由查询适配层引用。 const dto = readSource('src/api/users-dto.ts') assert.equal(/axios|from '\.\/http'/.test(dto), false, 'DTO 模块保持纯逻辑') assert.match(dto, /export interface UserPageResult/) const adapter = readSource('src/api/users.ts') assert.match(adapter, /users-dto/, '查询适配复用 DTO/参数归一') const model = readSource('src/api/users-model.ts') assert.match(model, /users-dto/, '解析模型复用 DTO 默认值') })