SPB Git

Repository Public

urls.test.ts 2.6 KB 49 lines typescript

1import { describe, expect, it } from "vitest";2import { canonical, formatTime, isSafeRelPath, parseTimeParam, sessionPath, sessionSlug, slugify, watchPath } from "@/modules/content-schema";3import { loadManifest } from "./helpers";45describe("URL generation", () => {6  it("builds French, ASCII slugs", () => {7    expect(slugify("Ingénierie du contexte")).toBe("ingenierie-du-contexte");8    expect(slugify("  Plongements et RAG !")).toBe("plongements-et-rag");9    expect(slugify("Cœur de l’œuvre")).toBe("coeur-de-l-oeuvre");10    expect(sessionSlug(5, "Ingénierie du contexte")).toBe("05-ingenierie-du-contexte");11  });12  it("builds session and watch paths with deep links", () => {13    expect(sessionPath("05-x")).toBe("/seances/05-x");14    expect(watchPath("05-x")).toBe("/seances/05-x/regarder");15    expect(watchPath("05-x", { capsule: "intro", t: 95.7 })).toBe("/seances/05-x/regarder?capsule=intro&t=95");16    expect(watchPath("05-x", { t: 0 })).toBe("/seances/05-x/regarder");17  });18  it("parses timestamps in every accepted form", () => {19    expect(parseTimeParam("95")).toBe(95);20    expect(parseTimeParam("1m35s")).toBe(95);21    expect(parseTimeParam("1:35")).toBe(95);22    expect(parseTimeParam("1h2m5s")).toBe(3725);23    expect(parseTimeParam("0:01:35")).toBe(95);24    expect(parseTimeParam("abc")).toBeNull();25    expect(parseTimeParam("")).toBeNull();26    expect(parseTimeParam(null)).toBeNull();27    expect(parseTimeParam("-5")).toBeNull();28  });29  it("formats times", () => {30    expect(formatTime(0)).toBe("0:00");31    expect(formatTime(95.9)).toBe("1:35");32    expect(formatTime(3725)).toBe("1:02:05");33  });34  it("builds canonical URLs without query, hash or trailing slash", () => {35    expect(canonical("https://atlas.spboucher.ai/", "/programme/?q=1#x")).toBe("https://atlas.spboucher.ai/programme");36    expect(canonical("https://atlas.spboucher.ai", "/")).toBe("https://atlas.spboucher.ai/");37  });38  it("rejects unsafe relative paths", () => {39    for (const bad of ["../x", "a/../b", "/etc/passwd", "a\\b", "a//b", "", "a/./b", "a\0b", "a b", "é.svg"]) expect(isSafeRelPath(bad), bad).toBe(false);40    for (const ok of ["S01/quiz.json", "sessions/S01/figures/S01-F01-taxonomie.svg", "a_b.c-d"]) expect(isSafeRelPath(ok), ok).toBe(true);41  });42  it("gives every session a unique slug and redirectable aliases", () => {43    const m = loadManifest();44    const all = m.sessions.flatMap((s) => [s.slug, ...s.aliases]);45    expect(new Set(all).size).toBe(all.length);46    for (const s of m.sessions) { expect(s.slug).toMatch(/^\d{2}-[a-z0-9-]+$/); expect(s.aliases).toContain(s.id.toLowerCase()); }47  });48});49
Files main
Menu

Theme