55 lines
2.6 KiB
TypeScript
55 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { mapUpstreamStreamError, resolveContentType } from "./stream.service.js";
|
|
|
|
describe("resolveContentType", () => {
|
|
it("keeps existing mime", () => {
|
|
expect(resolveContentType("video/mp4", "a.mkv")).toBe("video/mp4");
|
|
});
|
|
it("maps common video extensions", () => {
|
|
expect(resolveContentType(null, "/m/a.mp4")).toBe("video/mp4");
|
|
expect(resolveContentType("", "/m/a.m4v")).toBe("video/mp4");
|
|
expect(resolveContentType(undefined, "/m/a.mkv")).toBe("video/x-matroska");
|
|
expect(resolveContentType(null, "/m/a.webm")).toBe("video/webm");
|
|
});
|
|
it("falls back to video/<ext> or octet-stream", () => {
|
|
expect(resolveContentType(null, "/m/a.avi")).toBe("video/avi");
|
|
expect(resolveContentType(null, "/m/noext")).toBe("application/octet-stream");
|
|
});
|
|
});
|
|
|
|
describe("mapUpstreamStreamError", () => {
|
|
it("maps upstream 404 (webdav .status) to 410 gone", () => {
|
|
const e = Object.assign(new Error("Invalid response: 404 Not Found"), { status: 404 });
|
|
const mapped = mapUpstreamStreamError(e);
|
|
expect(mapped.status).toBe(410);
|
|
expect(mapped.message).toContain("片源不存在");
|
|
});
|
|
it("parses status from message when .status is absent", () => {
|
|
const mapped = mapUpstreamStreamError(new Error("Invalid response: 404 Not Found"));
|
|
expect(mapped.status).toBe(410);
|
|
});
|
|
it("maps upstream 401/403 to 410 credentials gone", () => {
|
|
const e401 = Object.assign(new Error("Invalid response: 401 Unauthorized"), {
|
|
status: 401,
|
|
});
|
|
const e403 = Object.assign(new Error("Invalid response: 403 Forbidden"), { status: 403 });
|
|
expect(mapUpstreamStreamError(e401).status).toBe(410);
|
|
expect(mapUpstreamStreamError(e401).message).toContain("挂载凭证");
|
|
expect(mapUpstreamStreamError(e403).status).toBe(410);
|
|
});
|
|
it("keeps other failures as 502 with truncated message", () => {
|
|
const mapped = mapUpstreamStreamError(new Error("connect ECONNREFUSED 10.0.0.1:5007"));
|
|
expect(mapped.status).toBe(502);
|
|
expect(mapped.message.startsWith("片源读取失败:")).toBe(true);
|
|
const long = mapUpstreamStreamError(new Error("x".repeat(300)));
|
|
expect(long.status).toBe(502);
|
|
expect(long.message.length).toBeLessThanOrEqual("片源读取失败:".length + 120);
|
|
});
|
|
it("maps upstream 500 to 502", () => {
|
|
const e = Object.assign(new Error("Invalid response: 500 Internal Server Error"), {
|
|
status: 500,
|
|
});
|
|
expect(mapUpstreamStreamError(e).status).toBe(502);
|
|
});
|
|
});
|