Files
knowledge-governance/web/src/VersionHistory.test.tsx
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

294 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @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<Parameters<typeof VersionHistoryView>[0]> = {}) =>
renderToStaticMarkup(
<VersionHistoryView
page={page}
build={null}
filter="all"
loading={false}
loadingMore={false}
error={null}
notice={null}
expanded={null}
canSync={false}
onRefresh={vi.fn()}
onFilter={vi.fn()}
onLoadMore={vi.fn()}
onSync={vi.fn()}
onExpand={vi.fn()}
{...over}
/>
)
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- <img src=x onerror=alert(1)>' },
],
},
})
expect(html).toContain('class="version-history-change-row"')
expect(html).toContain('class="version-history-change-tag fixed">修复</span>')
expect(html).toContain('<strong>标题</strong>')
expect(html).toContain('<code>字段</code>')
expect(html).toContain('### Fixed')
expect(html).not.toContain('<img src=x')
})
it('uses compact full-width record headers and defaults to the matching running commit', () => {
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('运行版本</dt>')
expect(html).toContain('构建时间(北京时间)</dt>')
expect(html).toContain(releaseItem.commit_sha)
expect(html).toContain('刷新</button>')
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('当前运行</span>')
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('发布</button>')
expect(html).not.toContain('部署</button>')
// 非管理员看不到同步入口
expect(html).not.toContain('同步记录</button>')
})
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('同步记录</button>')
})
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: '<img src=x onerror=alert(1)>', release_notes: '<script>x</script>' }],
},
})
expect(html).not.toContain('<img src=x')
expect(html).not.toContain('<script>x</script>')
expect(html).toContain('&lt;img src=x')
})
})