Files
cfw-autumn/server/tests/utils/cusProductUtils/resetTestUtils.ts

114 lines
2.6 KiB
TypeScript

import {
customerEntitlements,
fullCustomerToCustomerEntitlements,
} from "@autumn/shared";
import { findCustomerEntitlement } from "@tests/balances/utils/findCustomerEntitlement.js";
import type { TestContext } from "@tests/utils/testInitUtils/createTestContext.js";
import { eq } from "drizzle-orm";
import { CusService } from "@/internal/customers/CusService.js";
/**
* Redis cache patching was removed with the Redis migration. Tests that call
* this helper now rely on DB state and cache invalidation/rehydration paths.
*/
export const setCachedCusEntField = async (_params: {
orgId: string;
env: string;
customerId: string;
cusEntId: string;
field: string;
value: number | string | null;
}): Promise<void> => {};
/** Redis V2 subject balance patching was removed with the Redis migration. */
export const setCachedSubjectBalanceField = async (_params: {
ctx?: TestContext;
orgId: string;
env: string;
customerId: string;
featureId: string;
customerEntitlementId: string;
field: string;
value: number | string | null;
redisV2?: unknown;
}): Promise<void> => {};
/**
* Expire a cusEnt's next_reset_at in Postgres so the next read triggers a lazy
* reset. Returns the cusEnt for assertions.
*/
export const expireCusEntForReset = async ({
ctx,
customerId,
featureId,
pastTimeMs,
}: {
ctx: TestContext;
customerId: string;
featureId: string;
pastTimeMs?: number;
}) => {
const cusEnt = await findCustomerEntitlement({
ctx,
customerId,
featureId,
});
if (!cusEnt) {
throw new Error(
`cusEnt not found for customer=${customerId} feature=${featureId}`,
);
}
const pastTime = pastTimeMs ?? Date.now() - 1000;
await ctx.db
.update(customerEntitlements)
.set({ next_reset_at: pastTime })
.where(eq(customerEntitlements.id, cusEnt.id));
return cusEnt;
};
/**
* Expire ALL cusEnts for a given feature in Postgres.
* Use this for entity-level features where multiple cusEnts share the same feature_id.
*/
export const expireAllCusEntsForReset = async ({
ctx,
customerId,
featureId,
pastTimeMs,
}: {
ctx: TestContext;
customerId: string;
featureId: string;
pastTimeMs?: number;
}) => {
const fullCustomer = await CusService.getFull({
ctx,
idOrInternalId: customerId,
});
const cusEnts = fullCustomerToCustomerEntitlements({
fullCustomer,
featureId,
});
if (cusEnts.length === 0) {
throw new Error(
`No cusEnts found for customer=${customerId} feature=${featureId}`,
);
}
const pastTime = pastTimeMs ?? Date.now() - 1000;
for (const cusEnt of cusEnts) {
await ctx.db
.update(customerEntitlements)
.set({ next_reset_at: pastTime })
.where(eq(customerEntitlements.id, cusEnt.id));
}
return cusEnts;
};