File size: 3,838 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
import { GET } from "./route";
import { fetchBackend } from "@/server/backend-api";
import { getServerSession } from "@/server/session";

vi.mock("@/server/session", () => ({ getServerSession: vi.fn() }));
vi.mock("@/server/backend-api", async () => ({
  ...await vi.importActual<typeof import("@/server/backend-api")>("@/server/backend-api"),
  fetchBackend: vi.fn(),
}));

const path = ["task", "clips", "clip", "editor", "media", "clean.mp4"];
const signed = "https://storage.example/editor/clean.mp4?signature=private";
const context = (value = path) => ({ params: Promise.resolve({ path: value }) });

beforeEach(() => {
  vi.resetAllMocks();
  vi.mocked(getServerSession).mockResolvedValue({ user: { id: "owner" } } as never);
});
afterEach(() => vi.unstubAllGlobals());

it("streams signed editor media with ranges on the authenticated origin, without forwarding credentials", async () => {
  vi.mocked(fetchBackend).mockResolvedValue(new Response(null, { status: 307, headers: { Location: signed } }));
  const bytes = new Uint8Array([0, 0, 0, 32, 102, 116, 121, 112]);
  const storageFetch = vi.fn().mockResolvedValue(new Response(bytes, { status: 206, headers: {
    "Content-Type": "video/mp4", "Content-Range": "bytes 0-7/1000", "Content-Length": "8",
    "Cache-Control": "public, max-age=86400", "Accept-Ranges": "bytes",
  } }));
  vi.stubGlobal("fetch", storageFetch);
  const request = new Request("https://app.example/api/tasks/" + path.join("/"), { headers: {
    Range: "bytes=0-7", "If-Range": '"etag"', Accept: "video/*",
    Cookie: "private-session", Authorization: "Bearer private", "x-supoclip-signature": "private",
  } });
  expect(request.headers.get("range")).toBe("bytes=0-7");
  const response = await GET(request, context());
  expect(response.status).toBe(206);
  expect(response.headers.get("content-range")).toBe("bytes 0-7/1000");
  expect(response.headers.get("content-length")).toBe("8");
  expect(response.headers.get("content-type")).toBe("video/mp4");
  expect(response.headers.get("cache-control")).toBe("private, no-store");
  expect(new Uint8Array(await response.arrayBuffer())).toEqual(bytes);
  expect(storageFetch).toHaveBeenCalledOnce();
  expect(storageFetch).toHaveBeenCalledWith(signed, expect.objectContaining({ credentials: "omit", signal: request.signal, cache: "no-store" }));
  const forwarded = storageFetch.mock.calls[0][1].headers;
  expect(forwarded).toEqual({ accept: "video/*", "if-range": '"etag"', range: "bytes=0-7" });
});

it("requires authentication before resolving private editor media", async () => {
  vi.mocked(getServerSession).mockResolvedValue(null);
  expect((await GET(new Request("https://app.example/"), context())).status).toBe(401);
  expect(fetchBackend).not.toHaveBeenCalled();
});

it("preserves redirects for ordinary clip downloads", async () => {
  vi.mocked(fetchBackend).mockResolvedValue(new Response(null, { status: 307, headers: { Location: signed } }));
  const fetch = vi.fn(); vi.stubGlobal("fetch", fetch);
  const response = await GET(new Request("https://app.example/"), context(["task", "clips", "clip", "file"]));
  expect(response.status).toBe(307);
  expect(response.headers.get("location")).toBe(signed);
  expect(fetch).not.toHaveBeenCalled();
});

it("keeps local media responses unchanged", async () => {
  vi.mocked(fetchBackend).mockResolvedValue(new Response("local", { status: 206 }));
  const response = await GET(new Request("https://app.example/"), context());
  expect(response.status).toBe(206);
  expect(await response.text()).toBe("local");
});

it("rejects a malformed storage redirect", async () => {
  vi.mocked(fetchBackend).mockResolvedValue(new Response(null, { status: 307, headers: { Location: "file:///private" } }));
  expect((await GET(new Request("https://app.example/"), context())).status).toBe(502);
});