import { getClientsConfigCollection, incrementClientQuotaUsage, listClientIdsWithConfiguredQuota, resetClientQuotaUsage, } from "../../clientsConfig/clientsConfig.getters"; import { getClientsCollection } from "../clients.getters"; import { markQuotaAlertSent, markQuotaExpiredSent, updateQuotaAlertsConfig, } from "../clients.quota.getters"; describe("quota getters", () => { beforeEach(async () => { await getClientsConfigCollection().deleteMany({}); await getClientsCollection().deleteMany({}); }); const seed = async (clientId: string, configQuota: object, clientQuota?: object) => { await getClientsConfigCollection().insertOne({ clientId, products: {}, quota: configQuota, } as any); if (clientQuota !== undefined) { await getClientsCollection().insertOne({ clientId, createdAt: new Date(), updatedAt: new Date(), credits: 0, quota: clientQuota, } as any); } }; it("updateQuotaAlertsConfig stores alerts on clients", async () => { const clientId = "quota-alerts"; await seed(clientId, { totalQuota: 100, usedQuota: 5 }); await updateQuotaAlertsConfig(clientId, [ { thresholdPct: 50, recipientEmails: ["a@example.com"] }, { thresholdPct: 80, recipientEmails: ["b@example.com", "c@example.com"] }, ]); const client = await getClientsCollection().findOne({ clientId }); expect(client?.quota?.alerts).toEqual([ { thresholdPct: 50, recipientEmails: ["a@example.com"] }, { thresholdPct: 80, recipientEmails: ["b@example.com", "c@example.com"] }, ]); const config = await getClientsConfigCollection().findOne({ clientId }); expect(config?.quota).toEqual({ totalQuota: 100, usedQuota: 5 }); }); it("markQuotaAlertSent returns true only on first transition", async () => { const clientId = "quota-alert-sent"; await seed( clientId, { totalQuota: 100, usedQuota: 0 }, { alerts: [{ thresholdPct: 50, recipientEmails: ["a@b.com"] }] }, ); expect(await markQuotaAlertSent(clientId, 0)).toBe(true); expect(await markQuotaAlertSent(clientId, 0)).toBe(false); const client = await getClientsCollection().findOne({ clientId }); expect(client?.quota?.alerts?.[0]?.thresholdSent).toBe(true); }); it("markQuotaExpiredSent returns true only on first transition", async () => { const clientId = "quota-notify"; await seed(clientId, { totalQuota: 10, usedQuota: 10 }); expect(await markQuotaExpiredSent(clientId)).toBe(true); expect(await markQuotaExpiredSent(clientId)).toBe(false); const client = await getClientsCollection().findOne({ clientId }); expect(client?.quota?.expiredSent).toBe(true); }); it("listClientIdsWithConfiguredQuota returns clientIds with totalQuota set", async () => { await seed("quota-a", { totalQuota: 100, usedQuota: 0 }); await seed("quota-b", { totalQuota: 50, usedQuota: 10 }); await getClientsConfigCollection().insertOne({ clientId: "no-quota", products: {}, } as any); const ids = await listClientIdsWithConfiguredQuota(); expect(ids.sort()).toEqual(["quota-a", "quota-b"]); }); it("incrementClientQuotaUsage increments usedQuota on clientsConfig", async () => { const clientId = "quota-inc"; await seed(clientId, { totalQuota: 100, usedQuota: 3 }); await incrementClientQuotaUsage(clientId, 2); const config = await getClientsConfigCollection().findOne({ clientId }); expect(config?.quota?.usedQuota).toBe(5); }); it("resetClientQuotaUsage zeros usedQuota and clears client quota flags", async () => { const clientId = "quota-reset"; await seed( clientId, { totalQuota: 100, usedQuota: 50 }, { expiredSent: true, alerts: [{ thresholdPct: 50, recipientEmails: ["a@b.com"], thresholdSent: true }], }, ); await resetClientQuotaUsage(clientId); const config = await getClientsConfigCollection().findOne({ clientId }); expect(config?.quota?.usedQuota).toBe(0); const client = await getClientsCollection().findOne({ clientId }); expect(client?.quota?.expiredSent).toBeUndefined(); expect(client?.quota?.alerts?.[0]?.thresholdSent).toBeUndefined(); }); });