Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(web): align coverage with Drive storage uploads

Richie McIlroyf437de3ac237
diff --git a/apps/web/__tests__/integration/transcribe.test.ts b/apps/web/__tests__/integration/transcribe.test.ts
index 9cf8afe41..d00d638cf 100644
--- a/apps/web/__tests__/integration/transcribe.test.ts
+++ b/apps/web/__tests__/integration/transcribe.test.ts
@@ -29,22 +29,23 @@ let mockUploadQueryResult: unknown[] = [];
vi.mock("@cap/database", () => ({
db: () => ({
select: () => ({
- from: (table: unknown) =>
- table === schemaMocks.videoUploads
- ? {
- where: vi.fn().mockReturnValue({
- limit: vi.fn().mockResolvedValue(mockUploadQueryResult),
- }),
- }
- : {
- leftJoin: () => ({
- leftJoin: () => ({
- where: vi
- .fn()
- .mockImplementation(() => Promise.resolve(mockQueryResult)),
- }),
- }),
- },
+ from: (table: unknown) => {
+ if (table === schemaMocks.videoUploads) {
+ return {
+ where: vi.fn().mockReturnValue({
+ limit: vi.fn().mockResolvedValue(mockUploadQueryResult),
+ }),
+ };
+ }
+
+ const query = {
+ leftJoin: vi.fn(() => query),
+ where: vi
+ .fn()
+ .mockImplementation(() => Promise.resolve(mockQueryResult)),
+ };
+ return query;
+ },
}),
update: () => ({
set: () => ({
diff --git a/apps/web/__tests__/unit/instant-recording-uploader.test.ts b/apps/web/__tests__/unit/instant-recording-uploader.test.ts
index dc1eaf0d8..c8f05d05f 100644
--- a/apps/web/__tests__/unit/instant-recording-uploader.test.ts
+++ b/apps/web/__tests__/unit/instant-recording-uploader.test.ts
@@ -6,11 +6,12 @@ import {
import type { VideoId } from "@/app/(org)/dashboard/caps/components/web-recorder-dialog/web-recorder-types";
const STREAMED_PART_BYTES = 5 * 1024 * 1024 + 128;
+const DRIVE_PART_BYTES = 16 * 1024 * 1024;
const OVERFLOW_PART_BYTES = 129 * 1024 * 1024;
const FINALIZED_BLOB_BYTES = 129 * 1024 * 1024;
type UploadOutcome =
- | { type: "success"; etag: string }
+ | { type: "success"; etag?: string; status?: number }
| { type: "network-error" }
| { type: "pending" };
@@ -73,9 +74,9 @@ class MockXMLHttpRequest {
}
this.completed = true;
- this.status = 200;
- this.statusText = "OK";
- this.headers.set("etag", `"${outcome.etag}"`);
+ this.status = outcome.status ?? 200;
+ this.statusText = this.status === 308 ? "Resume Incomplete" : "OK";
+ if (outcome.etag) this.headers.set("etag", `"${outcome.etag}"`);
this.onload?.();
}
@@ -231,7 +232,80 @@ describe("InstantRecordingUploader", () => {
contentType: "video/webm;codecs=vp9,opus",
subpath: "raw-upload.webm",
}),
- ).resolves.toBe("upload-123");
+ ).resolves.toEqual({ uploadId: "upload-123", provider: "s3" });
+ });
+
+ it("uses aligned sequential resumable parts for Google Drive uploads", async () => {
+ const totalBytes = DRIVE_PART_BYTES + 4 * 1024 * 1024;
+ const fetchMock = vi.fn(
+ async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = input.toString();
+ const body = init?.body ? JSON.parse(init.body as string) : null;
+
+ if (url === "/api/upload/multipart/presign-part") {
+ return makeJsonResponse({
+ presignedUrl: `https://www.googleapis.com/upload/drive/v3/files/session-${body.partNumber}`,
+ provider: "googleDrive",
+ });
+ }
+
+ if (url === "/api/upload/multipart/complete") {
+ expect(body.parts).toEqual([
+ expect.objectContaining({
+ partNumber: 1,
+ etag: "drive-1",
+ size: DRIVE_PART_BYTES,
+ }),
+ expect.objectContaining({
+ partNumber: 2,
+ etag: "drive-2",
+ size: 4 * 1024 * 1024,
+ }),
+ ]);
+ return makeJsonResponse({ success: true });
+ }
+
+ throw new Error(`Unexpected fetch call: ${url}`);
+ },
+ );
+
+ vi.stubGlobal("fetch", fetchMock);
+ MockXMLHttpRequest.setOutcomes([
+ { type: "success", status: 308 },
+ { type: "success", status: 200 },
+ ]);
+
+ const uploader = new InstantRecordingUploader({
+ videoId,
+ uploadId: "drive-session",
+ provider: "googleDrive",
+ mimeType: "video/webm",
+ subpath: "raw-upload.webm",
+ setUploadStatus: vi.fn(),
+ sendProgressUpdate: vi.fn().mockResolvedValue(undefined),
+ });
+
+ uploader.handleChunk(
+ makeBlob(10 * 1024 * 1024, "video/webm"),
+ 10 * 1024 * 1024,
+ );
+ uploader.handleChunk(makeBlob(10 * 1024 * 1024, "video/webm"), totalBytes);
+
+ await uploader.finalize({
+ finalBlob: makeBlob(totalBytes, "video/webm"),
+ durationSeconds: 20,
+ subpath: "raw-upload.webm",
+ });
+
+ expect(MockXMLHttpRequest.recordedHeaders[0]?.get("content-range")).toBe(
+ `bytes 0-${DRIVE_PART_BYTES - 1}/*`,
+ );
+ expect(MockXMLHttpRequest.recordedHeaders[1]?.get("content-range")).toBe(
+ `bytes ${DRIVE_PART_BYTES}-${totalBytes - 1}/${totalBytes}`,
+ );
+ expect(MockXMLHttpRequest.recordedHeaders[0]?.get("content-type")).toBe(
+ "video/webm",
+ );
});
it("completes multipart uploads with parts ordered by part number", async () => {
diff --git a/apps/web/__tests__/unit/loom-import.test.ts b/apps/web/__tests__/unit/loom-import.test.ts
index e7785fbb5..168656cb1 100644
--- a/apps/web/__tests__/unit/loom-import.test.ts
+++ b/apps/web/__tests__/unit/loom-import.test.ts
@@ -1,9 +1,11 @@
+import { Effect, Option } from "effect";
import { beforeEach, describe, expect, it, vi } from "vitest";
const whereMock = vi.fn();
const valuesMock = vi.fn();
const startMock = vi.fn();
const revalidatePathMock = vi.fn();
+const storageGetWritableAccessForUserMock = vi.hoisted(() => vi.fn());
const mockDb = {
select: vi.fn(() => mockDb),
@@ -65,6 +67,12 @@ vi.mock("@cap/utils", () => ({
userIsPro: vi.fn(() => true),
}));
+vi.mock("@cap/web-backend", () => ({
+ Storage: {
+ getWritableAccessForUser: storageGetWritableAccessForUserMock,
+ },
+}));
+
vi.mock("@cap/web-domain", () => ({
Video: {
VideoId: {
@@ -82,6 +90,11 @@ vi.mock("next/cache", () => ({
revalidatePath: revalidatePathMock,
}));
+vi.mock("@/lib/server", async () => {
+ const { Effect } = await import("effect");
+ return { runPromise: Effect.runPromise };
+});
+
vi.mock("workflow/api", () => ({
start: startMock,
}));
@@ -107,6 +120,12 @@ describe("importFromLoom", () => {
valuesMock.mockResolvedValue(undefined);
whereMock.mockResolvedValue([]);
startMock.mockResolvedValue(undefined);
+ storageGetWritableAccessForUserMock.mockReturnValue(
+ Effect.succeed({
+ bucketId: Option.some("bucket-1"),
+ storageIntegrationId: Option.none(),
+ }),
+ );
mockGetCurrentUser.mockResolvedValue({
id: "user-123",
});
@@ -137,8 +156,7 @@ describe("importFromLoom", () => {
it("removes a stale Loom row and recreates it with the Cap video id", async () => {
whereMock
.mockResolvedValueOnce([{ importedVideoId: "stale-row", videoId: null }])
- .mockResolvedValueOnce(undefined)
- .mockResolvedValueOnce([{ id: "bucket-1" }]);
+ .mockResolvedValueOnce(undefined);
const fetchMock = vi.mocked(fetch);
fetchMock.mockImplementation(async (input) => {
diff --git a/apps/web/__tests__/unit/videos-policy.test.ts b/apps/web/__tests__/unit/videos-policy.test.ts
index 2e4d0a71a..bbf4e9a06 100644
--- a/apps/web/__tests__/unit/videos-policy.test.ts
+++ b/apps/web/__tests__/unit/videos-policy.test.ts
@@ -29,6 +29,7 @@ function makeVideo(
source: { type: "desktopMP4" },
metadata: Option.none(),
bucketId: Option.none(),
+ storageIntegrationId: Option.none(),
folderId: Option.none(),
transcriptionStatus: Option.none(),
width: Option.none(),