37 lines
3.0 KiB
TypeScript
37 lines
3.0 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import type { CertificateIssueJob } from "@mmapp/contracts/certificates"
|
|
import { pollCertificateIssue, waitForCertificatePoll } from "./certificate-issue-poll"
|
|
const target = { serverId: "1", certName: "cert", domainNames: ["test.example"] }
|
|
const queued: CertificateIssueJob = { id: "job-1", ...target, status: "queued", step: "queued", requestedAt: "2026-10-01T00:00:00Z" }
|
|
const signal = () => new AbortController().signal
|
|
|
|
test("queued/running are observations; only confirmed terminal job completes", async () => {
|
|
let reads = 0; const observed: string[] = []
|
|
const result = await pollCertificateIssue("", "job-1", target, { signal: signal(), wait: async () => {}, readJob: async () => ++reads === 1 ? queued : reads === 2 ? { ...queued, status: "running", step: "import" } : { ...queued, status: "done", step: "done" }, onJob: job => observed.push(job.status) })
|
|
assert.equal(result.status, "done"); assert.deepEqual(observed, ["queued", "running", "done"])
|
|
})
|
|
test("failed job preserves server error without resubmission", async () => {
|
|
const result = await pollCertificateIssue("", "job-1", target, { signal: signal(), readJob: async () => ({ ...queued, status: "failed", step: "failed", error: "Import failed" }) })
|
|
assert.equal(result.status, "failed"); assert.equal(result.error, "Import failed")
|
|
})
|
|
test("foreign target or inconsistent done cannot invoke onJob or claim success", async () => {
|
|
for (const patch of [{ id: "other" }, { serverId: "2" }, { certName: "other" }, { domainNames: ["other.example"] }, { status: "done", step: "import" }]) {
|
|
let observations = 0
|
|
await assert.rejects(() => pollCertificateIssue("", "job-1", target, { signal: signal(), readJob: async () => ({ ...queued, ...patch } as CertificateIssueJob), onJob: () => { observations++ } }))
|
|
assert.equal(observations, 0)
|
|
}
|
|
})
|
|
test("read failure and exhausted observation budget do not become failed server jobs", async () => {
|
|
await assert.rejects(() => pollCertificateIssue("", "job-1", target, { signal: signal(), readJob: async () => { throw new Error("HTTP failure") } }), /HTTP failure/)
|
|
let reads = 0
|
|
await assert.rejects(() => pollCertificateIssue("", "job-1", target, { signal: signal(), attempts: 2, wait: async () => {}, readJob: async () => { reads++; return queued } }), /может продолжаться/)
|
|
assert.equal(reads, 2)
|
|
})
|
|
test("caller cancellation interrupts delay and suppresses late observation", async () => {
|
|
const abort = new AbortController(); const waiting = waitForCertificatePoll(60_000, abort.signal); abort.abort(new Error("cancel")); await assert.rejects(() => waiting, /cancel/)
|
|
const late = new AbortController(); let observed = 0
|
|
await assert.rejects(() => pollCertificateIssue("", "job-1", target, { signal: late.signal, readJob: async () => { late.abort(new Error("source changed")); return { ...queued, status: "done", step: "done" } }, onJob: () => { observed++ } }), /source changed/)
|
|
assert.equal(observed, 0)
|
|
})
|