test(release-cleanup): 新增 validate/releases/tags/gitea-client 單元測試
以 node:test 內建測試器涵蓋純函式(isEmptyOrNull/requireValue/requireInteger、 selectReleasesToDelete、categorizeTags)與 GiteaClient 分頁/刪除邏輯,共 19 項測試。 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
b1aa8730a2
commit
15ffa4ffb6
@@ -0,0 +1,60 @@
|
||||
import { test, beforeEach, afterEach } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { GiteaClient } from '../gitea-client.js'
|
||||
|
||||
const realFetch = globalThis.fetch
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = realFetch
|
||||
})
|
||||
|
||||
test('建構子在有 token 時帶上 Authorization 標頭', () => {
|
||||
const client = new GiteaClient({ token: 'abc' })
|
||||
assert.equal(client.headers.Authorization, 'token abc')
|
||||
})
|
||||
|
||||
test('建構子在無 token 時不帶 Authorization 標頭', () => {
|
||||
const client = new GiteaClient({})
|
||||
assert.equal(client.headers.Authorization, undefined)
|
||||
})
|
||||
|
||||
test('fetchAllPages 逐頁讀取直到空陣列', async () => {
|
||||
const pages = {
|
||||
1: [{ id: 1 }, { id: 2 }],
|
||||
2: [{ id: 3 }],
|
||||
3: [],
|
||||
}
|
||||
const requested = []
|
||||
globalThis.fetch = async (url) => {
|
||||
const page = new URL(url).searchParams.get('page')
|
||||
requested.push(page)
|
||||
return { ok: true, json: async () => pages[page] }
|
||||
}
|
||||
|
||||
const client = new GiteaClient({})
|
||||
const items = await client.fetchAllPages('https://example.com/api')
|
||||
|
||||
assert.deepEqual(
|
||||
items.map((i) => i.id),
|
||||
[1, 2, 3],
|
||||
)
|
||||
assert.deepEqual(requested, ['1', '2', '3'])
|
||||
})
|
||||
|
||||
test('fetchAllPages 在 HTTP 錯誤時丟出例外', async () => {
|
||||
globalThis.fetch = async () => ({ ok: false, status: 500 })
|
||||
const client = new GiteaClient({})
|
||||
await assert.rejects(
|
||||
() => client.fetchAllPages('https://example.com/api'),
|
||||
/HTTP 500/,
|
||||
)
|
||||
})
|
||||
|
||||
test('deleteResource 回傳 HTTP 狀態碼', async () => {
|
||||
globalThis.fetch = async (url, opts) => {
|
||||
assert.equal(opts.method, 'DELETE')
|
||||
return { status: 204 }
|
||||
}
|
||||
const client = new GiteaClient({})
|
||||
assert.equal(await client.deleteResource('https://example.com/api/1'), 204)
|
||||
})
|
||||
Reference in New Issue
Block a user