test(chat): add test for defaulting nullable credit fields to zero

This commit is contained in:
2026-06-30 19:33:34 +08:00
parent a105820b88
commit b7bccaa4cf
2 changed files with 30 additions and 5 deletions
@@ -79,4 +79,29 @@ describe("UnlockPrivateResponse", () => {
expect(response.content).toBe("");
});
it("defaults nullable credit fields to zero", () => {
const response = UnlockPrivateResponse.from({
unlocked: false,
content: "",
reason: "insufficient_balance",
creditBalance: null,
creditsCharged: null,
requiredCredits: null,
shortfallCredits: null,
lockDetail: {
locked: true,
showContent: false,
showUpgrade: true,
reason: "insufficient_balance",
hint: null,
detail: null,
},
});
expect(response.creditBalance).toBe(0);
expect(response.creditsCharged).toBe(0);
expect(response.requiredCredits).toBe(0);
expect(response.shortfallCredits).toBe(0);
});
});
@@ -1,6 +1,6 @@
import { z } from "zod";
import { stringOrEmpty } from "../nullable-defaults";
import { numberOrZero, stringOrEmpty } from "../nullable-defaults";
import { ChatLockDetailSchema } from "./chat_payloads";
/**
@@ -12,10 +12,10 @@ export const UnlockPrivateResponseSchema = z.object({
unlocked: z.boolean(),
content: stringOrEmpty,
reason: UnlockPrivateReasonSchema,
creditBalance: z.number().default(0),
creditsCharged: z.number().default(0),
requiredCredits: z.number().default(0),
shortfallCredits: z.number().default(0),
creditBalance: numberOrZero,
creditsCharged: numberOrZero,
requiredCredits: numberOrZero,
shortfallCredits: numberOrZero,
lockDetail: ChatLockDetailSchema,
});