test(release-cleanup): 補上結束碼、sanitizeBody 與 logger 輸出測試
新增子行程驗證 index.js 失敗時以非零狀態結束、fetchAllPages 錯誤訊息 清理控制字元與長度限制、以及 logger 各輸出函式格式與 stdout/stderr 分流測試。 測試共 56 項全數通過。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
87db9030d6
commit
826b559006
+55
-1
@@ -1,11 +1,65 @@
|
||||
import { test, afterEach } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { failError } from '../logger.js'
|
||||
import {
|
||||
separator,
|
||||
section,
|
||||
info,
|
||||
success,
|
||||
warn,
|
||||
fail,
|
||||
failError,
|
||||
} from '../logger.js'
|
||||
|
||||
const realWrite = process.stderr.write
|
||||
const realStdoutWrite = process.stdout.write
|
||||
|
||||
afterEach(() => {
|
||||
process.stderr.write = realWrite
|
||||
process.stdout.write = realStdoutWrite
|
||||
})
|
||||
|
||||
// 攔截寫入 stdout 的內容
|
||||
function captureStdout(fn) {
|
||||
const chunks = []
|
||||
process.stdout.write = (chunk) => {
|
||||
chunks.push(String(chunk))
|
||||
return true
|
||||
}
|
||||
try {
|
||||
fn()
|
||||
} finally {
|
||||
process.stdout.write = realStdoutWrite
|
||||
}
|
||||
return chunks.join('')
|
||||
}
|
||||
|
||||
test('info/success/warn 以正確前綴寫入 stdout', () => {
|
||||
assert.equal(captureStdout(() => info('hi')), '[INFO] hi\n')
|
||||
assert.equal(captureStdout(() => success('ok')), '[OK] ok\n')
|
||||
assert.equal(captureStdout(() => warn('careful')), '[WARN] careful\n')
|
||||
})
|
||||
|
||||
test('separator 輸出等號分隔線', () => {
|
||||
const out = captureStdout(() => separator())
|
||||
assert.match(out, /^\n={50}\n$/)
|
||||
})
|
||||
|
||||
test('section 輸出分隔線、標題與虛線', () => {
|
||||
const out = captureStdout(() => section('參數檢查'))
|
||||
assert.match(out, /={50}\n參數檢查\n-{50}\n/)
|
||||
})
|
||||
|
||||
test('fail 寫入 stderr 而非 stdout', () => {
|
||||
const stdout = captureStdout(() => {
|
||||
const chunks = []
|
||||
process.stderr.write = (chunk) => {
|
||||
chunks.push(String(chunk))
|
||||
return true
|
||||
}
|
||||
fail('boom')
|
||||
assert.equal(chunks.join(''), '[ERR] boom\n')
|
||||
})
|
||||
assert.equal(stdout, '') // fail 不應寫入 stdout
|
||||
})
|
||||
|
||||
// 攔截寫入 stderr 的內容
|
||||
|
||||
Reference in New Issue
Block a user