test(release-cleanup): 補齊 cleanup/config/main 與新驗證函式測試

新增 cleanupReleases、cleanupOrphanTags 的失敗路徑與分類測試、
loadConfig 環境變數驗證測試、main 整合測試,以及 requireUrl/
requireRepository 與 content-type 檢查測試。測試共 41 項全數通過。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 10:55:45 +08:00
co-authored by Claude Opus 4.8
parent 154ab032bf
commit 7c225d4122
6 changed files with 295 additions and 6 deletions
+43 -3
View File
@@ -1,4 +1,4 @@
import { test, beforeEach, afterEach } from 'node:test'
import { test, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { GiteaClient } from '../gitea-client.js'
@@ -8,6 +8,17 @@ afterEach(() => {
globalThis.fetch = realFetch
})
// 建立模擬的 JSON 回應(含 content-type 標頭)
function jsonResponse(data) {
return {
ok: true,
status: 200,
headers: { get: () => 'application/json' },
json: async () => data,
text: async () => JSON.stringify(data),
}
}
test('建構子在有 token 時帶上 Authorization 標頭', () => {
const client = new GiteaClient({ token: 'abc' })
assert.equal(client.headers.Authorization, 'token abc')
@@ -28,7 +39,7 @@ test('fetchAllPages 逐頁讀取直到空陣列', async () => {
globalThis.fetch = async (url) => {
const page = new URL(url).searchParams.get('page')
requested.push(page)
return { ok: true, json: async () => pages[page] }
return jsonResponse(pages[page])
}
const client = new GiteaClient({})
@@ -41,8 +52,23 @@ test('fetchAllPages 逐頁讀取直到空陣列', async () => {
assert.deepEqual(requested, ['1', '2', '3'])
})
test('fetchAllPages 帶上逾時 signal', async () => {
let sawSignal = false
globalThis.fetch = async (_url, opts) => {
sawSignal = opts && typeof opts.signal === 'object' && opts.signal !== null
return jsonResponse([])
}
const client = new GiteaClient({})
await client.fetchAllPages('https://example.com/api')
assert.equal(sawSignal, true)
})
test('fetchAllPages 在 HTTP 錯誤時丟出例外', async () => {
globalThis.fetch = async () => ({ ok: false, status: 500 })
globalThis.fetch = async () => ({
ok: false,
status: 500,
text: async () => 'boom',
})
const client = new GiteaClient({})
await assert.rejects(
() => client.fetchAllPages('https://example.com/api'),
@@ -50,6 +76,20 @@ test('fetchAllPages 在 HTTP 錯誤時丟出例外', async () => {
)
})
test('fetchAllPages 在回應非 JSON 時丟出例外', async () => {
globalThis.fetch = async () => ({
ok: true,
status: 200,
headers: { get: () => 'text/html' },
text: async () => '<html>error</html>',
})
const client = new GiteaClient({})
await assert.rejects(
() => client.fetchAllPages('https://example.com/api'),
/non-JSON content-type/,
)
})
test('deleteResource 回傳 HTTP 狀態碼', async () => {
globalThis.fetch = async (url, opts) => {
assert.equal(opts.method, 'DELETE')