Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion backend-api/__tests__/readinessWarning.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
const { buildReadinessWarningDetail, buildReadinessWarningMetadata, buildReadinessWarningState, persistReadinessWarning } = require("../../workers/provisioner/readinessWarning");
const { shouldPersistReadinessWarning, buildReadinessWarningDetail, buildReadinessWarningMetadata, buildReadinessWarningState, persistReadinessWarning } = require("../../workers/provisioner/readinessWarning");

describe("shouldPersistReadinessWarning", () => {
it("returns true for degraded readiness", () => {
expect(shouldPersistReadinessWarning({ ok: false })).toBe(true);
});

it("returns false for healthy readiness", () => {
expect(shouldPersistReadinessWarning({ ok: true })).toBe(false);
});
});

describe("buildReadinessWarningDetail", () => {
it("formats a runtime-only readiness warning", () => {
Expand Down Expand Up @@ -117,6 +127,20 @@ describe("buildReadinessWarningState", () => {
});

describe("persistReadinessWarning", () => {
it("returns null and performs no writes for healthy readiness", async () => {
const db = { query: jest.fn().mockResolvedValue({}) };

const result = await persistReadinessWarning(db, {
agentId: "agent-healthy",
name: "Healthy Nora",
host: "agent.internal",
readiness: { ok: true },
});

expect(result).toBeNull();
expect(db.query).not.toHaveBeenCalled();
});

it("writes warning agent status, warning deployment status, and the runtime warning event in order", async () => {
const db = { query: jest.fn().mockResolvedValue({}) };
const readiness = {
Expand Down
10 changes: 9 additions & 1 deletion workers/provisioner/readinessWarning.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function shouldPersistReadinessWarning(readiness) {
return !readiness?.ok;
}

function buildReadinessWarningDetail(readiness) {
const problems = [];

Expand Down Expand Up @@ -39,6 +43,10 @@ function buildReadinessWarningState({ agentId, name, host, readiness }) {
}

async function persistReadinessWarning(db, { agentId, name, host, readiness }) {
if (!shouldPersistReadinessWarning(readiness)) {
return null;
}

const warningState = buildReadinessWarningState({ agentId, name, host, readiness });

await db.query(`UPDATE agents SET status = '${warningState.agentStatus}' WHERE id = $1`, [agentId]);
Expand All @@ -51,4 +59,4 @@ async function persistReadinessWarning(db, { agentId, name, host, readiness }) {
return warningState;
}

module.exports = { buildReadinessWarningDetail, buildReadinessWarningMetadata, buildReadinessWarningState, persistReadinessWarning };
module.exports = { shouldPersistReadinessWarning, buildReadinessWarningDetail, buildReadinessWarningMetadata, buildReadinessWarningState, persistReadinessWarning };
Loading