import { describe, expect, it } from 'vitest' import { RequestParameter } from '../../../../script/help/requestParameter' describe('RequestParameter', () => { it('constructor 初始化 parameter 结构', () => { const instance = new RequestParameter('ignored', ['ignored']) as any expect(instance.parameter).toEqual({ fieldGroup: { type: 'FieldGroup', group: true, andOr: 'AND', fields: [] }, ordSort: [], pageable: {} }) }) it('AndFields 正常处理普通值、LIKE 值、label 和数组值', () => { const instance = new RequestParameter() as any instance .AndFields({ name: 'name', value: 'tom', label: 'Tom Label', oper: 'EQUAL', andOr: 'AND' }) .AndFields({ name: 'keyword', value: 'search', label: 'Keyword Label', oper: 'LIKE', andOr: 'OR' }) .AndFields({ name: 'ids', value: [1, 2, 3], label: ['A', 'B'], oper: 'IN', andOr: 'AND' }) expect(instance.parameter.fieldGroup.fields).toEqual([ { type: 'Field', group: false, andOr: 'AND', name: 'name', oper: 'EQUAL', value: ['tom'], label: ['Tom Label'] }, { type: 'Field', group: false, andOr: 'OR', name: 'keyword', oper: 'LIKE', value: ['%search%'], label: ['%Keyword Label%'] }, { type: 'Field', group: false, andOr: 'AND', name: 'ids', oper: 'IN', value: [1, 2, 3], label: ['A', 'B'] } ]) }) it('AndFields 支持 Date 类型值', () => { const instance = new RequestParameter() as any const date = new Date('2024-01-01T00:00:00.000Z') instance.AndFields({ name: 'createTime', value: date, oper: 'GREATER_THAN', andOr: 'AND' }) expect(instance.parameter.fieldGroup.fields[0].value).toEqual([date]) }) it('AndFields 在 label 为空时写入空字符串', () => { const instance = new RequestParameter() as any instance.AndFields({ name: 'code', value: 100, label: '', oper: 'EQUAL', andOr: 'AND' }) expect(instance.parameter.fieldGroup.fields[0].label).toBe('') expect(instance.parameter.fieldGroup.fields[0].value).toEqual([100]) }) it('AndFields 对非法 andOr 抛错', () => { const instance = new RequestParameter() expect(() => { instance.AndFields({ name: 'name', value: 'tom', oper: 'EQUAL', andOr: 'XOR' as any }) }).toThrow(/andOr/) }) it('AndFields 对非法 oper 抛错', () => { const instance = new RequestParameter() expect(() => { instance.AndFields({ name: 'name', value: 'tom', oper: 'CONTAINS' as any, andOr: 'AND' }) }).toThrow(/oper/) }) it('AndFields 对非法 value 类型抛错', () => { const instance = new RequestParameter() expect(() => { instance.AndFields({ name: 'name', value: { text: 'tom' } as any, oper: 'EQUAL', andOr: 'AND' }) }).toThrow(/value/) }) it('AndFields 对空数组抛错', () => { const instance = new RequestParameter() expect(() => { instance.AndFields({ name: 'ids', value: [], oper: 'IN', andOr: 'AND' }) }).toThrow(/数组|不能为空/) }) it('Sort 正常追加排序项并支持链式调用', () => { const instance = new RequestParameter() as any const result = instance.Sort('createTime', 'ASC').Sort('updateTime', 'DESC') expect(result).toBe(instance) expect(instance.parameter.ordSort).toEqual([ { name: 'createTime', order: 'ASC' }, { name: 'updateTime', order: 'DESC' } ]) }) it('Sort 对非法排序值抛错', () => { const instance = new RequestParameter() expect(() => { instance.Sort('createTime', 'UP' as any) }).toThrow(/ASC|DESC/) }) it('Pageable 使用默认分页参数', () => { const instance = new RequestParameter() as any instance.Pageable() expect(instance.parameter.pageable).toEqual({ pageNum: 1, pageSize: 20, pageable: true }) }) it('Pageable 正常写入分页参数', () => { const instance = new RequestParameter() as any const result = instance.Pageable(2, 50, true) expect(result).toBe(instance) expect(instance.parameter.pageable).toEqual({ pageNum: 2, pageSize: 50, pageable: true }) }) it('Pageable 在 pageable=false 时仅写入开关', () => { const instance = new RequestParameter() as any instance.Pageable(1, 20, false) expect(instance.parameter.pageable).toEqual({ pageable: false }) }) it('Pageable 对非整数参数抛错', () => { const instance = new RequestParameter() expect(() => { instance.Pageable(1.5 as any, 20, true) }).toThrow(/整数/) expect(() => { instance.Pageable(1, '20a' as any, true) }).toThrow(/整数/) }) it('Pageable 对非布尔 pageable 抛错', () => { const instance = new RequestParameter() expect(() => { instance.Pageable(1, 20, 'true' as any) }).toThrow(/布尔值/) }) })