|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { runBillingLimitReconcileTick } from "~/v3/services/billingLimit/runBillingLimitReconcileTick.server"; |
| 3 | +import type { PendingBillingLimitResolve } from "~/v3/services/billingLimit/billingLimitPendingResolve.types"; |
| 4 | + |
| 5 | +describe("runBillingLimitReconcileTick", () => { |
| 6 | + const pending: PendingBillingLimitResolve = { |
| 7 | + organizationId: "org_pending", |
| 8 | + resumeMode: "queue", |
| 9 | + resolvedAt: "2026-06-17T12:00:00.000Z", |
| 10 | + }; |
| 11 | + |
| 12 | + it("runs pending resolves before collecting orgs and excludes still-pending orgs", async () => { |
| 13 | + const order: string[] = []; |
| 14 | + |
| 15 | + await runBillingLimitReconcileTick({ |
| 16 | + getPendingResolves: async () => ({ orgs: [pending] }), |
| 17 | + runPendingResolves: async (pendingResolves) => { |
| 18 | + order.push(`pending:${pendingResolves.map((row) => row.organizationId).join(",")}`); |
| 19 | + return new Set(["org_pending"]); |
| 20 | + }, |
| 21 | + collectOrgs: async (options) => { |
| 22 | + order.push(`collect:${[...options?.excludeOrgIds ?? []].join(",")}`); |
| 23 | + return { |
| 24 | + targets: [{ organizationId: "org_active", targetState: "grace" }], |
| 25 | + queuedOrgIds: ["org_active"], |
| 26 | + }; |
| 27 | + }, |
| 28 | + reconcileTarget: async (target) => { |
| 29 | + order.push(`reconcile:${target.organizationId}:${target.targetState}`); |
| 30 | + }, |
| 31 | + clearProcessedQueue: async (queuedOrgIds, processedOrgIds) => { |
| 32 | + order.push(`clear:${queuedOrgIds.join(",")}:${processedOrgIds.join(",")}`); |
| 33 | + }, |
| 34 | + bustCaches: () => {}, |
| 35 | + enqueueConverge: async () => undefined, |
| 36 | + }); |
| 37 | + |
| 38 | + expect(order).toEqual([ |
| 39 | + "pending:org_pending", |
| 40 | + "collect:org_pending", |
| 41 | + "reconcile:org_active:grace", |
| 42 | + "clear:org_active:org_active", |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it("reconciles collected targets when no pending resolves remain", async () => { |
| 47 | + const reconciled: Array<{ organizationId: string; targetState: string }> = []; |
| 48 | + |
| 49 | + await runBillingLimitReconcileTick({ |
| 50 | + getPendingResolves: async () => ({ orgs: [] }), |
| 51 | + runPendingResolves: async () => new Set(), |
| 52 | + collectOrgs: async () => ({ |
| 53 | + targets: [ |
| 54 | + { organizationId: "org_grace", targetState: "grace" }, |
| 55 | + { organizationId: "org_ok", targetState: "ok" }, |
| 56 | + ], |
| 57 | + queuedOrgIds: ["org_grace", "org_ok"], |
| 58 | + }), |
| 59 | + reconcileTarget: async (target, deps) => { |
| 60 | + reconciled.push(target); |
| 61 | + await deps.enqueueConverge(target.organizationId, target.targetState); |
| 62 | + }, |
| 63 | + clearProcessedQueue: async () => undefined, |
| 64 | + bustCaches: () => {}, |
| 65 | + enqueueConverge: async (organizationId, targetState) => { |
| 66 | + reconciled.push({ organizationId, targetState: `enqueued:${targetState}` }); |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(reconciled).toEqual([ |
| 71 | + { organizationId: "org_grace", targetState: "grace" }, |
| 72 | + { organizationId: "org_grace", targetState: "enqueued:grace" }, |
| 73 | + { organizationId: "org_ok", targetState: "ok" }, |
| 74 | + { organizationId: "org_ok", targetState: "enqueued:ok" }, |
| 75 | + ]); |
| 76 | + }); |
| 77 | +}); |
0 commit comments