Skip to content
Open
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
49 changes: 39 additions & 10 deletions src/tenancy/__tests__/heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,45 @@ describe("reportAgentReady", () => {
});

describe("reportFirstDmSent", () => {
// FIXME: this test fails on Bun 1.3.13 in CI with `expect(count).toBe(1)`
// receiving 0, despite the structurally-identical reportAgentReady
// "POSTs to ..." test passing on the same runtime. Two refactors did not
// fix it (removing the mock() cast, then switching to a globalThis.fetch
// override). The implementation is verified correct on local Bun 1.3.5
// (8/8 pass) and the reportAgentReady sibling test exercises the same
// postBestEffort path on every CI run. Marked todo to unblock the merge;
// tracked in follow-up notes for a Bun 1.3.13 closure-mutation
// investigation.
test.todo("POSTs to /v1/tenant_status/first_dm_sent with the slack_message_ts", () => {});
test("POSTs to /v1/tenant_status/first_dm_sent with the slack_message_ts", async () => {
// Mirrors the passing reportAgentReady sibling: override
// globalThis.fetch with a plain async function that mutates only
// scalar locals. The earlier closure-captured-array recorder was
// what surfaced the Bun 1.3.13 `expect(count).toBe(1)` -> 0 flake;
// scalar locals + the mandatory try/finally restore avoid it.
let count = 0;
let lastUrl = "";
let lastBody = "";
let lastMethod = "";
let lastContentType = "";

const originalFetch = globalThis.fetch;
try {
globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
count++;
lastUrl = typeof input === "string" ? input : input.toString();
lastBody = String(init?.body ?? "");
lastMethod = String(init?.method ?? "");
const headers = init?.headers as Record<string, string> | undefined;
lastContentType = headers?.["content-type"] ?? "";
return new Response(null, { status: 204 });
}) as typeof fetch;

await reportFirstDmSent({
metadataBaseUrl: "http://169.254.169.254",
slackMessageTs: "1715000000.000123",
// no fetchImpl: exercise the globalThis.fetch fallback path.
});

expect(count).toBe(1);
expect(lastUrl).toBe("http://169.254.169.254/v1/tenant_status/first_dm_sent");
expect(lastMethod).toBe("POST");
expect(lastContentType).toBe("application/json");
expect(JSON.parse(lastBody)).toEqual({ slack_message_ts: "1715000000.000123" });
} finally {
globalThis.fetch = originalFetch;
}
});

test("skips the POST when slack_message_ts is empty (caller must guard)", async () => {
const { fn, calls } = makeFetchOk();
Expand Down
Loading