import { describe, it, expect, vi, beforeEach } from 'vitest'; import { triggerObjectDetectionAction } from '../trigger-object-detection'; import { auth } from "@/lib/auth"; import { db } from "@/lib/db"; import { inngest } from "@/inngest/client"; // Mock dependencies vi.mock("@/lib/auth", () => ({ auth: { api: { getSession: vi.fn() } } })); vi.mock("@/lib/db", () => ({ db: { select: vi.fn().mockReturnThis(), from: vi.fn().mockReturnThis(), innerJoin: vi.fn().mockReturnThis(), where: vi.fn().mockReturnThis(), limit: vi.fn().mockReturnThis(), } })); vi.mock("@/inngest/client", () => ({ inngest: { send: vi.fn() } })); vi.mock("next/headers", () => ({ headers: vi.fn().mockResolvedValue({}) })); describe('Trigger Object Detection Action', () => { beforeEach(() => { vi.clearAllMocks(); }); it('should return error if unauthorized', async () => { // @ts-ignore auth.api.getSession.mockResolvedValue(null); const result = await triggerObjectDetectionAction('v123'); expect(result.success).toBe(false); expect(result.error).toBe('Unauthorized'); }); it('should trigger detection if authorized and owner', async () => { // @ts-ignore auth.api.getSession.mockResolvedValue({ user: { id: 'u123' } }); const mockResult = [{ id: 'v123', ytVideoId: 'yt123', creatorId: 'u123' }]; // @ts-ignore db.limit.mockResolvedValue(mockResult); const result = await triggerObjectDetectionAction('v123'); expect(result.success).toBe(true); expect(inngest.send).toHaveBeenCalledWith({ name: 'youtube/video.detect-objects', data: { videoId: 'v123', videoUrl: 'https://www.youtube.com/watch?v=yt123' } }); }); it('should return error if video not found or not owner', async () => { // @ts-ignore auth.api.getSession.mockResolvedValue({ user: { id: 'u123' } }); // @ts-ignore db.limit.mockResolvedValue([]); // No result const result = await triggerObjectDetectionAction('v123'); expect(result.success).toBe(false); expect(result.error).toBe('Video not found or permission denied'); expect(inngest.send).not.toHaveBeenCalled(); }); });