Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(developer): fix mocks for missing exports in developer API tests

Richie McIlroy553d3f24caba
diff --git a/apps/web/__tests__/unit/developer-actions.test.ts b/apps/web/__tests__/unit/developer-actions.test.ts
index cdacd9d5f..d2456de2d 100644
--- a/apps/web/__tests__/unit/developer-actions.test.ts
+++ b/apps/web/__tests__/unit/developer-actions.test.ts
@@ -31,6 +31,10 @@ vi.mock("@cap/database/helpers", () => ({
nanoIdLong: vi.fn(() => "test-nano-id-long-value"),
}));
+vi.mock("@cap/database/crypto", () => ({
+ encrypt: vi.fn(async (value: string) => `encrypted:${value}`),
+}));
+
vi.mock("@cap/database/schema", () => ({
developerApps: {
id: "id",
diff --git a/apps/web/__tests__/unit/developer-credits-checkout.test.ts b/apps/web/__tests__/unit/developer-credits-checkout.test.ts
index 895e346cc..6ed045687 100644
--- a/apps/web/__tests__/unit/developer-credits-checkout.test.ts
+++ b/apps/web/__tests__/unit/developer-credits-checkout.test.ts
@@ -51,6 +51,10 @@ vi.mock("@cap/database/schema", () => ({
}));
vi.mock("@cap/env", () => ({
+ buildEnv: {
+ NEXT_PUBLIC_IS_CAP: "",
+ NEXT_PUBLIC_WEB_URL: "https://cap.test",
+ },
serverEnv: () => ({
WEB_URL: "https://cap.test",
}),
@@ -70,6 +74,10 @@ const mockStripe = {
};
vi.mock("@cap/utils", () => ({
+ STRIPE_DEVELOPER_CREDITS_PRODUCT_ID: {
+ development: "prod_dev_test",
+ production: "prod_live_test",
+ },
stripe: () => mockStripe,
}));
@@ -126,8 +134,7 @@ describe("POST /api/developer/credits/checkout", () => {
makeRequest({ appId: "app-456", amountCents: 1000 }),
);
expect(res.status).toBe(401);
- const body = await res.json();
- expect(body.error).toBe("Unauthorized");
+ expect(await res.text()).toBe("User not authenticated");
});
it("returns 400 when appId is missing", async () => {
diff --git a/apps/web/__tests__/unit/developer-cron-storage.test.ts b/apps/web/__tests__/unit/developer-cron-storage.test.ts
index bf80901fa..36efa4bab 100644
--- a/apps/web/__tests__/unit/developer-cron-storage.test.ts
+++ b/apps/web/__tests__/unit/developer-cron-storage.test.ts
@@ -34,6 +34,7 @@ vi.mock("@cap/database/schema", () => ({
vi.mock("drizzle-orm", () => ({
and: vi.fn((...args: any[]) => args),
eq: vi.fn((a: any, b: any) => ({ eq: [a, b] })),
+ inArray: vi.fn((a: any, b: any) => ({ inArray: [a, b] })),
isNull: vi.fn((a: any) => ({ isNull: a })),
sql: vi.fn(),
}));
@@ -61,20 +62,36 @@ function makeChain(
select: vi.fn(() => chain),
from: vi.fn(() => chain),
where: vi.fn(() => chain),
+ groupBy: vi.fn(() => chain),
limit: vi.fn(() => Promise.resolve(result)),
insert: vi.fn(() => chain),
values: vi.fn(() => Promise.resolve()),
update: vi.fn(() => chain),
set: vi.fn(() => chain),
transaction: vi.fn(async (cb: any) => {
+ let currentOperation: "select" | "update" | "insert" | null = null;
const txChain: any = {
- select: vi.fn(() => txChain),
+ select: vi.fn(() => {
+ currentOperation = "select";
+ return txChain;
+ }),
from: vi.fn(() => txChain),
- where: vi.fn(() => txChain),
+ where: vi.fn(() => {
+ if (currentOperation === "update") {
+ return Promise.resolve([{ affectedRows: 1 }]);
+ }
+ return txChain;
+ }),
limit: vi.fn(() => Promise.resolve([{ balanceMicroCredits: 0 }])),
- insert: vi.fn(() => txChain),
+ insert: vi.fn(() => {
+ currentOperation = "insert";
+ return txChain;
+ }),
values: vi.fn(() => Promise.resolve()),
- update: vi.fn(() => txChain),
+ update: vi.fn(() => {
+ currentOperation = "update";
+ return txChain;
+ }),
set: vi.fn(() => txChain),
};
if (options?.onTransaction) {
@@ -148,7 +165,7 @@ describe("developer-storage cron job", () => {
setupDbSequence([
[{ id: "app-1" }],
[],
- [{ totalDurationMinutes: 10, videoCount: 5 }],
+ [{ appId: "app-1", totalDurationMinutes: 10, videoCount: 5 }],
[{ id: "account-1", appId: "app-1", balanceMicroCredits: 1000 }],
[],
]);
@@ -167,7 +184,7 @@ describe("developer-storage cron job", () => {
setupDbSequence([
[{ id: "app-1" }],
- [{ id: "snapshot-1", processedAt: new Date() }],
+ [{ id: "snapshot-1", appId: "app-1", processedAt: new Date() }],
]);
await GET(makeRequest(`Bearer ${CRON_SECRET}`));
@@ -184,7 +201,7 @@ describe("developer-storage cron job", () => {
setupDbSequence([
[{ id: "app-1" }],
[],
- [{ totalDurationMinutes: 0, videoCount: 0 }],
+ [{ appId: "app-1", totalDurationMinutes: 0, videoCount: 0 }],
]);
await GET(makeRequest(`Bearer ${CRON_SECRET}`));
@@ -201,7 +218,7 @@ describe("developer-storage cron job", () => {
setupDbSequence([
[{ id: "app-1" }],
[],
- [{ totalDurationMinutes: 10, videoCount: 5 }],
+ [{ appId: "app-1", totalDurationMinutes: 10, videoCount: 5 }],
[],
]);
@@ -223,7 +240,7 @@ describe("developer-storage cron job", () => {
[
[{ id: "app-1" }],
[],
- [{ totalDurationMinutes: 10, videoCount: 5 }],
+ [{ appId: "app-1", totalDurationMinutes: 10, videoCount: 5 }],
[{ id: "account-1", appId: "app-1", balanceMicroCredits: 1000 }],
[],
],
@@ -254,7 +271,7 @@ describe("developer-storage cron job", () => {
[
[{ id: "app-1" }],
[],
- [{ totalDurationMinutes: 10, videoCount: 3 }],
+ [{ appId: "app-1", totalDurationMinutes: 10, videoCount: 3 }],
[{ id: "account-1", appId: "app-1", balanceMicroCredits: 500 }],
[],
],