Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(mobile): extend mobile API client tests

Richie McIlroyd484442e5f15
diff --git a/apps/mobile/src/api/mobile.test.ts b/apps/mobile/src/api/mobile.test.ts
index 346226ba5..29487ec2b 100644
--- a/apps/mobile/src/api/mobile.test.ts
+++ b/apps/mobile/src/api/mobile.test.ts
@@ -1,3 +1,4 @@
+import { Space } from "@cap/web-domain";
import { describe, expect, it, vi } from "vitest";
import {
createMobileApiClient,
@@ -17,7 +18,7 @@ const fileSystemMock = vi.hoisted(() => ({
vi.mock("expo-file-system/legacy", () => fileSystemMock);
describe("createMobileApiClient", () => {
- it("decodes bootstrap responses through shared schemas", async () => {
+ it("returns typed bootstrap responses", async () => {
const calls: RequestInfo[] = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = (async (input: RequestInfo | URL) => {
@@ -41,6 +42,28 @@ describe("createMobileApiClient", () => {
],
activeOrganizationId: "org_123",
rootFolders: [],
+ spaces: [
+ {
+ id: "org_123",
+ name: "All Cap",
+ iconUrl: null,
+ kind: "organization",
+ privacy: "Public",
+ role: "owner",
+ canManage: true,
+ hasPassword: false,
+ },
+ {
+ id: "space_123",
+ name: "Product",
+ iconUrl: null,
+ kind: "space",
+ privacy: "Private",
+ role: "member",
+ canManage: false,
+ hasPassword: false,
+ },
+ ],
}),
{ status: 200 },
);
@@ -53,19 +76,86 @@ describe("createMobileApiClient", () => {
});
const result = await client.bootstrap();
expect(result.user.email).toBe("[email protected]");
+ expect(result.spaces?.[1]?.name).toBe("Product");
expect(String(calls[0])).toBe("https://cap.so/api/mobile/bootstrap");
} finally {
globalThis.fetch = originalFetch;
}
});
- it("decodes public auth provider config", async () => {
+ it("requests account deletion with explicit confirmation", async () => {
+ const calls: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (
+ input: RequestInfo | URL,
+ init?: RequestInit,
+ ) => {
+ calls.push({ input, init });
+ return new Response(JSON.stringify({ success: true }), { status: 200 });
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+
+ await expect(client.requestAccountDeletion()).resolves.toEqual({
+ success: true,
+ });
+ expect(String(calls[0]?.input)).toBe(
+ "https://cap.so/api/mobile/user/account-deletion",
+ );
+ expect(calls[0]?.init?.method).toBe("POST");
+ expect(JSON.parse(String(calls[0]?.init?.body))).toEqual({
+ confirmation: "DELETE",
+ });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("uses authenticated content safety endpoints", async () => {
+ const calls: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (
+ input: RequestInfo | URL,
+ init?: RequestInit,
+ ) => {
+ calls.push({ input, init });
+ return new Response(JSON.stringify({ success: true }), { status: 200 });
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+
+ await client.reportCap("video_123", "harassment");
+ await client.blockUser("user_456");
+
+ expect(calls.map((call) => String(call.input))).toEqual([
+ "https://cap.so/api/mobile/caps/video_123/report",
+ "https://cap.so/api/mobile/user/blocks",
+ ]);
+ expect(calls.map((call) => JSON.parse(String(call.init?.body)))).toEqual([
+ { reason: "harassment" },
+ { userId: "user_456" },
+ ]);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("returns typed public auth provider config", async () => {
const calls: RequestInfo[] = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = (async (input: RequestInfo | URL) => {
calls.push(input as RequestInfo);
return new Response(
JSON.stringify({
+ appleAuthAvailable: true,
googleAuthAvailable: false,
workosAuthAvailable: true,
}),
@@ -79,6 +169,7 @@ describe("createMobileApiClient", () => {
getToken: () => null,
});
const result = await client.getAuthConfig();
+ expect(result.appleAuthAvailable).toBe(true);
expect(result.googleAuthAvailable).toBe(false);
expect(result.workosAuthAvailable).toBe(true);
expect(String(calls[0])).toBe("https://cap.so/api/mobile/session/config");
@@ -87,6 +178,145 @@ describe("createMobileApiClient", () => {
}
});
+ it("uses authenticated profile endpoints", async () => {
+ const calls: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (
+ input: RequestInfo | URL,
+ init?: RequestInit,
+ ) => {
+ calls.push({ input, init });
+ return new Response(
+ JSON.stringify({
+ id: "user_123",
+ name: "Richie",
+ lastName: "McIlroy",
+ email: "[email protected]",
+ imageUrl: null,
+ activeOrganizationId: "org_123",
+ }),
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so",
+ getToken: () => "api-key",
+ });
+ await client.updateProfile({ name: "Richie", lastName: "McIlroy" });
+ await client.updateProfileImage({
+ data: "iVBORw0KGgo=",
+ contentType: "image/png",
+ fileName: "profile.png",
+ });
+ await client.removeProfileImage();
+
+ expect(calls.map((call) => String(call.input))).toEqual([
+ "https://cap.so/api/mobile/user/profile",
+ "https://cap.so/api/mobile/user/profile/image",
+ "https://cap.so/api/mobile/user/profile/image",
+ ]);
+ expect(calls.map((call) => call.init?.method)).toEqual([
+ "PATCH",
+ "PUT",
+ "DELETE",
+ ]);
+ expect(calls[0]?.init?.body).toBe(
+ JSON.stringify({ name: "Richie", lastName: "McIlroy" }),
+ );
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("loads Caps from a selected space", async () => {
+ const calls: RequestInfo[] = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (input: RequestInfo | URL) => {
+ calls.push(input as RequestInfo);
+ return new Response(
+ JSON.stringify({
+ folders: [],
+ caps: [],
+ page: 1,
+ limit: 30,
+ total: 0,
+ collectionTotal: 45,
+ hasMore: false,
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.listCaps({
+ folderId: null,
+ spaceId: "space_123",
+ page: 1,
+ limit: 30,
+ });
+
+ expect(String(calls[0])).toBe(
+ "https://cap.so/api/mobile/caps?spaceId=space_123&page=1&limit=30",
+ );
+ expect(result.collectionTotal).toBe(45);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("loads compact upload statuses in one authenticated request", async () => {
+ const calls: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (
+ input: RequestInfo | URL,
+ init?: RequestInit,
+ ) => {
+ calls.push({ input, init });
+ return new Response(
+ JSON.stringify({
+ caps: [
+ {
+ id: "video_123",
+ upload: {
+ uploaded: 500,
+ total: 1000,
+ phase: "uploading",
+ processingProgress: 0,
+ processingMessage: null,
+ processingError: null,
+ },
+ },
+ ],
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.getCapStatuses(["video_123"]);
+
+ expect(result.caps[0]?.upload?.uploaded).toBe(500);
+ expect(String(calls[0]?.input)).toBe(
+ "https://cap.so/api/mobile/caps/statuses",
+ );
+ expect(calls[0]?.init?.method).toBe("POST");
+ expect(JSON.parse(calls[0]?.init?.body as string)).toEqual({
+ ids: ["video_123"],
+ });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
it("keeps non-JSON error responses in the API error payload", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = (async () =>
@@ -109,6 +339,26 @@ describe("createMobileApiClient", () => {
}
});
+ it("rejects non-object success payloads", async () => {
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async () =>
+ new Response(JSON.stringify([]), { status: 200 })) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+
+ await expect(client.bootstrap()).rejects.toMatchObject({
+ status: 502,
+ payload: [],
+ });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
it("builds Google session request URLs", () => {
expect(
createSessionRequestUrl("https://cap.so/", "cap://auth", "google"),
@@ -117,6 +367,14 @@ describe("createMobileApiClient", () => {
);
});
+ it("builds Apple session request URLs", () => {
+ expect(
+ createSessionRequestUrl("https://cap.so/", "cap://auth", "apple"),
+ ).toBe(
+ "https://cap.so/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth&provider=apple",
+ );
+ });
+
it("builds WorkOS session request URLs", () => {
expect(
createSessionRequestUrl(
@@ -214,6 +472,7 @@ describe("createMobileApiClient", () => {
const result = await client.createFolder({
name: "Product",
color: "blue",
+ spaceId: Space.SpaceId.make("space_123"),
});
const body = calls[0]?.init?.body;
@@ -224,6 +483,7 @@ describe("createMobileApiClient", () => {
expect(JSON.parse(body as string)).toEqual({
name: "Product",
color: "blue",
+ spaceId: "space_123",
});
} finally {
globalThis.fetch = originalFetch;
@@ -406,6 +666,13 @@ describe("createMobileApiClient", () => {
);
const onProgress = vi.fn();
+ fileSystemMock.createUploadTask.mockClear();
+ fileSystemMock.getInfoAsync.mockResolvedValueOnce({
+ exists: true,
+ isDirectory: false,
+ size: 3,
+ uri: "file:///tmp/video.mp4",
+ });
fileSystemMock.createUploadTask.mockImplementation(
(
url: string,
@@ -458,6 +725,34 @@ describe("createMobileApiClient", () => {
expect(onProgress).toHaveBeenCalledWith({ loaded: 2, total: 3 });
});
+ it("rejects missing local files before creating a native upload task", async () => {
+ fileSystemMock.createUploadTask.mockClear();
+ fileSystemMock.getInfoAsync.mockResolvedValueOnce({
+ exists: false,
+ isDirectory: false,
+ uri: "file:///tmp/missing.mp4",
+ });
+
+ await expect(
+ uploadToTarget(
+ {
+ type: "driveResumable",
+ url: "https://uploads.example/drive",
+ headers: {
+ "Content-Type": "video/mp4",
+ },
+ },
+ {
+ uri: "file:///tmp/missing.mp4",
+ name: "missing.mp4",
+ type: "video/mp4",
+ size: 3,
+ },
+ ),
+ ).rejects.toThrow("The recording file is no longer available.");
+ expect(fileSystemMock.createUploadTask).not.toHaveBeenCalled();
+ });
+
it("sets the Drive resumable upload byte range for remote blobs", async () => {
class MockXMLHttpRequest {
static instances: MockXMLHttpRequest[] = [];
@@ -527,4 +822,74 @@ describe("createMobileApiClient", () => {
globalThis.XMLHttpRequest = originalXhr;
}
});
+
+ it("uses authenticated native product endpoints", async () => {
+ const calls: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (
+ input: RequestInfo | URL,
+ init?: RequestInit,
+ ) => {
+ calls.push({ input, init });
+ const pathname = new URL(String(input)).pathname;
+ if (pathname.endsWith("/analytics")) {
+ return new Response(JSON.stringify({ available: false, data: null }));
+ }
+ if (pathname.endsWith("/imports/loom")) {
+ return new Response(
+ JSON.stringify({
+ id: "video_456",
+ shareUrl: "https://cap.so/s/video_456",
+ }),
+ );
+ }
+ return new Response(
+ JSON.stringify({
+ id: "org_123",
+ name: "Cap",
+ role: "owner",
+ canManage: true,
+ iconUrl: null,
+ allowedEmailDomain: null,
+ customDomain: null,
+ domainVerified: false,
+ }),
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so",
+ getToken: () => "api-key",
+ });
+ await client.getCapAnalytics("video_123", "30d");
+ await client.updateOrganizationSettings({
+ name: "Cap",
+ allowedEmailDomain: null,
+ });
+ await client.updateOrganizationIcon({
+ data: "aW1hZ2U=",
+ contentType: "image/png",
+ fileName: "icon.png",
+ });
+ await client.removeOrganizationIcon();
+ await client.importLoom("https://www.loom.com/share/abcdefghij");
+
+ expect(String(calls[0]?.input)).toBe(
+ "https://cap.so/api/mobile/caps/video_123/analytics?range=30d",
+ );
+ expect(calls.map((call) => call.init?.method)).toEqual([
+ "GET",
+ "PATCH",
+ "PUT",
+ "DELETE",
+ "POST",
+ ]);
+ expect(String(calls[4]?.input)).toBe(
+ "https://cap.so/api/mobile/imports/loom",
+ );
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
});