import assert from "node:assert/strict" import { test } from "node:test" import { createCertificateIssueJob, getCertificateIssueJob, getAcmeSettings, putAcmeSettings, testAcmeSettings, getCertificateRenewSettings, putCertificateRenewSettings } from "./certificates" const base = "https://api.example.test" const issue = { serverId: "1", certName: "cert", domainNames: ["test.example"] } const acme = { directoryUrl: "https://acme.example.test/directory", tokenConfigured: false } const renew = { enabled: false, intervalSec: 300, renewBeforeDays: 30 } const job = { id: "job-1", serverId: "1", certName: "cert", domainNames: ["test.example"], status: "queued", step: "queued", requestedAt: "2026-10-01T00:00:00Z" } test("settings readers reject malformed and empty responses instead of defaults", async () => { const original = globalThis.fetch try { for (const payload of [{}, [], { ...acme, tokenConfigured: "false" }, { ...renew, enabled: "false" }]) { globalThis.fetch = async () => new Response(JSON.stringify(payload), { status: 200 }) await assert.rejects(() => getAcmeSettings(base)) await assert.rejects(() => getCertificateRenewSettings(base)) } globalThis.fetch = async () => new Response(null, { status: 204 }) await assert.rejects(() => putAcmeSettings(base, {})) await assert.rejects(() => putCertificateRenewSettings(base, { enabled: true })) } finally { globalThis.fetch = original } }) test("strict settings preserve false, carry caller cancellation and PUT body", async () => { const original = globalThis.fetch; let init: RequestInit | undefined globalThis.fetch = async (url, options) => { init = options; return new Response(JSON.stringify(String(url).includes("renew") ? renew : acme), { status: 200 }) } try { const abort = new AbortController() assert.equal((await getAcmeSettings(base, { signal: abort.signal })).tokenConfigured, false) const signal = init?.signal; abort.abort(); assert.equal(signal?.aborted, true) assert.equal((await putCertificateRenewSettings(base, { enabled: false })).enabled, false) assert.equal(init?.method, "PUT"); assert.equal(init?.body, JSON.stringify({ enabled: false })) } finally { globalThis.fetch = original } }) test("invalid request fields fail before sending mutation", async () => { const original = globalThis.fetch; let calls = 0 globalThis.fetch = async () => { calls++; return new Response("{}", { status: 200 }) } try { await assert.rejects(() => putAcmeSettings(base, { directoryUrl: "bad" })) await assert.rejects(() => putCertificateRenewSettings(base, { intervalSec: 0 })) await assert.rejects(() => createCertificateIssueJob(base, { ...issue, domainNames: [] })) assert.equal(calls, 0) } finally { globalThis.fetch = original } }) test("issue acknowledgment and polling require matching job identity", async () => { const original = globalThis.fetch try { for (const payload of [{}, { jobId: "" }, { jobId: " " }]) { globalThis.fetch = async () => new Response(JSON.stringify(payload), { status: 200 }) await assert.rejects(() => createCertificateIssueJob(base, issue)) } globalThis.fetch = async () => new Response(JSON.stringify(job), { status: 200 }) assert.equal((await getCertificateIssueJob(base, "job-1")).status, "queued") await assert.rejects(() => getCertificateIssueJob(base, "job-other"), /другой задаче/) } finally { globalThis.fetch = original } }) test("Cloudflare failure preserves backend message and malformed success is rejected", async () => { const original = globalThis.fetch try { globalThis.fetch = async () => new Response(JSON.stringify({ ok: false, message: "Cloudflare отказал" }), { status: 400 }) await assert.rejects(() => testAcmeSettings(base), /Cloudflare отказал/) globalThis.fetch = async () => new Response(JSON.stringify({}), { status: 200 }) await assert.rejects(() => testAcmeSettings(base)) globalThis.fetch = async () => new Response(JSON.stringify({ ok: false, message: "denied" }), { status: 200 }) assert.equal((await testAcmeSettings(base)).ok, false) } finally { globalThis.fetch = original } }) test("requestId survives POST and mismatched acknowledgement is rejected", async () => { const original = globalThis.fetch; const requestId = "11111111-1111-4111-8111-111111111111"; let returnedId = "other"; let body: Record = {} globalThis.fetch = async (_url, init) => { body = JSON.parse(String(init?.body)); return new Response(JSON.stringify({ jobId: returnedId }), { status: 200 }) } try { await assert.rejects(() => createCertificateIssueJob(base, { ...issue, requestId }), /другому запросу/) assert.equal(body.requestId, requestId) returnedId = requestId; assert.equal((await createCertificateIssueJob(base, { ...issue, requestId })).jobId, requestId) } finally { globalThis.fetch = original } })