import { getBackgroundToolResultsCollection, upsertBackgroundToolResult, findBackgroundToolResult, findBackgroundToolResultsByCallSid, } from "../backgroundToolResults.getters"; import { BackgroundToolStatus } from "../backgroundToolResults.types"; describe("backgroundToolResults getters", () => { it('should return the "backgroundToolResults" collection', () => { const collection = getBackgroundToolResultsCollection(); expect(collection.collectionName).toBe("backgroundToolResults"); }); it("should create a new background tool result with correct fields and default TTL", async () => { const callSid = "CA-background-call-1"; const toolName = "exampleTool"; const status: BackgroundToolStatus = "pending"; const result = null; const before = new Date(); const doc = await upsertBackgroundToolResult({ callSid, toolName, status, result, }); const after = new Date(); expect(doc).not.toBeNull(); expect(doc?.callSid).toBe(callSid); expect(doc?.toolName).toBe(toolName); expect(doc?.status).toBe(status); expect(doc?.result).toBe(result); expect(doc?.createdAt).toBeInstanceOf(Date); expect(doc?.updatedAt).toBeInstanceOf(Date); expect(doc?.expiredAt).toBeInstanceOf(Date); const createdAt = doc!.createdAt.getTime(); const expiredAt = doc!.expiredAt.getTime(); const ttlMs = expiredAt - createdAt; expect(ttlMs).toBeGreaterThanOrEqual(60 * 60_000 - 1000); expect(ttlMs).toBeLessThanOrEqual(60 * 60_000 + 1000); expect(doc!.createdAt.getTime()).toBeGreaterThanOrEqual(before.getTime()); expect(doc!.updatedAt.getTime()).toBeLessThanOrEqual(after.getTime()); }); it("should upsert by callSid + toolName and keep createdAt while updating other fields", async () => { const callSid = "CA-background-call-2"; const toolName = "aggregationTool"; const first = await upsertBackgroundToolResult({ callSid, toolName, status: "pending", result: null, }); expect(first).not.toBeNull(); const firstCreatedAt = first!.createdAt; const firstUpdatedAt = first!.updatedAt; const firstExpiredAt = first!.expiredAt; const customTtlMs = 5 * 60_000; const secondResultPayload = { value: 42 }; const second = await upsertBackgroundToolResult({ callSid, toolName, status: "success", result: secondResultPayload, ttlMs: customTtlMs, }); expect(second).not.toBeNull(); expect(second!._id.toHexString()).toBe(first!._id.toHexString()); expect(second!.callSid).toBe(callSid); expect(second!.toolName).toBe(toolName); expect(second!.status).toBe("success"); expect(second!.result).toEqual(secondResultPayload); expect(second!.createdAt.getTime()).toBe(firstCreatedAt.getTime()); expect(second!.updatedAt.getTime()).toBeGreaterThanOrEqual( firstUpdatedAt.getTime(), ); expect(second!.expiredAt.getTime()).not.toBe(firstExpiredAt.getTime()); const ttlMs = second!.expiredAt.getTime() - second!.updatedAt.getTime(); expect(ttlMs).toBeGreaterThanOrEqual(customTtlMs - 1000); expect(ttlMs).toBeLessThanOrEqual(customTtlMs + 1000); }); it("should find background tool result by callSid and toolName", async () => { const callSid = "CA-background-call-3"; const toolName = "fetchUserData"; await upsertBackgroundToolResult({ callSid, toolName, status: "success", result: { foo: "bar" }, }); const found = await findBackgroundToolResult(callSid, toolName); expect(found).not.toBeNull(); expect(found?.callSid).toBe(callSid); expect(found?.toolName).toBe(toolName); }); it("should return null when background tool result does not exist", async () => { const found = await findBackgroundToolResult( "non-existing-callSid", "nonExistingTool", ); expect(found).toBeNull(); }); it("should return all background tool results for a callSid ordered by createdAt", async () => { const callSid = "CA-background-call-4"; await upsertBackgroundToolResult({ callSid, toolName: "toolA", status: "pending", result: null, }); await new Promise((resolve) => setTimeout(resolve, 10)); await upsertBackgroundToolResult({ callSid, toolName: "toolB", status: "success", result: { ok: true }, }); const results = await findBackgroundToolResultsByCallSid(callSid); expect(results).toHaveLength(2); expect(results[0].createdAt.getTime()).toBeLessThanOrEqual( results[1].createdAt.getTime(), ); }); });