Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: reconcile recording storage with current Drive APIs
Richie McIlroy5a576f2801c7
diff --cc apps/web/__tests__/unit/google-drive-storage.test.tsindex 000000000,b01f32999..5f1202e5amode 000000,100644..100644--- a/apps/web/__tests__/unit/google-drive-storage.test.ts+++ b/apps/web/__tests__/unit/google-drive-storage.test.ts@@@ -1,0 -1,1492 +1,1506 @@@+ import { Organisation, Storage, User, Video } from "@cap/web-domain";-import { Effect, Layer, Option } from "effect";++import { Cause, Effect, Exit, Layer, Option } from "effect";+ import { afterEach, describe, expect, it, vi } from "vitest";++ vi.mock("@cap/env", () => ({+ serverEnv: () => ({+ GOOGLE_CLIENT_ID: "client-id",+ GOOGLE_CLIENT_SECRET: "client-secret",+ WEB_URL: "https://cap.test",+ }),+ }));++ import { S3Buckets } from "../../../../packages/web-backend/src/S3Buckets";+ import { Storage as StorageService } from "../../../../packages/web-backend/src/Storage";+ import {+ copyGoogleDriveFile,+ createGoogleDriveResumableUpload,+ GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ type GoogleDriveTokenStore,+ syncGoogleDriveVideoNames,+ } from "../../../../packages/web-backend/src/Storage/GoogleDrive";+ import { getGoogleDriveVideoNames } from "../../../../packages/web-backend/src/Storage/google-drive-names";+ import {+ type GoogleDriveIntegrationConfig,+ type StorageObjectInput,+ type StorageObjectUpdate,+ StorageRepo,+ } from "../../../../packages/web-backend/src/Storage/StorageRepo";++ const integrationId = Storage.StorageIntegrationId.make("integration-1");+ const otherIntegrationId = Storage.StorageIntegrationId.make("integration-2");+ const integrationOwnerId = User.UserId.make("integration-owner");+ const videoOwnerId = User.UserId.make("video-owner");+ const videoId = Video.VideoId.make("video-1");+ const resultKey = `${videoOwnerId}/${videoId}/result.mp4`;++ type StoredObject = {+ id: Storage.StorageObjectId;+ integrationId: Storage.StorageIntegrationId;+ ownerId: User.UserId;+ videoId: Video.VideoId | null;+ objectKey: string;+ objectKeyHash: string;+ providerObjectId: string;+ uploadSessionUrl: string | null;+ uploadStatus: "pending" | "complete" | "error";+ contentType: string | null;+ contentLength: number | null;+ metadata: Storage.StorageObjectMetadata | null;+ createdAt: Date;+ updatedAt: Date;+ };++ type VideoForNameSync = {+ id: Video.VideoId;+ name: string;+ ownerId: User.UserId;+ storageIntegrationId: Storage.StorageIntegrationId | null;+ };++ type RepoHarness = {+ repo: StorageRepo;+ objects: Map<string, StoredObject>;+ reservations: StorageObjectInput[];+ upserts: StorageObjectInput[];+ updates: Array<{ object: StoredObject; input: StorageObjectUpdate }>;+ fileNameUpdates: Array<{ object: StoredObject; fileName: string }>;+ putObject: (input: StorageObjectInput) => StoredObject;+ setVideo: (video: VideoForNameSync) => void;+ setFileNameUpdateResult: (+ result: (object: StoredObject, fileName: string) => boolean,+ ) => void;+ };++ const objectMapKey = (+ storageIntegrationId: Storage.StorageIntegrationId,+ key: string,+ ) => `${storageIntegrationId}:${key}`;++ const snapshotObject = (object: StoredObject): StoredObject => ({+ ...object,+ metadata: object.metadata ? { ...object.metadata } : null,+ createdAt: new Date(object.createdAt),+ updatedAt: new Date(object.updatedAt),+ });++ const makeRepoHarness = (videos: VideoForNameSync[] = []): RepoHarness => {+ const objects = new Map<string, StoredObject>();+ const reservations: StorageObjectInput[] = [];+ const upserts: StorageObjectInput[] = [];+ const updates: Array<{ object: StoredObject; input: StorageObjectUpdate }> =+ [];+ const fileNameUpdates: Array<{ object: StoredObject; fileName: string }> = [];+ const videoMap = new Map(videos.map((video) => [video.id, video]));+ let objectSequence = 0;+ let fileNameUpdateResult: (+ object: StoredObject,+ fileName: string,+ ) => boolean = () => true;++ const putObject = (input: StorageObjectInput) => {+ const key = objectMapKey(input.integrationId, input.objectKey);+ const existing = objects.get(key);+ const next: StoredObject = {+ id:+ existing?.id ??+ Storage.StorageObjectId.make(`storage-object-${++objectSequence}`),+ integrationId: input.integrationId,+ ownerId: existing?.ownerId ?? input.ownerId,+ videoId: existing?.videoId ?? input.videoId,+ objectKey: input.objectKey,+ objectKeyHash: existing?.objectKeyHash ?? `hash:${input.objectKey}`,+ providerObjectId: input.providerObjectId,+ uploadSessionUrl: input.uploadSessionUrl ?? null,+ uploadStatus: input.uploadStatus ?? "pending",+ contentType: input.contentType ?? null,+ contentLength: input.contentLength ?? null,+ metadata:+ existing && input.preserveMetadata+ ? existing.metadata+ : (input.metadata ?? null),+ createdAt: existing?.createdAt ?? new Date("2026-01-01T00:00:00.000Z"),+ updatedAt: new Date("2026-01-01T00:00:00.000Z"),+ };+ objects.set(key, next);+ return next;+ };++ const repo = {+ getIntegrationById: (id: Storage.StorageIntegrationId) =>+ Effect.succeed(+ id === integrationId+ ? Option.some({ id, ownerId: integrationOwnerId })+ : Option.none(),+ ),+ getGoogleDriveConfig: () => Effect.succeed(makeConfig()),+ getGoogleDriveAccessTokenCache: () =>+ Effect.succeed(+ Option.some({+ accessToken: "access-token",+ expiresAt: new Date("2100-01-01T00:00:00.000Z"),+ }),+ ),+ getGoogleDriveAccessTokenCacheById: () =>+ Effect.succeed(+ Option.some({+ accessToken: "access-token",+ expiresAt: new Date("2100-01-01T00:00:00.000Z"),+ }),+ ),+ claimGoogleDriveTokenRefreshLease: () => Effect.succeed(true),+ saveGoogleDriveAccessTokenCache: () => Effect.succeed(true),+ releaseGoogleDriveTokenRefreshLease: () => Effect.void,+ getObjectByKey: (+ storageIntegrationId: Storage.StorageIntegrationId,+ key: string,+ ) =>+ Effect.succeed(+ Option.fromNullable(+ objects.get(objectMapKey(storageIntegrationId, key)),+ ).pipe(Option.map(snapshotObject)),+ ),+ getVideoForNameSync: (id: Video.VideoId) =>+ Effect.succeed(Option.fromNullable(videoMap.get(id))),+ reserveObject: (input: StorageObjectInput) =>+ Effect.sync(() => {+ reservations.push(input);+ const key = objectMapKey(input.integrationId, input.objectKey);+ return snapshotObject(objects.get(key) ?? putObject(input));+ }),+ upsertObject: (input: StorageObjectInput) =>+ Effect.sync(() => {+ upserts.push(input);+ putObject(input);+ }),+ updateObjectIfCurrent: (object: StoredObject, input: StorageObjectUpdate) =>+ Effect.sync(() => {+ updates.push({ object, input });+ const current = objects.get(+ objectMapKey(object.integrationId, object.objectKey),+ );+ if (+ !current ||+ current.id !== object.id ||+ current.integrationId !== object.integrationId ||+ current.objectKey !== object.objectKey ||+ current.objectKeyHash !== object.objectKeyHash ||+ current.providerObjectId !== object.providerObjectId ||+ current.uploadStatus !== object.uploadStatus+ )+ return false;+ const metadata = input.preserveMetadata+ ? current.metadata+ : (input.metadata ?? null);+ putObject({+ ...input,+ integrationId: current.integrationId,+ ownerId: current.ownerId,+ videoId: current.videoId,+ objectKey: current.objectKey,+ metadata,+ });+ return true;+ }),+ markObjectComplete: (+ storageIntegrationId: Storage.StorageIntegrationId,+ key: string,+ contentLength?: number | null,+ ) =>+ Effect.sync(() => {+ const object = objects.get(objectMapKey(storageIntegrationId, key));+ if (!object) return;+ object.uploadStatus = "complete";+ if (contentLength !== undefined) object.contentLength = contentLength;+ }),+ deleteObjectByKey: (+ storageIntegrationId: Storage.StorageIntegrationId,+ key: string,++ providerObjectId?: string,+ ) =>+ Effect.sync(() => {- objects.delete(objectMapKey(storageIntegrationId, key));++ const mapKey = objectMapKey(storageIntegrationId, key);++ if (++ providerObjectId === undefined ||++ objects.get(mapKey)?.providerObjectId === providerObjectId++ )++ objects.delete(mapKey);+ }),+ updateObjectFileName: (object: StoredObject, fileName: string) =>+ Effect.sync(() => {+ fileNameUpdates.push({ object, fileName });+ const current = objects.get(+ objectMapKey(object.integrationId, object.objectKey),+ );+ if (current) {+ current.metadata = { ...(current.metadata ?? {}), fileName };+ }+ return fileNameUpdateResult(object, fileName);+ }),+ } as unknown as StorageRepo;++ return {+ repo,+ objects,+ reservations,+ upserts,+ updates,+ fileNameUpdates,+ putObject,+ setVideo: (video) => {+ videoMap.set(video.id, video);+ },+ setFileNameUpdateResult: (result) => {+ fileNameUpdateResult = result;+ },+ };+ };++ let tokenSequence = 0;++ const makeTokenStore = (): GoogleDriveTokenStore => {+ const token = {+ accessToken: "access-token",+ expiresAt: new Date("2100-01-01T00:00:00.000Z"),+ };+ return {+ cacheKey: `google-drive-test-${++tokenSequence}`,+ getInitialAccessTokenCache: () => Effect.succeed(Option.some(token)),+ getAccessTokenCache: () => Effect.succeed(Option.some(token)),+ claimRefreshLease: () => Effect.succeed(true),+ saveAccessTokenCache: () => Effect.succeed(true),+ releaseRefreshLease: () => Effect.void,+ };+ };++ const makeConfig = (+ folderLayout: GoogleDriveIntegrationConfig["folderLayout"] = "video",+ ): GoogleDriveIntegrationConfig => ({+ refreshToken: "refresh-token",+ folderId: "root-folder",+ folderLayout,+ });++ const jsonResponse = (body: unknown, init: ResponseInit = {}): Response =>+ new Response(JSON.stringify(body), {+ status: 200,+ headers: { "Content-Type": "application/json" },+ ...init,+ });++ const response = (status: number, headers?: HeadersInit) =>+ new Response(status === 204 ? null : "", { status, headers });++ const requestBody = (init?: RequestInit) =>+ typeof init?.body === "string" && init.body.startsWith("{")+ ? (JSON.parse(init.body) as Record<string, unknown>)+ : null;++ const storedObjectInput = (+ key: string,+ providerObjectId: string,+ options: {+ status?: StoredObject["uploadStatus"];+ contentType?: string;+ contentLength?: number | null;+ fileName?: string;+ ownerId?: User.UserId;+ uploadSessionUrl?: string | null;+ } = {},+ ): StorageObjectInput => ({+ integrationId,+ ownerId: options.ownerId ?? integrationOwnerId,+ videoId,+ objectKey: key,+ providerObjectId,+ uploadSessionUrl: options.uploadSessionUrl,+ uploadStatus: options.status ?? "complete",+ contentType: options.contentType ?? "video/mp4",+ contentLength: options.contentLength,+ metadata: {+ videoId,+ fileName: options.fileName ?? key.split("/").at(-1),+ contentType: options.contentType ?? "video/mp4",+ },+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+ });++ describe("Google Drive video names", () => {+ it("preserves exact valid titles and rejects blank or control-character names", () => {+ expect(getGoogleDriveVideoNames("Launch 🚀: Q3/Q4? (final)")).toEqual({+ folderName: "Launch 🚀: Q3/Q4? (final)",+ fileName: "Launch 🚀: Q3/Q4? (final).mp4",+ });+ expect(getGoogleDriveVideoNames("Already.MP4")).toEqual({+ folderName: "Already.MP4",+ fileName: "Already.MP4",+ });+ expect(getGoogleDriveVideoNames("Same title")).toEqual(+ getGoogleDriveVideoNames("Same title"),+ );+ expect(getGoogleDriveVideoNames(" \t ")).toBeNull();+ expect(getGoogleDriveVideoNames("Visible\u0085control")).toBeNull();+ });++ it.each(["video", "userVideo"] as const)(+ "creates %s-layout folders and final MP4 names from an explicit pre-insert title",+ async (folderLayout) => {+ const harness = makeRepoHarness();+ const generatedIds = [+ "result-id",+ "owner-folder-id",+ "video-folder-id",+ "warning-id",+ ];+ const folderBodies: Record<string, unknown>[] = [];+ let filePostSawReservation = false;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request, init?: RequestInit) => {+ const url = String(input);+ if (url.includes("/files/generateIds")) {+ return jsonResponse({ ids: [generatedIds.shift()] });+ }+ if (+ url.includes("/drive/v3/files?") &&+ !url.includes("uploadType=") &&+ init?.method === "POST"+ ) {+ const body = requestBody(init);+ if (body) folderBodies.push(body);+ return jsonResponse({ id: body?.id, name: body?.name });+ }+ if (url.includes("uploadType=multipart")) {+ return jsonResponse({ id: "warning-id" });+ }+ if (url.includes("uploadType=resumable") && init?.method === "POST") {+ filePostSawReservation = harness.objects.has(+ objectMapKey(integrationId, resultKey),+ );+ return response(200, { Location: "https://upload.test/session" });+ }+ throw new Error(+ `Unexpected Drive request: ${init?.method ?? "GET"} ${url}`,+ );+ }),+ );++ await Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(folderLayout),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ videoTitle: "Café / roadmap 🚀",+ },+ makeTokenStore(),+ ),+ );++ const folderNames = folderBodies+ .filter((body) => body.mimeType === GOOGLE_DRIVE_FOLDER_MIME_TYPE)+ .map((body) => body.name);+ expect(folderNames).toContain("Café / roadmap 🚀");+ if (folderLayout === "userVideo")+ expect(folderNames).toContain(videoOwnerId);+ expect(filePostSawReservation).toBe(true);+ const filePost = (+ vi.mocked(fetch).mock.calls as Array<[string, RequestInit]>+ ).find(+ ([url, init]) =>+ String(url).includes("uploadType=resumable") &&+ init.method === "POST",+ );+ expect(requestBody(filePost?.[1])).toMatchObject({+ id: "result-id",+ name: "Café / roadmap 🚀.mp4",+ parents: [+ folderLayout === "userVideo" ? "video-folder-id" : "owner-folder-id",+ ],+ appProperties: { capObjectKey: resultKey },+ });+ expect(harness.updates.at(-1)?.input).toMatchObject({+ providerObjectId: "result-id",+ metadata: { fileName: "Café / roadmap 🚀.mp4" },+ });+ },+ );++ it("uses a matching video owner and integration for final names but leaves auxiliary and mismatched objects alone", async () => {+ const matching = makeRepoHarness([+ {+ id: videoId,+ name: "Org member's demo",+ ownerId: videoOwnerId,+ storageIntegrationId: integrationId,+ },+ ]);+ matching.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ matching.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ const postedNames: string[] = [];+ let nextId = 0;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request, init?: RequestInit) => {+ const url = String(input);+ if (url.includes("/files/generateIds")) {+ nextId += 1;+ return jsonResponse({ ids: [`new-file-${nextId}`] });+ }+ if (url.includes("uploadType=resumable") && init?.method === "POST") {+ postedNames.push(String(requestBody(init)?.name));+ return response(200, { Location: `https://upload.test/${nextId}` });+ }+ throw new Error(+ `Unexpected Drive request: ${init?.method ?? "GET"} ${url}`,+ );+ }),+ );++ for (const key of [resultKey, `${videoOwnerId}/${videoId}/thumbnail.png`]) {+ await Effect.runPromise(+ createGoogleDriveResumableUpload(+ matching.repo,+ makeConfig(),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key,+ contentType: key.endsWith(".mp4") ? "video/mp4" : "image/png",+ },+ makeTokenStore(),+ ),+ );+ }++ const mismatched = makeRepoHarness([+ {+ id: videoId,+ name: "Must not leak",+ ownerId: videoOwnerId,+ storageIntegrationId: otherIntegrationId,+ },+ ]);+ mismatched.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ mismatched.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ await Effect.runPromise(+ createGoogleDriveResumableUpload(+ mismatched.repo,+ makeConfig(),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ },+ makeTokenStore(),+ ),+ );++ expect(postedNames).toEqual([+ "Org member's demo.mp4",+ "thumbnail.png",+ "result.mp4",+ ]);+ });+ });++ describe("Google Drive upload updates and copies", () => {+ it("overwrites only the content type while preserving the file id and metadata", async () => {+ const harness = makeRepoHarness();+ const existing = harness.putObject(+ storedObjectInput(resultKey, "existing-file-id", {+ contentLength: 4321,+ fileName: "User renamed this in Drive.mp4",+ }),+ );+ vi.stubGlobal(+ "fetch",+ vi.fn(async () =>+ response(200, { Location: "https://upload.test/existing" }),+ ),+ );++ await Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ },+ makeTokenStore(),+ ),+ );++ const [url, init] = vi.mocked(fetch).mock.calls[0] as [string, RequestInit];+ expect(url).toContain("/files/existing-file-id?uploadType=resumable");+ expect(init.method).toBe("PATCH");+ expect(requestBody(init)).toEqual({ mimeType: "video/mp4" });+ expect(harness.updates.at(-1)?.input).toMatchObject({+ providerObjectId: "existing-file-id",+ contentLength: null,+ preserveMetadata: true,+ });+ expect(+ harness.objects.get(objectMapKey(integrationId, resultKey))?.metadata,+ ).toEqual(existing.metadata);+ });++ it.each([404, 410])(+ "recreates a missing complete file after %i with its current Cap title and stable identity",+ async (missingStatus) => {+ const harness = makeRepoHarness([+ {+ id: videoId,+ name: "Current café / launch",+ ownerId: videoOwnerId,+ storageIntegrationId: integrationId,+ },+ ]);+ const existing = harness.putObject(+ storedObjectInput(resultKey, "missing-file-id", {+ fileName: "Stale managed title.mp4",+ }),+ );+ existing.metadata = {+ ...existing.metadata,+ duration: "42",+ resolution: "3840x2160",+ };+ harness.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ harness.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ vi.stubGlobal(+ "fetch",+ vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {+ if (init?.method === "PATCH") return response(missingStatus);+ if (init?.method === "POST")+ return response(200, { Location: "https://upload.test/recreated" });+ throw new Error(`Unexpected Drive method: ${init?.method}`);+ }),+ );++ const uploadUrl = await Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ },+ makeTokenStore(),+ ),+ );++ expect(uploadUrl).toBe("https://upload.test/recreated");+ const [, postInit] = vi.mocked(fetch).mock.calls[1] as [+ string,+ RequestInit,+ ];+ expect(postInit.method).toBe("POST");+ expect(requestBody(postInit)).toEqual({+ id: "missing-file-id",+ name: "Current café / launch.mp4",+ mimeType: "video/mp4",+ parents: ["folder-id"],+ appProperties: { capObjectKey: resultKey },+ });+ expect(harness.updates.at(-1)?.input).toMatchObject({+ providerObjectId: "missing-file-id",+ preserveMetadata: false,+ metadata: {+ fileName: "Current café / launch.mp4",+ duration: "42",+ resolution: "3840x2160",+ },+ });+ },+ );++ it.each([+ [404, false],+ [410, false],+ [404, true],+ [410, true],+ ] as const)(+ "reuses a reserved id after %i (replacement: %s) when POST conflicts",+ async (missingStatus, useReplacement) => {+ const harness = makeRepoHarness();+ const finalFileId = useReplacement+ ? "replacement-file-id"+ : "reserved-file-id";+ const reserved = harness.putObject(+ storedObjectInput(resultKey, "reserved-file-id", {+ status: useReplacement ? "complete" : "pending",+ fileName: "Reserved display.mp4",+ }),+ );+ harness.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ harness.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ let patchCount = 0;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request, init?: RequestInit) => {+ if (String(input).includes("/files/generateIds"))+ return jsonResponse({ ids: [finalFileId] });+ if (init?.method === "PATCH") {+ patchCount += 1;+ return patchCount === 1+ ? response(missingStatus)+ : response(200, { Location: "https://upload.test/recovered" });+ }+ if (init?.method === "POST") {+ return response(+ useReplacement && requestBody(init)?.id === "reserved-file-id"+ ? 400+ : 409,+ );+ }+ throw new Error(`Unexpected Drive method: ${init?.method}`);+ }),+ );++ const uploadUrl = await Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ },+ makeTokenStore(),+ ),+ );++ expect(uploadUrl).toBe("https://upload.test/recovered");+ expect(patchCount).toBe(2);+ expect(harness.updates.at(-1)?.input).toMatchObject({+ providerObjectId: finalFileId,+ preserveMetadata: true,+ });+ expect(+ harness.objects.get(objectMapKey(integrationId, resultKey))?.metadata,+ ).toEqual(reserved.metadata);+ },+ );++ it("allows only one concurrent replacement CAS winner after the old file disappears", async () => {+ const harness = makeRepoHarness([+ {+ id: videoId,+ name: "Concurrent replacement",+ ownerId: videoOwnerId,+ storageIntegrationId: integrationId,+ },+ ]);+ harness.putObject(storedObjectInput(resultKey, "missing-file-id"));+ harness.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ harness.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ let releaseInitialPatch!: () => void;+ const initialPatchBarrier = new Promise<void>((resolve) => {+ releaseInitialPatch = resolve;+ });+ let patchCount = 0;+ let generatedIdCount = 0;+ let replacementPostCompleted = false;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request, init?: RequestInit) => {+ const url = String(input);+ if (+ init?.method === "PATCH" &&+ (url.includes("missing-file-id") || url.includes("replacement-"))+ ) {+ patchCount += 1;+ if (patchCount === 2) releaseInitialPatch();+ if (patchCount <= 2) await initialPatchBarrier;+ return response(404);+ }+ if (url.includes("/files/generateIds")) {+ const id =+ generatedIdCount++ === 0 ? "replacement-a" : "replacement-b";+ return jsonResponse({ ids: [id] });+ }+ if (init?.method === "POST") {+ const body = requestBody(init);+ if (typeof body?.id === "string" && body.id === "missing-file-id")+ return response(404);+ if (+ typeof body?.id === "string" &&+ body.id.startsWith("replacement-")+ ) {+ if (replacementPostCompleted) return response(404);+ replacementPostCompleted = true;+ }+ return response(200, {+ Location: `https://upload.test/${body?.id}`,+ });+ }+ throw new Error(+ `Unexpected Drive request: ${init?.method ?? "GET"} ${url}`,+ );+ }),+ );++ const upload = () =>+ Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(),+ {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ },+ makeTokenStore(),+ ),+ );+ const results = await Promise.allSettled([upload(), upload()]);+ const fulfilled = results.filter(+ (result): result is PromiseFulfilledResult<string> =>+ result.status === "fulfilled",+ );+ const successfulPosts = vi+ .mocked(fetch)+ .mock.calls.map(([, init]) => requestBody(init))+ .filter(+ (body): body is Record<string, unknown> =>+ typeof body?.id === "string" && body.id.startsWith("replacement-"),+ );+ const mapped = harness.objects.get(objectMapKey(integrationId, resultKey));++ expect(+ results.filter((result) => result.status === "rejected"),+ ).toHaveLength(1);+ expect(fulfilled).toHaveLength(1);+ expect(successfulPosts).toHaveLength(1);+ expect(mapped?.providerObjectId).toBe(successfulPosts[0]?.id);+ expect(mapped?.uploadSessionUrl).toBe(fulfilled[0]?.value);+ expect(+ harness.updates.filter(({ input }) =>+ input.providerObjectId.startsWith("replacement-"),+ ),+ ).toHaveLength(3);+ expect(+ harness.updates.filter(+ ({ input }) =>+ input.providerObjectId.startsWith("replacement-") &&+ input.uploadSessionUrl === undefined,+ ),+ ).toHaveLength(2);+ const lateResult = await Promise.allSettled([upload()]);+ expect(lateResult[0]?.status).toBe("rejected");+ expect(generatedIdCount).toBe(2);+ expect(+ new Set(+ vi+ .mocked(fetch)+ .mock.calls.filter(([, init]) => init?.method === "POST")+ .map(([, init]) => requestBody(init)?.id)+ .filter(+ (id): id is string =>+ typeof id === "string" && id.startsWith("replacement-"),+ ),+ ),+ ).toHaveLength(1);+ });++ it("does not let a stale session finalization replace a newer provider mapping", async () => {+ const harness = makeRepoHarness([+ {+ id: videoId,+ name: "Stale session race",+ ownerId: videoOwnerId,+ storageIntegrationId: integrationId,+ },+ ]);+ harness.putObject(storedObjectInput(resultKey, "missing-file-id"));+ harness.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ harness.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ let releaseStalePatch!: () => void;+ let firstPatchSeen!: () => void;+ const stalePatchSeen = new Promise<void>((resolve) => {+ firstPatchSeen = resolve;+ });+ const stalePatchRelease = new Promise<void>((resolve) => {+ releaseStalePatch = resolve;+ });+ let patchCount = 0;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request, init?: RequestInit) => {+ const url = String(input);+ if (init?.method === "PATCH" && url.includes("missing-file-id")) {+ patchCount += 1;+ if (patchCount === 1) {+ firstPatchSeen();+ await stalePatchRelease;+ return response(200, {+ Location: "https://upload.test/stale-session",+ });+ }+ return response(404);+ }+ if (url.includes("/files/generateIds"))+ return jsonResponse({ ids: ["replacement-id"] });+ if (init?.method === "POST") {+ const body = requestBody(init);+ if (body?.id === "missing-file-id") return response(404);+ return response(200, {+ Location: "https://upload.test/replacement-session",+ });+ }+ throw new Error(+ `Unexpected Drive request: ${init?.method ?? "GET"} ${url}`,+ );+ }),+ );++ const uploadInput = {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ };+ const stale = Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(),+ uploadInput,+ makeTokenStore(),+ ),+ );+ await stalePatchSeen;+ const replacement = await Effect.runPromise(+ createGoogleDriveResumableUpload(+ harness.repo,+ makeConfig(),+ uploadInput,+ makeTokenStore(),+ ),+ );+ releaseStalePatch();+ const staleResult = await Promise.allSettled([stale]);+ const mapped = harness.objects.get(objectMapKey(integrationId, resultKey));++ expect(replacement).toBe("https://upload.test/replacement-session");+ expect(staleResult[0]?.status).toBe("rejected");+ expect(mapped?.providerObjectId).toBe("replacement-id");+ expect(mapped?.uploadSessionUrl).toBe(replacement);+ });++ it("copies a final MP4 with the target title and records its managed metadata", async () => {+ const harness = makeRepoHarness([+ {+ id: videoId,+ name: "Copied: launch/demo",+ ownerId: videoOwnerId,+ storageIntegrationId: integrationId,+ },+ ]);+ harness.putObject(+ storedObjectInput(".cap-folders/video-1", "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ }),+ );+ harness.putObject(+ storedObjectInput(+ ".cap-warnings/video-1/DO_NOT_EDIT_OR_DELETE.txt",+ "warning-id",+ { contentType: "text/plain" },+ ),+ );+ vi.stubGlobal(+ "fetch",+ vi.fn(async () =>+ jsonResponse({+ id: "copied-file-id",+ name: "Copied: launch/demo.mp4",+ mimeType: "video/mp4",+ size: "1234",+ }),+ ),+ );++ await Effect.runPromise(+ copyGoogleDriveFile({+ repo: harness.repo,+ config: makeConfig(),+ sourceFileId: "source-file-id",+ input: {+ integrationId,+ ownerId: integrationOwnerId,+ videoId,+ key: resultKey,+ contentType: "video/mp4",+ },+ tokenStore: makeTokenStore(),+ }),+ );++ const [, init] = vi.mocked(fetch).mock.calls[0] as [string, RequestInit];+ expect(requestBody(init)).toEqual({+ name: "Copied: launch/demo.mp4",+ parents: ["folder-id"],+ appProperties: { capObjectKey: resultKey },+ });+ expect(harness.upserts.at(-1)).toMatchObject({+ providerObjectId: "copied-file-id",+ contentLength: 1234,+ metadata: {+ videoId,+ fileName: "Copied: launch/demo.mp4",+ contentType: "video/mp4",+ },+ });+ });+ });++ type DriveFileState = {+ id: string;+ name: string;+ mimeType: string;+ size?: string;+ version?: string;+ md5Checksum?: string;+ parents: string[];+ trashed: boolean;+ appProperties?: Record<string, string>;+ capabilities: { canRename: boolean };+ };++ const makeDriveFileFetch = (+ files: Map<string, DriveFileState>,+ options: {+ failPatchFor?: string;+ events?: string[];+ } = {},+ ) => {+ let failed = false;+ return vi.fn(async (input: string | URL | Request, init?: RequestInit) => {+ const url = new URL(String(input));+ const fileId = decodeURIComponent(url.pathname.split("/").at(-1) ?? "");+ const file = files.get(fileId);+ if (!file) return response(404);+ if (!init?.method || init.method === "GET") {+ options.events?.push(`GET:${fileId}`);+ const fields = new Set(+ url.searchParams+ .get("fields")+ ?.split(",")+ .map((field) => field.split("(")[0]),+ );+ return jsonResponse(+ Object.fromEntries(+ Object.entries(file).filter(+ ([field]) => fields.size === 0 || fields.has(field),+ ),+ ),+ );+ }+ if (init.method === "PATCH") {+ options.events?.push(`PATCH:${fileId}`);+ if (options.failPatchFor === fileId && !failed) {+ failed = true;+ return response(503);+ }+ const body = requestBody(init);+ if (typeof body?.name === "string") {+ file.name = body.name;+ if (file.version !== undefined)+ file.version = (BigInt(file.version) + 1n).toString();+ }+ return jsonResponse({ id: file.id, name: file.name });+ }+ throw new Error(`Unexpected Drive request: ${init.method} ${url}`);+ });+ };++ const makeSyncHarness = (+ fileStatus: StoredObject["uploadStatus"] = "complete",+ ) => {+ const harness = makeRepoHarness();+ const folderKey = ".cap-folders/video-1";+ const folderObject = harness.putObject(+ storedObjectInput(folderKey, "folder-id", {+ contentType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ fileName: "Old title",+ }),+ );+ const fileObject = harness.putObject(+ storedObjectInput(resultKey, "file-id", {+ status: fileStatus,+ fileName: "Old title.mp4",+ }),+ );+ const files = new Map<string, DriveFileState>([+ [+ "folder-id",+ {+ id: "folder-id",+ name: "Old title",+ mimeType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,+ parents: ["root-folder"],+ trashed: false,+ capabilities: { canRename: true },+ },+ ],+ [+ "file-id",+ {+ id: "file-id",+ name: "Old title.mp4",+ mimeType: "video/mp4",+ size: "5678",+ md5Checksum: "original-md5",+ parents: ["folder-id"],+ trashed: false,+ appProperties: {+ capObjectKey: resultKey,+ customerTag: "keep-me",+ },+ capabilities: { canRename: true },+ },+ ],+ ]);+ return { harness, files, folderObject, fileObject };+ };++ const syncVideo = {+ id: videoId,+ ownerId: videoOwnerId,+ name: "New title: 测试 🎬",+ storageIntegrationId: integrationId,+ };++ const makeStorageVideo = (): Video.Video =>+ new Video.Video({+ id: videoId,+ ownerId: videoOwnerId,+ orgId: Organisation.OrganisationId.make("org-1"),+ name: "Recovery test",+ public: false,+ source: { type: "webMP4" },+ metadata: Option.none(),+ bucketId: Option.none(),+ storageIntegrationId: Option.some(integrationId),+ folderId: Option.none(),+ transcriptionStatus: Option.none(),+ width: Option.none(),+ height: Option.none(),+ duration: Option.none(),+ createdAt: new Date("2026-01-01T00:00:00.000Z"),+ updatedAt: new Date("2026-01-01T00:00:00.000Z"),+ });++ const getStorageAccess = (harness: RepoHarness) => {+ const dependencies = Layer.merge(+ Layer.succeed(StorageRepo, harness.repo),+ Layer.succeed(S3Buckets, {} as S3Buckets),+ );+ const storageLayer = StorageService.DefaultWithoutDependencies.pipe(+ Layer.provide(dependencies),+ );+ return Effect.runPromise(+ StorageService.getAccessForVideo(makeStorageVideo()).pipe(+ Effect.provide(storageLayer),+ Effect.map(([access]) => access),+ ),+ );+ };+-const getStorageObject = async (harness: RepoHarness) => {++const getStorageObjectExit = async (harness: RepoHarness) => {+ const access = await getStorageAccess(harness);- return Effect.runPromise(access.getObject(resultKey));++ return Effect.runPromiseExit(access.getObject(resultKey));+ };++ describe("Google Drive title synchronization", () => {+ it("does not serve or clobber a newer pending mapping during Drive recovery", async () => {+ const harness = makeRepoHarness();+ harness.putObject(+ storedObjectInput(resultKey, "stale-file-id", { status: "complete" }),+ );+ let switched = false;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request) => {+ const url = String(input);+ if (url.includes("alt=media")) return response(404);+ if (url.includes("/files?")) {+ if (!switched) {+ switched = true;+ harness.putObject(+ storedObjectInput(resultKey, "new-pending-id", {+ status: "pending",+ }),+ );+ }+ return jsonResponse({+ files: [+ {+ id: "recovered-stale-id",+ name: "Recovered.mp4",+ mimeType: "video/mp4",+ size: "123",+ modifiedTime: "2026-01-01T00:00:00.000Z",+ },+ ],+ });+ }+ throw new Error(`Unexpected Drive request: ${url}`);+ }),+ );+- await expect(getStorageObject(harness)).resolves.toEqual(Option.none());++ const result = await getStorageObjectExit(harness);++ if (Exit.isSuccess(result)) throw new Error("Recovery conflict was hidden");++ expect(Option.getOrThrow(Cause.failureOption(result.cause)).cause).toEqual(++ new Error("Storage object changed during Google Drive recovery; retry"),++ );+ const mapped = harness.objects.get(objectMapKey(integrationId, resultKey));+ expect(mapped).toMatchObject({+ providerObjectId: "new-pending-id",+ uploadStatus: "pending",+ });+ expect(+ vi+ .mocked(fetch)+ .mock.calls.some(([input]) =>+ String(input).includes("recovered-stale-id"),+ ),+ ).toBe(false);+ expect(harness.updates).toHaveLength(1);+ });++ it("rejects a different Drive candidate when the stored object is still pending", async () => {+ const harness = makeRepoHarness();+ const pending = harness.putObject(+ storedObjectInput(resultKey, "pending-file-id", { status: "pending" }),+ );+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: string | URL | Request) => {+ const url = String(input);+ if (url.includes("alt=media")) return response(404);+ if (url.includes("/files?"))+ return jsonResponse({+ files: [+ {+ id: "different-file-id",+ name: "Different.mp4",+ mimeType: "video/mp4",+ size: "123",+ modifiedTime: "2026-01-01T00:00:00.000Z",+ },+ ],+ });+ throw new Error(`Unexpected Drive request: ${url}`);+ }),+ );+- await expect(getStorageObject(harness)).resolves.toEqual(Option.none());++ const result = await getStorageObjectExit(harness);++ if (Exit.isSuccess(result)) throw new Error("Pending upload was hidden");++ expect(Option.getOrThrow(Cause.failureOption(result.cause)).cause).toEqual(++ new Error("Cannot replace a Google Drive upload that is not complete"),++ );+ expect(+ harness.objects.get(objectMapKey(integrationId, resultKey)),+ ).toMatchObject({+ providerObjectId: pending.providerObjectId,+ uploadStatus: "pending",+ });+ expect(harness.updates).toHaveLength(0);+ });++ it("re-reads the current title and converges when it changes during the first patch", async () => {+ const initialVideo = { ...syncVideo, name: "First title" };+ const latestVideo = { ...syncVideo, name: "Latest title" };+ const { harness, files } = makeSyncHarness();+ harness.setVideo(initialVideo);+ const driveFetch = makeDriveFileFetch(files);+ let patchCount = 0;+ const fetchMock = vi.fn(+ async (input: string | URL | Request, init?: RequestInit) => {+ const result = await driveFetch(input, init);+ if (init?.method === "PATCH" && ++patchCount === 1) {+ harness.setVideo(latestVideo);+ }+ return result;+ },+ );+ vi.stubGlobal("fetch", fetchMock);+ const dependencies = Layer.merge(+ Layer.succeed(StorageRepo, harness.repo),+ Layer.succeed(S3Buckets, {} as S3Buckets),+ );+ const storageLayer = StorageService.DefaultWithoutDependencies.pipe(+ Layer.provide(dependencies),+ );++ await Effect.runPromise(+ StorageService.syncVideoDisplayNames(videoId).pipe(+ Effect.provide(storageLayer),+ ),+ );++ expect(+ fetchMock.mock.calls+ .filter(([, init]) => init?.method === "PATCH")+ .map(([, init]) => requestBody(init)),+ ).toEqual([+ { name: "First title" },+ { name: "First title.mp4" },+ { name: "Latest title" },+ { name: "Latest title.mp4" },+ ]);+ expect(files.get("folder-id")?.name).toBe("Latest title");+ expect(files.get("file-id")?.name).toBe("Latest title.mp4");+ });++ it("uses name-only patches and verifies ids, parents, hashes, and app properties", async () => {+ const { harness, files } = makeSyncHarness();+ harness.setFileNameUpdateResult(() => false);+ const fetchMock = makeDriveFileFetch(files);+ vi.stubGlobal("fetch", fetchMock);++ await expect(+ Effect.runPromise(+ syncGoogleDriveVideoNames(+ harness.repo,+ makeConfig(),+ syncVideo,+ makeTokenStore(),+ ),+ ),+ ).resolves.toBe(true);++ const patchBodies = fetchMock.mock.calls+ .filter(([, init]) => init?.method === "PATCH")+ .map(([, init]) => requestBody(init));+ expect(patchBodies).toEqual([+ { name: "New title: 测试 🎬" },+ { name: "New title: 测试 🎬.mp4" },+ ]);+ expect(files.get("folder-id")).toMatchObject({+ id: "folder-id",+ name: "New title: 测试 🎬",+ parents: ["root-folder"],+ });+ expect(files.get("file-id")).toMatchObject({+ id: "file-id",+ name: "New title: 测试 🎬.mp4",+ size: "5678",+ md5Checksum: "original-md5",+ parents: ["folder-id"],+ appProperties: {+ capObjectKey: resultKey,+ customerTag: "keep-me",+ },+ });+ expect(harness.fileNameUpdates).toHaveLength(2);+ });++ it("refreshes verification ETags after a name-only rename without changing media bytes", async () => {+ const { harness, files } = makeSyncHarness();+ const file = files.get("file-id");+ if (!file) throw new Error("Missing Drive file fixture");+ file.version = "41";+ vi.stubGlobal("fetch", makeDriveFileFetch(files));+ const access = await getStorageAccess(harness);+ const before = await Effect.runPromise(access.headObject(resultKey));++ await Effect.runPromise(+ syncGoogleDriveVideoNames(+ harness.repo,+ makeConfig(),+ syncVideo,+ makeTokenStore(),+ ),+ );++ const after = await Effect.runPromise(access.headObject(resultKey));+ expect(before).toMatchObject({+ ETag: '"file-id:41"',+ ContentLength: 5678,+ });+ expect(after).toMatchObject({+ ETag: '"file-id:42"',+ ContentLength: 5678,+ });+ expect(file).toMatchObject({+ id: "file-id",+ name: "New title: 测试 🎬.mp4",+ md5Checksum: "original-md5",+ parents: ["folder-id"],+ appProperties: { capObjectKey: resultKey, customerTag: "keep-me" },+ });+ });++ it("renames a usable pending result only after every target passes provider preflight", async () => {+ const { harness, files, fileObject } = makeSyncHarness("pending");+ const events: string[] = [];+ const fetchMock = makeDriveFileFetch(files, { events });+ vi.stubGlobal("fetch", fetchMock);++ await expect(+ Effect.runPromise(+ syncGoogleDriveVideoNames(+ harness.repo,+ makeConfig(),+ syncVideo,+ makeTokenStore(),+ ),+ ),+ ).resolves.toBe(true);+ expect(events.indexOf("PATCH:folder-id")).toBeGreaterThan(+ events.indexOf("GET:file-id"),+ );+ expect(fileObject).toMatchObject({+ providerObjectId: "file-id",+ uploadStatus: "pending",+ contentLength: null,+ });+ });++ it.each([+ ["zero provider size", "0", null],+ ["stored length mismatch", "5678", 1234],+ ] as const)(+ "retries a pending result with %s without patching",+ async (_, size, contentLength) => {+ const { harness, files, fileObject } = makeSyncHarness("pending");+ fileObject.contentLength = contentLength;+ const providerFile = files.get("file-id");+ if (providerFile) providerFile.size = size;+ const fetchMock = makeDriveFileFetch(files);+ vi.stubGlobal("fetch", fetchMock);++ await expect(+ Effect.runPromise(+ syncGoogleDriveVideoNames(+ harness.repo,+ makeConfig(),+ syncVideo,+ makeTokenStore(),+ ),+ ),+ ).rejects.toBeDefined();+ expect(+ fetchMock.mock.calls.filter(([, init]) => init?.method === "PATCH"),+ ).toHaveLength(0);+ expect(fileObject.uploadStatus).toBe("pending");+ },+ );++ it("resumes idempotently after a partial Drive failure", async () => {+ const { harness, files } = makeSyncHarness();+ const fetchMock = makeDriveFileFetch(files, { failPatchFor: "file-id" });+ vi.stubGlobal("fetch", fetchMock);++ await expect(+ Effect.runPromise(+ syncGoogleDriveVideoNames(+ harness.repo,+ makeConfig(),+ syncVideo,+ makeTokenStore(),+ ),+ ),+ ).rejects.toBeDefined();+ expect(files.get("folder-id")?.name).toBe("New title: 测试 🎬");+ expect(files.get("file-id")?.name).toBe("Old title.mp4");++ await expect(+ Effect.runPromise(+ syncGoogleDriveVideoNames(+ harness.repo,+ makeConfig(),+ syncVideo,+ makeTokenStore(),+ ),+ ),+ ).resolves.toBe(true);+ expect(files.get("file-id")?.name).toBe("New title: 测试 🎬.mp4");+ });+ });diff --cc apps/web/__tests__/unit/recording-storage-lifecycle.test.tsindex b90707558,000000000..84bab590amode 100644,000000..100644--- a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts+++ b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts@@@ -1,1648 -1,0 +1,1743 @@@+import * as S3 from "@aws-sdk/client-s3";+import * as Db from "@cap/database/schema";+import {+ CurrentUser,+ DatabaseError,+ type ImageUpload,+ Organisation,+ Storage as StorageDomain,+ User,+ Video,+} from "@cap/web-domain";+import type { SQL } from "drizzle-orm";+import { MySqlDialect } from "drizzle-orm/mysql-core";+import { Cause, Effect, Exit, Logger, Option, Schema } from "effect";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ database: vi.fn(),+ bucketAccess: vi.fn(),+ driveDelete: vi.fn(),+ driveMetadata: vi.fn(),+ driveCopy: vi.fn(),+ driveText: vi.fn(),+ driveFind: vi.fn(),+ nextId: "duplicate-video",+}));++vi.mock("@cap/database", () => ({ db: mocks.database }));+vi.mock("@cap/database/helpers", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@cap/database/helpers")>()),+ nanoId: () => mocks.nextId,+}));+vi.mock("@cap/web-backend/src/S3Buckets/index.ts", async () => {+ const { Effect } = await import("effect");+ class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {+ sync: () => ({ getBucketAccess: mocks.bucketAccess }),+ }) {}+ return { S3Buckets };+});+vi.mock("@cap/web-backend/src/Tinybird/index.ts", async () => {+ const { Effect } = await import("effect");+ class Tinybird extends Effect.Service<Tinybird>()("Tinybird", {+ sync: () => ({ querySql: () => Effect.succeed({ data: [] }) }),+ }) {}+ return { Tinybird };+});+vi.mock(+ "@cap/web-backend/src/Storage/GoogleDrive.ts",+ async (importOriginal) => ({+ ...(await importOriginal<+ typeof import("@cap/web-backend/src/Storage/GoogleDrive")+ >()),+ deleteGoogleDriveFile: mocks.driveDelete,+ getGoogleDriveFileMetadata: mocks.driveMetadata,+ copyGoogleDriveFile: mocks.driveCopy,+ getGoogleDriveObjectText: mocks.driveText,+ findGoogleDriveFileByObjectKey: mocks.driveFind,+ }),+);++import { S3Buckets } from "@cap/web-backend/src/S3Buckets";+import { createS3BucketAccess } from "@cap/web-backend/src/S3Buckets/S3BucketAccess";+import { S3BucketClientProvider } from "@cap/web-backend/src/S3Buckets/S3BucketClientProvider";+import { Storage } from "@cap/web-backend/src/Storage";+import {+ type copyGoogleDriveFile,+ type GoogleDriveFile,+ GoogleDriveRequestError,+} from "@cap/web-backend/src/Storage/GoogleDrive";+import { StorageRepo } from "@cap/web-backend/src/Storage/StorageRepo";+import { Videos } from "@cap/web-backend/src/Videos";+import { VideosRepo } from "@cap/web-backend/src/Videos/VideosRepo";++const ownerId = User.UserId.make("owned-user");+const videoId = Video.VideoId.make("owned-video");+const orgId = Organisation.OrganisationId.make("owned-org");+const prefix = `${ownerId}/${videoId}/`;+const newPrefix = `${ownerId}/duplicate-video/`;+const outputKey = `${prefix}.recording/outputs/generation/attempt.mp4`;+const thumbnailKey = `${prefix}.recording/outputs/generation/attempt/screenshot.jpg`;+const previewKey = `${prefix}.recording/outputs/generation/attempt/preview.gif`;+const currentUser = {+ id: ownerId,+ email: "[email protected]",+ activeOrganizationId: orgId,+ iconUrlOrKey: Option.none<ImageUpload.ImageUrlOrKey>(),+};++function recording(+ source: Video.Video["source"] = {+ type: "desktopMP4",+ outputKey,+ thumbnailKey,+ previewKey,+ },+) {+ return Video.Video.make({+ id: videoId,+ ownerId,+ orgId,+ name: "Retained recording",+ public: true,+ source,+ metadata: Option.some({+ sourceName: "Test display",+ desktopRecordingUpload: {+ version: 1,+ artifact: { kind: "segments", manifestSha256: "a".repeat(64) },+ fileSize: 4096,+ duration: 5,+ hasAudio: true,+ fullDecode: true,+ requiredAudioVerified: true,+ objectIdentity: '"old-output"',+ outputKey,+ },+ }),+ bucketId: Option.none(),+ storageIntegrationId: Option.none(),+ folderId: Option.none(),+ transcriptionStatus: Option.none(),+ width: Option.some(320),+ height: Option.some(180),+ duration: Option.some(5),+ createdAt: new Date(),+ updatedAt: new Date(),+ });+}++type DatabaseMutation = {+ operation: "insert" | "delete";+ table: unknown;+ id?: string;+ values?: Record<string, unknown>;+};++function databaseFixture(video = recording()) {+ const encoded = Schema.encodeSync(Video.Video)(video);+ const original = {+ ...encoded,+ bucket: encoded.bucketId,+ createdAt: video.createdAt,+ updatedAt: video.updatedAt,+ };+ const rows = new Map<string, Record<string, unknown>>([[videoId, original]]);+ const jobs = new Map<string, Record<string, unknown>>([+ [+ videoId,+ {+ videoId,+ ownerId,+ generation: "active-generation",+ state: "processing",+ },+ ],+ ]);+ const uploads = new Map<string, typeof Db.videoUploads.$inferSelect>();+ const mutations: DatabaseMutation[] = [];+ const events: string[] = [];++ const objectDeleteConditions: SQL[] = [];+ let beforeJobDelete: (() => Promise<void>) | undefined;+ let beforePrepareDelete: (() => Promise<void>) | undefined;+ let loseNextCreateResponse = false;+ const idFrom = (condition: SQL) => {+ const id = new MySqlDialect().sqlToQuery(condition).params[0];+ if (typeof id !== "string") throw new Error("Missing database video id");+ return id;+ };+ const read = (table: unknown, condition: SQL) => {+ const source =+ table === Db.videos+ ? rows+ : table === Db.videoProcessingJobs+ ? jobs+ : null;+ if (!source) throw new Error("Unexpected table read");+ const row = source.get(idFrom(condition));+ return row ? [structuredClone(row)] : [];+ };+ function transactionHandle(pending: DatabaseMutation[]) {+ return {+ select: () => ({+ from: (table: unknown) => ({+ where: (condition: SQL) => ({+ for: async () => read(table, condition),+ }),+ }),+ }),+ delete: (table: unknown) => ({+ where: async (condition: SQL) => {+ const id = idFrom(condition);+ if (table === Db.videoProcessingJobs) {+ events.push("job-delete-request");+ await beforeJobDelete?.();+ events.push("job-delete-complete");+ } else {+ events.push("dependent-delete");+ }+ pending.push({ operation: "delete", table, id });+ return [{ affectedRows: 1 }];+ },+ }),+ insert: (table: unknown) => ({+ values: (+ values: Record<string, unknown>[] | Record<string, unknown>,+ ) => {+ if (Array.isArray(values)) {+ for (const value of values) {+ pending.push({ operation: "insert", table, values: value });+ }+ return Promise.resolve([{ affectedRows: 1 }]);+ }+ return {+ onDuplicateKeyUpdate: async ({+ set,+ }: {+ set: Record<string, unknown>;+ }) => {+ events.push("job-fence-request");+ await beforePrepareDelete?.();+ const id = values.videoId;+ if (typeof id !== "string")+ throw new Error("Missing retired job id");+ pending.push({+ operation: "insert",+ table,+ values: jobs.has(id) ? { ...jobs.get(id), ...set } : values,+ });+ return [{ affectedRows: 1 }];+ },+ };+ },+ }),+ };+ }+ const transaction = vi.fn(+ async (+ operation: (tx: ReturnType<typeof transactionHandle>) => Promise<unknown>,+ ) => {+ const pending: DatabaseMutation[] = [];+ const result = await operation(transactionHandle(pending));+ for (const mutation of pending) {+ mutations.push(mutation);+ const table =+ mutation.table === Db.videos+ ? rows+ : mutation.table === Db.videoProcessingJobs+ ? jobs+ : null;+ if (!table) continue;+ if (mutation.operation === "delete" && mutation.id)+ table.delete(mutation.id);+ if (mutation.operation === "insert" && mutation.values) {+ const id =+ mutation.table === Db.videos+ ? mutation.values.id+ : mutation.values.videoId;+ if (typeof id !== "string") throw new Error("Missing inserted id");+ table.set(id, mutation.values);+ }+ }+ if (+ loseNextCreateResponse &&+ pending.some(+ ({ operation, table }) =>+ operation === "insert" && table === Db.videos,+ )+ ) {+ loseNextCreateResponse = false;+ throw new Error("Committed video creation response was lost");+ }+ return result;+ },+ );+ mocks.database.mockReturnValue({+ select: () => ({+ from: (table: unknown) => ({+ where: async (condition: SQL) => read(table, condition),+ leftJoin: (joined: unknown) => ({+ where: async (condition: SQL) => {+ if (table !== Db.videoUploads || joined !== Db.videoProcessingJobs)+ throw new Error("Unexpected joined progress read");+ const id = idFrom(condition);+ const upload = uploads.get(id);+ return upload+ ? [{ ...upload, processingJobState: jobs.get(id)?.state ?? null }]+ : [];+ },+ }),+ }),+ }),+ delete: (table: unknown) => ({- where: async () => {++ where: async (condition: SQL) => {+ if (table !== Db.storageObjects)+ throw new Error("Unexpected direct delete");++ objectDeleteConditions.push(condition);+ return [{ affectedRows: 1 }];+ },+ }),+ transaction,+ });+ return {+ rows,+ jobs,+ uploads,+ mutations,+ events,++ objectDeleteConditions,+ transaction,+ beforeJobDelete: (callback: () => Promise<void>) => {+ beforeJobDelete = callback;+ },+ beforePrepareDelete: (callback: () => Promise<void>) => {+ beforePrepareDelete = callback;+ },+ loseNextCreateResponse: () => {+ loseNextCreateResponse = true;+ },+ };+}++const clients: S3.S3Client[] = [];++async function storageFixture(seed: Iterable<readonly [string, string]>) {+ const objects = new Map(seed);+ const sizes = new Map(+ [...objects].map(([key, value]) => [key, value.length]),+ );+ const identities = new Map(+ [...objects.keys()].map((key, index) => [key, `"source-${index}"`]),+ );+ const requests: Array<{+ operation:+ | "copy"+ | "list"+ | "delete"+ | "head"+ | "multipart-create"+ | "multipart-part"+ | "multipart-complete"+ | "multipart-abort";+ source?: string;+ key?: string;+ keys?: string[];+ token?: string;+ }> = [];+ const copyInputs: S3.CopyObjectCommandInput[] = [];+ const partInputs: S3.UploadPartCopyCommandInput[] = [];+ const completeInputs: S3.CompleteMultipartUploadCommandInput[] = [];+ const createInputs: S3.CreateMultipartUploadCommandInput[] = [];+ const uploads = new Map<+ string,+ {+ key: string;+ parts: Map<+ number,+ { source: string; start: number; end: number; etag: string }+ >;+ }+ >();+ const deniedDeletes = new Set<string>();+ let copyFailure: string | undefined;+ let listFailure: string | undefined;+ let failedPart: number | undefined;+ let malformedPagination: "missing" | "repeated" | undefined;+ let beforeDelete: (() => void) | undefined;+ let beforeCopy: (() => void) | undefined;+ let afterCopy: (() => void) | undefined;+ const client = new S3.S3Client({+ credentials: { accessKeyId: "test-key", secretAccessKey: "test-secret" },+ endpoint: "http://storage.test",+ region: "us-east-1",+ forcePathStyle: true,+ });+ clients.push(client);+ const sourceKeyFrom = (source: string | undefined) => {+ if (!source?.startsWith("test-bucket/"))+ throw new Error("Invalid copy source");+ return decodeURIComponent(source.slice("test-bucket/".length));+ };+ const copy = (+ sourceKey: string,+ key: string,+ size: number,+ identity: string,+ ) => {+ const content = objects.get(sourceKey);+ if (content === undefined) throw new Error("Copy source not found");+ objects.set(key, content);+ sizes.set(key, size);+ identities.set(key, identity);+ afterCopy?.();+ };+ vi.spyOn(client, "send").mockImplementation(async (command) => {+ if (command instanceof S3.HeadObjectCommand) {+ const key = command.input.Key;+ if (!key || !objects.has(key)) throw new Error("Object not found");+ requests.push({ operation: "head", key });+ return {+ ContentLength: sizes.get(key),+ ETag: identities.get(key),+ ContentType: "video/mp4",+ Metadata: { recording: "retained" },+ $metadata: {},+ };+ }+ if (command instanceof S3.CopyObjectCommand) {+ const sourceKey = sourceKeyFrom(command.input.CopySource);+ const key = command.input.Key;+ if (!key) throw new Error("Missing copy destination");+ beforeCopy?.();+ requests.push({ operation: "copy", source: sourceKey, key });+ copyInputs.push(command.input);+ if (key === copyFailure) throw new Error("Copy unavailable");+ if (command.input.CopySourceIfMatch !== identities.get(sourceKey))+ throw new Error("Source precondition failed");+ const size = sizes.get(sourceKey);+ if (size === undefined || size > 5 * 1024 ** 3)+ throw new Error("Single-copy size limit");+ const identity = `"copied-${key}"`;+ copy(sourceKey, key, size, identity);+ return { CopyObjectResult: { ETag: identity }, $metadata: {} };+ }+ if (command instanceof S3.CreateMultipartUploadCommand) {+ const key = command.input.Key;+ if (!key) throw new Error("Missing multipart destination");+ beforeCopy?.();+ const UploadId = `copy-upload-${createInputs.length}`;+ createInputs.push(command.input);+ uploads.set(UploadId, { key, parts: new Map() });+ requests.push({ operation: "multipart-create", key });+ return { UploadId, $metadata: {} };+ }+ if (command instanceof S3.UploadPartCopyCommand) {+ const {+ Key: key,+ UploadId: uploadId,+ PartNumber: partNumber,+ } = command.input;+ const upload = uploadId ? uploads.get(uploadId) : undefined;+ const sourceKey = sourceKeyFrom(command.input.CopySource);+ if (!upload || upload.key !== key || !partNumber)+ throw new Error("Invalid multipart part");+ partInputs.push(command.input);+ requests.push({ operation: "multipart-part", key, source: sourceKey });+ if (partNumber === failedPart) throw new Error("Part copy unavailable");+ if (command.input.CopySourceIfMatch !== identities.get(sourceKey))+ throw new Error("Source precondition failed");+ const range = /^bytes=(\d+)-(\d+)$/.exec(+ command.input.CopySourceRange ?? "",+ );+ if (!range) throw new Error("Missing part range");+ const start = Number(range[1]);+ const end = Number(range[2]);+ const etag = `"part-${partNumber}"`;+ upload.parts.set(partNumber, { source: sourceKey, start, end, etag });+ return { CopyPartResult: { ETag: etag }, $metadata: {} };+ }+ if (command instanceof S3.CompleteMultipartUploadCommand) {+ const { Key: key, UploadId: uploadId } = command.input;+ const upload = uploadId ? uploads.get(uploadId) : undefined;+ if (+ !key ||+ !upload ||+ command.input.IfNoneMatch !== "*" ||+ objects.has(key)+ )+ throw new Error("Invalid multipart completion");+ completeInputs.push(command.input);+ requests.push({ operation: "multipart-complete", key });+ let position = 0;+ let sourceKey: string | undefined;+ for (const [index, completed] of (+ command.input.MultipartUpload?.Parts ?? []+ ).entries()) {+ const part = upload.parts.get(index + 1);+ if (+ !part ||+ completed.PartNumber !== index + 1 ||+ completed.ETag !== part.etag ||+ part.start !== position+ )+ throw new Error("Incomplete multipart sequence");+ position = part.end + 1;+ sourceKey = part.source;+ }+ if (!sourceKey || position !== sizes.get(sourceKey))+ throw new Error("Incomplete multipart copy");+ const identity = '"multipart-output-etag"';+ copy(sourceKey, key, position, identity);+ if (uploadId) uploads.delete(uploadId);+ return { ETag: identity, $metadata: {} };+ }+ if (command instanceof S3.AbortMultipartUploadCommand) {+ if (command.input.UploadId) uploads.delete(command.input.UploadId);+ requests.push({ operation: "multipart-abort", key: command.input.Key });+ return { $metadata: {} };+ }+ if (command instanceof S3.ListObjectsV2Command) {+ const token = command.input.ContinuationToken;+ requests.push({ operation: "list", token });+ if (token !== undefined && token === listFailure)+ throw new Error("Listing unavailable");+ const keys = [...objects.keys()]+ .filter(+ (key) =>+ key.startsWith(command.input.Prefix ?? "") &&+ (token === undefined || key > token),+ )+ .sort();+ const page = keys.slice(0, 1000);+ return {+ Contents: page.map((Key) => ({ Key, Size: sizes.get(Key) })),+ IsTruncated:+ malformedPagination !== undefined || keys.length > page.length,+ NextContinuationToken:+ malformedPagination === "missing"+ ? undefined+ : malformedPagination === "repeated"+ ? "repeated-token"+ : keys.length > page.length+ ? page.at(-1)+ : undefined,+ $metadata: {},+ };+ }+ if (command instanceof S3.DeleteObjectsCommand) {+ beforeDelete?.();+ const keys = (command.input.Delete?.Objects ?? [])+ .map((object) => object.Key)+ .filter((key): key is string => key !== undefined);+ requests.push({ operation: "delete", keys });+ for (const key of keys) {+ if (!deniedDeletes.has(key)) {+ objects.delete(key);+ sizes.delete(key);+ identities.delete(key);+ }+ }+ return {+ Deleted: keys+ .filter((key) => !deniedDeletes.has(key))+ .map((Key) => ({ Key })),+ Errors: keys+ .filter((key) => deniedDeletes.has(key))+ .map((Key) => ({ Key, Code: "AccessDenied" })),+ $metadata: {},+ };+ }+ throw new Error("Unexpected S3 request");+ });+ const access = await Effect.runPromise(+ createS3BucketAccess.pipe(+ Effect.provideService(S3BucketClientProvider, {+ bucket: "test-bucket",+ getInternal: Effect.succeed(client),+ getPublic: Effect.succeed(client),+ isPathStyle: true,+ }),+ ),+ );+ mocks.bucketAccess.mockReturnValue(Effect.succeed([access, Option.none()]));+ return {+ objects,+ sizes,+ identities,+ requests,+ copyInputs,+ partInputs,+ completeInputs,+ createInputs,+ uploads,+ deniedDeletes,+ failCopy: (key: string) => {+ copyFailure = key;+ },+ failList: (token: string) => {+ listFailure = token;+ },+ failPart: (partNumber: number) => {+ failedPart = partNumber;+ },+ malformedPagination: (kind: "missing" | "repeated") => {+ malformedPagination = kind;+ },+ beforeDelete: (callback: () => void) => {+ beforeDelete = callback;+ },+ beforeCopy: (callback: () => void) => {+ beforeCopy = callback;+ },+ afterCopy: (callback: () => void) => {+ afterCopy = callback;+ },+ };+}++async function driveFixture() {- databaseFixture();++ const database = databaseFixture();+ const integrationId =+ StorageDomain.StorageIntegrationId.make("drive-integration");+ const now = new Date();+ const integration: typeof Db.storageIntegrations.$inferSelect = {+ id: integrationId,+ ownerId,+ organizationId: orgId,+ provider: "googleDrive",+ displayName: "Test Drive",+ status: "active",+ active: true,+ encryptedConfig: "unused-encrypted-fixture",+ googleDriveAccessToken: null,+ googleDriveAccessTokenExpiresAt: null,+ googleDriveTokenRefreshLeaseId: null,+ googleDriveTokenRefreshLeaseExpiresAt: null,+ googleDriveStorageQuotaCache: null,+ createdAt: now,+ updatedAt: now,+ };+ const original: typeof Db.storageObjects.$inferSelect = {+ id: StorageDomain.StorageObjectId.make("source-object"),+ integrationId,+ ownerId,+ videoId,+ objectKey: outputKey,+ objectKeyHash: "a".repeat(64),+ providerObjectId: "original-file",+ uploadSessionUrl: null,+ uploadStatus: "complete",+ contentType: "video/mp4",+ contentLength: 10,+ metadata: null,+ createdAt: now,+ updatedAt: now,+ };+ const records = new Map([[outputKey, original]]);+ const files = new Map<string, GoogleDriveFile>([+ [+ "original-file",+ { id: "original-file", version: "1", size: "10", mimeType: "video/mp4" },+ ],+ ]);+ const repo = await Effect.runPromise(+ StorageRepo.pipe(Effect.provide(StorageRepo.Default)),+ );+ vi.spyOn(repo, "getIntegrationById").mockReturnValue(+ Effect.succeed(Option.some(integration)),+ );+ vi.spyOn(repo, "getGoogleDriveConfig").mockReturnValue(+ Effect.succeed({+ refreshToken: "fixture-token",+ folderId: "fixture-folder",+ }),+ );+ const getIndex = vi+ .spyOn(repo, "getObjectByKey")+ .mockImplementation((_integration, key) =>- Effect.sync(() => Option.fromNullable(records.get(key))),++ Effect.sync(() =>++ Option.map(Option.fromNullable(records.get(key)), (object) =>++ structuredClone(object),++ ),++ ),+ );- const upsertIndex = vi- .spyOn(repo, "upsertObject")- .mockImplementation((input) =>++ const updateIndex = vi++ .spyOn(repo, "updateObjectIfCurrent")++ .mockImplementation((object, input) =>+ Effect.sync(() => {- records.set(input.objectKey, {- ...(records.get(input.objectKey) ?? original),- objectKey: input.objectKey,++ const current = records.get(object.objectKey);++ if (++ !current ||++ current.id !== object.id ||++ current.integrationId !== object.integrationId ||++ current.objectKey !== object.objectKey ||++ current.objectKeyHash !== object.objectKeyHash ||++ current.providerObjectId !== object.providerObjectId ||++ current.uploadStatus !== object.uploadStatus++ )++ return false;++ records.set(object.objectKey, {++ ...current,+ providerObjectId: input.providerObjectId,++ uploadStatus: input.uploadStatus ?? "pending",+ contentType: input.contentType ?? null,+ contentLength: input.contentLength ?? null,- metadata: input.metadata ?? null,++ metadata: input.preserveMetadata++ ? current.metadata++ : (input.metadata ?? null),+ });++ return true;+ }),+ );+ const deleteFromRepo = repo.deleteObjectByKey;+ const deleteIndex = vi+ .spyOn(repo, "deleteObjectByKey")+ .mockImplementation((...args) =>+ deleteFromRepo(...args).pipe(- Effect.tap(() => Effect.sync(() => records.delete(args[1]))),++ Effect.tap(() =>++ Effect.sync(() => {++ if (++ args[2] === undefined ||++ records.get(args[1])?.providerObjectId === args[2]++ )++ records.delete(args[1]);++ }),++ ),+ ),+ );+ mocks.driveDelete.mockReset().mockReturnValue(Effect.void);+ mocks.driveText+ .mockReset()+ .mockReturnValue(Effect.succeed("fixture-recording-text"));+ mocks.driveFind+ .mockReset()+ .mockReturnValue(Effect.succeed(Option.none<GoogleDriveFile>()));+ mocks.driveMetadata+ .mockReset()+ .mockImplementation((_config: unknown, fileId: string) =>+ Effect.sync(() => {+ const file = files.get(fileId);+ if (!file) throw new Error("Missing Google Drive file fixture");+ return { ...file };+ }),+ );+ mocks.driveCopy+ .mockReset()+ .mockImplementation(+ ({ input, sourceFileId }: Parameters<typeof copyGoogleDriveFile>[0]) =>+ Effect.sync(() => {+ const source = files.get(sourceFileId);+ if (!source)+ throw new Error("Missing Google Drive copy source fixture");+ files.set("copied-file", {+ ...source,+ id: "copied-file",+ version: "1",+ });+ records.set(input.key, {+ ...original,+ id: StorageDomain.StorageObjectId.make("copied-object"),+ objectKey: input.key,+ providerObjectId: "copied-file",+ });+ }),+ );+ const video = Video.Video.make({+ ...recording({ type: "desktopMP4", outputKey }),+ storageIntegrationId: Option.some(integrationId),+ });+ const [access] = await Effect.runPromise(+ Effect.flatMap(Storage, (storage) => storage.getAccessForVideo(video)).pipe(+ Effect.provide(Storage.DefaultWithoutDependencies),+ Effect.provideService(StorageRepo, repo),+ Effect.provide(S3Buckets.Default),+ ),+ );+ return {+ access,+ records,+ files,+ integrationId,+ deleteIndex,++ deleteConditions: database.objectDeleteConditions,+ getIndex,- upsertIndex,++ updateIndex,+ };+}++function runVideoOperation(+ operation: "duplicate" | "delete",+ userId = ownerId,+) {+ return Effect.runPromiseExit(+ Effect.gen(function* () {+ const videos = yield* Videos;+ return yield* videos[operation](videoId);+ }).pipe(+ Effect.provide(Videos.Default),+ Effect.provideService(CurrentUser, { ...currentUser, id: userId }),+ Effect.provide(Logger.remove(Logger.defaultLogger)),+ ),+ );+}++beforeEach(() => {+ mocks.nextId = "duplicate-video";+});++afterEach(() => {+ for (const client of clients.splice(0)) client.destroy();+});++describe("recording storage lifecycle", () => {+ it.each([+ "committing",+ "queued",+ "processing",+ "retry",+ "source-blocked",+ null,+ ] as const)(+ "reports durable state %s without treating recoverable worker failures as final failures",+ async (state) => {+ const database = databaseFixture();+ const previousUpdate = new Date(Date.now() - 60 * 60 * 1000);+ database.uploads.set(videoId, {+ videoId,+ uploaded: 100,+ total: 100,+ startedAt: previousUpdate,+ updatedAt: previousUpdate,+ mode: "multipart",+ phase: "error",+ processingProgress: 50,+ processingMessage: "Source retained",+ processingError: "Previous processing attempt failed",+ rawFileKey: null,+ });+ if (state) database.jobs.set(videoId, { state });+ else database.jobs.delete(videoId);+ const result = await Effect.runPromise(+ Effect.flatMap(Videos, (videos) =>+ videos.getUploadProgress(videoId),+ ).pipe(+ Effect.provide(Videos.Default),+ Effect.provideService(CurrentUser, currentUser),+ Effect.provide(Logger.remove(Logger.defaultLogger)),+ ),+ );+ const progress = Option.getOrThrow(result);+ expect(progress.updatedAt).toEqual(previousUpdate);+ if (state === "source-blocked" || state === null) {+ expect(progress.phase).toBe("error");+ expect(progress.automaticRetry).toBe(false);+ expect(progress.processingError).toEqual(+ Option.some("Previous processing attempt failed"),+ );+ } else {+ expect(progress.phase).toBe("processing");+ expect(progress.automaticRetry).toBe(true);+ expect(progress.processingError).toEqual(Option.none());+ }+ },+ );++ it("duplicates the published video and hidden assets into canonical destinations", async () => {+ const database = databaseFixture();+ const storage = await storageFixture([+ [outputKey, "verified-video"],+ [thumbnailKey, "verified-thumbnail"],+ [previewKey, "verified-preview"],+ [`${prefix}result.mp4`, "stale-canonical-video"],+ [`${prefix}screenshot/screen-capture.jpg`, "stale-thumbnail"],+ ]);+ storage.beforeCopy(() =>+ expect(database.rows.has("duplicate-video")).toBe(false),+ );+ const result = await runVideoOperation("duplicate");+ expect(Exit.isSuccess(result)).toBe(true);+ expect(storage.objects.get(`${newPrefix}result.mp4`)).toBe(+ "verified-video",+ );+ expect(+ storage.objects.get(`${newPrefix}screenshot/screen-capture.jpg`),+ ).toBe("verified-thumbnail");+ expect(+ storage.objects.get(`${newPrefix}preview/animated-preview.gif`),+ ).toBe("verified-preview");+ expect(database.rows.get("duplicate-video")).toMatchObject({+ source: { type: "desktopMP4" },+ metadata: { sourceName: "Test display" },+ });+ expect(database.rows.get("duplicate-video")?.metadata).not.toHaveProperty(+ "desktopRecordingUpload",+ );+ expect(database.rows.get("owned-video")?.metadata).toHaveProperty(+ "desktopRecordingUpload",+ );+ expect(+ storage.requests.filter(({ operation }) => operation === "copy"),+ ).toEqual([+ { operation: "copy", source: outputKey, key: `${newPrefix}result.mp4` },+ {+ operation: "copy",+ source: thumbnailKey,+ key: `${newPrefix}screenshot/screen-capture.jpg`,+ },+ {+ operation: "copy",+ source: previewKey,+ key: `${newPrefix}preview/animated-preview.gif`,+ },+ ]);+ });++ it("does not duplicate source inventories, raw fragments, or comment attachments across pages", async () => {+ const database = databaseFixture();+ const retained = Array.from(+ { length: 1100 },+ (_, index) =>+ [+ `${prefix}.recording/sources/generation/snapshot/video/${String(index).padStart(5, "0")}.m4s`,+ "source-fragment",+ ] as const,+ );+ const storage = await storageFixture([+ ...retained,+ [outputKey, "verified-video"],+ [thumbnailKey, "verified-thumbnail"],+ [previewKey, "verified-preview"],+ [+ `${prefix}.recording/sources/generation/snapshot/inventory.json`,+ "inventory",+ ],+ [+ `${prefix}.recording/outputs/old-generation/old-attempt.mp4`,+ "old-output",+ ],+ [`${prefix}segments/video/0.m4s`, "legacy-raw-fragment"],+ [`${prefix}comments/comment/attachment.mp4`, "comment-attachment"],+ [`${prefix}transcription.vtt`, "published-transcription"],+ [`${prefix}chapter-data.json`, "published-chapters"],+ ]);+ const result = await runVideoOperation("duplicate");+ expect(Exit.isSuccess(result)).toBe(true);+ expect(+ storage.requests.filter(({ operation }) => operation === "list"),+ ).toHaveLength(2);+ expect(+ [...storage.objects.keys()]+ .filter((key) => key.startsWith(newPrefix))+ .sort(),+ ).toEqual([+ `${newPrefix}chapter-data.json`,+ `${newPrefix}preview/animated-preview.gif`,+ `${newPrefix}result.mp4`,+ `${newPrefix}screenshot/screen-capture.jpg`,+ `${newPrefix}transcription.vtt`,+ ]);+ expect(database.rows.get("duplicate-video")?.source).toEqual({+ type: "desktopMP4",+ });+ expect(storage.objects.get(retained.at(-1)?.[0] ?? "")).toBe(+ "source-fragment",+ );+ });++ it("copies a published MP4 snapshot without retaining its original output pointer", async () => {+ const snapshotKey = `${prefix}.recording/sources/generation/snapshot/mp4/0.mp4`;+ const database = databaseFixture(+ recording({ type: "desktopMP4", outputKey: snapshotKey }),+ );+ const storage = await storageFixture([+ [snapshotKey, "verified-snapshot"],+ [`${prefix}result.mp4`, "new-unverified-upload"],+ ]);+ expect(Exit.isSuccess(await runVideoOperation("duplicate"))).toBe(true);+ expect(storage.objects.get(`${newPrefix}result.mp4`)).toBe(+ "verified-snapshot",+ );+ expect(database.rows.get("duplicate-video")?.source).toEqual({+ type: "desktopMP4",+ });+ expect(+ storage.objects.has(+ `${newPrefix}.recording/sources/generation/snapshot/mp4/0.mp4`,+ ),+ ).toBe(false);+ });++ it("preserves the legacy canonical MP4 copy contract without copying cleanup receipts", async () => {+ const database = databaseFixture(recording({ type: "desktopMP4" }));+ const storage = await storageFixture([+ [`${prefix}result.mp4`, "legacy-recording"],+ [`${prefix}screenshot/screen-capture.jpg`, "legacy-thumbnail"],+ ]);+ expect(Exit.isSuccess(await runVideoOperation("duplicate"))).toBe(true);+ expect(storage.objects.get(`${newPrefix}result.mp4`)).toBe(+ "legacy-recording",+ );+ expect(database.rows.get("duplicate-video")?.metadata).toEqual({+ sourceName: "Test display",+ });+ });++ it.each(["duplicate", "delete"] as const)(+ "does not %s another owner's recording or media",+ async (operation) => {+ const database = databaseFixture();+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ const result = await runVideoOperation(+ operation,+ User.UserId.make("different-user"),+ );+ expect(Exit.isFailure(result)).toBe(true);+ expect(database.rows.has(videoId)).toBe(true);+ expect(database.mutations).toEqual([]);+ expect(storage.requests).toEqual([]);+ },+ );++ it("waits for job deletion before deleting any dependent database row", async () => {+ const database = databaseFixture();+ let release: (() => void) | undefined;+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });+ database.beforeJobDelete(() => gate);+ const pending = Effect.runPromise(+ Effect.flatMap(VideosRepo, (repo) => repo.delete(videoId)).pipe(+ Effect.provide(VideosRepo.Default),+ ),+ );+ await vi.waitFor(() =>+ expect(database.events).toEqual(["job-delete-request"]),+ );+ expect(database.rows.has(videoId)).toBe(true);+ expect(database.mutations).toEqual([]);+ if (!release) throw new Error("Missing job deletion resolver");+ release();+ await pending;+ expect(database.events.slice(0, 2)).toEqual([+ "job-delete-request",+ "job-delete-complete",+ ]);+ expect(database.mutations.at(0)?.table).toBe(Db.videoProcessingJobs);+ expect(database.rows.has(videoId)).toBe(false);+ });++ it("keeps database rows and storage intact when retiring the worker fails", async () => {+ const database = databaseFixture();+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ database.beforePrepareDelete(async () => {+ throw new Error("Job retirement unavailable");+ });+ expect(Exit.isFailure(await runVideoOperation("delete"))).toBe(true);+ expect(database.rows.has(videoId)).toBe(true);+ expect(database.mutations).toEqual([]);+ expect(storage.requests).toEqual([]);+ });++ it("does not report successful deletion when S3 rejects a retained source object", async () => {+ const database = databaseFixture();+ const retainedKey = `${prefix}.recording/sources/generation/snapshot/video/0.m4s`;+ const storage = await storageFixture([+ [outputKey, "verified-video"],+ [retainedKey, "retained-fragment"],+ ]);+ storage.deniedDeletes.add(retainedKey);+ expect(Exit.isFailure(await runVideoOperation("delete"))).toBe(true);+ expect(storage.objects.get(retainedKey)).toBe("retained-fragment");+ expect(database.rows.has(videoId)).toBe(true);+ expect(database.jobs.get(videoId)).toMatchObject({+ state: "source-blocked",+ errorCode: "video-deleting",+ });+ storage.deniedDeletes.clear();+ expect(Exit.isSuccess(await runVideoOperation("delete"))).toBe(true);+ expect(database.rows.has(videoId)).toBe(false);+ expect(database.jobs.has(videoId)).toBe(false);+ expect(storage.objects.has(retainedKey)).toBe(false);+ });++ it("does not leave a published duplicate when copying its output fails", async () => {+ const database = databaseFixture();+ const storage = await storageFixture([+ [outputKey, "verified-video"],+ [thumbnailKey, "verified-thumbnail"],+ [previewKey, "verified-preview"],+ ]);+ storage.failCopy(`${newPrefix}result.mp4`);+ expect(Exit.isFailure(await runVideoOperation("duplicate"))).toBe(true);+ expect(database.rows.has("duplicate-video")).toBe(false);+ expect(database.rows.has(videoId)).toBe(true);+ expect(storage.objects.get(outputKey)).toBe("verified-video");+ });++ it("returns the copy failure without publishing when rollback cleanup also fails", async () => {+ const database = databaseFixture();+ const storage = await storageFixture([+ [outputKey, "verified-video"],+ [thumbnailKey, "verified-thumbnail"],+ ]);+ storage.failCopy(`${newPrefix}screenshot/screen-capture.jpg`);+ storage.deniedDeletes.add(`${newPrefix}result.mp4`);+ const result = await runVideoOperation("duplicate");+ expect(Exit.isFailure(result)).toBe(true);+ expect(database.rows.has("duplicate-video")).toBe(false);+ expect(database.rows.has(videoId)).toBe(true);+ expect(storage.requests).toContainEqual(+ expect.objectContaining({ operation: "delete" }),+ );+ expect(storage.objects.get(`${newPrefix}result.mp4`)).toBe(+ "verified-video",+ );+ expect(storage.objects.get(outputKey)).toBe("verified-video");+ });++ it("keeps cloned media when video creation committed but its response was lost", async () => {+ const database = databaseFixture(+ recording({ type: "desktopMP4", outputKey }),+ );+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ database.loseNextCreateResponse();+ expect(Exit.isFailure(await runVideoOperation("duplicate"))).toBe(true);+ expect(database.rows.has("duplicate-video")).toBe(true);+ expect(storage.objects.get(`${newPrefix}result.mp4`)).toBe(+ "verified-video",+ );+ expect(+ storage.requests.filter(({ operation }) => operation === "delete"),+ ).toEqual([]);+ });++ it("does not leave a partial duplicate when a later object listing fails", async () => {+ const database = databaseFixture();+ const retained = Array.from(+ { length: 1100 },+ (_, index) =>+ [+ `${prefix}.recording/sources/generation/snapshot/video/${String(index).padStart(5, "0")}.m4s`,+ "fragment",+ ] as const,+ );+ const storage = await storageFixture([+ ...retained,+ [outputKey, "verified-video"],+ [thumbnailKey, "verified-thumbnail"],+ [previewKey, "verified-preview"],+ ]);+ const firstPageEnd = [...storage.objects.keys()].sort().at(999);+ if (!firstPageEnd) throw new Error("Missing pagination boundary");+ storage.failList(firstPageEnd);+ expect(Exit.isFailure(await runVideoOperation("duplicate"))).toBe(true);+ expect(database.rows.has("duplicate-video")).toBe(false);+ expect(database.rows.has(videoId)).toBe(true);+ expect(+ [...storage.objects.keys()].some((key) => key.startsWith(newPrefix)),+ ).toBe(false);+ });++ it("pins single-copy source identities and permits empty ancillary objects", async () => {+ databaseFixture(recording({ type: "desktopMP4", outputKey }));+ const ancillaryKey = `${prefix}empty.vtt`;+ const storage = await storageFixture([+ [outputKey, "verified-video"],+ [ancillaryKey, ""],+ ]);+ expect(Exit.isSuccess(await runVideoOperation("duplicate"))).toBe(true);+ expect(storage.copyInputs).toHaveLength(2);+ expect(storage.copyInputs).toContainEqual(+ expect.objectContaining({+ CopySource: `test-bucket/${ancillaryKey}`,+ CopySourceIfMatch: storage.identities.get(ancillaryKey),+ }),+ );+ expect(storage.objects.get(`${newPrefix}empty.vtt`)).toBe("");+ expect(storage.sizes.get(`${newPrefix}empty.vtt`)).toBe(0);+ });++ it("copies an 8 GiB recording in complete conditional server-side ranges", async () => {+ const database = databaseFixture(+ recording({ type: "desktopMP4", outputKey }),+ );+ const storage = await storageFixture([[outputKey, "large-verified-video"]]);+ const fileSize = 8 * 1024 ** 3;+ storage.sizes.set(outputKey, fileSize);+ const sourceIdentity = storage.identities.get(outputKey);+ expect(Exit.isSuccess(await runVideoOperation("duplicate"))).toBe(true);+ expect(storage.copyInputs).toHaveLength(0);+ expect(storage.createInputs).toEqual([+ expect.objectContaining({+ Key: `${newPrefix}result.mp4`,+ ContentType: "video/mp4",+ Metadata: { recording: "retained" },+ }),+ ]);+ expect(storage.partInputs).toHaveLength(64);+ let position = 0;+ for (const [index, part] of storage.partInputs.entries()) {+ expect(part.CopySourceIfMatch).toBe(sourceIdentity);+ expect(part.CopySource).toBe(`test-bucket/${outputKey}`);+ expect(part.PartNumber).toBe(index + 1);+ const range = /^bytes=(\d+)-(\d+)$/.exec(part.CopySourceRange ?? "");+ if (!range) throw new Error("Missing multipart range");+ expect(Number(range[1])).toBe(position);+ position = Number(range[2]) + 1;+ }+ expect(position).toBe(fileSize);+ expect(storage.completeInputs).toEqual([+ expect.objectContaining({+ IfNoneMatch: "*",+ MultipartUpload: {+ Parts: Array.from({ length: 64 }, (_, index) => ({+ PartNumber: index + 1,+ ETag: `"part-${index + 1}"`,+ })),+ },+ }),+ ]);+ expect(storage.sizes.get(`${newPrefix}result.mp4`)).toBe(fileSize);+ expect(storage.identities.get(`${newPrefix}result.mp4`)).not.toBe(+ sourceIdentity,+ );+ expect(database.rows.has("duplicate-video")).toBe(true);+ expect(storage.uploads.size).toBe(0);+ });++ it("aborts a failed multipart copy without publishing or deleting the original", async () => {+ const database = databaseFixture(+ recording({ type: "desktopMP4", outputKey }),+ );+ const storage = await storageFixture([[outputKey, "large-verified-video"]]);+ storage.sizes.set(outputKey, 8 * 1024 ** 3);+ storage.failPart(2);+ expect(Exit.isFailure(await runVideoOperation("duplicate"))).toBe(true);+ expect(storage.completeInputs).toHaveLength(0);+ expect(+ storage.requests.filter(+ ({ operation }) => operation === "multipart-abort",+ ),+ ).toHaveLength(1);+ expect(storage.uploads.size).toBe(0);+ expect(database.rows.has("duplicate-video")).toBe(false);+ expect(storage.objects.get(outputKey)).toBe("large-verified-video");+ });++ it.each([+ "source-changed",+ "copy-truncated",+ "copy-replaced",+ "weak-source-identity",+ ])("does not publish a duplicate with %s", async (reason) => {+ const database = databaseFixture(+ recording({ type: "desktopMP4", outputKey }),+ );+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ if (reason === "source-changed")+ storage.afterCopy(() =>+ storage.identities.set(outputKey, '"replacement-source"'),+ );+ else if (reason === "copy-truncated")+ storage.afterCopy(() => storage.sizes.set(`${newPrefix}result.mp4`, 1));+ else if (reason === "copy-replaced")+ storage.afterCopy(() =>+ storage.identities.set(`${newPrefix}result.mp4`, '"unexpected-output"'),+ );+ else storage.identities.set(outputKey, 'W/"weak"');+ expect(Exit.isFailure(await runVideoOperation("duplicate"))).toBe(true);+ expect(database.rows.has("duplicate-video")).toBe(false);+ expect(storage.objects.has(`${newPrefix}result.mp4`)).toBe(false);+ expect(storage.objects.get(outputKey)).toBe("verified-video");+ });++ it.each([+ { operation: "delete" as const, kind: "missing" as const },+ { operation: "delete" as const, kind: "repeated" as const },+ { operation: "duplicate" as const, kind: "missing" as const },+ { operation: "duplicate" as const, kind: "repeated" as const },+ ])(+ "fails $operation safely on a $kind continuation token",+ async ({ operation, kind }) => {+ const database = databaseFixture(+ recording({ type: "desktopMP4", outputKey }),+ );+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ storage.malformedPagination(kind);+ expect(Exit.isFailure(await runVideoOperation(operation))).toBe(true);+ expect(+ storage.requests.filter(({ operation }) => operation === "list"),+ ).toHaveLength(kind === "missing" ? 1 : 2);+ expect(database.rows.has(videoId)).toBe(true);+ if (operation === "delete")+ expect(database.jobs.get(videoId)?.errorCode).toBe("video-deleting");+ else {+ expect(database.rows.has("duplicate-video")).toBe(false);+ expect(storage.objects.has(`${newPrefix}result.mp4`)).toBe(false);+ }+ },+ );++ it("rolls back the deletion fence if the owner changes before the locked read", async () => {+ const database = databaseFixture();+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ database.beforePrepareDelete(async () => {+ const row = database.rows.get(videoId);+ if (!row) throw new Error("Missing video row");+ row.ownerId = "new-owner";+ });+ expect(Exit.isFailure(await runVideoOperation("delete"))).toBe(true);+ expect(database.jobs.get(videoId)?.generation).toBe("active-generation");+ expect(storage.requests).toEqual([]);+ });++ it("retains the video and deletion fence if ownership changes during cloud cleanup", async () => {+ const database = databaseFixture();+ const storage = await storageFixture([[outputKey, "verified-video"]]);+ storage.beforeDelete(() => {+ const row = database.rows.get(videoId);+ if (!row) throw new Error("Missing video row");+ row.ownerId = "new-owner";+ });+ expect(Exit.isFailure(await runVideoOperation("delete"))).toBe(true);+ expect(database.rows.get(videoId)?.ownerId).toBe("new-owner");+ expect(database.jobs.get(videoId)?.errorCode).toBe("video-deleting");+ expect(+ database.mutations.filter(({ operation }) => operation === "delete"),+ ).toEqual([]);+ });++ it("deletes every retained source page after fencing the worker and removes the row last", async () => {+ const database = databaseFixture();+ const retained = Array.from(+ { length: 1100 },+ (_, index) =>+ [+ `${prefix}.recording/sources/generation/snapshot/video/${String(index).padStart(5, "0")}.m4s`,+ "fragment",+ ] as const,+ );+ const storage = await storageFixture([+ ...retained,+ [outputKey, "verified-output"],+ [`${prefix}comments/comment/attachment.mp4`, "comment-attachment"],+ ["other-user/other-video/result.mp4", "unrelated"],+ ]);+ storage.beforeDelete(() => {+ expect(database.rows.has(videoId)).toBe(true);+ expect(database.jobs.get(videoId)?.errorCode).toBe("video-deleting");+ });+ const result = await runVideoOperation("delete");+ expect(Exit.isSuccess(result)).toBe(true);+ expect(database.rows.has(videoId)).toBe(false);+ expect(database.events.slice(0, 3)).toEqual([+ "job-fence-request",+ "job-delete-request",+ "job-delete-complete",+ ]);+ expect(storage.objects).toEqual(+ new Map([["other-user/other-video/result.mp4", "unrelated"]]),+ );+ expect(+ storage.requests.filter(({ operation }) => operation === "list"),+ ).toHaveLength(2);+ const deleted = storage.requests.filter(+ ({ operation }) => operation === "delete",+ );+ expect(deleted.map(({ keys }) => keys?.length)).toEqual([1000, 102]);+ });++ it.each([+ { suffix: "segments/manifest.json", status: 403 },+ { suffix: "segments/manifest.json", status: 503 },+ {+ suffix: ".recording/sources/generation/snapshot/pages/0.json",+ status: 403,+ },+ {+ suffix: ".recording/sources/generation/snapshot/pages/0.json",+ status: 503,+ },+ ])(+ "preserves a $status Drive read failure for $suffix without recovery or index changes",+ async ({ suffix, status }) => {+ const drive = await driveFixture();+ const original = drive.records.get(outputKey);+ if (!original) throw new Error("Missing indexed Drive object");+ const key = `${prefix}${suffix}`;+ drive.records.set(key, { ...original, objectKey: key });+ const error = new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(status, "fixture read failure"),+ });+ mocks.driveText.mockReturnValue(Effect.fail(error));+ const result = await Effect.runPromiseExit(drive.access.getObject(key));+ if (Exit.isSuccess(result))+ throw new Error("Transient Drive failure was not propagated");+ expect(Option.getOrThrow(Cause.failureOption(result.cause))).toBe(error);+ expect(drive.records.get(key)?.providerObjectId).toBe("original-file");+ expect(mocks.driveFind).not.toHaveBeenCalled();- expect(drive.upsertIndex).not.toHaveBeenCalled();++ expect(drive.updateIndex).not.toHaveBeenCalled();+ },+ );++ it.each([403, 503])(+ "does not replace the Drive index after a %s metadata failure",+ async (status) => {+ const drive = await driveFixture();+ const error = new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(status, "fixture metadata failure"),+ });+ mocks.driveMetadata.mockReturnValue(Effect.fail(error));+ const result = await Effect.runPromiseExit(+ drive.access.headObject(outputKey),+ );+ if (Exit.isSuccess(result))+ throw new Error("Transient Drive metadata failure was not propagated");+ expect(Option.getOrThrow(Cause.failureOption(result.cause))).toBe(error);+ expect(mocks.driveFind).not.toHaveBeenCalled();+ expect(drive.records.get(outputKey)?.providerObjectId).toBe(+ "original-file",+ );+ },+ );++ it("keeps network and database read errors distinct from missing Drive objects", async () => {+ const drive = await driveFixture();+ const networkError = new StorageDomain.StorageError({+ cause: new Error("Network read unavailable"),+ });+ mocks.driveText.mockReturnValue(Effect.fail(networkError));+ const networkResult = await Effect.runPromiseExit(+ drive.access.getObject(outputKey),+ );+ if (Exit.isSuccess(networkResult))+ throw new Error("Network failure was not propagated");+ expect(Option.getOrThrow(Cause.failureOption(networkResult.cause))).toBe(+ networkError,+ );+ const databaseError = new DatabaseError({+ cause: new Error("Index read unavailable"),+ });+ drive.getIndex.mockReturnValue(Effect.fail(databaseError));+ const databaseResult = await Effect.runPromiseExit(+ drive.access.getObject(outputKey),+ );+ if (Exit.isSuccess(databaseResult))+ throw new Error("Database failure was not propagated");+ expect(+ Option.getOrThrow(Cause.failureOption(databaseResult.cause)).cause,+ ).toBe(databaseError);+ expect(mocks.driveFind).not.toHaveBeenCalled();- expect(drive.upsertIndex).not.toHaveBeenCalled();++ expect(drive.updateIndex).not.toHaveBeenCalled();+ });++ it("returns none for a genuinely missing Drive file with no replacement", async () => {+ const drive = await driveFixture();+ mocks.driveText.mockReturnValue(+ Effect.fail(+ new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(404, "Missing file"),+ }),+ ),+ );+ expect(await Effect.runPromise(drive.access.getObject(outputKey))).toEqual(+ Option.none(),+ );+ expect(mocks.driveFind).toHaveBeenCalledTimes(1);+ expect(drive.records.get(outputKey)?.providerObjectId).toBe(+ "original-file",+ );- expect(drive.upsertIndex).not.toHaveBeenCalled();++ expect(drive.updateIndex).not.toHaveBeenCalled();+ });++ it("returns none for a missing index without requesting or recovering a Drive file", async () => {+ const drive = await driveFixture();+ drive.records.delete(outputKey);+ expect(await Effect.runPromise(drive.access.getObject(outputKey))).toEqual(+ Option.none(),+ );+ expect(mocks.driveText).not.toHaveBeenCalled();+ expect(mocks.driveFind).not.toHaveBeenCalled();+ });++ it.each(["index", "provider"])(+ "returns a typed missing-object error from Drive HEAD for an absent %s object",+ async (origin) => {+ const drive = await driveFixture();+ if (origin === "index") {+ drive.records.delete(outputKey);+ } else {+ mocks.driveMetadata.mockReturnValue(+ Effect.fail(+ new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(404, "Missing file"),+ }),+ ),+ );+ }+ const result = await Effect.runPromiseExit(+ drive.access.headObject(outputKey),+ );+ if (Exit.isSuccess(result))+ throw new Error("Missing Drive metadata was not reported");+ const error = Option.getOrThrow(Cause.failureOption(result.cause));+ const cause =+ error.cause instanceof StorageDomain.StorageError+ ? error.cause.cause+ : error.cause;+ expect(cause).toBeInstanceOf(GoogleDriveRequestError);+ expect(cause).toHaveProperty("status", 404);+ expect(mocks.driveFind).toHaveBeenCalledTimes(origin === "index" ? 0 : 1);- expect(drive.upsertIndex).not.toHaveBeenCalled();++ expect(drive.updateIndex).not.toHaveBeenCalled();+ },+ );++ it("recovers an indexed 404 to the replacement file before returning its text", async () => {+ const drive = await driveFixture();++ const original = drive.records.get(outputKey);++ if (!original) throw new Error("Missing indexed Drive object");++ const metadata = { fileName: "Original recording.mp4", videoId };++ drive.records.set(outputKey, { ...original, metadata });+ const text = '{"source":"retained"}';+ mocks.driveText+ .mockReturnValueOnce(+ Effect.fail(+ new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(404, "Missing indexed file"),+ }),+ ),+ )+ .mockReturnValueOnce(Effect.succeed(text));+ mocks.driveFind.mockReturnValue(+ Effect.succeed(+ Option.some({+ id: "recovered-file",++ name: "Stale Drive name.mp4",+ size: String(text.length),+ mimeType: "application/json",+ }),+ ),+ );+ expect(await Effect.runPromise(drive.access.getObject(outputKey))).toEqual(+ Option.some(text),+ );+ expect(mocks.driveText.mock.calls.map((args) => args[1])).toEqual([+ "original-file",+ "recovered-file",+ ]);+ expect(drive.records.get(outputKey)?.providerObjectId).toBe(+ "recovered-file",+ );- expect(drive.upsertIndex).toHaveBeenCalledTimes(1);++ expect(drive.records.get(outputKey)?.metadata).toEqual(metadata);++ expect(drive.updateIndex).toHaveBeenCalledTimes(1);+ });++ it("propagates a provider failure while looking for a replacement of an indexed 404", async () => {+ const drive = await driveFixture();+ mocks.driveText.mockReturnValue(+ Effect.fail(+ new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(404, "Missing indexed file"),+ }),+ ),+ );+ const error = new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(503, "Recovery unavailable"),+ });+ mocks.driveFind.mockReturnValue(Effect.fail(error));+ const result = await Effect.runPromiseExit(+ drive.access.getObject(outputKey),+ );+ if (Exit.isSuccess(result))+ throw new Error("Recovery failure was not propagated");+ expect(Option.getOrThrow(Cause.failureOption(result.cause))).toBe(error);+ expect(drive.records.get(outputKey)?.providerObjectId).toBe(+ "original-file",+ );- expect(drive.upsertIndex).not.toHaveBeenCalled();++ expect(drive.updateIndex).not.toHaveBeenCalled();+ });++ it.each([403, 503])(+ "preserves the Drive object index when provider deletion returns %s",+ async (status) => {+ const drive = await driveFixture();+ mocks.driveDelete.mockReturnValue(+ Effect.fail(+ new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(+ status,+ "fixture provider failure",+ ),+ }),+ ),+ );+ const result = await Effect.runPromiseExit(+ drive.access.deleteObjects([{ Key: outputKey }]),+ );+ expect(Exit.isFailure(result)).toBe(true);+ expect(drive.records.get(outputKey)?.providerObjectId).toBe(+ "original-file",+ );+ expect(drive.deleteIndex).not.toHaveBeenCalled();+ },+ );++ it("clears a genuinely missing Drive object and makes repeated deletion idempotent", async () => {+ const drive = await driveFixture();+ mocks.driveDelete.mockReturnValue(+ Effect.fail(+ new StorageDomain.StorageError({+ cause: new GoogleDriveRequestError(404, "missing fixture file"),+ }),+ ),+ );+ await Effect.runPromise(drive.access.deleteObject(outputKey));+ await Effect.runPromise(drive.access.deleteObject(outputKey));+ expect(drive.records.has(outputKey)).toBe(false);+ expect(drive.deleteIndex).toHaveBeenCalledExactlyOnceWith(+ drive.integrationId,+ outputKey,++ "original-file",+ );+ expect(mocks.driveDelete).toHaveBeenCalledTimes(1);+ });++ it("retains the Drive index until the provider confirms deletion", async () => {+ const drive = await driveFixture();+ let release: (() => void) | undefined;+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });+ mocks.driveDelete.mockReturnValue(Effect.promise(() => gate));+ const pending = Effect.runPromise(drive.access.deleteObject(outputKey));+ await vi.waitFor(() => expect(mocks.driveDelete).toHaveBeenCalledTimes(1));+ expect(drive.records.has(outputKey)).toBe(true);+ expect(drive.deleteIndex).not.toHaveBeenCalled();+ if (!release) throw new Error("Missing Drive delete resolver");+ release();+ await pending;+ expect(drive.records.has(outputKey)).toBe(false);+ });+++ it.each([204, 404])(++ "preserves a concurrent Drive remapping when deleting the old file returns %s",++ async (status) => {++ const drive = await driveFixture();++ const original = drive.records.get(outputKey);++ if (!original) throw new Error("Missing indexed Drive object");++ let release: (() => void) | undefined;++ const gate = new Promise<void>((resolve) => {++ release = resolve;++ });++ mocks.driveDelete.mockReturnValue(++ Effect.promise(() => gate).pipe(++ Effect.zipRight(++ status === 404++ ? Effect.fail(++ new StorageDomain.StorageError({++ cause: new GoogleDriveRequestError(404, "Old file missing"),++ }),++ )++ : Effect.void,++ ),++ ),++ );++ const pending = Effect.runPromise(drive.access.deleteObject(outputKey));++ await vi.waitFor(() =>++ expect(mocks.driveDelete).toHaveBeenCalledTimes(1),++ );++ const replacement = {++ ...original,++ providerObjectId: "new-pending-file",++ uploadStatus: "pending" as const,++ };++ drive.records.set(outputKey, replacement);++ if (!release) throw new Error("Missing Drive delete resolver");++ release();++ await pending;++ expect(drive.records.get(outputKey)).toEqual(replacement);++ expect(mocks.driveDelete).toHaveBeenCalledWith(++ expect.anything(),++ "original-file",++ expect.anything(),++ );++ expect(drive.deleteIndex).toHaveBeenCalledExactlyOnceWith(++ drive.integrationId,++ outputKey,++ "original-file",++ );++ expect(drive.deleteConditions).toHaveLength(1);++ const [condition] = drive.deleteConditions;++ if (!condition) throw new Error("Missing Drive index delete condition");++ expect(new MySqlDialect().sqlToQuery(condition).params).toEqual([++ drive.integrationId,++ expect.stringMatching(/^[a-f0-9]{64}$/),++ "original-file",++ ]);++ },++ );+++ it("copies the published Drive object and checks both file identities after native copy", async () => {+ const drive = await driveFixture();+ await Effect.runPromise(+ drive.access.copyObjectForRecording(+ `google-drive/${prefix}result.mp4`,+ `${newPrefix}result.mp4`,+ ),+ );+ expect(mocks.driveCopy).toHaveBeenCalledWith(+ expect.objectContaining({+ sourceFileId: "original-file",+ input: expect.objectContaining({+ key: `${newPrefix}result.mp4`,+ contentType: "video/mp4",+ }),+ }),+ );+ expect(mocks.driveMetadata.mock.calls.map((args) => args[1])).toEqual([+ "original-file",+ "original-file",+ "copied-file",+ ]);+ expect(drive.records.get(`${newPrefix}result.mp4`)?.providerObjectId).toBe(+ "copied-file",+ );+ });++ it.each(["source-version", "destination-size"] as const)(+ "rejects a Drive copy when %s changes during readback",+ async (change) => {+ const drive = await driveFixture();+ const nativeCopy = mocks.driveCopy.getMockImplementation();+ if (!nativeCopy) throw new Error("Missing native Drive copy fixture");+ mocks.driveCopy.mockImplementation(+ (input: Parameters<typeof copyGoogleDriveFile>[0]) =>+ nativeCopy(input).pipe(+ Effect.tap(() =>+ Effect.sync(() => {+ if (change === "source-version") {+ const original = drive.files.get("original-file");+ if (!original)+ throw new Error("Missing original Drive fixture");+ original.version = "2";+ } else {+ const destination = drive.files.get("copied-file");+ if (!destination)+ throw new Error("Missing copied Drive fixture");+ destination.size = "9";+ }+ }),+ ),+ ),+ );+ const result = await Effect.runPromiseExit(+ drive.access.copyObjectForRecording(+ `google-drive/${outputKey}`,+ `${newPrefix}result.mp4`,+ ),+ );+ expect(Exit.isFailure(result)).toBe(true);+ expect(drive.records.has(outputKey)).toBe(true);+ },+ );+});diff --cc packages/web-backend/src/Storage/index.tsindex e9c17e6e8,b70381d0a..ca6d24328--- a/packages/web-backend/src/Storage/index.ts+++ b/packages/web-backend/src/Storage/index.ts@@@ -25,8 -24,8 +25,9 @@@ importgetGoogleDriveObjectResponse,getGoogleDriveObjectText,parseVideoIdFromObjectKey,+ syncGoogleDriveVideoNames,} from "./GoogleDrive.ts";+import { resolveRecordingObjectKey } from "./recording-output.ts";import { createStorageObjectToken } from "./SignedObject.ts";import type { GoogleDriveIntegrationConfig } from "./StorageRepo.ts";import { StorageRepo } from "./StorageRepo.ts";@@@ -582,128 -421,10 +605,134 @@@ const makeGoogleDriveAccess = (read: (fileId: string) => Effect.Effect<A, StorageDomain.StorageError>,) =>read(object.providerObjectId).pipe(- Effect.catchTag("StorageError", () =>- recoverDriveFileId(key, object).pipe(Effect.flatMap(read)),+ Effect.catchTag("StorageError", (error) =>+ isMissingDriveObject(error)+ ? recoverDriveFileId(key, object).pipe(Effect.flatMap(read))+ : Effect.fail(error),+ ),+ );+ const copyObject = (+ source: string,+ key: string,+ args?: Omit<S3.CopyObjectCommandInput, "Bucket" | "CopySource" | "Key">,+ ) =>+ getObjectRecord(parseSourceKey(source)).pipe(+ Effect.flatMap((sourceObject) =>+ copyGoogleDriveFile({+ repo,+ config,+ sourceFileId: sourceObject.providerObjectId,+ input: {+ integrationId,+ ownerId,+ videoId: parseObjectKeyVideoId(key),+ key,+ contentType:+ args?.ContentType ??+ sourceObject.contentType ??+ "application/octet-stream",+ },+ tokenStore,+ }).pipe(mapStorageError),),);+ const deleteObject = (key: string) =>+ Effect.gen(function* () {+ const stored = yield* mapStorageError(+ repo.getObjectByKey(integrationId, key),+ );+ if (Option.isNone(stored)) return;+ yield* deleteGoogleDriveFile(+ config,+ stored.value.providerObjectId,+ tokenStore,+ ).pipe(+ Effect.catchAll((error) =>+ isMissingDriveObject(error) ? Effect.void : Effect.fail(error),+ ),+ );- yield* mapStorageError(repo.deleteObjectByKey(integrationId, key));++ yield* mapStorageError(++ repo.deleteObjectByKey(++ integrationId,++ key,++ stored.value.providerObjectId,++ ),++ );+ });+ const copyObjectForRecording = (source: string, key: string) =>+ Effect.gen(function* () {+ const sourceKey = parseSourceKey(source);+ if (!source.startsWith("google-drive/") || sourceKey === key) {+ return yield* Effect.fail(+ new StorageDomain.StorageError({+ cause: new Error(+ "Recording copy requires a distinct object in the same storage integration",+ ),+ }),+ );+ }+ const sourceObject = yield* getObjectRecord(sourceKey);+ const before = yield* withRecoveredDriveFile(+ sourceKey,+ sourceObject,+ (fileId) => getGoogleDriveFileMetadata(config, fileId, tokenStore),+ );+ const { fileSize } = yield* recordingCopyMetadata({+ ContentLength: parseGoogleDriveContentLength(before) ?? undefined,+ ETag:+ before.id && before.version+ ? `"${before.id}:${before.version}"`+ : undefined,+ });+ yield* copyGoogleDriveFile({+ repo,+ config,+ sourceFileId: before.id,+ input: {+ integrationId,+ ownerId,+ videoId: parseObjectKeyVideoId(key),+ key,+ contentType:+ before.mimeType ??+ sourceObject.contentType ??+ "application/octet-stream",+ },+ tokenStore,+ }).pipe(mapStorageError);+ const currentSource = yield* getObjectRecord(sourceKey);+ const destination = yield* getObjectRecord(key);+ const [after, copied] = yield* Effect.all([+ getGoogleDriveFileMetadata(config, before.id, tokenStore),+ getGoogleDriveFileMetadata(+ config,+ destination.providerObjectId,+ tokenStore,+ ),+ ]);+ yield* recordingCopyMetadata({+ ContentLength: parseGoogleDriveContentLength(copied) ?? undefined,+ ETag:+ copied.id && copied.version+ ? `"${copied.id}:${copied.version}"`+ : undefined,+ });+ if (+ currentSource.providerObjectId !== before.id ||+ after.id !== before.id ||+ after.version !== before.version ||+ parseGoogleDriveContentLength(after) !== fileSize ||+ parseGoogleDriveContentLength(copied) !== fileSize+ ) {+ return yield* Effect.fail(+ new StorageDomain.StorageError({+ cause: new Error(+ "Recording copy changed during transfer or failed readback",+ ),+ }),+ );+ }+ });const multipart: MultipartAccess = {create: (@@@ -1236,13 -1013,12 +1318,17 @@@ export class Storage extends Effect.SerEffect.flatMap(Storage, (storage) =>storage.getOrganizationWritableAccess(organizationId),);- static getAccessForVideo = (video: Video.Video) =>- Effect.flatMap(Storage, (storage) => storage.getAccessForVideo(video));+ static getAccessForVideo = (+ video: Video.Video,+ options?: { resolvePublishedOutput?: boolean },+ ) =>+ Effect.flatMap(Storage, (storage) =>+ storage.getAccessForVideo(video, options),+ );+ static syncVideoDisplayNames = (videoId: Video.VideoId) =>+ Effect.flatMap(Storage, (storage) =>+ storage.syncVideoDisplayNames(videoId),+ );static createUploadTargetForUser = (userId: User.UserId,key: string,