import { describe, it, expect, beforeEach, jest } from "@jest/globals"; import { ObjectId } from "mongodb"; import { getAllRetryAnalyzeDocs, upsertRetryAnalyzeDoc, insertRetryAnalyzeDoc, updateRetryAnalyzeErrorLog, } from "../retryAnalyze.getters"; import { setDb } from "../../index"; import { RetryAnalyzeDoc } from "../retryAnalyze.types"; describe("retryAnalyze.getters", () => { let mockCollection: any; let mockDb: any; beforeEach(() => { mockCollection = { find: jest.fn().mockReturnThis(), toArray: jest.fn(), insertOne: jest.fn(), updateOne: jest.fn(), deleteOne: jest.fn(), }; mockDb = { collection: jest.fn().mockReturnValue(mockCollection), }; setDb(mockDb as any); }); describe("getAllRetryAnalyzeDocs", () => { it("should filter documents with retry >= 5 and include old format docs", async () => { const mockDocs = [ { _id: new ObjectId(), callSid: "old-doc", errorLog: "{}" }, { _id: new ObjectId(), callSid: "retry-1", errorLog: "{}", retry: 1 }, { _id: new ObjectId(), callSid: "retry-5", errorLog: "{}", retry: 5 }, ]; mockCollection.toArray.mockResolvedValue(mockDocs); await getAllRetryAnalyzeDocs(); expect(mockCollection.find).toHaveBeenCalledWith({ $or: [{ retry: { $lt: 5 } }, { retry: { $exists: false } }], }); }); }); describe("upsertRetryAnalyzeDoc", () => { const error429 = JSON.stringify({ error: { code: 429, message: "Resource exhausted. Please try again later.", status: "RESOURCE_EXHAUSTED", }, }); it("should upsert document correctly with $setOnInsert for clientId and createdAt", async () => { const doc = { callSid: "call-123", errorLog: error429, clientId: "client-1", }; mockCollection.updateOne.mockResolvedValue({ upsertedId: new ObjectId(), matchedCount: 0, }); await upsertRetryAnalyzeDoc(doc); expect(mockCollection.updateOne).toHaveBeenCalledWith( { callSid: doc.callSid }, expect.objectContaining({ $set: expect.objectContaining({ errorLog: doc.errorLog, updatedAt: expect.any(Date), }), $setOnInsert: expect.objectContaining({ createdAt: expect.any(Date), clientId: doc.clientId, retry: 0, }), }), { upsert: true }, ); }); it("should ensure only one document exists for the same callSid even on multiple failures", async () => { const doc = { callSid: "CA_SINGLE_DOC", errorLog: error429, clientId: "client-1", }; mockCollection.updateOne .mockResolvedValueOnce({ upsertedId: new ObjectId(), matchedCount: 0 }) .mockResolvedValueOnce({ upsertedId: null, matchedCount: 1 }); await upsertRetryAnalyzeDoc(doc); await upsertRetryAnalyzeDoc(doc); expect(mockCollection.updateOne).toHaveBeenCalledWith( { callSid: doc.callSid }, expect.objectContaining({ $setOnInsert: expect.objectContaining({ retry: 0 }), }), { upsert: true }, ); expect(mockCollection.updateOne).toHaveBeenLastCalledWith( { callSid: doc.callSid }, { $inc: { retry: 1 } }, ); }); }); describe("insertRetryAnalyzeDoc", () => { it("should perform a simple insert", async () => { const doc: Omit = { callSid: "call-456", retry: 0, clientId: "client-2", errorLog: "err", updatedAt: new Date(), createdAt: new Date(), }; mockCollection.insertOne.mockResolvedValue({ insertedId: new ObjectId(), }); await insertRetryAnalyzeDoc(doc); expect(mockCollection.insertOne).toHaveBeenCalledWith(doc); }); }); describe("updateRetryAnalyzeErrorLog", () => { it("should update errorLog, updatedAt and increment retry", async () => { const id = new ObjectId(); const errorLog = "new retry error"; mockCollection.updateOne.mockResolvedValue({ modifiedCount: 1 }); await updateRetryAnalyzeErrorLog(id, errorLog); expect(mockCollection.updateOne).toHaveBeenCalledWith( { _id: id }, { $set: { errorLog, updatedAt: expect.any(Date) }, $inc: { retry: 1 }, }, ); }); }); });