// @ts-expect-error The test runner provides Node; the browser bundle intentionally omits @types/node. import { readFileSync } from 'node:fs' import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' import { VersionHistoryView } from './VersionHistory' import type { VersionHistoryItem, VersionHistoryPage } from './versionHistory' const releaseItem: VersionHistoryItem = { commit_sha: '6de5d5236380ad619fe35e4bd20f49b2d967903e', short_sha: '6de5d5236380', parents: ['97f4b296613e23889997dd14004cd23f6c3a2c3f'], committed_at: '2026-10-13T16:45:56+08:00', author_display: '姚章浩', subject: 'feat(version): 开发号随提交自动递增 + 首个正式版本 0.1.0', body: '正文第一行\n正文第二行', record_kind: 'release', version_label: '0.1.0', version_state: 'verified', release_tags: [{ name: 'v0.1.0', url: 'https://gitea.example/Weknora/knowledge-governance/src/tag/v0.1.0' }], release_notes: '### Added\n\n- 首个正式版本', commit_url: 'https://gitea.example/Weknora/knowledge-governance/commit/6de5d5236380ad619fe35e4bd20f49b2d967903e', version_reason: 'annotated_tag_and_changelog_top_match', is_current_running: true, } const developmentItem: VersionHistoryItem = { ...releaseItem, commit_sha: '950c1b429bc814d9fa7c2c1449f999a05537af21', short_sha: '950c1b429bc8', subject: 'feat(release): 抬档门禁', body: '', record_kind: 'development', version_label: '0.1.1', version_state: 'verified', release_tags: [], release_notes: '', is_current_running: false, } const historicalItem: VersionHistoryItem = { ...releaseItem, commit_sha: 'e1c5f68c0d8a4f2b9c3d5e7a1b2c3d4e5f607182', short_sha: 'e1c5f68c0d8a', subject: 'chore(deploy): 部署与备份脚本', body: '', record_kind: 'historical', version_label: '', version_state: 'unknown', release_tags: [], release_notes: '', version_reason: 'no_baseline', is_current_running: false, commit_url: 'https://gitea.example/Weknora/knowledge-governance/commit/e1c5f68c0d8a4f2b9c3d5e7a1b2c3d4e5f607182', } const page: VersionHistoryPage = { items: [releaseItem, developmentItem, historicalItem], next_cursor: 'cursor-2', has_more: true, sync: { available: true, status: 'completed', complete: true, last_success_at: '2026-10-13T20:00:00+08:00', fetched_count: 111, expected_count: 111, error_code: '', source_head_sha: 'ad9c5d20d467…', active_snapshot_id: 12, repo: 'Weknora/knowledge-governance', }, current_running: { state: 'matched', commit_sha: '6de5d5236380ad619fe35e4bd20f49b2d967903e', display_version: '0.1.0', matched_commit_sha: '6de5d5236380ad619fe35e4bd20f49b2d967903e', }, } const render = (over: Partial[0]> = {}) => renderToStaticMarkup( ) describe('VersionHistoryView', () => { it('renders safe readable change rows while retaining the untouched raw notes', () => { const html = render({ expanded: developmentItem.commit_sha, page: { ...page, items: [ { ...developmentItem, release_notes: '### Fixed\n\n- **标题**:修复 `字段`\n- ' }, ], }, }) expect(html).toContain('class="version-history-change-row"') expect(html).toContain('class="version-history-change-tag fixed">修复') expect(html).toContain('标题') expect(html).toContain('字段') expect(html).toContain('### Fixed') expect(html).not.toContain(' { const html = render({ expanded: undefined }) expect(html).toContain('data-version-commit="' + releaseItem.commit_sha + '"') expect(html).toContain('is-current') expect(html).toContain('aria-controls="version-detail-' + releaseItem.commit_sha + '"') expect(html.match(/aria-expanded="true"/g)).toHaveLength(1) expect(html).toContain('正文第一行') expect(html).toContain('原始记录与提交信息') expect(render({ expanded: null })).not.toContain('正文第一行') }) it('separates the actual current build into a readable summary card with a read-only refresh', () => { const html = render({ build: { release_version: '0.1.0', build_kind: 'development', commit_sha: releaseItem.commit_sha, display_version: '0.1.0', built_at: '2026-10-13T08:45:56Z', identity_available: true, }, }) expect(html).toContain('aria-label="当前版本"') expect(html).toContain('运行版本') expect(html).toContain('构建时间(北京时间)') expect(html).toContain(releaseItem.commit_sha) expect(html).toContain('刷新') expect(html).toContain('每一版改了什么') }) it('renders the exact development changelog instead of hiding all non-release notes', () => { const html = render({ expanded: developmentItem.commit_sha, page: { ...page, items: [{ ...developmentItem, release_notes: '这次开发独有的变更说明' }] }, }) expect(html).toContain('这次开发独有的变更说明') }) it('lists commits newest-first with kind badges, short sha, times and links', () => { const html = render({ expanded: releaseItem.commit_sha }) expect(html).toContain('版本记录') expect(html).toContain('正式版本') expect(html).toContain('开发版本') expect(html).toContain('历史提交') expect(html).toContain('0.1.0') expect(html).toContain('0.1.1') expect(html).toContain('950c1b429bc8') expect(html).toContain('查看提交') expect(html).toContain('查看 tag v0.1.0') expect(html.indexOf('0.1.0')).toBeLessThan(html.indexOf('0.1.1')) }) it('marks only the exact-match commit as current running', () => { const html = render() const flags = html.match(/当前运行/g) ?? [] // 顶部一句「当前运行:…」+ 恰好一个条目徽标 expect(flags.length).toBe(2) expect(html).toContain('已匹配到下面的提交') }) it('加载失败时不说"未接线",并把服务端原因显示出来', () => { // 真实案例:部署后第一次打开版本页,API 500(快照同步进行中)⇒ page=null, // 页面却同时显示"当前运行版本未知"和"版本记录未接线",把故障说成未配置。 const html = render({ page: null, error: '加载版本记录失败(HTTP 500):读取版本记录失败(version_history_unavailable)', }) expect(html).toContain('加载版本记录失败') expect(html).toContain('version_history_unavailable') expect(html).not.toContain('版本记录未接线') expect(html).not.toContain('未配置只读的仓库来源') expect(html).not.toContain('当前运行版本未知') }) it('never labels a commit when the running identity is unknown', () => { const html = render({ page: { ...page, current_running: { state: 'unknown', commit_sha: '', display_version: 'dev', matched_commit_sha: '' }, }, }) expect(html).toContain('当前运行版本未知') expect(html).not.toContain('当前运行') expect(html).not.toContain('已匹配到下面的提交') }) it('does not invent a version number for unverified history commits', () => { const html = render() const start = html.indexOf('历史提交 · e1c5f68c0d8a') expect(start).toBeGreaterThan(-1) expect(html).toContain('版本号未能核验:找不到可核验的发布基线') expect(html).not.toMatch(/历史提交 · e1c5f68c0d8a[\s\S]{0,200}?\d+\.\d+\.\d+[\s\S]{0,40}?版本号未能核验/) }) it('keeps two commits that resolve to the same number as separate entries', () => { const sameNumber: VersionHistoryItem = { ...developmentItem, commit_sha: 'c'.repeat(40), short_sha: 'cccccccccccc', subject: '另一条同号提交', } const html = render({ page: { ...page, items: [developmentItem, sameNumber] } }) expect(html).toContain('950c1b429bc8') expect(html).toContain('cccccccccccc') expect((html.match(/0\.1\.1/g) ?? []).length).toBe(2) }) it('shows an honest empty state and never offers tag/publish/deploy actions', () => { const html = render({ page: { ...page, items: [], has_more: false } }) expect(html).toContain('暂无记录') expect(html).not.toContain('打 tag') expect(html).not.toContain('发布') expect(html).not.toContain('部署') // 非管理员看不到同步入口 expect(html).not.toContain('同步记录') }) it('exposes the admin sync entry only for admins and reflects sync failures', () => { const html = render({ canSync: true }) expect(html).toContain('同步记录') const failed = render({ canSync: true, page: { ...page, sync: { ...page.sync, status: 'failed', complete: false, error_code: 'gitea_unreachable' }, }, }) expect(failed).toContain('上次同步失败(gitea_unreachable)') expect(failed).toContain('仍展示上次成功的数据') expect(failed).toContain('同步失败') }) it('degrades to "not wired" instead of failing when the source is unconfigured', () => { const html = render({ page: { ...page, items: [], sync: { ...page.sync, available: false, status: '' } }, }) expect(html).toContain('未接线') expect(html).not.toContain('同步记录') }) it('never renders a tag link without a URL (an empty href looks broken)', () => { const withURL = render({ expanded: releaseItem.commit_sha }) expect(withURL).toContain('查看 tag v0.1.0') expect(withURL).toContain('https://gitea.example/Weknora/knowledge-governance/src/tag/v0.1.0') const withoutURL = render({ expanded: releaseItem.commit_sha, page: { ...page, items: [{ ...releaseItem, release_tags: [{ name: 'v0.1.0', url: '' }] }], }, }) expect(withoutURL).not.toContain('查看 tag') }) it('keeps mobile single-column rules so nothing overflows horizontally', () => { const css = readFileSync(new URL('./versionHistory.css', import.meta.url), 'utf8') expect(css).toMatch(/@media \(max-width: 760px\)[\s\S]*?\.version-history[\s\S]*?overflow-wrap:\s*anywhere/) expect(css).toMatch( /@media \(max-width: 760px\)[\s\S]*?\.version-history-entry-head\s*\{\s*flex-direction:\s*column/ ) }) it('renders commit bodies as text, never as markup', () => { const html = render({ expanded: developmentItem.commit_sha, page: { ...page, items: [{ ...developmentItem, body: '', release_notes: '' }], }, }) expect(html).not.toContain('x') expect(html).toContain('<img src=x') }) })