cc-haha/scripts/pr/changed-files.test.ts
程序员阿江(Relakkes) fa47adc33a fix: prevent upgrade crashes from stale persistence
Desktop users can carry provider indexes, managed settings, localStorage state, and native update state from builds that no longer match current readers. This adds startup migrations and recovery paths before server and React state are consumed, plus a persistence upgrade gate so future storage protocol changes ship with old-format fixtures.

Constraint: Existing installs may contain malformed or legacy JSON/localStorage that must not block startup.
Constraint: Local verify should evaluate the current worktree diff rather than unrelated detached-worktree history.
Rejected: Treat invalid persisted state as fatal | reproduces white-screen and startup failure behavior for existing users.
Rejected: Bypass PR policy locally | hides real gate behavior and does not fix detached-worktree false positives.
Confidence: high
Scope-risk: moderate
Directive: Any local JSON, localStorage, or app config shape change must add a migration fixture and keep `bun run check:persistence-upgrade` green.
Tested: bun run check:persistence-upgrade; bun run check:policy; bun run check:desktop; bun run check:server; bun run check:native; bun run verify (9 passed, 1 coverage baseline failure)
Not-tested: Live provider baseline; existing user configs beyond covered fixtures
2026-05-06 23:20:21 +08:00

78 lines
2.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { changedFilesForLocalPrCheck } from './changed-files'
let originalCwd: string
let originalBaseRef: string | undefined
let tempDir: string
function runGit(args: string[]) {
const result = Bun.spawnSync(['git', ...args], {
cwd: tempDir,
stdout: 'pipe',
stderr: 'pipe',
})
if (result.exitCode !== 0) {
throw new Error(new TextDecoder().decode(result.stderr) || new TextDecoder().decode(result.stdout))
}
}
function writeFile(relativePath: string, content: string) {
const filePath = join(tempDir, relativePath)
mkdirSync(join(filePath, '..'), { recursive: true })
writeFileSync(filePath, content)
}
function commit(message: string) {
runGit(['add', '.'])
runGit(['commit', '-m', message])
}
describe('changedFilesForLocalPrCheck', () => {
beforeEach(() => {
originalCwd = process.cwd()
originalBaseRef = process.env.PR_BASE_REF
delete process.env.PR_BASE_REF
tempDir = mkdtempSync(join(tmpdir(), 'cc-haha-changed-files-'))
process.chdir(tempDir)
runGit(['init', '-b', 'main'])
runGit(['config', 'user.email', 'test@example.com'])
runGit(['config', 'user.name', 'Test User'])
writeFile('README.md', '# test\n')
commit('base')
})
afterEach(() => {
process.chdir(originalCwd)
if (originalBaseRef === undefined) {
delete process.env.PR_BASE_REF
} else {
process.env.PR_BASE_REF = originalBaseRef
}
rmSync(tempDir, { recursive: true, force: true })
})
test('uses only local changes in a dirty detached worktree', async () => {
writeFile('scripts/quality-gate/coverage-thresholds.json', '{}\n')
commit('historical policy change')
runGit(['checkout', '--detach', 'HEAD'])
writeFile('src/server/current.ts', 'export const current = true\n')
await expect(changedFilesForLocalPrCheck()).resolves.toEqual(['src/server/current.ts'])
})
test('keeps branch commits and local changes on a normal branch', async () => {
runGit(['checkout', '-b', 'feature/test'])
writeFile('src/server/committed.ts', 'export const committed = true\n')
commit('feature change')
writeFile('desktop/src/local.ts', 'export const local = true\n')
await expect(changedFilesForLocalPrCheck()).resolves.toEqual([
'src/server/committed.ts',
'desktop/src/local.ts',
])
})
})