update tests

This commit is contained in:
Alex Holliday committed 2026-06-15 12:22:55 -07:00
1 parent abf6dc2908
commit 306f2a60a2
2 files changed
+57 -5

No files matched your search

@@ -0,0 +1,50 @@
import { describe, expect, it, beforeAll, afterAll, beforeEach } from "@jest/globals";
import mongoose from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";
import { MonitorModel } from "../../src/domain/monitors/monitor.model.ts";
import { backfillMonitorLastEvaluatedAt } from "../../src/db/migration/0008_backfillMonitorLastEvaluatedAt.ts";
// ── Real-Mongo harness ─────────────────────────────────────────────────────────
// Legacy monitors are inserted through the raw driver, NOT MonitorModel.create — the
// schema default (lastEvaluatedAt: 0) would otherwise stamp the field and the migration's
// { $exists: false } filter would skip it, defeating the test.
let mongod: MongoMemoryServer;
beforeAll(async () => {
mongod = await MongoMemoryServer.create();
await mongoose.connect(mongod.getUri());
}, 120_000);
afterAll(async () => {
await mongoose.disconnect();
await mongod.stop();
});
beforeEach(async () => {
await MonitorModel.collection.deleteMany({});
});
describe("0008_backfillMonitorLastEvaluatedAt", () => {
it("stamps the upgrade moment (not 0) on monitors missing lastEvaluatedAt", async () => {
await MonitorModel.collection.insertOne({ name: "legacy" }); // pre-upgrade doc, no lastEvaluatedAt
const before = Date.now();
await backfillMonitorLastEvaluatedAt();
const doc = await MonitorModel.collection.findOne({ name: "legacy" });
// Must be a recent timestamp — backfilling 0 would replay the whole check history on the first evaluate.
expect(doc?.lastEvaluatedAt).not.toBe(0);
expect(doc?.lastEvaluatedAt).toBeGreaterThanOrEqual(before);
expect(doc?.lastEvaluatedAt).toBeLessThanOrEqual(Date.now());
});
it("leaves monitors that already have lastEvaluatedAt untouched (idempotent)", async () => {
await MonitorModel.collection.insertOne({ name: "current", lastEvaluatedAt: 12345 });
await backfillMonitorLastEvaluatedAt();
const doc = await MonitorModel.collection.findOne({ name: "current" });
expect(doc?.lastEvaluatedAt).toBe(12345);
});
});
@@ -56,11 +56,13 @@ const readRow = (id: string) => JobModel.findById(id).lean<JobDocument>();
// lock must be stamped with the same id the repo will check against.
const ownedBy = (r: MongoJobsRepository) => (r as unknown as { workerId: string }).workerId;
const WORKER_ID = "worker-under-test";
describe("MongoJobsRepository", () => {
let repo: MongoJobsRepository;
beforeEach(() => {
repo = new MongoJobsRepository();
repo = new MongoJobsRepository(WORKER_ID);
});
// ── claimDue ───────────────────────────────────────────────────────────────
@@ -119,7 +121,7 @@ describe("MongoJobsRepository", () => {
await seedJob();
// Distinct repo instances = distinct workerIds = realistic competing workers.
const workers = Array.from({ length: 20 }, () => new MongoJobsRepository());
const workers = Array.from({ length: 20 }, (_, i) => new MongoJobsRepository(`worker-${i}`));
const results = await Promise.all(workers.map((w) => w.claimDue("check", NOW)));
const winners = results.filter((r): r is Job => r !== null);
@@ -131,7 +133,7 @@ describe("MongoJobsRepository", () => {
await seedJob({ _id: "check:a", refId: "a" });
await seedJob({ _id: "check:b", refId: "b" });
const [w1, w2] = [new MongoJobsRepository(), new MongoJobsRepository()];
const [w1, w2] = [new MongoJobsRepository("worker-1"), new MongoJobsRepository("worker-2")];
const [r1, r2] = await Promise.all([w1.claimDue("check", NOW), w2.claimDue("check", NOW)]);
expect(r1).not.toBeNull();
@@ -234,7 +236,7 @@ describe("MongoJobsRepository", () => {
expect(await repo.renewLocks(["check:mon-1"], renewAt)).toBe(1);
// a second worker scans PAST the original lease end — without renewal it would reclaim
const other = new MongoJobsRepository();
const other = new MongoJobsRepository("competing-worker");
expect(await other.claimDue("check", NOW + LOCK_MS + 1)).toBeNull();
const row = await readRow("check:mon-1");
@@ -302,7 +304,7 @@ describe("MongoJobsRepository", () => {
});
it("concurrent upserts for one monitor produce exactly one row", async () => {
const workers = Array.from({ length: 20 }, () => new MongoJobsRepository());
const workers = Array.from({ length: 20 }, (_, i) => new MongoJobsRepository(`worker-${i}`));
await Promise.all(workers.map((w) => w.upsertEvaluate("mon-1", NOW)));
const count = await JobModel.countDocuments({ type: "evaluate", refId: "mon-1" });