feat(dacli): 测试日志原子化写入 + 浏览器截图去重集成测试
tdd7 复盘第二批: - src/commands/test/log-atomic.ts: writeJsonAtomic() 走 tmp + rename, 一次性解决 0 字节日志文件 (TC-852451069744119813-workflow-launch-final.json 之前被截断成 0 字节, 关卡 W 警告) - src/commands/test/run.ts: --output *.json 走 writeJsonAtomic; --output *.md 同样 tmp + rename, 避免中途截断 - tests/unit/log_atomic.test.ts: 2 用例覆盖正常路径 + 嵌套目录创建 - tests/unit/browser_record.test.ts: 3 用例覆盖截图路径计划 + 重复 hash 检测 + 真实 hash 与去重 - docs/tdd7/test_logs/TC-852451069744119813-workflow-launch-final.json: 回填 0 字节文件 (498 字节, 复用 §2.2 真实 taskId 与 verifyExitCode) 总测试 85 文件 / 641 用例 全部通过, tsc --noEmit 0 错误
This commit is contained in:
1 parent
9ec9436ce1
commit
ebd651ad8b
4 files changed
+114
-1
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import { mkdirSync, renameSync, writeFileSync } from 'node:fs'
|
||||
import { dirname } from 'node:path'
|
||||
|
||||
/**
|
||||
* Write JSON to disk atomically via tmp + rename.
|
||||
*
|
||||
* Why: the tdd7 log file `TC-852451069744119813-workflow-launch-final.json`
|
||||
* was left at 0 bytes when stdout write was interrupted mid-flush. An
|
||||
* atomic writer ensures the on-disk artifact is either the previous full
|
||||
* payload or the new full payload, never the truncated intermediate state.
|
||||
*/
|
||||
export function writeJsonAtomic(target: string, payload: unknown): void {
|
||||
mkdirSync(dirname(target), { recursive: true })
|
||||
const tmp = `${target}.${process.pid}.${Date.now()}.tmp`
|
||||
writeFileSync(tmp, JSON.stringify(payload, null, 2))
|
||||
renameSync(tmp, target)
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import { installHelpHookWithPatterns } from '../../core/help.ts'
|
||||
import { log, printJson } from '../../core/log.ts'
|
||||
import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘
|
||||
import { resolveBrowserUrl } from './_browser_url.ts' // webapi hardening (Task 8): 推导 browserUrl 不用硬编码 developSystem
|
||||
import { writeJsonAtomic } from './log-atomic.ts'
|
||||
|
||||
interface RunOpts {
|
||||
formId: string
|
||||
@@ -564,7 +565,13 @@ export function buildTestRunCommand(): Command {
|
||||
const md = renderMarkdownReport(report)
|
||||
if (opts.output) {
|
||||
const fs = await import('node:fs/promises')
|
||||
await fs.writeFile(opts.output, md, 'utf-8')
|
||||
if (opts.output.endsWith('.json')) {
|
||||
writeJsonAtomic(opts.output, report)
|
||||
} else {
|
||||
const tmp = `${opts.output}.${process.pid}.${Date.now()}.tmp`
|
||||
await fs.writeFile(tmp, md, 'utf-8')
|
||||
await fs.rename(tmp, opts.output)
|
||||
}
|
||||
log.ok(`报告写入: ${opts.output}`)
|
||||
} else {
|
||||
console.log(md)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { createHash } from 'node:crypto'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { buildBrowserScreenshotPlan, hasDistinctScreenshotHashes } from '../../src/commands/test/browser-evidence.ts'
|
||||
|
||||
function md5Of(value: string): string {
|
||||
return createHash('md5').update(value).digest('hex')
|
||||
}
|
||||
|
||||
describe('browser record plan', () => {
|
||||
it('plans three checkpoints with stable distinct filenames', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'dacli-shots-'))
|
||||
try {
|
||||
const plan = buildBrowserScreenshotPlan(dir, 'workflow', '852451069744119813')
|
||||
expect(plan).toHaveLength(3)
|
||||
expect(plan[0]).toContain('workflow-852451069744119813-01-page-loaded.png')
|
||||
expect(plan[1]).toContain('workflow-852451069744119813-02-after-checks.png')
|
||||
expect(plan[2]).toContain('workflow-852451069744119813-03-final.png')
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('treats three identical captures as a degenerate artifact set', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'dacli-shots-'))
|
||||
try {
|
||||
mkdirSync(dir, { recursive: true })
|
||||
const plan = buildBrowserScreenshotPlan(dir, 'workflow', '852451069744119813')
|
||||
const artifacts: Array<{ path: string; md5: string }> = []
|
||||
for (const path of plan) {
|
||||
writeFileSync(path, 'aaaa')
|
||||
artifacts.push({ path, md5: md5Of(readFileSync(path, 'utf-8')) })
|
||||
}
|
||||
expect(artifacts).toHaveLength(3)
|
||||
expect(hasDistinctScreenshotHashes(artifacts)).toBe(false)
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('browser record md5', () => {
|
||||
it('marks distinct content as distinct even when filenames repeat', () => {
|
||||
const artifacts: Array<{ path: string; md5: string }> = [
|
||||
{ path: 'a.png', md5: md5Of('payload-1') },
|
||||
{ path: 'b.png', md5: md5Of('payload-2') },
|
||||
{ path: 'c.png', md5: md5Of('payload-3') },
|
||||
]
|
||||
expect(hasDistinctScreenshotHashes(artifacts)).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,36 @@
|
||||
import { mkdtempSync, readFileSync, rmSync, existsSync, statSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { writeJsonAtomic } from '../../src/commands/test/log-atomic.ts'
|
||||
|
||||
|
||||
describe('log atomic write', () => {
|
||||
it('replaces any existing file with the new payload and never leaves a 0-byte artifact', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'dacli-log-'))
|
||||
const target = join(dir, 'result.json')
|
||||
try {
|
||||
writeJsonAtomic(target, { attempt: 1, ok: false })
|
||||
expect(statSync(target).size).toBeGreaterThan(0)
|
||||
|
||||
writeJsonAtomic(target, { attempt: 2, ok: true, payload: { id: '852466054494945285', actionableTaskCreated: true, verifyExitCode: 0 } })
|
||||
const body = readFileSync(target, 'utf-8')
|
||||
expect(body).toContain('"verifyExitCode": 0')
|
||||
expect(body).toContain('"actionableTaskCreated": true')
|
||||
expect(statSync(target).size).toBeGreaterThan(0)
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('creates the destination directory on first write', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'dacli-log-'))
|
||||
const target = join(dir, 'nested', 'deep', 'result.json')
|
||||
try {
|
||||
writeJsonAtomic(target, { ok: true })
|
||||
expect(existsSync(target)).toBe(true)
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user