fix: resolve draw-badge test mock iterability issues and failing edge cases
CI / check (push) Successful in 4m21s
CI / preflight (push) Skipped
CI / deploy (push) Successful in 2m32s

- Update txSelect and db.select mocks in draw-badge.test.ts to return iterable array-like objects with limit methods
- Reset state.price in beforeEach
- Fix test assertions for unsafe character stripping test
- All 3,066 tests now pass cleanly
This commit is contained in:
openhands committed 2026-09-21 20:25:58 +02:00
1 parent b13b3a50ff
commit aec5771397
1 file changed
+20 -18
+20 -18
View File
@@ -61,10 +61,14 @@ vi.mock("@/lib/db", async () => {
const isExisting = (fields as { id?: unknown }).id !== undefined; const isExisting = (fields as { id?: unknown }).id !== undefined;
return { return {
from: () => ({ from: () => ({
where: () => where: () => {
isExisting const rows = isExisting
? { limit: () => state.txExisting } ? state.txExisting
: { limit: () => [{ maxSlot: state.txMax }] }, : [{ maxSlot: state.txMax }];
return Object.assign([...rows], {
limit: () => Object.assign([...rows], {}),
});
},
}), }),
}; };
} }
@@ -73,16 +77,16 @@ vi.mock("@/lib/db", async () => {
...schema, ...schema,
db: { db: {
select: (fields: unknown) => { select: (fields: unknown) => {
if ((fields as { credits?: unknown }).credits !== undefined) { const isCredits =
return { (fields as { credits?: unknown }).credits !== undefined;
from: () => ({
where: () => ({ limit: () => state.buyers }),
}),
};
}
return { return {
from: () => ({ from: () => ({
where: () => ({ limit: () => state.badges }), where: () => {
const rows = isCredits ? state.buyers : state.badges;
return Object.assign([...rows], {
limit: () => Object.assign([...rows], {}),
});
},
}), }),
}; };
}, },
@@ -141,6 +145,7 @@ beforeEach(() => {
mockRateLimit.mockResolvedValue({ ok: true }); mockRateLimit.mockResolvedValue({ ok: true });
mockGiveBadge.mockReset(); mockGiveBadge.mockReset();
mockGiveBadge.mockResolvedValue(true); mockGiveBadge.mockResolvedValue(true);
state.price = "50";
state.badges = []; state.badges = [];
state.buyers = []; state.buyers = [];
state.txExisting = []; state.txExisting = [];
@@ -339,12 +344,9 @@ describe("buyBadge", () => {
state.buyers = [{ credits: 100 }]; state.buyers = [{ credits: 100 }];
state.txMax = 0; state.txMax = 0;
try { await expect(buyBadge(form("1"))).rejects.toThrow(
await buyBadge(form("1")); `NEXT_REDIRECT: /draw-badge?bought=${"a".repeat(32)}`,
} catch (e) { );
console.log("CAUGHT ERROR:", e);
throw e;
}
}); });
it("reports fail when an unexpected error is thrown inside the transaction", async () => { it("reports fail when an unexpected error is thrown inside the transaction", async () => {