Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@cap/env", () => ({
serverEnv: vi.fn(() => ({
ASSEMBLY_API_KEY: "test-assembly-api-key",
DATABASE_URL: "mysql://test@localhost/test",
})),
}));
const mockStart = vi.hoisted(() => vi.fn());
const schemaMocks = vi.hoisted(() => ({
videos: {
id: "id",
settings: "settings",
transcriptionStatus: "transcriptionStatus",
},
organizations: { id: "id", settings: "settings" },
s3Buckets: { id: "id" },
videoUploads: { videoId: "videoId", phase: "phase" },
}));
vi.mock("workflow/api", () => ({
start: mockStart,
}));
vi.mock("@/workflows/transcribe", () => ({
transcribeVideoWorkflow: vi.fn(),
}));
let mockQueryResult: unknown[] = [];
let mockUploadQueryResult: unknown[] = [];
let mockUpdateResult: unknown = [{ affectedRows: 1 }];
vi.mock("@cap/database", () => ({
db: () => ({
select: () => ({
from: (table: unknown) => {
if (table === schemaMocks.videoUploads) {
return {
where: vi.fn().mockReturnValue({
limit: vi.fn().mockResolvedValue(mockUploadQueryResult),
}),
};
}
const query = {
leftJoin: vi.fn(() => query),
where: vi
.fn()
.mockImplementation(() => Promise.resolve(mockQueryResult)),
};
return query;
},
}),
update: () => ({
set: () => ({
where: vi.fn().mockResolvedValue(mockUpdateResult),
}),
}),
}),
}));
vi.mock("@cap/database/schema", () => ({
videos: schemaMocks.videos,
organizations: schemaMocks.organizations,
s3Buckets: schemaMocks.s3Buckets,
videoUploads: schemaMocks.videoUploads,
}));
vi.mock("drizzle-orm", () => ({
and: vi.fn((...conditions) => ({ conditions })),
eq: vi.fn((field, value) => ({ field, value })),
isNull: vi.fn((field) => ({ field, operator: "isNull" })),
}));
import type { Video } from "@cap/web-domain";
import { transcribeVideo } from "@/lib/transcribe";
import { transcribeVideoWorkflow } from "@/workflows/transcribe";
describe("transcribeVideo", () => {
beforeEach(() => {
vi.clearAllMocks();
mockQueryResult = [];
mockUploadQueryResult = [];
mockUpdateResult = [{ affectedRows: 1 }];
});
describe("input validation", () => {
it("requires ASSEMBLY_API_KEY environment variable", async () => {
const { serverEnv } = await import("@cap/env");
vi.mocked(serverEnv).mockReturnValueOnce({
ASSEMBLY_API_KEY: undefined,
} as ReturnType<typeof serverEnv>);
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(false);
expect(result.message).toContain("environment variables");
});
it("rejects empty videoId", async () => {
const result = await transcribeVideo("" as Video.VideoId, "user-456");
expect(result.success).toBe(false);
expect(result.message).toBe("userId or videoId not supplied");
});
it("rejects empty userId", async () => {
const result = await transcribeVideo("video-123" as Video.VideoId, "");
expect(result.success).toBe(false);
expect(result.message).toBe("userId or videoId not supplied");
});
it("rejects when both videoId and userId are empty", async () => {
const result = await transcribeVideo("" as Video.VideoId, "");
expect(result.success).toBe(false);
expect(result.message).toBe("userId or videoId not supplied");
});
});
describe("video lookup", () => {
it("returns error when video does not exist", async () => {
mockQueryResult = [];
const result = await transcribeVideo(
"nonexistent-video" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(false);
expect(result.message).toBe("Video does not exist");
});
it("returns error when video result is malformed", async () => {
mockQueryResult = [
{ video: null, bucket: null, settings: null, orgSettings: null },
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(false);
expect(result.message).toBe("Video information is missing");
});
});
describe("transcription disabled scenarios", () => {
it("defers to a provably fresh live transcription instead of racing it", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: null,
settings: null,
metadata: {
liveTranscript: {
status: "active",
updatedAt: new Date().toISOString(),
},
},
},
bucket: null,
settings: null,
orgSettings: null,
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("Live transcription in progress");
expect(mockStart).not.toHaveBeenCalled();
});
it("ignores a stale live claim so a dead workflow can never block transcription", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: null,
settings: null,
metadata: {
liveTranscript: {
status: "active",
updatedAt: new Date(Date.now() - 10 * 60 * 1000).toISOString(),
},
},
},
bucket: null,
settings: null,
orgSettings: null,
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(mockStart).toHaveBeenCalledTimes(1);
});
it("skips transcription when video settings disable it", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: null,
settings: { disableTranscript: true },
},
bucket: null,
settings: { disableTranscript: true },
orgSettings: null,
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("disabled");
expect(mockStart).not.toHaveBeenCalled();
});
it("skips transcription when org settings disable it", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: null,
settings: null,
},
bucket: null,
settings: null,
orgSettings: { disableTranscript: true },
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("disabled");
});
it("video settings take precedence over org settings", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: null,
settings: { disableTranscript: false },
},
bucket: null,
settings: { disableTranscript: false },
orgSettings: { disableTranscript: true },
},
];
mockStart.mockResolvedValue({ id: "run-123" });
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(mockStart).toHaveBeenCalled();
});
});
describe("existing transcription status", () => {
it("returns early when transcription is already complete", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: "COMPLETE",
settings: null,
},
bucket: null,
settings: null,
orgSettings: null,
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("already completed");
expect(mockStart).not.toHaveBeenCalled();
});
it("returns early when transcription is in progress", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: "PROCESSING",
settings: null,
},
bucket: null,
settings: null,
orgSettings: null,
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("in progress");
expect(mockStart).not.toHaveBeenCalled();
});
it("returns early when transcription failed until manual retry resets it", async () => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: "ERROR",
settings: null,
},
bucket: null,
settings: null,
orgSettings: null,
},
];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("in progress");
expect(mockStart).not.toHaveBeenCalled();
});
});
describe("workflow triggering", () => {
beforeEach(() => {
mockQueryResult = [
{
video: {
id: "video-123",
transcriptionStatus: null,
settings: null,
},
bucket: { id: "bucket-456" },
settings: null,
orgSettings: null,
},
];
mockStart.mockResolvedValue({ id: "workflow-run-123" });
});
it("does not trigger while upload is still active", async () => {
mockUploadQueryResult = [{ phase: "processing" }];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toBe("Video upload is still in progress");
expect(mockStart).not.toHaveBeenCalled();
});
it("triggers workflow for valid video", async () => {
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toBe("Transcription workflow started");
expect(mockStart).toHaveBeenCalledTimes(1);
});
it("does not trigger workflow when another request claimed transcription first", async () => {
mockUpdateResult = [{ affectedRows: 0 }];
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(true);
expect(result.message).toContain("in progress");
expect(mockStart).not.toHaveBeenCalled();
});
it("passes correct payload to workflow", async () => {
await transcribeVideo("video-123" as Video.VideoId, "user-456", true);
expect(mockStart).toHaveBeenCalledWith(transcribeVideoWorkflow, [
{
videoId: "video-123",
userId: "user-456",
aiGenerationEnabled: true,
},
]);
});
it("defaults aiGenerationEnabled to false", async () => {
await transcribeVideo("video-123" as Video.VideoId, "user-456");
expect(mockStart).toHaveBeenCalledWith(transcribeVideoWorkflow, [
{
videoId: "video-123",
userId: "user-456",
aiGenerationEnabled: false,
},
]);
});
it("handles workflow trigger failure gracefully", async () => {
mockStart.mockRejectedValue(new Error("Workflow service unavailable"));
const result = await transcribeVideo(
"video-123" as Video.VideoId,
"user-456",
);
expect(result.success).toBe(false);
expect(result.message).toBe("Failed to start transcription workflow");
});
});
});