Repository Public
1import { describe, expect, it } from "vitest";2import { parseM3u, splitTitle } from "@/server/m3u";3import { parseXmltv, parseXmltvTime, rankGuides } from "@/server/epg";45const SAMPLE = `#EXTM3U x-tvg-url="https://example.org/guide.xml"6#EXTINF:-1 tvg-id="LCN.ca@SD" tvg-logo="https://i.imgur.com/a.png" group-title="News",LCN (720p) [Geo-blocked]7https://example.org/lcn/master.m3u88#EXTINF:-1 tvg-id="2MMonde.ma@SD" tvg-logo="" http-referrer="http://www.radio2m.ma/" http-user-agent="UA, with comma" group-title="General;Culture",2M Monde (360p) [Not 24/7]9#EXTVLCOPT:http-referrer=http://www.radio2m.ma/10#EXTVLCOPT:http-user-agent=UA, with comma11http://example.org/2m.m3u812#EXTINF:-1 group-title="Undefined",Chaîne, sans identifiant13https://example.org/x.m3u814https://example.org/orphan.m3u815`;1617describe("M3U parser", () => {18 const r = parseM3u(SAMPLE);19 it("extracts every #EXTINF entry and reports orphans", () => {20 expect(r.entries).toHaveLength(3);21 expect(r.errors).toEqual(["Ligne 10 : URL sans #EXTINF"]);22 expect(r.epgUrls).toEqual(["https://example.org/guide.xml"]);23 });24 it("splits id, feed, quality, labels, logo and groups", () => {25 expect(r.entries[0]).toMatchObject({ channelId: "LCN.ca", feed: "SD", name: "LCN", quality: "720p", labels: ["Geo-blocked"], logo: "https://i.imgur.com/a.png", group: ["News"], url: "https://example.org/lcn/master.m3u8" });26 });27 it("keeps commas inside quoted attributes and reads VLC options", () => {28 expect(r.entries[1]).toMatchObject({ name: "2M Monde", referrer: "http://www.radio2m.ma/", userAgent: "UA, with comma", group: ["General", "Culture"], labels: ["Not 24/7"], logo: null });29 });30 it("handles entries without tvg-id and ignores the Undefined group", () => {31 expect(r.entries[2]).toMatchObject({ channelId: null, name: "Chaîne, sans identifiant", group: [] });32 });33 it("flags a missing header", () => { expect(parseM3u("hello").errors[0]).toMatch(/EXTM3U/); });34 it("splitTitle", () => { expect(splitTitle("TV5 (1080p)")).toEqual({ name: "TV5", quality: "1080p", labels: [] }); expect(splitTitle("Canal (Sur) 24")).toMatchObject({ name: "Canal (Sur) 24", quality: null }); });35});3637describe("XMLTV", () => {38 it("parses times with offsets", () => {39 expect(parseXmltvTime("20261115050000 +0000")).toBe(Date.UTC(2026, 10, 15, 5));40 expect(parseXmltvTime("20261115000000 -0500")).toBe(Date.UTC(2026, 10, 15, 5));41 expect(Number.isNaN(parseXmltvTime("bad"))).toBe(true);42 });43 it("parses programmes, decodes entities, drops invalid rows", () => {44 const xml = `<tv><programme start="20261115050000 +0000" stop="20261115060000 +0000" channel="BFMTV.fr@SD"><title lang="fr">L'info & vous</title><desc>Un "tour"</desc><category>News</category></programme>45 <programme start="20261115060000 +0000" stop="20261115050000 +0000" channel="X"><title>inversé</title></programme><programme start="x" stop="y" channel="X"><title>t</title></programme></tv>`;46 const p = parseXmltv(xml);47 expect(p).toHaveLength(1);48 expect(p[0]).toMatchObject({ channelId: "BFMTV.fr@SD", title: "L'info & vous", description: 'Un "tour"', category: "News", subtitle: null });49 });50 it("ranks guide sites: channel language first, one row per site", () => {51 const g = (site: string, lang: string) => ({ channel_id: "c", feed: null, site, site_id: "1", site_name: null, lang });52 expect(rankGuides([g("plex.tv", "en"), g("tvpassport.com", "en"), g("tvhebdo.com", "fr"), g("tvhebdo.com", "fr")], ["fra"]).map((x) => x.site)).toEqual(["tvhebdo.com", "tvpassport.com", "plex.tv"]);53 });54});55