106 lines
4.4 KiB
JavaScript
106 lines
4.4 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { escapeHtml, pickBuildEnv, pickPublicEnv, resolveSiteConfig } from '../src/site-config.js'
|
|
|
|
test('defaults preserve the original site and hide unconfigured registrations', () => {
|
|
const config = resolveSiteConfig({}, {}, 2026)
|
|
assert.equal(config.projectName, '未完待续')
|
|
assert.equal(config.copyright, '© 2026 未完待续')
|
|
assert.equal(config.icpNumber, '')
|
|
assert.equal(config.publicSecurityNumber, '')
|
|
})
|
|
|
|
test('runtime overrides build values while keeping unspecified build settings', () => {
|
|
const config = resolveSiteConfig({
|
|
VITE_PROJECT_NAME: '构建项目',
|
|
VITE_PROJECT_NAME_EN: 'BUILD NAME',
|
|
VITE_ICP_NUMBER: '构建备案号',
|
|
}, { VITE_PROJECT_NAME: '运行项目' }, 2026)
|
|
assert.equal(config.projectName, '运行项目')
|
|
assert.equal(config.projectNameEn, 'BUILD NAME')
|
|
assert.equal(config.icpNumber, '构建备案号')
|
|
assert.equal(config.title, '运行项目 · 美好的事,即将发生')
|
|
assert.equal(config.copyright, '© 2026 运行项目')
|
|
})
|
|
|
|
test('Rainbond BUILD_VITE variables override build defaults and preserve empty values', () => {
|
|
const config = pickBuildEnv({
|
|
VITE_PROJECT_NAME: '本地默认名称',
|
|
VITE_ICP_NUMBER: '本地备案号',
|
|
BUILD_VITE_PROJECT_NAME: 'Rainbond 项目',
|
|
BUILD_VITE_ICP_NUMBER: '',
|
|
BUILD_VITE_OTHER_SECRET: 'not-public',
|
|
})
|
|
assert.deepEqual(config, { VITE_PROJECT_NAME: 'Rainbond 项目', VITE_ICP_NUMBER: '' })
|
|
assert.equal(resolveSiteConfig(config).title, 'Rainbond 项目 · 美好的事,即将发生')
|
|
assert.equal(resolveSiteConfig(config).icpNumber, '')
|
|
})
|
|
|
|
test('Rainbond build values do not override runtime VITE settings', () => {
|
|
const buildEnv = pickBuildEnv({ BUILD_VITE_PROJECT_NAME: '构建名称' })
|
|
const config = resolveSiteConfig(buildEnv, {
|
|
VITE_PROJECT_NAME: '运行名称',
|
|
BUILD_VITE_PROJECT_NAME: '旧构建名称',
|
|
})
|
|
assert.equal(config.projectName, '运行名称')
|
|
})
|
|
|
|
test('empty runtime values hide optional build content and fall back for required text', () => {
|
|
const config = resolveSiteConfig({
|
|
VITE_PROJECT_NAME: '构建项目',
|
|
VITE_PAGE_TITLE: '构建标题',
|
|
VITE_ICP_NUMBER: '已有备案号',
|
|
}, {
|
|
VITE_PROJECT_NAME: ' ',
|
|
VITE_PAGE_TITLE: '',
|
|
VITE_PROJECT_NAME_EN: '',
|
|
VITE_COPYRIGHT: '',
|
|
VITE_FOOTER_NOTE: '',
|
|
VITE_ICP_NUMBER: '',
|
|
})
|
|
assert.equal(config.projectName, '未完待续')
|
|
assert.equal(config.title, '未完待续 · 美好的事,即将发生')
|
|
for (const key of ['projectNameEn', 'copyright', 'footerNote', 'icpNumber']) {
|
|
assert.equal(config[key], '')
|
|
}
|
|
})
|
|
|
|
test('copyright substitutes placeholders without interpreting project name as a template', () => {
|
|
const config = resolveSiteConfig({
|
|
VITE_PROJECT_NAME: '项目 {year}',
|
|
VITE_COPYRIGHT: '© {year} {projectName} · 保留所有权利',
|
|
}, {}, 2030)
|
|
assert.equal(config.copyright, '© 2030 项目 {year} · 保留所有权利')
|
|
})
|
|
|
|
test('registration URLs allow only HTTP or HTTPS and keep labels for invalid links', () => {
|
|
const config = resolveSiteConfig({
|
|
VITE_ICP_NUMBER: '测试备案号',
|
|
VITE_ICP_URL: 'javascript:alert(1)',
|
|
VITE_PUBLIC_SECURITY_NUMBER: '测试公安备案号',
|
|
VITE_PUBLIC_SECURITY_URL: 'https://example.com/registration?id=123',
|
|
})
|
|
assert.equal(config.icpNumber, '测试备案号')
|
|
assert.equal(config.icpUrl, '')
|
|
assert.equal(config.publicSecurityUrl, 'https://example.com/registration?id=123')
|
|
assert.equal(resolveSiteConfig({ VITE_ICP_URL: 'data:text/html,test' }).icpUrl, '')
|
|
})
|
|
|
|
test('only explicitly supported string ENV values are published', () => {
|
|
const env = Object.assign(Object.create({ VITE_PROJECT_NAME_EN: 'inherited' }), {
|
|
VITE_PROJECT_NAME: '公开名称',
|
|
VITE_ICP_NUMBER: '',
|
|
VITE_COPYRIGHT: null,
|
|
VITE_OTHER_SECRET: 'do-not-publish',
|
|
DATABASE_PASSWORD: 'do-not-publish',
|
|
})
|
|
assert.deepEqual(pickPublicEnv(env), { VITE_PROJECT_NAME: '公开名称', VITE_ICP_NUMBER: '' })
|
|
})
|
|
|
|
test('special characters are escaped and multiline descriptions have plain metadata', () => {
|
|
assert.equal(escapeHtml(`<script title="'">&</script>`), '<script title="'">&</script>')
|
|
const config = resolveSiteConfig({ VITE_PROJECT_DESCRIPTION: '第一行\n第二行' })
|
|
assert.equal(config.description, '第一行\n第二行')
|
|
assert.equal(config.metaDescription, '第一行 第二行')
|
|
})
|