baa-conductor


baa-conductor / apps / status-api / src
codex@macbookpro  ·  2026-03-31

index.test.js

  1import assert from "node:assert/strict";
  2import test from "node:test";
  3
  4import {
  5  createStatusSnapshotFromControlApiPayload,
  6  readStatusSnapshot,
  7  resolveStatusApiControlApiBase,
  8  resolveStatusApiTruthSourceBase,
  9  StaticStatusSnapshotLoader
 10} from "../dist/apps/status-api/src/data-source.js";
 11import { createStatusApiHandler } from "../dist/apps/status-api/src/service.js";
 12
 13test("status-api truth source prefers BAA_CONDUCTOR_LOCAL_API over legacy compatibility base", () => {
 14  const env = {
 15    BAA_CONDUCTOR_LOCAL_API: "http://100.71.210.78:4317",
 16    BAA_CONTROL_API_BASE: "https://conductor.makefile.so"
 17  };
 18
 19  assert.equal(resolveStatusApiTruthSourceBase(env), "http://100.71.210.78:4317/");
 20  assert.equal(resolveStatusApiControlApiBase(env), "http://100.71.210.78:4317/");
 21});
 22
 23test("status-api truth source falls back to BAA_CONTROL_API_BASE only when local conductor base is absent", () => {
 24  const env = {
 25    BAA_CONDUCTOR_LOCAL_API: "   ",
 26    BAA_CONTROL_API_BASE: "https://conductor.makefile.so"
 27  };
 28
 29  assert.equal(resolveStatusApiTruthSourceBase(env), "https://conductor.makefile.so/");
 30});
 31
 32test("status-api truth source defaults to the canonical conductor local API", () => {
 33  assert.equal(resolveStatusApiTruthSourceBase({}), "http://100.71.210.78:4317/");
 34});
 35
 36test("status snapshots mark conductor-api as the upstream source", () => {
 37  const snapshot = createStatusSnapshotFromControlApiPayload(
 38    {
 39      ok: true,
 40      data: {
 41        mode: "running",
 42        queue_depth: 2,
 43        active_runs: 1
 44      }
 45    },
 46    new Date("2026-03-25T00:00:00.000Z")
 47  );
 48
 49  assert.equal(snapshot.source, "conductor-api");
 50});
 51
 52test("status snapshots normalize millisecond automation timestamps against second-based D1 lease timestamps", async () => {
 53  const snapshot = await readStatusSnapshot(
 54    {
 55      countActiveRuns: async () => 1,
 56      countQueuedTasks: async () => 2,
 57      getAutomationState: async () => ({
 58        mode: "paused",
 59        stateKey: "automation",
 60        updatedAt: Date.UTC(2026, 2, 31, 12, 4, 0),
 61        valueJson: JSON.stringify({
 62          mode: "paused"
 63        })
 64      }),
 65      getCurrentLease: async () => ({
 66        holderHost: "mini",
 67        holderId: "mini-main",
 68        leaseExpiresAt: Math.floor(Date.UTC(2026, 2, 31, 12, 10, 0) / 1000),
 69        leaseName: "global",
 70        metadataJson: null,
 71        preferredHolderId: "mini-main",
 72        renewedAt: Math.floor(Date.UTC(2026, 2, 31, 12, 5, 0) / 1000),
 73        term: 7
 74      })
 75    },
 76    new Date("2026-03-31T12:05:30.000Z")
 77  );
 78
 79  assert.equal(snapshot.mode, "paused");
 80  assert.equal(snapshot.source, "d1");
 81  assert.equal(snapshot.queueDepth, 2);
 82  assert.equal(snapshot.activeRuns, 1);
 83  assert.equal(snapshot.leaseExpiresAt, "2026-03-31T12:10:00.000Z");
 84  assert.equal(snapshot.leaseActive, true);
 85  assert.equal(snapshot.updatedAt, "2026-03-31T12:05:00.000Z");
 86});
 87
 88test("status-api describe reports conductor local truth with legacy compatibility note", async () => {
 89  const handler = createStatusApiHandler(new StaticStatusSnapshotLoader(), {
 90    truthSourceBaseUrl: "http://100.71.210.78:4317"
 91  });
 92
 93  const response = await handler.handle({
 94    method: "GET",
 95    path: "/describe"
 96  });
 97  const payload = JSON.parse(response.body);
 98
 99  assert.equal(payload.ok, true);
100  assert.equal(payload.data.truth_source.type, "conductor-api");
101  assert.equal(payload.data.truth_source.base_url, "http://100.71.210.78:4317");
102  assert.deepEqual(payload.data.notes, [
103    "Status API is read-only.",
104    "Preferred entry lives on conductor: /v1/status and /v1/status/ui.",
105    "Default truth source comes from BAA_CONDUCTOR_LOCAL_API.",
106    "Use BAA_CONTROL_API_BASE only for legacy ad-hoc compatibility overrides."
107  ]);
108});