'use strict'; const assert = require('node:assert/strict'); const { getAuthToken, invokeSystemMethod, } = require('../../support/system_method_client'); const EXTRA_HEADERS = { platform: 'web', project: 'all', }; describe('test/app/controller/system.setting.printTemplate.test.js', () => { let token = ''; let businessType = ''; let createdTemplateId = 0; const uniqueName = `autotest_template_${Date.now()}`; before(async () => { const auth = await getAuthToken(); token = auth.token; const treeRes = await invokeSystemMethod('setting.printTemplate.list', {}, token, EXTRA_HEADERS); assert.equal(treeRes.status, 200); assert.equal(treeRes.body.flag, 'success'); const presetTree = Array.isArray(treeRes.body?.presetTree) ? treeRes.body.presetTree : []; assert.ok(presetTree.length > 0, '缺少可用业务类型,无法执行打印模板测试'); for (const item of presetTree) { if (!item || !item.businessType) { continue; } const listRes = await invokeSystemMethod('setting.printTemplate.listTemplatesByType', { businessType: item.businessType, }, token, EXTRA_HEADERS); assert.equal(listRes.status, 200); assert.equal(listRes.body.flag, 'success'); const templates = Array.isArray(listRes.body?.data) ? listRes.body.data : []; const hasDefaultCustomTemplate = templates.some(template => template && template.platform === 'default' && template.template_type === 'CUSTOM' && template.warehouse_id !== null ); if (!hasDefaultCustomTemplate) { businessType = item.businessType; break; } } assert.ok(businessType, '当前账号下所有业务类型都已存在默认 CUSTOM 模板,无法执行创建测试'); }); it('normal-1 should create print template successfully', async () => { const createRes = await invokeSystemMethod('setting.printTemplate.create', { template_name: uniqueName, business_type: businessType, page_width: 100, page_height: 180, }, token, EXTRA_HEADERS); assert.equal(createRes.status, 200); assert.equal(createRes.body.flag, 'success'); assert.equal(createRes.body.code, 200); const templateId = Number(createRes.body?.data?.id); assert.ok(templateId > 0, '创建成功但未返回模板ID'); createdTemplateId = templateId; assert.match(createRes.body.message, /模板创建成功|ok/); }); it('normal-2 should list created template by business type', async () => { const listRes = await invokeSystemMethod('setting.printTemplate.listTemplatesByType', { businessType, }, token, EXTRA_HEADERS); assert.equal(listRes.status, 200); assert.equal(listRes.body.flag, 'success'); assert.equal(listRes.body.code, 200); const templates = Array.isArray(listRes.body?.data) ? listRes.body.data : []; const hit = templates.find(item => Number(item.id) === createdTemplateId); assert.ok(hit, '按业务类型查询未包含新建模板'); assert.equal(hit.template_name, uniqueName); assert.equal(hit.platform, 'default'); assert.equal(hit.template_type, 'CUSTOM'); assert.equal(hit.template_url, ''); }); it('normal-3 should delete created template successfully', async () => { const deleteRes = await invokeSystemMethod('setting.printTemplate.delete', { id: createdTemplateId, }, token, EXTRA_HEADERS); assert.equal(deleteRes.status, 200); assert.equal(deleteRes.body.flag, 'success'); assert.equal(deleteRes.body.code, 200); assert.equal(Number(deleteRes.body?.data?.id), createdTemplateId); assert.match(deleteRes.body.message, /模板删除成功|ok/); const listRes = await invokeSystemMethod('setting.printTemplate.listTemplatesByType', { businessType, }, token, EXTRA_HEADERS); assert.equal(listRes.status, 200); assert.equal(listRes.body.flag, 'success'); const templates = Array.isArray(listRes.body?.data) ? listRes.body.data : []; const hit = templates.find(item => Number(item.id) === createdTemplateId); assert.equal(hit, undefined, '删除后模板仍出现在查询结果中'); }); it('error-1 should fail when create without page_width', async () => { const res = await invokeSystemMethod('setting.printTemplate.create', { template_name: `${uniqueName}_missing_width`, business_type: businessType, page_height: 180, }, token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'failure'); assert.equal(res.body.code, 400); assert.match(res.body.message, /page_width/); }); it('error-2 should fail when create without page_height', async () => { const res = await invokeSystemMethod('setting.printTemplate.create', { template_name: `${uniqueName}_missing_height`, business_type: businessType, page_width: 100, }, token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'failure'); assert.equal(res.body.code, 400); assert.match(res.body.message, /page_height/); }); it('error-3 should fail when delete the same template twice', async () => { const res = await invokeSystemMethod('setting.printTemplate.delete', { id: createdTemplateId, }, token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'failure'); assert.equal(res.body.code, 404); assert.match(res.body.message, /模板不存在或无权限/); }); });