import { test } from 'node:test' import assert from 'node:assert/strict' import { parseCompanyList, parseCompany } from '../src/api/companies-model.ts' import { COMPANY_NAME_MAX_LENGTH, toCompanyPayload, validateCompanyDraft, } from '../src/api/companies-dto.ts' /** 公司目录(设计文档 §2.3)前端侧:Java VO 是 snake_case,请求 DTO 是 camelCase。 */ test('parseCompanyList 按 snake_case 线上字段解析公司列表', () => { const payload = { success: true, data: [ { id: 1, company_name: '厦门米椿花', status: 1, remark: '初始公司', created_at: '2026-10-04 01:00:00', updated_at: '2026-10-04 02:00:00' }, { id: 2, company_name: '福州数富', status: 0, remark: null }, ], } assert.deepEqual(parseCompanyList(payload), [ { id: 1, companyName: '厦门米椿花', status: 1, remark: '初始公司', createdAt: '2026-10-04 01:00:00', updatedAt: '2026-10-04 02:00:00' }, { id: 2, companyName: '福州数富', status: 0, remark: null, createdAt: null, updatedAt: null }, ]) }) test('parseCompanyList 不认 camelCase:证明上一条不是在测兜底', () => { const rows = parseCompanyList({ success: true, data: [{ id: 3, companyName: '某某公司', status: 1 }] }) assert.equal(rows.length, 1) assert.equal(rows[0].companyName, '', '线上字段是 company_name,camelCase 不该被读出来') }) test('parseCompanyList 丢弃无 id 的行并把未知 status 当停用', () => { const rows = parseCompanyList({ success: true, data: [ { company_name: '没有 id', status: 1 }, { id: 4, company_name: '状态未知', status: null }, ] }) assert.deepEqual(rows, [{ id: 4, companyName: '状态未知', status: 0, remark: null, createdAt: null, updatedAt: null }]) }) test('parseCompanyList 对空/异常负载返回空数组', () => { for (const payload of [null, undefined, {}, { success: true, data: null }, 'x']) { assert.deepEqual(parseCompanyList(payload), [], `负载 ${JSON.stringify(payload)} 应解析为空列表`) } }) test('parseCompanyList 把 success=false 的后端文案抛出来', () => { assert.throws(() => parseCompanyList({ success: false, message: '无权限访问公司目录' }), /无权限访问公司目录/) }) test('parseCompany 解析单条(新建/改名回包)', () => { const item = parseCompany({ success: true, data: { id: 5, company_name: '厦门米椿花', status: 1 } }) assert.ok(item) assert.equal(item.id, 5) assert.equal(item.companyName, '厦门米椿花') assert.equal(parseCompany({ success: true, data: { company_name: '无 id' } }), null) }) test('公司名必填、去空格后不得为空,且不超过列宽', () => { assert.equal(validateCompanyDraft({ companyName: ' ' }), '公司名称不能为空') assert.equal(validateCompanyDraft({ companyName: ' 厦门米椿花 ' }), '') const tooLong = 'x'.repeat(COMPANY_NAME_MAX_LENGTH + 1) assert.match(validateCompanyDraft({ companyName: tooLong }), /长度/) }) test('toCompanyPayload 发 camelCase 且 trim 公司名', () => { assert.deepEqual(toCompanyPayload({ companyName: ' 福州数富 ', status: 1, remark: ' ' }), { companyName: '福州数富', status: 1, remark: null, }) })