Files
release-cleanup/app/test/config.test.js
T
JefferyandClaude Opus 4.8 7c225d4122 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>
2026-06-26 10:55:45 +08:00

59 lines
1.6 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { loadConfig } from '../config.js'
const base = {
GITEA_SERVER_URL: 'https://gitea.example.com',
GITEA_REPOSITORY: 'owner/repo',
KEEP_COUNT: '2',
}
test('loadConfig 在缺少 GITEA_SERVER_URL 時丟出', () => {
assert.throws(
() => loadConfig({ GITEA_REPOSITORY: 'owner/repo', KEEP_COUNT: '2' }),
/GITEA_SERVER_URL is required/,
)
})
test('loadConfig 在 KEEP_COUNT 非整數時丟出', () => {
assert.throws(
() => loadConfig({ ...base, KEEP_COUNT: 'abc' }),
/non-negative integer/,
)
})
test('loadConfig 在 GITEA_SERVER_URL 非合法 URL 時丟出', () => {
assert.throws(
() => loadConfig({ ...base, GITEA_SERVER_URL: 'not a url' }),
/must be a valid URL/,
)
})
test('loadConfig 在 GITEA_REPOSITORY 含路徑穿越時丟出', () => {
assert.throws(
() => loadConfig({ ...base, GITEA_REPOSITORY: '../evil' }),
/owner\/repo without path traversal/,
)
})
test('loadConfig 在有效輸入時回傳完整設定物件', () => {
const cfg = loadConfig(base)
assert.equal(cfg.serverUrl, 'https://gitea.example.com')
assert.equal(cfg.repository, 'owner/repo')
assert.equal(cfg.token, null)
assert.equal(cfg.keepCount, 2)
assert.equal(
cfg.releaseApiUrl,
'https://gitea.example.com/api/v1/repos/owner/repo/releases',
)
assert.equal(
cfg.tagApiUrl,
'https://gitea.example.com/api/v1/repos/owner/repo/tags',
)
})
test('loadConfig 在提供 token 時保留 token 值', () => {
const cfg = loadConfig({ ...base, GITEA_TOKEN: 'secret' })
assert.equal(cfg.token, 'secret')
})