import test from 'node:test' import assert from 'node:assert/strict' import { readSource } from './helpers.ts' import { emptyShopManageForm, toShopManageCreateRequest, validateShopManageForm, } from '../src/pages/shop/shop-manage-form-model.ts' test('test_task_091_shop_manage_create_form_normal_primary_path', () => { // 正常主路径:合法表单校验通过并序列化为 Java camelCase 创建请求体。 const form = { groupId: 3, shopName: 'BlueWave', mallName: '亚马逊美国站', znUsername: 'robot', account: 'sale@blue.com', password: 'p@ss' } const { valid, errors } = validateShopManageForm(form) assert.equal(valid, true) assert.deepEqual(errors, {}) assert.deepEqual(toShopManageCreateRequest(form, 8), { groupId: 3, shopName: 'BlueWave', mallName: '亚马逊美国站', znUsername: 'robot', account: 'sale@blue.com', password: 'p@ss', createdById: 8, }) }) test('test_task_091_shop_manage_create_form_normal_variant_input', () => { // 正常变体:znUsername 可选留空不下发;字段首尾空白去除。 const request = toShopManageCreateRequest( { groupId: 1, shopName: ' A店 ', mallName: ' M ', znUsername: ' ', account: 'acc', password: 'p' }, 2, ) assert.equal(request.shopName, 'A店') assert.equal(request.mallName, 'M') assert.equal('znUsername' in request, false) }) test('test_task_091_shop_manage_create_form_repeated_is_idempotent', () => { // 正常重复:序列化不改输入、结果稳定。 const form = { groupId: 2, shopName: 'S', mallName: 'M', znUsername: '', account: 'a', password: 'p' } assert.deepEqual(toShopManageCreateRequest(form, 1), toShopManageCreateRequest(form, 1)) assert.equal((form as { shopName: string }).shopName, 'S') }) test('test_task_091_shop_manage_create_form_boundary_empty_input', () => { // 边界空值:空表单校验给出必填字段错误。 const { valid, errors } = validateShopManageForm(emptyShopManageForm()) assert.equal(valid, false) assert.equal(errors.groupId, '分组不能为空') assert.equal(errors.shopName, '店铺名称不能为空') assert.equal(errors.account, '账号不能为空') assert.equal(errors.password, '密码不能为空') }) test('test_task_091_shop_manage_create_form_boundary_single_item', () => { // 边界单元素:最短合法表单通过。 const { valid } = validateShopManageForm({ groupId: 1, shopName: 'S', mallName: 'M', znUsername: '', account: 'a', password: 'p' }) assert.equal(valid, true) }) test('test_task_091_shop_manage_create_form_boundary_limit_or_missing_field', () => { // 边界上限/缺字段:znUsername 超长拒绝,缺分组序列化不产生无效请求。 const overZn = validateShopManageForm({ groupId: 1, shopName: 'S', mallName: 'M', znUsername: 'x'.repeat(129), account: 'a', password: 'p' }) assert.equal(overZn.valid, false) assert.match(overZn.errors.znUsername || '', /128/) assert.throws(() => toShopManageCreateRequest({ ...emptyShopManageForm(), shopName: 'S' }, 1), /分组/) }) test('test_task_091_shop_manage_create_form_invalid_input_rejected', () => { // 异常输入:空白店铺名/账号/密码与未选分组被拒。 const { errors } = validateShopManageForm({ groupId: null, shopName: ' ', mallName: '', znUsername: '', account: '', password: ' ' }) assert.equal(errors.groupId, '分组不能为空') assert.equal(errors.shopName, '店铺名称不能为空') assert.equal(errors.account, '账号不能为空') assert.equal(errors.password, '密码不能为空') }) test('test_task_091_shop_manage_create_form_dependency_failure_returns_actionable_message', () => { // 依赖失败/提交走 adapter:表单模型纯逻辑,adapter POST /api/admin/shop-manages。 const model = readSource('src/pages/shop/shop-manage-form-model.ts') assert.equal(/axios|http\.|vue/.test(model), false, '表单模型保持纯逻辑') assert.match(model, /敏感|密码/) const api = readSource('src/pages/shop/shop-manage-api.ts') assert.match(api, /submitCreateShopManage/) assert.match(api, /http\.post/) assert.match(api, /\/api\/admin\/shop-manages/) })