import { describe, expect, it, vi } from "vitest"; import { buildShareUrl, buildShortSharePath, resolveShareOrigin, shareOrCopy, } from "./share"; describe("buildShareUrl", () => { it("ต่อ origin กับ path และตัด / ซ้ำท้าย origin", () => { expect(buildShareUrl("https://alterflixhub.com/", "/cinema/lido")).toBe( "https://alterflixhub.com/cinema/lido", ); expect(buildShareUrl("http://localhost:8080", "cinema/lido")).toBe( "http://localhost:8080/cinema/lido", ); }); it("คงพาธที่มีภาษาไทยไว้ตามเดิม", () => { expect(buildShareUrl("https://x.test", "/cinema/เมืองทองรามา")).toBe( "https://x.test/cinema/เมืองทองรามา", ); }); }); describe("resolveShareOrigin", () => { it("ใช้โดเมนที่ตั้งไว้ และตัด / ท้ายออก", () => { expect( resolveShareOrigin( "https://alterflixhub.hobbypj.com/", "http://localhost:8080", ), ).toBe("https://alterflixhub.hobbypj.com"); }); it("ไม่ได้ตั้ง (ว่าง/undefined) → ใช้ origin ของหน้าเว็บ", () => { expect(resolveShareOrigin("", "http://localhost:8080")).toBe( "http://localhost:8080", ); expect(resolveShareOrigin(undefined, "http://localhost:8080")).toBe( "http://localhost:8080", ); expect(resolveShareOrigin(" ", "http://localhost:8080")).toBe( "http://localhost:8080", ); }); }); describe("buildShortSharePath", () => { it("สร้างลิงก์ย่อ /c/", () => { expect(buildShortSharePath("kku-cinema")).toBe("/c/kku-cinema"); }); }); describe("shareOrCopy", () => { const data = { title: "LIDO CONNECT", url: "https://x.test/cinema/lido" }; it("มี navigator.share → เรียกแผงแชร์ แล้วคัดลอกสำรองด้วย คืน shared", async () => { const share = vi.fn().mockResolvedValue(undefined); const writeText = vi.fn().mockResolvedValue(undefined); expect(await shareOrCopy(data, { share, writeText })).toBe("shared"); expect(share).toHaveBeenCalledWith({ title: data.title, url: data.url }); expect(writeText).toHaveBeenCalledWith(data.url); }); it("ไม่มี navigator.share → คัดลอกแล้วคืน copied", async () => { const writeText = vi.fn().mockResolvedValue(undefined); expect(await shareOrCopy(data, { writeText })).toBe("copied"); expect(writeText).toHaveBeenCalledWith(data.url); }); it("ผู้ใช้ยกเลิกแผงแชร์ แต่คัดลอกได้ → ยังคืน shared", async () => { const share = vi.fn().mockRejectedValue(new Error("AbortError")); const writeText = vi.fn().mockResolvedValue(undefined); expect(await shareOrCopy(data, { share, writeText })).toBe("shared"); }); it("clipboard ใช้ไม่ได้ → ตกไปใช้ legacyCopy", async () => { const writeText = vi.fn().mockRejectedValue(new Error("denied")); const legacyCopy = vi.fn().mockReturnValue(true); expect(await shareOrCopy(data, { writeText, legacyCopy })).toBe("copied"); expect(legacyCopy).toHaveBeenCalledWith(data.url); }); it("ทุกทางล้มเหลว → failed", async () => { expect(await shareOrCopy(data, {})).toBe("failed"); }); });