108 lines
4.3 KiB
TypeScript
108 lines
4.3 KiB
TypeScript
// releases.spec.ts — Issue#4 守门三件:
|
|
// 1. 发布日志数据一致性(单一事实源:releases.ts 头条 ↔ package.json,防漂移);
|
|
// 2. 迷你 Markdown 渲染器(utils/markdown.ts:格式 + 全量转义防注入);
|
|
// 3. 版本已读状态(stores/release.ts:红点判定/持久化/新版本再亮)。
|
|
import { beforeEach, describe, expect, it } from 'vitest'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
import pkg from '../package.json'
|
|
import { APP_VERSION, RELEASE_NOTES } from '@/data/releases'
|
|
import { renderMarkdown } from '@/utils/markdown'
|
|
import { useReleaseStore } from '@/stores/release'
|
|
|
|
describe('Issue#4 发布日志数据(单一事实源)', () => {
|
|
it('APP_VERSION = 最新一条(头部)', () => {
|
|
expect(APP_VERSION).toBe(RELEASE_NOTES[0]!.version)
|
|
})
|
|
|
|
it('package.json version 与 APP_VERSION 一致(防两处漂移)', () => {
|
|
expect(pkg.version).toBe(APP_VERSION)
|
|
})
|
|
|
|
it('版本号语义化(x.y.z)且严格降序', () => {
|
|
const nums = RELEASE_NOTES.map((r) => r.version.split('.').map(Number))
|
|
for (const v of nums) {
|
|
expect(v.length === 3 && v.every((n) => Number.isInteger(n)), `${v.join('.')} 非 x.y.z`).toBe(true)
|
|
}
|
|
for (let i = 1; i < nums.length; i++) {
|
|
const a = nums[i - 1]!
|
|
const b = nums[i]!
|
|
const newer = a[0]! > b[0]! || (a[0]! === b[0]! && (a[1]! > b[1]! || (a[1]! === b[1]! && a[2]! > b[2]!)))
|
|
expect(newer, `${a.join('.')} 应新于 ${b.join('.')}`).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('日期 YYYY-MM-DD / 标签白名单 / markdown 非空', () => {
|
|
expect(RELEASE_NOTES.length).toBeGreaterThanOrEqual(1)
|
|
for (const r of RELEASE_NOTES) {
|
|
expect(r.date).toMatch(/^\d{4}-\d{2}-\d{2}$/)
|
|
expect(['新功能', '优化', '修复']).toContain(r.tag)
|
|
expect(r.markdown.trim().length).toBeGreaterThan(0)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('Issue#4 迷你 Markdown 渲染器', () => {
|
|
it('标题 #~####', () => {
|
|
expect(renderMarkdown('### 标题')).toContain('<h3>标题</h3>')
|
|
expect(renderMarkdown('#### 小标题')).toContain('<h4>小标题</h4>')
|
|
})
|
|
|
|
it('无序 / 有序列表', () => {
|
|
expect(renderMarkdown('- 甲\n- 乙')).toContain('<ul><li>甲</li><li>乙</li></ul>')
|
|
expect(renderMarkdown('1. 第一步\n2、第二步')).toContain('<ol><li>第一步</li><li>第二步</li></ol>')
|
|
})
|
|
|
|
it('粗体 / 行内代码 / 段内换行 <br>', () => {
|
|
expect(renderMarkdown('这是**重点**')).toContain('<strong>重点</strong>')
|
|
expect(renderMarkdown('按 `Ctrl` 键')).toContain('<code>Ctrl</code>')
|
|
expect(renderMarkdown('第一行\n第二行')).toContain('<br>')
|
|
})
|
|
|
|
it('链接仅允许 http(s)(javascript: 伪协议不渲染为 <a>)', () => {
|
|
expect(renderMarkdown('[官网](https://example.com)')).toContain('<a href="https://example.com"')
|
|
expect(renderMarkdown('[x](javascript:alert(1))')).not.toContain('<a ')
|
|
})
|
|
|
|
it('HTML 全量转义(内容自控仍防注入)', () => {
|
|
const h = renderMarkdown('<script>alert(1)</script> & "引号"')
|
|
expect(h).not.toContain('<script>')
|
|
expect(h).toContain('<script>')
|
|
expect(h).toContain('&')
|
|
})
|
|
|
|
it('围栏代码块内容保持原样(不做行内格式化)', () => {
|
|
expect(renderMarkdown('```\n**raw**\n```')).toContain('<pre><code>**raw**</code></pre>')
|
|
})
|
|
|
|
it('行内代码内的 ** 不误判粗体', () => {
|
|
expect(renderMarkdown('`a**b`')).toContain('<code>a**b</code>')
|
|
expect(renderMarkdown('`a**b`')).not.toContain('<strong>')
|
|
})
|
|
})
|
|
|
|
describe('Issue#4 版本已读状态(红点判定)', () => {
|
|
beforeEach(() => {
|
|
localStorage.clear()
|
|
setActivePinia(createPinia())
|
|
})
|
|
|
|
it('首访(无记录)= 有更新(引导看一次发布日志)', () => {
|
|
expect(useReleaseStore().hasUpdate).toBe(true)
|
|
})
|
|
|
|
it('markSeen 后红点消失且 localStorage 持久(刷新仍已读)', () => {
|
|
const s = useReleaseStore()
|
|
s.markSeen()
|
|
expect(s.hasUpdate).toBe(false)
|
|
expect(localStorage.getItem('gosms_seen_ver')).toBe(APP_VERSION)
|
|
// 模拟刷新:新 pinia 实例从 localStorage 恢复,仍为已读
|
|
setActivePinia(createPinia())
|
|
expect(useReleaseStore().hasUpdate).toBe(false)
|
|
})
|
|
|
|
it('发新版本后(本地记录为旧版本)红点重新亮起', () => {
|
|
localStorage.setItem('gosms_seen_ver', '0.0.1')
|
|
expect(useReleaseStore().hasUpdate).toBe(true)
|
|
})
|
|
})
|