Repository Public
1import { describe, expect, it } from "vitest";2import { buildView, canDownload, canSee, contentAllowed, mediaAllowed, sessionAccess } from "@/modules/content-schema";3import { sitemapPaths, structuredData } from "@/lib/seo";4import { capsule, loadManifest, withPublishedS01 } from "./helpers";56describe("publication states", () => {7 it("opens a session only when published, or in preview when the preview mode is on", () => {8 expect(sessionAccess({ publication: "published" }, "production")).toBe("open");9 expect(sessionAccess({ publication: "preview" }, "production")).toBe("upcoming");10 expect(sessionAccess({ publication: "preview" }, "preview")).toBe("open");11 for (const mode of ["production", "preview"] as const) {12 expect(sessionAccess({ publication: "planned" }, mode)).toBe("upcoming");13 expect(sessionAccess({ publication: "draft" }, mode)).toBe("upcoming");14 expect(sessionAccess({ publication: "archived" }, mode)).toBe("archived");15 }16 });1718 it("never shows private items, in any mode", () => {19 expect(canSee("private", "production")).toBe(false);20 expect(canSee("private", "preview")).toBe(false);21 expect(canSee("preview", "production")).toBe(false);22 expect(canSee("preview", "preview")).toBe(true);23 expect(canSee("public", "production")).toBe(true);24 });2526 it("offers a download only with visibility, permission and a recorded licence", () => {27 const item = { visibility: "public" as const, download: true };28 expect(canDownload(item, null, "production")).toBe(false); // no licence chosen29 expect(canDownload(item, "CC BY 4.0", "production")).toBe(true);30 expect(canDownload({ ...item, download: false }, "CC BY 4.0", "production")).toBe(false);31 expect(canDownload({ ...item, visibility: "preview" }, "CC BY 4.0", "production")).toBe(false);32 expect(canDownload({ ...item, releaseAfter: "2999-01-01T00:00:00Z" }, "CC BY 4.0", "production")).toBe(false);33 expect(canDownload({ ...item, releaseAfter: "2000-01-01T00:00:00Z" }, "CC BY 4.0", "production")).toBe(true);34 });35});3637describe("the committed production manifest", () => {38 const manifest = loadManifest();39 const view = buildView(manifest, "production");4041 it("has the 24 sessions of the course plan in six parts", () => {42 expect(view.sessions).toHaveLength(24);43 expect(view.parts).toHaveLength(6);44 expect(view.sessions.map((s) => s.number)).toEqual(Array.from({ length: 24 }, (_, i) => i + 1));45 });4647 it("shows every session as « À venir » and exposes nothing but the public plan", () => {48 for (const s of view.sessions) {49 expect(s.access, s.id).toBe("upcoming");50 expect(s.sections, s.id).toEqual({});51 expect(s.capsules, s.id).toEqual([]);52 expect(s.resources, s.id).toEqual([]);53 expect(s.effortMinutes, s.id).toBeNull();54 expect(s.title.length).toBeGreaterThan(5);55 expect(s.outcomes.length).toBeGreaterThan(0);56 }57 expect(view.counts).toMatchObject({ open: 0, published: 0, capsules: 0, quizzes: 0 });58 expect(view.diagrams).toEqual([]);59 });6061 it("has no published state and no public section at all: the importer never publishes", () => {62 expect(manifest.sessions.filter((s) => s.publication === "published")).toEqual([]);63 for (const s of manifest.sessions) for (const ref of Object.values(s.sections)) expect(ref!.visibility).not.toBe("public");64 expect(manifest.course.license).toBeNull();65 });6667 it("refuses media and imported content of anything not visible", () => {68 expect(mediaAllowed(view, "S01/test-technique/master.m3u8")).toBe(false);69 expect(contentAllowed(view, "S01", "figures")).toBe(false);70 expect(contentAllowed(view, "S01", "quiz")).toBe(false);71 });7273 it("keeps drafts closed even in preview mode", () => {74 const preview = buildView(manifest, "preview");75 for (const s of preview.sessions) {76 const src = manifest.sessions.find((m) => m.id === s.id)!;77 expect(s.access === "open", s.id).toBe(src.publication === "preview");78 if (s.access !== "open") expect(s.sections).toEqual({});79 }80 const s01 = preview.sessions.find((s) => s.id === "S01")!;81 expect(s01.access).toBe("open");82 expect(s01.previewOnly).toBe(true);83 expect(Object.keys(s01.sections).sort()).toEqual(["bibliography", "figures", "glossary", "lab", "quiz", "script"]);84 // Preview material is never counted as published.85 expect(preview.counts.published).toBe(0);86 });87});8889describe("media permissions", () => {90 it("allows only keys under a visible capsule", () => {91 const m = loadManifest();92 const s = m.sessions.find((x) => x.id === "S01")!;93 // Permission follows the files a capsule names (since Phase 4 an encode lives in its own directory), not its identifier.94 s.capsules.push(capsule({ id: "test-technique", visibility: "preview", synthetic: true, media: { hls: "S01/test-technique/master.m3u8", mp4: "S01/test-technique/fallback.mp4", captions: [{ lang: "fr-CA", label: "Français", path: "S01/test-technique/captions.fr.vtt" }] } }));95 const preview = buildView(m, "preview");96 expect(mediaAllowed(preview, "S01/test-technique/v360/seg_000.ts")).toBe(true);97 expect(mediaAllowed(preview, "S01/test-technique/captions.fr.vtt")).toBe(true);98 expect(mediaAllowed(preview, "S01/test-technique-2/master.m3u8")).toBe(false);99 expect(mediaAllowed(preview, "S01/autre/master.m3u8")).toBe(false);100 expect(mediaAllowed(preview, "S02/test-technique/master.m3u8")).toBe(false);101 expect(mediaAllowed(preview, "S01/test-technique")).toBe(false);102 expect(mediaAllowed(buildView(m, "production"), "S01/test-technique/master.m3u8")).toBe(false);103 // The synthetic capsule is never counted.104 expect(preview.counts.capsules).toBe(0);105 });106});107108describe("sitemap and structured data", () => {109 const base = "https://atlas.spboucher.ai";110 it("lists public pages and session plans, never watch pages, admin, preview or diagrams, while nothing is published", () => {111 const paths = sitemapPaths(buildView(loadManifest(), "production"));112 expect(paths).toContain("/programme");113 expect(paths.filter((p) => p.startsWith("/seances/"))).toHaveLength(24);114 expect(paths.some((p) => /regarder|admin|apercu|schemas|telecharger/.test(p))).toBe(false);115 });116 it("is empty for a preview build", () => {117 expect(sitemapPaths(buildView(loadManifest(), "preview"))).toEqual([]);118 });119 it("emits only WebSite while nothing is published — no Course, no VideoObject", () => {120 for (const mode of ["production", "preview"] as const) {121 const types = structuredData(buildView(loadManifest(), mode), base).map((d) => d["@type"]);122 expect(types).toEqual(["WebSite"]);123 }124 });125 it("emits Course and VideoObject once a session is really published, and never for a synthetic capsule", () => {126 const m = withPublishedS01();127 m.sessions.find((s) => s.id === "S01")!.capsules.push(capsule({ id: "test-technique", synthetic: true }));128 const view = buildView(m, "production");129 const data = structuredData(view, base);130 expect(data.map((d) => d["@type"])).toEqual(["WebSite", "Course", "VideoObject"]);131 expect(JSON.stringify(data)).not.toContain("test-technique");132 expect(view.counts).toMatchObject({ published: 1, capsules: 1, quizzes: 1 });133 expect(sitemapPaths(view)).toContain(`/seances/${view.sessions[0]!.slug}/regarder`);134 });135});136