100 lines
4.0 KiB
JavaScript
100 lines
4.0 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const test = require('node:test');
|
|
|
|
const GenerationPages = require('../public/js/generation-pages.js');
|
|
|
|
test('字辈页只从 URL 读取真实家谱编号', () => {
|
|
assert.equal(GenerationPages.getCurrentGenealogyId('?genealogyId=900001001'), '900001001');
|
|
assert.equal(GenerationPages.getCurrentGenealogyId(''), '');
|
|
});
|
|
|
|
test('字辈新增和修改只构造 Apifox 定义的字段', () => {
|
|
assert.deepEqual(
|
|
GenerationPages.buildGenerationPoemBody({
|
|
generationNo: '3',
|
|
generationText: ' 万 ',
|
|
description: '第三世',
|
|
sortOrder: '30',
|
|
status: '1',
|
|
content: '旧字段',
|
|
stopMissingOldGeneration: true
|
|
}),
|
|
{
|
|
generationNo: 3,
|
|
generationText: '万',
|
|
description: '第三世',
|
|
sortOrder: 30,
|
|
status: '1'
|
|
}
|
|
);
|
|
});
|
|
|
|
test('批量字辈请求只提交 poemText 与 disableMissing', () => {
|
|
assert.deepEqual(
|
|
GenerationPages.buildBatchBody({
|
|
poemText: '德 承 家 亦',
|
|
disableMissing: true,
|
|
content: '旧字段',
|
|
stopMissingOldGeneration: false
|
|
}),
|
|
{
|
|
poemText: '德 承 家 亦',
|
|
disableMissing: true
|
|
}
|
|
);
|
|
});
|
|
|
|
test('字辈列表要求管理接口返回稳定的直接字段', () => {
|
|
assert.deepEqual(
|
|
GenerationPages.normalizeGenerationPoem({
|
|
poemId: '2060000000000000000',
|
|
generationNo: 3,
|
|
generationText: '万',
|
|
description: '第三世',
|
|
sortOrder: 30,
|
|
status: '0'
|
|
}),
|
|
{
|
|
poemId: '2060000000000000000',
|
|
generationNo: 3,
|
|
generationText: '万',
|
|
status: '0',
|
|
description: '第三世',
|
|
sortOrder: 30
|
|
}
|
|
);
|
|
assert.equal(GenerationPages.normalizeGenerationPoem({ poemId: 1, generationNo: 3, content: '旧字段', status: '0' }), null);
|
|
assert.equal(GenerationPages.normalizeGenerationPoem({
|
|
poemId: Number('2060000000000000000'),
|
|
generationNo: 3,
|
|
generationText: '万',
|
|
status: '0'
|
|
}), null);
|
|
});
|
|
|
|
test('字辈批量输入按 Apifox 限制校验', () => {
|
|
assert.equal(GenerationPages.validateGenerationPoemBody({ generationNo: 0, generationText: '万' }), '世代序号必须是 1 到 2147483647 之间的整数');
|
|
assert.equal(GenerationPages.validateGenerationPoemBody({ generationNo: 3, generationText: '' }), '请填写字辈文字');
|
|
assert.equal(GenerationPages.validateGenerationPoemBody(GenerationPages.buildGenerationPoemBody({ generationNo: 3, generationText: '万', sortOrder: '1.5' })), '排序值必须是整数');
|
|
assert.equal(GenerationPages.validateGenerationPoemBody({ generationNo: 3, generationText: '万', sortOrder: 2147483648 }), '排序值必须在 -2147483648 到 2147483647 之间');
|
|
assert.equal(GenerationPages.validateBatchBody({ poemText: '', disableMissing: false }), '请填写批量字辈内容');
|
|
assert.deepEqual(GenerationPages.splitPoemText('德,承;家、亦/传|芳\n远'), ['德', '承', '家', '亦', '传', '芳', '远']);
|
|
assert.equal(GenerationPages.validateBatchBody({ poemText: '甲'.repeat(51), disableMissing: false }), '单个字辈不能超过 50 个字符');
|
|
assert.equal(GenerationPages.validateBatchBody({ poemText: Array(502).fill('甲').join(' '), disableMissing: false }), '一次最多导入 500 个世代');
|
|
assert.equal(GenerationPages.validateBatchBody({ poemText: '德承家亦', disableMissing: false }), '');
|
|
});
|
|
|
|
test('字辈批量示例明确使用 Apifox 支持的分隔符', () => {
|
|
const page = fs.readFileSync(path.join(__dirname, '..', 'profile-generation.html'), 'utf8');
|
|
|
|
assert.match(page, /placeholder="例如:德 承 家 亦(用空格或标点分隔)"/);
|
|
assert.doesNotMatch(page, /placeholder="例如:德承家亦"/);
|
|
});
|
|
|
|
test('字辈管理接口的 403 不是登录失效', () => {
|
|
assert.equal(GenerationPages.isForbidden({ status: 403 }), true);
|
|
assert.equal(GenerationPages.shouldRedirectToLogin({ getToken() { return 'token'; } }, { status: 403 }), false);
|
|
});
|