Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions apps/web/__tests__/unit/video-processing.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MySqlDialect } from "drizzle-orm/mysql-core";
import { beforeEach, describe, expect, it, vi } from "vitest";

const updateWhereMock = vi.fn();
Expand Down Expand Up @@ -36,12 +37,16 @@ describe("video processing starts", () => {
vi.clearAllMocks();
});

it("does not start a duplicate workflow when processing is already running", async () => {
it.each([
["processing", "already-processing"],
["generating_thumbnail", "already-processing"],
["complete", "already-complete"],
])("does not start a duplicate workflow during %s", async (phase, status) => {
updateWhereMock.mockResolvedValueOnce({ affectedRows: 0 });
selectWhereMock.mockResolvedValueOnce([
{
videoId: "video-123",
phase: "processing",
phase,
rawFileKey: "user-123/video-123/raw-upload.webm",
},
]);
Expand All @@ -59,9 +64,13 @@ describe("video processing starts", () => {
processingMessage: "Starting video processing...",
startFailureMessage: "Video processing could not start.",
}),
).resolves.toBe("already-processing");
).resolves.toBe(status);

expect(startMock).not.toHaveBeenCalled();
const condition = updateWhereMock.mock.calls[0]?.[0];
const query = new MySqlDialect().sqlToQuery(condition);
expect(query.params).toContain(phase);
expect(query.sql).toContain("`video_uploads`.`phase` <> ?");
});

it("starts the workflow after claiming the upload row", async () => {
Expand Down Expand Up @@ -131,6 +140,10 @@ describe("video processing starts", () => {
).resolves.toBe("started");

expect(startMock).toHaveBeenCalledTimes(1);
const condition = updateWhereMock.mock.calls[0]?.[0];
expect(new MySqlDialect().sqlToQuery(condition).params).toEqual([
"video-123",
]);
});

it("marks the upload as errored when workflow start fails", async () => {
Expand Down
6 changes: 5 additions & 1 deletion apps/web/lib/video-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export async function transitionVideoToProcessing({
: and(
eq(videoUploads.videoId, videoId),
ne(videoUploads.phase, "processing"),
ne(videoUploads.phase, "generating_thumbnail"),
// "complete" is terminal for implicit starts: a multipart
// completion retried after an uncertain network outcome can
// land after the first processing run already finished, and
Expand All @@ -89,7 +90,10 @@ export async function transitionVideoToProcessing({
throw new Error("No upload record found");
}

if (upload.phase === "processing") {
if (
upload.phase === "processing" ||
upload.phase === "generating_thumbnail"
) {
return "already-processing";
}

Expand Down
Loading