69 lines
3.7 KiB
TypeScript
69 lines
3.7 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { LinkError } from "../src/core/links";
|
|
import { infoUrl, isSameTapasSeries, isTapasUrl, normalizeImageUrl, parseTapasUrl, slugOf } from "../src/core/sources/tapas/urls";
|
|
|
|
const u = (s: string) => new URL(s);
|
|
|
|
describe("links", () => {
|
|
test("series links in every form", () => {
|
|
expect(parseTapasUrl(u("https://tapas.io/series/villains-are-destined-to-die"))).toEqual({ kind: "series", key: "villains-are-destined-to-die" });
|
|
expect(parseTapasUrl(u("https://tapas.io/series/villains-are-destined-to-die/info"))).toEqual({ kind: "series", key: "villains-are-destined-to-die" });
|
|
expect(parseTapasUrl(u("https://www.tapas.io/series/RU_Screwed/"))).toEqual({ kind: "series", key: "RU_Screwed" });
|
|
expect(parseTapasUrl(u("https://m.tapas.io/series/186414/info?x=1"))).toEqual({ kind: "series", key: "186414" });
|
|
expect(parseTapasUrl(u("https://tapas.io/episode/1901076"))).toEqual({ kind: "episode", id: "1901076" });
|
|
});
|
|
|
|
test("anything else on tapas.io is not a series link", () => {
|
|
for (const s of ["https://tapas.io/comics", "https://tapas.io/series/", "https://tapas.io/episode/abc", "https://example.org/series/x"]) {
|
|
expect(isTapasUrl(u(s))).toBe(false);
|
|
expect(() => parseTapasUrl(u(s))).toThrow(LinkError);
|
|
}
|
|
expect(isTapasUrl(u("https://tapas.io/series/x/info"))).toBe(true);
|
|
});
|
|
|
|
test("the page we fetch, and the slug of a canonical URL", () => {
|
|
expect(infoUrl("villains-are-destined-to-die")).toBe("https://tapas.io/series/villains-are-destined-to-die/info");
|
|
expect(infoUrl("186414")).toBe("https://tapas.io/series/186414/info");
|
|
expect(slugOf("https://tapas.io/series/villains-are-destined-to-die/info")).toBe("villains-are-destined-to-die");
|
|
});
|
|
});
|
|
|
|
describe("image URLs", () => {
|
|
test("the original on us-a.tapas.io: no size suffix, no query", () => {
|
|
expect(normalizeImageUrl("https://us-a.tapas.io/sa/d1/e57ba991-1815-41f4-99b8-70057b563642_z.jpg"))
|
|
.toBe("https://us-a.tapas.io/sa/d1/e57ba991-1815-41f4-99b8-70057b563642.jpg");
|
|
expect(normalizeImageUrl("https://d30womf5coomej.cloudfront.net/sa/c6/8eca19f0-84e2-4d86-bc5e-c2d7ffa6fd1c_z.jpg"))
|
|
.toBe("https://us-a.tapas.io/sa/c6/8eca19f0-84e2-4d86-bc5e-c2d7ffa6fd1c.jpg");
|
|
expect(normalizeImageUrl("https://us-a.tapas.io/sa/60/4cb8adab-92ea-4b38-b814-94d3d5606bf5.jpg?x=1"))
|
|
.toBe("https://us-a.tapas.io/sa/60/4cb8adab-92ea-4b38-b814-94d3d5606bf5.jpg");
|
|
expect(normalizeImageUrl("https://us-a.tapas.io/sa/cf/7d3083ef-163e-4349-bf05-1aeea3aca97d_m.png"))
|
|
.toBe("https://us-a.tapas.io/sa/cf/7d3083ef-163e-4349-bf05-1aeea3aca97d.png");
|
|
});
|
|
|
|
test("other hosts are left alone", () => {
|
|
expect(normalizeImageUrl("https://example.org/a_z.jpg?x=1")).toBe("https://example.org/a_z.jpg?x=1");
|
|
});
|
|
});
|
|
|
|
describe("same series", () => {
|
|
const slug = "villains-are-destined-to-die";
|
|
test("by slug (any case, /info, trailing slash) or by id", () => {
|
|
for (const link of [
|
|
"https://tapas.io/series/villains-are-destined-to-die",
|
|
"http://www.tapas.io/series/Villains-Are-Destined-To-Die/info",
|
|
"https://tapas.io/series/villains-are-destined-to-die/",
|
|
"https://tapas.io/series/186414/info",
|
|
]) expect(isSameTapasSeries(link, slug, "186414")).toBe(true);
|
|
});
|
|
|
|
test("a longer slug (the novel) or another id is a different series", () => {
|
|
for (const link of [
|
|
"https://tapas.io/series/villains-are-destined-to-die-novel",
|
|
"https://tapas.io/series/villains-are-destined-to-die-novel/info",
|
|
"https://tapas.io/series/266599",
|
|
"https://tapas.io/episode/1901076",
|
|
"not a url",
|
|
]) expect(isSameTapasSeries(link, slug, "186414")).toBe(false);
|
|
});
|
|
});
|