const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const test = require('node:test'); const GenealogyEntryPages = require('../public/js/genealogy-entry-pages.js'); function read(relativePath) { return fs.readFileSync(path.resolve(__dirname, '..', relativePath), 'utf8'); } test('家谱入口只接受本地家谱业务页作为返回目标', () => { assert.equal(GenealogyEntryPages.getTargetPage('?next=profile-feed.html'), 'profile-feed.html'); assert.equal(GenealogyEntryPages.getTargetPage('?next=https%3A%2F%2Fevil.example'), 'profile-family-home.html'); }); test('家谱入口只展示当前 PC 已定义的额度,不伪造家谱编号', () => { const message = GenealogyEntryPages.buildStatus({ createRemaining: 1, joinRemaining: 2 }, 'profile-feed.html'); assert.match(message, /还可创建 1 部/); assert.match(message, /还可加入 2 部/); assert.match(message, /不能伪造家谱编号/); }); test('负数额度不向用户展示为可用数量', () => { assert.doesNotMatch( GenealogyEntryPages.buildStatus({ createRemaining: -1, joinRemaining: -1 }, 'profile-feed.html'), /-1/ ); }); test('个人中心和内容发布在缺少家谱上下文时统一进入家谱入口', () => { const profile = read('profile.html'); const content = read('profile-content.html'); const familyHome = read('profile-family-home.html'); const familyEntry = read('profile-families.html'); const profileCommon = read('public/js/profile-common.js'); assert.match(profile, /进入我的家谱/); assert.doesNotMatch(profile, /四川武胜汤氏族|四川达州刘氏族/); assert.match(profile, /href="profile-family-admin\.html" data-genealogy-context-link/); assert.match(content, /href="profile-feed\.html" data-feature-link="available" data-genealogy-context-link/); assert.match(familyHome, /href="profile-family-admin\.html" data-genealogy-context-link/); assert.match(familyEntry, /href="profile-family-admin\.html" data-genealogy-context-link/); assert.match(profileCommon, /function getGenealogyEntryUrl\(href\)/); assert.match(profileCommon, /genealogyId \? withGenealogyId\([^\n]+\) : getGenealogyEntryUrl\(/); });