const test = require("node:test"); const assert = require("node:assert"); const { loadPopupScript, wait } = require("./helpers"); test("defaults to app.plex.tv preset with both toggles on", async () => { const { window } = loadPopupScript(); await wait(20); assert.strictEqual(window.document.getElementById("urlPreset").value, "default"); assert.strictEqual(window.document.getElementById("skipIntro").checked, true); assert.strictEqual(window.document.getElementById("skipCredits").checked, true); assert.strictEqual(window.document.getElementById("customUrlRow").style.display, "none"); }); test("shows custom URL field pre-filled when a custom URL is already saved", async () => { const { window } = loadPopupScript({ initialSettings: { plexUrl: "http://192.168.1.50:32400/web" } }); await wait(20); assert.strictEqual(window.document.getElementById("urlPreset").value, "custom"); assert.strictEqual(window.document.getElementById("customUrl").value, "192.168.1.50:32400/web"); }); test("saving the default preset persists the default Plex URL", async () => { const { window, savedCalls } = loadPopupScript(); await wait(20); window.document.getElementById("saveBtn").click(); await wait(20); assert.strictEqual(savedCalls.length, 1); assert.strictEqual(savedCalls[0].plexUrl, "https://app.plex.tv"); }); test("saving a custom URL normalizes and persists it", async () => { const { window, savedCalls } = loadPopupScript(); await wait(20); const urlPreset = window.document.getElementById("urlPreset"); urlPreset.value = "custom"; urlPreset.dispatchEvent(new window.Event("change")); window.document.getElementById("customUrl").value = "192.168.1.50:32400/web"; window.document.getElementById("saveBtn").click(); await wait(20); assert.strictEqual(savedCalls.length, 1); assert.strictEqual(savedCalls[0].plexUrl, "http://192.168.1.50:32400/web"); }); test("saving Custom URL with a blank field does not silently fall back to the default", async () => { const { window, savedCalls } = loadPopupScript(); await wait(20); const urlPreset = window.document.getElementById("urlPreset"); urlPreset.value = "custom"; urlPreset.dispatchEvent(new window.Event("change")); window.document.getElementById("customUrl").value = " "; window.document.getElementById("saveBtn").click(); await wait(20); assert.strictEqual(savedCalls.length, 0, "should not save when the custom URL field is blank"); assert.match(window.document.getElementById("status").textContent, /enter|url/i); }); test("saving an unparseable custom URL is rejected with an error, not saved", async () => { const { window, savedCalls } = loadPopupScript(); await wait(20); const urlPreset = window.document.getElementById("urlPreset"); urlPreset.value = "custom"; urlPreset.dispatchEvent(new window.Event("change")); window.document.getElementById("customUrl").value = "http://"; window.document.getElementById("saveBtn").click(); await wait(20); assert.strictEqual(savedCalls.length, 0, "should not save an invalid URL"); }); test("unchecking a toggle and saving persists it as false", async () => { const { window, savedCalls } = loadPopupScript(); await wait(20); window.document.getElementById("skipCredits").checked = false; window.document.getElementById("saveBtn").click(); await wait(20); assert.strictEqual(savedCalls.length, 1); assert.strictEqual(savedCalls[0].skipCredits, false); assert.strictEqual(savedCalls[0].skipIntro, true); });