Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

34.9 KB
Raw Blame
import type { User, Video } from "@cap/web-domain";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
	attachRemoteJob,
	claimProcessingAttempt,
	type DesktopRecordingAttemptFence,
	type DesktopRecordingJob,
	ensureSegmentProcessingJob,
	getDesktopRecordingRetryDelay,
	getDesktopRecordingWorkerCheckpoint,
	heartbeatAttempt,
	initializeSourceCommitCheckpoint,
	isDesktopRecordingJobRecoverable,
	listRecoverableSegmentJobs,
	markSourceBlocked,
	persistCommittedSource,
	persistSourceCommitCheckpoint,
	retireDesktopRecordingJobForOutputReplacement,
	retryBlockedDesktopRecording,
	scheduleRetry,
	waitForDesktopRecordingCapacity,
} from "@/lib/desktop-recording-jobs";
import type {
	RecordingUploadReceipt,
	RecordingVerification,
} from "@/lib/desktop-recording-verification";

const mocks = vi.hoisted(() => ({ db: vi.fn() }));
vi.mock("@cap/database", () => ({ db: mocks.db }));
vi.mock("@cap/database/schema", () => {
	const table = (name: string, fields: string[]) =>
		Object.fromEntries([
			["table", name],
			...fields.map((field) => [field, `${name}.${field}`]),
		]);
	return {
		videos: table("videos", ["id", "ownerId", "source"]),
		videoUploads: table("uploads", ["videoId"]),
		mediaProcessingBudgets: table("budgets", ["id"]),
		videoProcessingJobs: table("jobs", [
			"videoId",
			"ownerId",
			"generation",
			"state",
			"attemptId",
			"attemptCount",
			"source",
			"nextRetryAt",
			"leaseExpiresAt",
			"remoteJobId",
			"errorCode",
			"verification",
			"output",
		]),
	};
});
vi.mock("drizzle-orm", () => ({
	and: (...args: unknown[]) => ({ op: "and", args }),
	or: (...args: unknown[]) => ({ op: "or", args }),
	eq: (column: string, value: unknown) => ({ op: "eq", column, value }),
	ne: (column: string, value: unknown) => ({ op: "ne", column, value }),
	lte: (column: string, value: unknown) => ({ op: "lte", column, value }),
	gt: (column: string, value: unknown) => ({ op: "gt", column, value }),
	inArray: (column: string, value: unknown[]) => ({ op: "in", column, value }),
	isNull: (column: string) => ({ op: "null", column }),
	asc: (column: string) => column,
	getTableColumns: (table: Record<string, string>) => table,
}));

type Row = Record<string, unknown>;
type Table = { table: string };
type Condition = {
	op: string;
	args?: (Condition | undefined)[];
	column?: string;
	value?: unknown;
};

const videoId = "video" as Video.VideoId;
const userId = "user" as User.UserId;
const now = new Date("2026-09-02T12:00:00.000Z");
const manifestSha256 = "a".repeat(64);
const source = {
	version: 1 as const,
	kind: "segments" as const,
	manifestSha256,
	inventorySha256: "b".repeat(64),
	inventoryKey: "user/video/.recording/sources/generation/inventory.json",
	requiredAudio: true,
};
const verification: RecordingVerification = {
	version: 1,
	artifact: { kind: "segments", manifestSha256 },
	requiredAudio: true,
};

let rows: Record<string, Row[]>;
let lockingOperations: string[];

function getJobRow(): Row {
	const job = rows.jobs?.[0];
	if (!job) throw new Error("Missing job fixture");
	return job;
}

function matches(row: Row, condition?: Condition): boolean {
	if (!condition) return true;
	if (condition.op === "and")
		return (condition.args ?? []).every((part) => matches(row, part));
	if (condition.op === "or")
		return (condition.args ?? []).some((part) => matches(row, part));
	const value = row[condition.column?.split(".")[1] ?? ""];
	if (condition.op === "eq") return value === condition.value;
	if (condition.op === "ne") return value !== condition.value;
	if (condition.op === "null") return value === null || value === undefined;
	if (condition.op === "in")
		return Array.isArray(condition.value) && condition.value.includes(value);
	if (condition.op === "lte") return Number(value) <= Number(condition.value);
	if (condition.op === "gt") return Number(value) > Number(condition.value);
	throw new Error(`Unexpected condition ${condition.op}`);
}

function createClient() {
	const client = {
		select(fields?: Record<string, string>) {
			let table = "";
			let condition: Condition | undefined;
			const run = () =>
				(rows[table] ?? [])
					.filter((row) => matches(row, condition))
					.map((row) => {
						if (!fields) return { ...row };
						return Object.fromEntries(
							Object.entries(fields).map(([name, column]) => [
								name,
								row[column.split(".")[1] ?? ""],
							]),
						);
					});
			const query = {
				from(value: Table) {
					table = value.table;
					return query;
				},
				innerJoin() {
					return query;
				},
				orderBy() {
					return query;
				},
				where(value: Condition) {
					condition = value;
					return query;
				},
				for: async () => {
					lockingOperations.push(`select:${table}`);
					return run();
				},
				limit: async (limit: number) => run().slice(0, limit),
			};
			return query;
		},
		update(table: Table) {
			return {
				set(values: Row) {
					return {
						where: async (condition: Condition) => {
							const matching = (rows[table.table] ?? []).filter((row) =>
								matches(row, condition),
							);
							for (const row of matching) Object.assign(row, values);
							return [{ affectedRows: matching.length }];
						},
					};
				},
			};
		},
		delete(table: Table) {
			return {
				where: async (condition: Condition) => {
					const before = rows[table.table] ?? [];
					rows[table.table] = before.filter((row) => !matches(row, condition));
					return [
						{ affectedRows: before.length - (rows[table.table]?.length ?? 0) },
					];
				},
			};
		},
		insert(table: Table) {
			return {
				values(values: Row) {
					const run = async (update?: Row) => {
						lockingOperations.push(
							`${update ? "upsert" : "insert"}:${table.table}`,
						);
						const stored = (rows[table.table] ?? []).find(
							(row) => row.videoId === values.videoId,
						);
						if (stored) {
							if (!update) throw new Error("Duplicate row");
							Object.assign(stored, update);
						} else {
							rows[table.table]?.push({ ...values });
						}
						return [{ affectedRows: 1 }];
					};
					let updates: Row | undefined;
					const pending = Promise.resolve().then(() => run(updates));
					return Object.assign(pending, {
						onDuplicateKeyUpdate: ({ set }: { set: Row }) => {
							updates = set;
							return pending;
						},
					});
				},
			};
		},
	};
	let tail = Promise.resolve();
	return {
		...client,
		async transaction(callback: (tx: typeof client) => Promise<unknown>) {
			const previous = tail;
			let release = () => {};
			tail = new Promise<void>((resolve) => {
				release = resolve;
			});
			await previous;
			const before = structuredClone(rows);
			try {
				return await callback(client);
			} catch (error) {
				rows = before;
				throw error;
			} finally {
				release();
			}
		},
	};
}

beforeEach(() => {
	vi.useFakeTimers({ toFake: ["Date"] });
	vi.setSystemTime(now);
	lockingOperations = [];
	rows = {
		videos: [
			{ id: videoId, ownerId: userId, source: { type: "desktopSegments" } },
		],
		jobs: [],
		uploads: [],
		budgets: [],
	};
	mocks.db.mockReturnValue(createClient());
});

afterEach(() => vi.useRealTimers());

async function createAttempt(withVerification = true) {
	const { job } = await ensureSegmentProcessingJob({
		videoId,
		userId,
		verification: withVerification ? verification : undefined,
	});
	const attempt = await claimProcessingAttempt({
		videoId,
		generation: job.generation,
	});
	if (!attempt) throw new Error("Fixture attempt was not claimed");
	return attempt;
}

describe("durable recording job ownership", () => {
	it("establishes the job row before taking locking reads to prevent concurrent first-insert deadlocks", async () => {
		await ensureSegmentProcessingJob({ videoId, userId });
		expect(lockingOperations.slice(0, 3)).toEqual([
			"upsert:jobs",
			"select:jobs",
			"select:videos",
		]);
	});

	it("creates one logical generation for concurrent completion requests", async () => {
		const results = await Promise.all([
			ensureSegmentProcessingJob({ videoId, userId, verification }),
			ensureSegmentProcessingJob({ videoId, userId, verification }),
		]);
		expect(results.filter((result) => result.created)).toHaveLength(1);
		expect(new Set(results.map((result) => result.job.generation)).size).toBe(
			1,
		);
		expect(rows.jobs).toHaveLength(1);
	});

	it("claims only one attempt when two workflows wake together", async () => {
		const { job } = await ensureSegmentProcessingJob({ videoId, userId });
		const results = await Promise.all([
			claimProcessingAttempt({ videoId, generation: job.generation }),
			claimProcessingAttempt({ videoId, generation: job.generation }),
		]);
		expect(results.filter(Boolean)).toHaveLength(1);
		expect(rows.jobs?.[0]?.attemptCount).toBe(1);
	});

	const stepIds = (attemptCount: number) => `step-attempt-${attemptCount}`;

	it("returns a live attempt to the caller that named it", async () => {
		const { job } = await ensureSegmentProcessingJob({ videoId, userId });
		const first = await claimProcessingAttempt({
			videoId,
			generation: job.generation,
			attemptIdFor: stepIds,
		});
		expect(first).toMatchObject({
			attemptId: "step-attempt-1",
			attemptCount: 1,
		});
		const output = {
			kind: "desktop-recording-source-commit",
			revision: 3,
		};
		Object.assign(rows.jobs?.[0] ?? {}, { output });
		const resumed = await claimProcessingAttempt({
			videoId,
			generation: job.generation,
			attemptIdFor: stepIds,
		});
		expect(resumed).toMatchObject({
			attemptId: "step-attempt-1",
			attemptCount: 1,
			output,
		});
		expect(
			await claimProcessingAttempt({
				videoId,
				generation: job.generation,
				attemptIdFor: (attemptCount) => `other-step-${attemptCount}`,
			}),
		).toBeNull();
		expect(
			await claimProcessingAttempt({ videoId, generation: job.generation }),
		).toBeNull();
		expect(rows.jobs?.[0]?.attemptCount).toBe(1);
	});

	it("names a replacement for an expired attempt so a later retry still recognises it", async () => {
		const { job } = await ensureSegmentProcessingJob({ videoId, userId });
		await claimProcessingAttempt({
			videoId,
			generation: job.generation,
			attemptIdFor: stepIds,
		});
		vi.setSystemTime(new Date(now.getTime() + 6 * 60_000));
		const replacement = await claimProcessingAttempt({
			videoId,
			generation: job.generation,
			attemptIdFor: stepIds,
		});
		expect(replacement).toMatchObject({
			attemptId: "step-attempt-2",
			attemptCount: 2,
		});
		const retried = await claimProcessingAttempt({
			videoId,
			generation: job.generation,
			attemptIdFor: stepIds,
		});
		expect(retried).toMatchObject({
			attemptId: "step-attempt-2",
			attemptCount: 2,
		});
		expect(rows.jobs?.[0]?.attemptCount).toBe(2);
	});

	it("does not resume a finished attempt under its old id", async () => {
		const { job } = await ensureSegmentProcessingJob({ videoId, userId });
		const first = await claimProcessingAttempt({
			videoId,
			generation: job.generation,
			attemptIdFor: stepIds,
		});
		if (!first) throw new Error("Fixture attempt was not claimed");
		Object.assign(rows.jobs?.[0] ?? {}, { state: "verified" });
		expect(
			await claimProcessingAttempt({
				videoId,
				generation: job.generation,
				attemptIdFor: stepIds,
			}),
		).toBeNull();
	});

	it("keeps physical worker leases private and clears their checkpoint for a new attempt", async () => {
		const first = await createAttempt();
		expect(await persistCommittedSource(first, source)).toBe(true);
		const row = rows.jobs?.[0];
		if (!row) throw new Error("Missing processing job");
		Object.assign(row, {
			remoteJobId: "physical-worker",
			output: {
				version: 1,
				kind: "recording-worker",
				generation: first.generation,
				attemptId: first.attemptId,
				jobId: "physical-worker",
				sequence: 5,
				phase: "processing",
				progress: 60,
				payloadSha256: "d".repeat(64),
				updatedAt: now.toISOString(),
				stateChangedAt: now.toISOString(),
			},
		});
		expect(await heartbeatAttempt(first)).toBe(false);
		vi.setSystemTime(new Date(now.getTime() + 6 * 60_000));
		const next = await claimProcessingAttempt({
			videoId,
			generation: first.generation,
		});
		expect(next).toMatchObject({ source, remoteJobId: null, output: null });
		expect(next?.attemptId).not.toBe(first.attemptId);
	});

	it("rejects a worker checkpoint transplanted from another physical attempt", async () => {
		const first = await createAttempt();
		expect(() =>
			getDesktopRecordingWorkerCheckpoint({
				...first,
				remoteJobId: "current-worker",
				output: {
					version: 1,
					kind: "recording-worker",
					generation: first.generation,
					attemptId: first.attemptId,
					jobId: "other-worker",
					sequence: 0,
					phase: "queued",
					progress: 0,
					payloadSha256: "d".repeat(64),
					updatedAt: now.toISOString(),
					stateChangedAt: now.toISOString(),
				},
			}),
		).toThrow("does not match its owner");
	});

	it("rejects stale attempt heartbeats, remote jobs, and errors after a retry takes over", async () => {
		const old = await createAttempt();
		vi.setSystemTime(new Date(now.getTime() + 6 * 60_000));
		const next = await claimProcessingAttempt({
			videoId,
			generation: old.generation,
		});
		expect(next?.attemptId).not.toBe(old.attemptId);
		expect(await heartbeatAttempt(old)).toBe(false);
		expect(await attachRemoteJob({ ...old, remoteJobId: "stale-worker" })).toBe(
			false,
		);
		expect(
			await scheduleRetry({
				...old,
				errorCode: "stale-error",
				errorMessage: "old callback",
			}),
		).toBe(false);
		expect(rows.jobs?.[0]?.attemptId).toBe(next?.attemptId);
	});

	it("does not mutate an upload owned by another account", async () => {
		await expect(
			ensureSegmentProcessingJob({ videoId, userId: "other" as User.UserId }),
		).rejects.toThrow("does not exist");
		expect(rows.jobs).toEqual([]);
	});

	it("does not revive an expired attempt before another worker has claimed it", async () => {
		const expired = await createAttempt();
		const checkpoint = await initializeSourceCommitCheckpoint(expired);
		if (!checkpoint) throw new Error("Missing checkpoint");
		vi.setSystemTime(new Date(now.getTime() + 6 * 60_000));
		expect(await heartbeatAttempt(expired)).toBe(false);
		expect(
			await attachRemoteJob({ ...expired, remoteJobId: "late-worker" }),
		).toBe(false);
		expect(await persistCommittedSource(expired, source)).toBe(false);
		expect(
			await persistSourceCommitCheckpoint(expired, {
				...checkpoint,
				revision: 1,
			}),
		).toBe(false);
		expect(rows.jobs?.[0]).toMatchObject({
			attemptId: expired.attemptId,
			source: null,
			remoteJobId: null,
			output: checkpoint,
		});
		const next = await claimProcessingAttempt({
			videoId,
			generation: expired.generation,
		});
		if (!next) throw new Error("Expired attempt was not recoverable");
		expect(next.attemptId).not.toBe(expired.attemptId);
		expect(await initializeSourceCommitCheckpoint(next)).toEqual(checkpoint);
	});
});

describe("late verification and source commitment", () => {
	it("retains bounded source-copy progress across retries and rejects stale checkpoint writes", async () => {
		const first = await createAttempt(false);
		const checkpoint = await initializeSourceCommitCheckpoint(first);
		if (!checkpoint) throw new Error("Missing checkpoint");
		const advanced = {
			...checkpoint,
			revision: 1,
			phase: "enumerate" as const,
		};
		expect(await persistSourceCommitCheckpoint(first, advanced)).toBe(true);
		expect(await persistSourceCommitCheckpoint(first, advanced)).toBe(false);
		await scheduleRetry({
			...first,
			errorCode: "worker-lost",
			errorMessage: "interrupted",
		});
		vi.setSystemTime(new Date(now.getTime() + 60_000));
		const second = await claimProcessingAttempt({
			videoId,
			generation: first.generation,
		});
		if (!second) throw new Error("Missing replacement attempt");
		expect(await initializeSourceCommitCheckpoint(second)).toEqual(advanced);
		expect(
			await persistSourceCommitCheckpoint(first, { ...advanced, revision: 2 }),
		).toBe(false);
		expect(
			await persistSourceCommitCheckpoint(second, { ...advanced, revision: 2 }),
		).toBe(true);
		const late = await ensureSegmentProcessingJob({
			videoId,
			userId,
			verification,
		});
		expect(late.job.generation).toBe(first.generation);
		expect(await persistCommittedSource(second, source)).toBe(true);
		expect(rows.jobs?.[0]).toMatchObject({
			source,
			verification,
			output: null,
		});
		expect(
			await persistSourceCommitCheckpoint(second, { ...advanced, revision: 3 }),
		).toBe(false);
	});

	it("starts a fresh uncommitted plan after missing or replaced source objects are repaired", async () => {
		const first = await createAttempt(false);
		const checkpoint = await initializeSourceCommitCheckpoint(first);
		if (!checkpoint) throw new Error("Missing checkpoint");
		await markSourceBlocked({
			...first,
			errorCode: "source-changed",
			errorMessage: "Original object was replaced",
		});
		const reopened = await ensureSegmentProcessingJob({ videoId, userId });
		expect(reopened.job).toMatchObject({
			generation: first.generation,
			source: null,
			output: null,
		});
		const second = await claimProcessingAttempt({
			videoId,
			generation: first.generation,
		});
		if (!second) throw new Error("Missing repaired-source attempt");
		const replacement = await initializeSourceCommitCheckpoint(second);
		expect(replacement?.snapshotId).not.toBe(checkpoint.snapshotId);
		expect(
			await persistSourceCommitCheckpoint(first, {
				...checkpoint,
				revision: 1,
			}),
		).toBe(false);
	});

	it("discards an obsolete checkpoint when a proof changes during a resumed source commit", async () => {
		const attempt = await createAttempt(false);
		await initializeSourceCommitCheckpoint(attempt);
		await ensureSegmentProcessingJob({ videoId, userId, verification });
		expect(
			await persistCommittedSource(attempt, {
				...source,
				manifestSha256: "e".repeat(64),
			}),
		).toBe(false);
		expect(rows.jobs?.[0]).toMatchObject({
			source: null,
			output: null,
			state: "retry",
			errorCode: "source-intent-changed",
		});
	});

	it("attaches a matching proof arriving during a legacy snapshot to the same generation", async () => {
		const attempt = await createAttempt(false);
		const attached = await ensureSegmentProcessingJob({
			videoId,
			userId,
			verification,
		});
		expect(attached.created).toBe(false);
		expect(attached.job.generation).toBe(attempt.generation);
		expect(attached.job.attemptId).toBe(attempt.attemptId);
		expect(await persistCommittedSource(attempt, source)).toBe(true);
		expect(rows.jobs?.[0]).toMatchObject({
			generation: attempt.generation,
			state: "processing",
			source,
			verification,
		});
	});

	it("retries the snapshot instead of accepting a copy made before the completed intent changed", async () => {
		const attempt = await createAttempt(false);
		await ensureSegmentProcessingJob({ videoId, userId, verification });
		expect(
			await persistCommittedSource(attempt, {
				...source,
				manifestSha256: "c".repeat(64),
			}),
		).toBe(false);
		expect(rows.jobs?.[0]).toMatchObject({
			generation: attempt.generation,
			source: null,
			state: "retry",
			errorCode: "source-intent-changed",
		});
	});

	it("requires missing audio to be reconciled before committing a late strict segmented request", async () => {
		const attempt = await createAttempt(false);
		await ensureSegmentProcessingJob({ videoId, userId, verification });
		expect(
			await persistCommittedSource(attempt, {
				...source,
				requiredAudio: false,
			}),
		).toBe(false);
		expect(rows.jobs?.[0]?.source).toBeNull();
	});

	it("adds the late strict MP4 duration to an identical already-copied source", async () => {
		const video = rows.videos?.[0];
		if (video) video.source = { type: "desktopMP4" };
		const attempt = await createAttempt(false);
		const request: RecordingVerification = {
			version: 1,
			artifact: {
				kind: "mp4",
				fileSize: 1000,
				duration: 91.6,
				objectIdentity: '"original"',
			},
			requiredAudio: true,
		};
		await ensureSegmentProcessingJob({
			videoId,
			userId,
			verification: request,
		});
		expect(
			await persistCommittedSource(attempt, {
				...source,
				kind: "mp4",
				manifestSha256: undefined,
				requiredAudio: false,
				mp4: { fileSize: 1000, objectIdentity: '"original"' },
			}),
		).toBe(true);
		expect(rows.jobs?.[0]?.source).toMatchObject({
			requiredAudio: true,
			mp4: { fileSize: 1000, duration: 91.6, objectIdentity: '"original"' },
		});
	});

	it("keeps stronger audio verification when an older client repeats a weaker request", async () => {
		const attempt = await createAttempt();
		await persistCommittedSource(attempt, source);
		const weak = { ...verification, requiredAudio: false };
		const result = await ensureSegmentProcessingJob({
			videoId,
			userId,
			verification: weak,
		});
		expect(result.job.verification?.requiredAudio).toBe(true);
		expect(result.job.generation).toBe(attempt.generation);
	});

	it("fences an old attempt when an authenticated completion identifies a new artifact", async () => {
		const old = await createAttempt();
		await persistCommittedSource(old, source);
		const next = await ensureSegmentProcessingJob({
			videoId,
			userId,
			verification: {
				...verification,
				artifact: { kind: "segments", manifestSha256: "f".repeat(64) },
			},
		});
		expect(next.job.generation).not.toBe(old.generation);
		expect(await persistCommittedSource(old, source)).toBe(false);
		expect(next.job.source).toBeNull();
	});
});

describe("operator retry of blocked recordings", () => {
	async function blockedWithSource(errorCode: string) {
		const attempt = await createAttempt();
		await persistCommittedSource(attempt, source);
		Object.assign(getJobRow(), {
			state: "source-blocked",
			leaseExpiresAt: null,
			nextRetryAt: new Date(now.getTime() + 3_600_000),
			attemptCount: 4,
			errorCode,
			errorMessage: "earlier failure",
		});
		rows.uploads = [{ videoId, phase: "error", processingError: "earlier" }];
		const { recordingBudgetKey } = await import(
			"@/lib/media-processing-budget"
		);
		rows.budgets = [
			{
				id: recordingBudgetKey(videoId, attempt.generation),
				reservedBytes: 900,
			},
			{ id: recordingBudgetKey("other-video", "other"), reservedBytes: 1 },
		];
		return attempt;
	}

	it.each([
		"processing-budget-exhausted",
		"source-invalid",
		"processing-retry-exhausted",
	])(
		"releases a %s recording with a committed source back into normal processing",
		async (errorCode) => {
			const attempt = await blockedWithSource(errorCode);
			expect(await retryBlockedDesktopRecording({ videoId })).toEqual({
				status: "queued",
				ownerId: userId,
				previousErrorCode: errorCode,
			});
			expect(getJobRow()).toMatchObject({
				generation: attempt.generation,
				state: "retry",
				source,
				attemptCount: 0,
				leaseExpiresAt: null,
				nextRetryAt: now,
				errorCode: null,
				errorMessage: null,
			});
			expect(rows.budgets?.map((row) => row.reservedBytes)).toEqual([1]);
			expect(rows.uploads?.[0]).toMatchObject({
				phase: "processing",
				processingError: null,
			});
			const next = await claimProcessingAttempt({
				videoId,
				generation: attempt.generation,
			});
			expect(next).toMatchObject({
				state: "processing",
				attemptCount: 1,
				source,
			});
		},
	);

	it.each([
		["a recording without a committed source", { source: null }],
		["an intentionally replaced output", { errorCode: "output-replaced" }],
		["a recording being deleted", { errorCode: "video-deleting" }],
		["a verified recording", { state: "verified", errorCode: null }],
		["a recording still processing", { state: "processing", errorCode: null }],
	])("leaves %s untouched", async (_name, change) => {
		await blockedWithSource("processing-budget-exhausted");
		Object.assign(getJobRow(), change);
		const before = structuredClone(rows);
		expect(await retryBlockedDesktopRecording({ videoId })).toMatchObject({
			status: "not-retryable",
		});
		expect(rows).toEqual(before);
	});

	it("reports a missing job", async () => {
		expect(await retryBlockedDesktopRecording({ videoId })).toEqual({
			status: "not-found",
		});
	});
});

describe("retained-source retry policy", () => {
	it("persists capacity waiting while retaining the attempt and extending its lease past backoff", async () => {
		const attempt = await createAttempt();
		Object.assign(getJobRow(), {
			state: "processing",
			source,
			remoteJobId: null,
			attemptCount: 5,
		});
		expect(
			await waitForDesktopRecordingCapacity({
				...attempt,
				now,
				retryAfterMs: 320_000,
			}),
		).toBe(true);
		expect(getJobRow()).toMatchObject({
			state: "processing",
			source,
			attemptId: attempt.attemptId,
			attemptCount: 5,
			output: { kind: "desktop-recording-capacity-wait" },
			nextRetryAt: new Date(now.getTime() + 320_000),
		});
		expect((getJobRow().leaseExpiresAt as Date).getTime()).toBeGreaterThan(
			now.getTime() + 320_000,
		);
		expect(rows.uploads?.[0]?.processingMessage).toContain(
			"Waiting for a processing slot",
		);
	});

	it.each(["owned", "expired"])(
		"does not overwrite %s work with a capacity wait",
		async (condition) => {
			const attempt = await createAttempt();
			Object.assign(getJobRow(), {
				state: "processing",
				source,
				...(condition === "owned"
					? { remoteJobId: "worker" }
					: { leaseExpiresAt: now }),
			});
			expect(
				await waitForDesktopRecordingCapacity({
					...attempt,
					now,
					retryAfterMs: 30_000,
				}),
			).toBe(false);
		},
	);

	it.each([null, source])(
		"does not recreate a recording while deletion is pending",
		async (retainedSource) => {
			const attempt = await createAttempt();
			const deleting = {
				...attempt,
				generation: "deletion-generation",
				state: "source-blocked" as const,
				source: retainedSource,
				leaseExpiresAt: null,
				nextRetryAt: now,
				errorCode: "video-deleting",
			};
			rows.jobs = [deleting];
			expect(isDesktopRecordingJobRecoverable(deleting, now)).toBe(false);
			expect(await heartbeatAttempt(attempt)).toBe(false);
			await expect(
				ensureSegmentProcessingJob({ videoId, userId, verification }),
			).rejects.toThrow("being deleted");
			expect(rows.jobs?.[0]).toMatchObject({
				generation: "deletion-generation",
				source: retainedSource,
				errorCode: "video-deleting",
			});
		},
	);

	it("retains the source and verified output receipt while fencing workers after an intentional edit", async () => {
		const attempt = await createAttempt();
		await persistCommittedSource(attempt, source);
		const output: RecordingUploadReceipt & { verifiedAt: string } = {
			version: 1,
			artifact: verification.artifact,
			fileSize: 1000,
			duration: 91.6,
			hasAudio: true,
			fullDecode: true,
			requiredAudioVerified: true,
			objectIdentity: '"verified-output"',
			outputKey: `user/video/.recording/outputs/${attempt.generation}/${attempt.attemptId}.mp4`,
			outputSha256: "c".repeat(64),
			sourceProof: {
				version: 1,
				manifestSha256,
				inventorySha256: source.inventorySha256,
				sourcePreserved: true,
				videoDuration: 91.6,
				hasAudio: true,
				audioVerified: true,
			},
			verifiedAt: now.toISOString(),
		};
		rows.jobs = [
			{ ...attempt, source, output, state: "verified", leaseExpiresAt: null },
		];
		await mocks
			.db()
			.transaction(
				(
					tx: Parameters<
						typeof retireDesktopRecordingJobForOutputReplacement
					>[0],
				) =>
					retireDesktopRecordingJobForOutputReplacement(tx, {
						videoId,
						userId,
					}),
			);
		expect(rows.jobs?.[0]).toMatchObject({
			source,
			verification,
			state: "source-blocked",
			errorCode: "output-replaced",
			attemptId: null,
		});
		expect(rows.jobs?.[0]?.output).toEqual(output);
		expect(rows.jobs?.[0]?.generation).not.toBe(attempt.generation);
		expect(await heartbeatAttempt(attempt)).toBe(false);
		await expect(
			ensureSegmentProcessingJob({ videoId, userId, verification }),
		).rejects.toThrow("intentionally edited or replaced");
	});

	it("keeps a replacement tombstone even when the original predated durable jobs", async () => {
		await mocks
			.db()
			.transaction(
				(
					tx: Parameters<
						typeof retireDesktopRecordingJobForOutputReplacement
					>[0],
				) =>
					retireDesktopRecordingJobForOutputReplacement(tx, {
						videoId,
						userId,
					}),
			);
		expect(rows.jobs?.[0]).toMatchObject({
			source: null,
			errorCode: "output-replaced",
		});
		await expect(
			ensureSegmentProcessingJob({ videoId, userId }),
		).rejects.toThrow("intentionally edited or replaced");
	});
	it("keeps worker failures processing without exposing reupload-required errors", async () => {
		const attempt = await createAttempt();
		await persistCommittedSource(attempt, source);
		expect(
			await scheduleRetry({
				...attempt,
				errorCode: "worker-lost",
				errorMessage: "worker restarted",
			}),
		).toBe(true);
		expect(rows.jobs?.[0]).toMatchObject({
			state: "retry",
			source,
			errorCode: "worker-lost",
		});
		expect(rows.uploads?.[0]).toMatchObject({
			phase: "processing",
			processingError: null,
		});
	});

	it("pauses a repeatedly failing recording and retains its source", async () => {
		const attempt = await createAttempt();
		await persistCommittedSource(attempt, source);
		Object.assign(getJobRow(), { attemptCount: 5 });
		expect(
			await scheduleRetry({
				...attempt,
				errorCode: "output-invalid",
				errorMessage: "Timeline mismatch",
			}),
		).toBe(true);
		expect(rows.jobs?.[0]).toMatchObject({
			state: "source-blocked",
			source,
			errorCode: "processing-retry-exhausted",
			errorMessage: "output-invalid: Timeline mismatch",
		});
		expect(rows.uploads?.[0]).toMatchObject({ phase: "error" });
		vi.setSystemTime(new Date(now.getTime() + 24 * 60 * 60_000));
		expect(
			await claimProcessingAttempt({ videoId, generation: attempt.generation }),
		).toBeNull();
		expect(rows.jobs?.[0]?.attemptCount).toBe(5);
	});

	it("pauses an exhausted legacy job before downloading its source again", async () => {
		const attempt = await createAttempt();
		Object.assign(getJobRow(), {
			attemptCount: 220,
			state: "retry",
			leaseExpiresAt: null,
			nextRetryAt: now,
		});
		expect(
			await claimProcessingAttempt({ videoId, generation: attempt.generation }),
		).toBeNull();
		expect(rows.jobs?.[0]).toMatchObject({
			attemptCount: 220,
			errorCode: "processing-retry-exhausted",
		});
		await ensureSegmentProcessingJob({ videoId, userId });
		expect(rows.jobs?.[0]).toMatchObject({
			state: "source-blocked",
			errorCode: "processing-retry-exhausted",
		});
	});

	it("does not interrupt an active final attempt", async () => {
		const attempt = await createAttempt();
		Object.assign(getJobRow(), { attemptCount: 5 });
		expect(
			await claimProcessingAttempt({ videoId, generation: attempt.generation }),
		).toBeNull();
		expect(rows.jobs?.[0]).toMatchObject({
			state: "committing",
			attemptId: attempt.attemptId,
		});
	});

	it("recovers old jobs regardless of recording age or previous attempt count", async () => {
		const attempt = await createAttempt();
		const job: DesktopRecordingJob = {
			...attempt,
			state: "retry",
			source,
			leaseExpiresAt: null,
			attemptCount: 500,
			createdAt: new Date("2020-01-01T00:00:00Z"),
			nextRetryAt: now,
		};
		expect(isDesktopRecordingJobRecoverable(job, now)).toBe(true);
		expect(
			isDesktopRecordingJobRecoverable({ ...job, state: "verified" }, now),
		).toBe(false);
		expect(
			isDesktopRecordingJobRecoverable(
				{ ...job, state: "source-blocked" },
				now,
			),
		).toBe(false);
		expect(
			isDesktopRecordingJobRecoverable(
				{ ...job, state: "source-blocked", source: null },
				now,
			),
		).toBe(true);
	});

	it("caps retry backoff without overflowing after many worker restarts", () => {
		expect(getDesktopRecordingRetryDelay(1)).toBe(15_000);
		expect(getDesktopRecordingRetryDelay(100_000)).toBe(3_600_000);
	});

	it("does not let a different generation attach a processing job", async () => {
		const attempt = await createAttempt();
		const fence: DesktopRecordingAttemptFence = {
			...attempt,
			generation: "other",
		};
		expect(await attachRemoteJob({ ...fence, remoteJobId: "remote" })).toBe(
			false,
		);
	});
});

describe("recovery admission", () => {
	it.each([null, source])(
		"gives a verified re-upload fresh attempts without reviving the old worker: %j",
		async (retainedSource) => {
			const old = await createAttempt();
			rows.jobs = [
				{
					...old,
					state: "source-blocked",
					source: retainedSource,
					attemptCount: 70,
					leaseExpiresAt: null,
					errorCode: "source-reupload-required",
				},
			];
			const resumed = await ensureSegmentProcessingJob({
				videoId,
				userId,
				verification: { ...verification, requiredAudio: false },
			});
			expect(resumed.created).toBe(true);
			expect(resumed.job.generation).not.toBe(old.generation);
			expect(resumed.job).toMatchObject({
				state: "committing",
				attemptCount: 0,
				source: null,
				verification: { requiredAudio: true },
			});
			expect(await heartbeatAttempt(old)).toBe(false);
			expect(
				await claimProcessingAttempt({
					videoId,
					generation: resumed.job.generation,
				}),
			).toMatchObject({ attemptCount: 1 });
			const repeated = await ensureSegmentProcessingJob({
				videoId,
				userId,
				verification,
			});
			expect(repeated.created).toBe(false);
			expect(repeated.job.generation).toBe(resumed.job.generation);
			expect(repeated.job.attemptCount).toBe(1);
		},
	);

	it("keeps an inspected missing source paused until a completion request resumes it", async () => {
		const attempt = await createAttempt();
		const paused = {
			...attempt,
			state: "source-blocked" as const,
			source: null,
			leaseExpiresAt: null,
			errorCode: "source-reupload-required",
			nextRetryAt: now,
		};
		rows.jobs = [paused];
		expect(isDesktopRecordingJobRecoverable(paused, now)).toBe(false);
		expect(await listRecoverableSegmentJobs({ now })).toEqual([]);
		const resumed = await ensureSegmentProcessingJob({
			videoId,
			userId,
			verification,
			now,
		});
		expect(resumed.job).toMatchObject({ state: "committing", errorCode: null });
		expect(isDesktopRecordingJobRecoverable(resumed.job, now)).toBe(true);
	});

	it("prioritizes interrupted new recordings over an older missing-source backlog", async () => {
		await createAttempt();
		const current = {
			...getJobRow(),
			state: "committing",
			source: null,
			leaseExpiresAt: new Date(now.getTime() - 1),
			nextRetryAt: now,
		};
		rows.jobs = Array.from({ length: 30 }, (_, index) => ({
			...current,
			videoId: `old-${index}`,
			state: "source-blocked",
			errorCode: "source-missing",
			nextRetryAt: new Date(now.getTime() - 24 * 60 * 60_000),
		}));
		rows.jobs.push(current);
		const selected = await listRecoverableSegmentJobs({ now, limit: 3 });
		expect(selected[0]?.videoId).toBe(videoId);
		expect(selected).toHaveLength(3);
	});

	it("excludes exhausted and intentionally retired jobs before applying the recovery limit", async () => {
		await createAttempt();
		const current = {
			...getJobRow(),
			state: "retry",
			source: null,
			leaseExpiresAt: null,
			nextRetryAt: now,
		};
		rows.jobs = [
			"processing-retry-exhausted",
			"source-reupload-required",
			"output-replaced",
			"video-deleting",
		].map((errorCode) => ({ ...current, videoId: errorCode, errorCode }));
		rows.jobs.push(current);
		const selected = await listRecoverableSegmentJobs({ now, limit: 1 });
		expect(selected.map((job) => job.videoId)).toEqual([videoId]);
	});
});