import { describe, expect, it } from "vitest"; import { ytCountFrom, ytSearchFrom, ytSearchParams } from "./ytSearchUrlState"; const p = (q: string) => new URLSearchParams(q); describe("the live YouTube search's address", () => { it("reads the term, trimmed; a blank one names no search", () => { expect(ytSearchFrom(p("yt=%20lofi%20"))).toBe("lofi"); expect(ytSearchFrom(p("yt=%20%20"))).toBeNull(); expect(ytSearchFrom(p("q=lofi"))).toBeNull(); }); it("reads one of the offered counts, else the default", () => { expect(ytCountFrom(p("ytn=60"))).toBe(60); expect(ytCountFrom(p("ytn=61"))).toBe(20); expect(ytCountFrom(p("ytn=x"))).toBe(20); expect(ytCountFrom(p(""))).toBe(20); }); it("writes the count only off its default, and nothing with no search", () => { expect(ytSearchParams("lofi", 40)).toEqual({ yt: "lofi", ytn: "40" }); expect(ytSearchParams("lofi", 20)).toEqual({ yt: "lofi", ytn: null }); expect(ytSearchParams(null, 40)).toEqual({ yt: null, ytn: null }); }); it("round-trips", () => { const q = new URLSearchParams(); for (const [k, v] of Object.entries(ytSearchParams("lofi beats", 100))) if (v) q.set(k, v); expect([ytSearchFrom(q), ytCountFrom(q)]).toEqual(["lofi beats", 100]); }); });