File size: 2,422 Bytes
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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();
    });
});