Repository Public
1import { describe, expect, it } from "vitest";2import { createEngine, editDistance, highlight, matchQuality, queryTerms, snippet, tokenize, type SearchDoc } from "@/modules/search";34const docs: SearchDoc[] = [5 { id: "seance:S05", type: "seance", title: "Fenêtres de contexte et ingénierie du contexte", text: "Planifier un budget de jetons. Diagnostiquer la troncature.", url: "/seances/05", part: "II", session: "S05", level: "intermédiaire" },6 { id: "seance:S02", type: "seance", title: "Jetons, entraînement, inférence et paramètres", text: "Découper un texte en jetons et estimer son coût.", url: "/seances/02", part: "I", session: "S02", level: "débutant" },7 { id: "seance:S09", type: "seance", title: "Qu’est-ce qu’un agent d’IA, exactement ?", text: "Programmer une boucle d’agent bornée.", url: "/seances/09", part: "III", session: "S09", level: "intermédiaire" },8 { id: "glossaire:jeton", type: "glossaire", title: "jeton", text: "token unité de texte traitée par un LLM", url: "/glossaire#jeton", part: "I", session: "S01", level: "débutant" },9 { id: "transcription:S01:c:30", type: "transcription", title: "Capsule", text: "Changement de chapitre : la tonalité passe à 550 hertz.", url: "/seances/01/regarder?t=30", t: 30, part: "I", session: "S01", level: "débutant" },10 { id: "page:/a-propos", type: "page", title: "À propos", text: "Le cours, l’enseignant, la méthode.", url: "/a-propos" },11];12const engine = createEngine(docs);13const ids = (q: string, f = {}) => engine.search(q, f).hits.map((h) => h.doc.id);1415describe("text handling", () => {16 it("tokenizes without accents, case, ligatures or typographic apostrophes", () => {17 expect(tokenize("L’Œuvre d'Élève, déjà-vu !")).toEqual(["l", "oeuvre", "d", "eleve", "deja", "vu"]);18 });19 it("drops stop words unless nothing else is left", () => {20 expect(queryTerms("la fenêtre de contexte")).toEqual(["fenetre", "contexte"]);21 expect(queryTerms("de la")).toEqual(["de", "la"]);22 expect(queryTerms(" ")).toEqual([]);23 });24 it("computes Damerau–Levenshtein distance with an early exit", () => {25 expect(editDistance("jeton", "jeton", 2)).toBe(0);26 expect(editDistance("jeton", "jetno", 2)).toBe(1); // transposition27 expect(editDistance("contexte", "contxte", 2)).toBe(1);28 expect(editDistance("agent", "contexte", 2)).toBe(3); // capped at max + 129 });30 it("ranks exact above prefix above typo, and refuses typos on short words", () => {31 expect(matchQuality("jeton", "jeton")).toBe(1);32 expect(matchQuality("jet", "jetons")).toBe(0.75);33 expect(matchQuality("jeotn", "jeton")).toBe(0.45);34 expect(matchQuality("ia", "il")).toBe(0);35 expect(matchQuality("rag", "rat")).toBe(0);36 });37});3839describe("search", () => {40 it("is accent-insensitive in both directions", () => {41 expect(ids("fenetre")).toContain("seance:S05");42 expect(ids("FENÊTRE")).toContain("seance:S05");43 expect(ids("inference")).toEqual(["seance:S02"]);44 });45 it("tolerates typos", () => {46 expect(ids("contxte")).toContain("seance:S05");47 expect(ids("tonaltié")).toEqual(["transcription:S01:c:30"]);48 expect(ids("ingenirie")).toContain("seance:S05");49 });50 it("matches prefixes and plurals", () => {51 expect(ids("jeton")).toEqual(expect.arrayContaining(["glossaire:jeton", "seance:S02", "seance:S05"]));52 expect(ids("agen")).toEqual(["seance:S09"]);53 });54 it("requires every term (AND) and ranks title matches first", () => {55 expect(ids("budget jetons")).toEqual(["seance:S05"]);56 expect(ids("jetons paramètres")[0]).toBe("seance:S02");57 expect(ids("licorne")).toEqual([]);58 expect(ids("jetons licorne")).toEqual([]);59 });60 it("returns timestamped transcript hits", () => {61 const hit = engine.search("hertz").hits[0]!;62 expect(hit.doc).toMatchObject({ type: "transcription", t: 30, url: "/seances/01/regarder?t=30" });63 });64 it("filters by facet and reports facet counts before filtering", () => {65 const all = engine.search("jeton");66 expect(all.facets.type).toEqual({ seance: 2, glossaire: 1 });67 expect(all.facets.level).toEqual({ "intermédiaire": 1, "débutant": 2 });68 const only = engine.search("jeton", { type: "glossaire" });69 expect(only.hits.map((h) => h.doc.id)).toEqual(["glossaire:jeton"]);70 expect(only.facets.type).toEqual(all.facets.type);71 expect(ids("jeton", { part: "II" })).toEqual(["seance:S05"]);72 expect(ids("jeton", { session: "S02" })).toEqual(["seance:S02"]);73 expect(ids("jeton", { level: "débutant", type: "seance" })).toEqual(["seance:S02"]);74 });75 it("returns nothing for an empty query", () => {76 expect(engine.search("").hits).toEqual([]);77 });78});7980describe("highlighting", () => {81 it("marks matching words whatever their accents, keeping the original text intact", () => {82 const parts = highlight("Fenêtres de contexte.", ["fenetre", "contexte"]);83 expect(parts.map((p) => p.text).join("")).toBe("Fenêtres de contexte.");84 expect(parts.filter((p) => p.hit).map((p) => p.text)).toEqual(["Fenêtres", "contexte"]);85 });86 it("cuts a snippet around the first match", () => {87 const long = `${"mot ".repeat(80)}tonalité de test ${"fin ".repeat(80)}`;88 const s = snippet(long, ["tonalite"], 40);89 expect(s).toContain("tonalité"); expect(s.startsWith("…")).toBe(true); expect(s.endsWith("…")).toBe(true); expect(s.length).toBeLessThan(110);90 expect(snippet("court", ["x"])).toBe("court");91 });92});93