Skip to content

Commit 553d3f2

Browse files
committed
test(developer): fix mocks for missing exports in developer API tests
1 parent 6c195a4 commit 553d3f2

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

apps/web/__tests__/unit/developer-actions.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ vi.mock("@cap/database/helpers", () => ({
3131
nanoIdLong: vi.fn(() => "test-nano-id-long-value"),
3232
}));
3333

34+
vi.mock("@cap/database/crypto", () => ({
35+
encrypt: vi.fn(async (value: string) => `encrypted:${value}`),
36+
}));
37+
3438
vi.mock("@cap/database/schema", () => ({
3539
developerApps: {
3640
id: "id",

apps/web/__tests__/unit/developer-credits-checkout.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ vi.mock("@cap/database/schema", () => ({
5151
}));
5252

5353
vi.mock("@cap/env", () => ({
54+
buildEnv: {
55+
NEXT_PUBLIC_IS_CAP: "",
56+
NEXT_PUBLIC_WEB_URL: "https://cap.test",
57+
},
5458
serverEnv: () => ({
5559
WEB_URL: "https://cap.test",
5660
}),
@@ -70,6 +74,10 @@ const mockStripe = {
7074
};
7175

7276
vi.mock("@cap/utils", () => ({
77+
STRIPE_DEVELOPER_CREDITS_PRODUCT_ID: {
78+
development: "prod_dev_test",
79+
production: "prod_live_test",
80+
},
7381
stripe: () => mockStripe,
7482
}));
7583

@@ -126,8 +134,7 @@ describe("POST /api/developer/credits/checkout", () => {
126134
makeRequest({ appId: "app-456", amountCents: 1000 }),
127135
);
128136
expect(res.status).toBe(401);
129-
const body = await res.json();
130-
expect(body.error).toBe("Unauthorized");
137+
expect(await res.text()).toBe("User not authenticated");
131138
});
132139

133140
it("returns 400 when appId is missing", async () => {

apps/web/__tests__/unit/developer-cron-storage.test.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ vi.mock("@cap/database/schema", () => ({
3434
vi.mock("drizzle-orm", () => ({
3535
and: vi.fn((...args: any[]) => args),
3636
eq: vi.fn((a: any, b: any) => ({ eq: [a, b] })),
37+
inArray: vi.fn((a: any, b: any) => ({ inArray: [a, b] })),
3738
isNull: vi.fn((a: any) => ({ isNull: a })),
3839
sql: vi.fn(),
3940
}));
@@ -61,20 +62,36 @@ function makeChain(
6162
select: vi.fn(() => chain),
6263
from: vi.fn(() => chain),
6364
where: vi.fn(() => chain),
65+
groupBy: vi.fn(() => chain),
6466
limit: vi.fn(() => Promise.resolve(result)),
6567
insert: vi.fn(() => chain),
6668
values: vi.fn(() => Promise.resolve()),
6769
update: vi.fn(() => chain),
6870
set: vi.fn(() => chain),
6971
transaction: vi.fn(async (cb: any) => {
72+
let currentOperation: "select" | "update" | "insert" | null = null;
7073
const txChain: any = {
71-
select: vi.fn(() => txChain),
74+
select: vi.fn(() => {
75+
currentOperation = "select";
76+
return txChain;
77+
}),
7278
from: vi.fn(() => txChain),
73-
where: vi.fn(() => txChain),
79+
where: vi.fn(() => {
80+
if (currentOperation === "update") {
81+
return Promise.resolve([{ affectedRows: 1 }]);
82+
}
83+
return txChain;
84+
}),
7485
limit: vi.fn(() => Promise.resolve([{ balanceMicroCredits: 0 }])),
75-
insert: vi.fn(() => txChain),
86+
insert: vi.fn(() => {
87+
currentOperation = "insert";
88+
return txChain;
89+
}),
7690
values: vi.fn(() => Promise.resolve()),
77-
update: vi.fn(() => txChain),
91+
update: vi.fn(() => {
92+
currentOperation = "update";
93+
return txChain;
94+
}),
7895
set: vi.fn(() => txChain),
7996
};
8097
if (options?.onTransaction) {
@@ -148,7 +165,7 @@ describe("developer-storage cron job", () => {
148165
setupDbSequence([
149166
[{ id: "app-1" }],
150167
[],
151-
[{ totalDurationMinutes: 10, videoCount: 5 }],
168+
[{ appId: "app-1", totalDurationMinutes: 10, videoCount: 5 }],
152169
[{ id: "account-1", appId: "app-1", balanceMicroCredits: 1000 }],
153170
[],
154171
]);
@@ -167,7 +184,7 @@ describe("developer-storage cron job", () => {
167184

168185
setupDbSequence([
169186
[{ id: "app-1" }],
170-
[{ id: "snapshot-1", processedAt: new Date() }],
187+
[{ id: "snapshot-1", appId: "app-1", processedAt: new Date() }],
171188
]);
172189

173190
await GET(makeRequest(`Bearer ${CRON_SECRET}`));
@@ -184,7 +201,7 @@ describe("developer-storage cron job", () => {
184201
setupDbSequence([
185202
[{ id: "app-1" }],
186203
[],
187-
[{ totalDurationMinutes: 0, videoCount: 0 }],
204+
[{ appId: "app-1", totalDurationMinutes: 0, videoCount: 0 }],
188205
]);
189206

190207
await GET(makeRequest(`Bearer ${CRON_SECRET}`));
@@ -201,7 +218,7 @@ describe("developer-storage cron job", () => {
201218
setupDbSequence([
202219
[{ id: "app-1" }],
203220
[],
204-
[{ totalDurationMinutes: 10, videoCount: 5 }],
221+
[{ appId: "app-1", totalDurationMinutes: 10, videoCount: 5 }],
205222
[],
206223
]);
207224

@@ -223,7 +240,7 @@ describe("developer-storage cron job", () => {
223240
[
224241
[{ id: "app-1" }],
225242
[],
226-
[{ totalDurationMinutes: 10, videoCount: 5 }],
243+
[{ appId: "app-1", totalDurationMinutes: 10, videoCount: 5 }],
227244
[{ id: "account-1", appId: "app-1", balanceMicroCredits: 1000 }],
228245
[],
229246
],
@@ -254,7 +271,7 @@ describe("developer-storage cron job", () => {
254271
[
255272
[{ id: "app-1" }],
256273
[],
257-
[{ totalDurationMinutes: 10, videoCount: 3 }],
274+
[{ appId: "app-1", totalDurationMinutes: 10, videoCount: 3 }],
258275
[{ id: "account-1", appId: "app-1", balanceMicroCredits: 500 }],
259276
[],
260277
],

0 commit comments

Comments
 (0)