File size: 4,112 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { headers } from "next/headers";

import { POST } from "./route";
import { auth } from "@/lib/auth";
import { buildBackendAuthHeaders } from "@/lib/backend-auth";
import { parseApiError } from "@/lib/api-error";

vi.mock("next/headers", () => ({
  headers: vi.fn(),
}));

vi.mock("@/lib/auth", () => ({
  auth: {
    api: {
      getSession: vi.fn(),
    },
  },
}));

vi.mock("@/lib/backend-auth", () => ({
  buildBackendAuthHeaders: vi.fn(),
}));

describe("/api/tasks/create", () => {
  beforeEach(() => {
    vi.resetAllMocks();
    vi.stubGlobal("fetch", vi.fn());
    vi.mocked(headers).mockResolvedValue(new Headers());
  });

  it("returns 401 when unauthenticated", async () => {
    vi.mocked(auth.api.getSession).mockResolvedValue(null as never);

    const response = await POST(
      new Request("http://localhost/api/tasks/create", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ source: { url: "https://example.com/video.mp4" } }),
      }),
    );

    expect(response.status).toBe(401);
    await expect(response.json()).resolves.toEqual({ error: "Unauthorized" });
  });

  it("proxies task creation to the backend", async () => {
    vi.mocked(auth.api.getSession).mockResolvedValue({
      user: { id: "user-1" },
    } as never);
    vi.mocked(buildBackendAuthHeaders).mockReturnValue({
      "x-supoclip-user-id": "user-1",
    });
    vi.mocked(fetch).mockResolvedValue(
      new Response(JSON.stringify({ task_id: "task-1" }), {
        status: 200,
        headers: { "Content-Type": "application/json" },
      }),
    );

    const payload = {
      source: { url: "https://www.youtube.com/watch?v=demo" },
    };

    const response = await POST(
      new Request("http://localhost/api/tasks/create", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload),
      }),
    );

    expect(fetch).toHaveBeenCalledWith(
      "http://localhost:8000/tasks/",
      expect.objectContaining({
        method: "POST",
        body: JSON.stringify(payload),
        headers: expect.objectContaining({
          "Content-Type": "application/json",
          "x-supoclip-user-id": "user-1",
        }),
      }),
    );
    expect(response.status).toBe(200);
    const init = vi.mocked(fetch).mock.calls[0][1] as RequestInit;
    expect(init.headers).not.toEqual(expect.objectContaining({
      "idempotency-key": expect.anything(),
    }));
  });

  it("forwards an idempotency key when the client sends one", async () => {
    vi.mocked(auth.api.getSession).mockResolvedValue({
      user: { id: "user-1" },
    } as never);
    vi.mocked(buildBackendAuthHeaders).mockReturnValue({
      "x-supoclip-user-id": "user-1",
    });
    vi.mocked(fetch).mockResolvedValue(new Response("{}", { status: 200 }));

    await POST(new Request("http://localhost/api/tasks/create", {
      method: "POST",
      headers: { "idempotency-key": "retry-key-1" },
      body: JSON.stringify({ source: { url: "upload://clip.mp4" } }),
    }));

    const init = vi.mocked(fetch).mock.calls[0][1] as RequestInit;
    expect(init.headers).toEqual(expect.objectContaining({
      "idempotency-key": "retry-key-1",
    }));
  });

  it("preserves the duration rejection and actionable message for the form", async () => {
    vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: "user-1" } } as never);
    const message = "Your plan allows YouTube videos up to 90 minutes. No generation was used.";
    vi.mocked(fetch).mockResolvedValue(new Response(JSON.stringify({
      detail: { code: "VIDEO_TOO_LONG", message, max_duration_seconds: 5400 },
    }), { status: 422, headers: { "Content-Type": "application/json" } }));

    const response = await POST(new Request("http://localhost/api/tasks/create", {
      method: "POST",
      body: JSON.stringify({ source: { url: "https://youtu.be/abcdefghijk" } }),
    }));

    expect(response.status).toBe(422);
    expect((await parseApiError(response, "Failed to process video")).message).toBe(message);
  });
});