From 306f2a60a286dd9c60556eac9b02acf912566311 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 15 Jun 2026 12:22:55 -0700 Subject: [PATCH] update tests --- .../backfillLastEvaluatedAt.test.ts | 50 +++++++++++++++++++ .../test/integration/jobsRepository.test.ts | 12 +++-- 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 server/test/integration/backfillLastEvaluatedAt.test.ts diff --git a/server/test/integration/backfillLastEvaluatedAt.test.ts b/server/test/integration/backfillLastEvaluatedAt.test.ts new file mode 100644 index 000000000..53dc6c544 --- /dev/null +++ b/server/test/integration/backfillLastEvaluatedAt.test.ts @@ -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); + }); +}); diff --git a/server/test/integration/jobsRepository.test.ts b/server/test/integration/jobsRepository.test.ts index d09174b84..431c185d4 100644 --- a/server/test/integration/jobsRepository.test.ts +++ b/server/test/integration/jobsRepository.test.ts @@ -56,11 +56,13 @@ const readRow = (id: string) => JobModel.findById(id).lean(); // 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" });