diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7798a9d0..9cc7cc55 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -30,7 +30,6 @@ If we later introduce **dual-licensing** options (e.g., commercial licenses for * Add tests where reasonable; keep UI accessible. * Follow existing formatting/lint rules. * Update docs and user-facing text when behavior changes. -* Check `KNOWN_ISSUES.md` for pre-existing environmental quirks or test failures before debugging. ## Agent PR Conventions @@ -62,13 +61,16 @@ To set up the project locally: ```bash npm run build ``` - *Note: This generates CSS and other assets.* -3. **Verify Environment**: +3. **Run Tests**: + ```bash + npm run test:unit + ``` + +4. **Format & Lint**: ```bash npm run format npm run lint - npm run test:unit ``` For a full guide, see the [Local Setup section in README.md](./README.md#local-setup). diff --git a/artifacts/npm-outdated.json b/artifacts/npm-outdated.json index cc279a16..9c6c6246 100644 --- a/artifacts/npm-outdated.json +++ b/artifacts/npm-outdated.json @@ -27,13 +27,6 @@ "dependent": "app", "location": "/app/node_modules/pixelmatch" }, - "prettier": { - "current": "3.8.0", - "wanted": "3.8.1", - "latest": "3.8.1", - "dependent": "app", - "location": "/app/node_modules/prettier" - }, "prettier-plugin-tailwindcss": { "current": "0.6.14", "wanted": "0.6.14", diff --git a/components/disclaimer.html b/components/disclaimer.html index dfddca2b..80910136 100644 --- a/components/disclaimer.html +++ b/components/disclaimer.html @@ -7,7 +7,6 @@ aria-labelledby="disclaimerModalTitle" aria-describedby="disclaimerModalDescription" data-ds="new" - data-testid="disclaimer-modal" >
@@ -131,7 +130,6 @@ id="acceptDisclaimer" class="modal-primary" type="button" - data-testid="disclaimer-accept-btn" > I Understand diff --git a/components/lockdown-modal.html b/components/lockdown-modal.html index 596e693d..62ff260d 100644 --- a/components/lockdown-modal.html +++ b/components/lockdown-modal.html @@ -6,7 +6,6 @@ aria-labelledby="lockdownModalTitle" aria-describedby="lockdownModalDescription" data-ds="new" - data-testid="lockdown-modal" >
diff --git a/components/login-modal.html b/components/login-modal.html index f6543a22..1bf08fec 100644 --- a/components/login-modal.html +++ b/components/login-modal.html @@ -6,7 +6,6 @@ aria-labelledby="loginModalTitle" aria-describedby="loginModalDescription" data-ds="new" - data-testid="login-modal" >
diff --git a/components/profile-modal.html b/components/profile-modal.html index 55304256..d3861443 100644 --- a/components/profile-modal.html +++ b/components/profile-modal.html @@ -8,7 +8,6 @@ tabindex="-1" data-ds="new" data-mobile-view="menu" - data-testid="profile-modal" >
diff --git a/components/upload-modal.html b/components/upload-modal.html index fea00e60..3033ac62 100644 --- a/components/upload-modal.html +++ b/components/upload-modal.html @@ -7,7 +7,6 @@ aria-labelledby="uploadModalTitle" aria-describedby="uploadModalDescription" data-ds="new" - data-testid="upload-modal" >
diff --git a/components/video-modal.html b/components/video-modal.html index 47d3ee47..0969fd66 100644 --- a/components/video-modal.html +++ b/components/video-modal.html @@ -6,7 +6,6 @@ aria-modal="true" data-ds="new" hidden - data-testid="video-modal" >
diff --git a/docs/nostr-event-schemas.md b/docs/nostr-event-schemas.md index 51a9c2f3..5b58df44 100644 --- a/docs/nostr-event-schemas.md +++ b/docs/nostr-event-schemas.md @@ -7,19 +7,6 @@ no longer requires hunting through the codebase. It also exposes helpers for building events and for overriding the schema at runtime when you need to experiment. -## Validation - -To verify that event builders produce spec-compliant events, run the validation script: - -```bash -node scripts/agent/validate-events.mjs -``` - -This script generates example events for all defined builders and checks them against their schemas, ensuring: -- Correct kind -- Presence of required tags (topic, identifier, etc.) -- Correct content format (JSON vs text vs empty) - ## Runtime helpers ```js diff --git a/examples/reproducers/login-modal-bug-repro.md b/examples/reproducers/login-modal-bug-repro.md deleted file mode 100644 index 47041478..00000000 --- a/examples/reproducers/login-modal-bug-repro.md +++ /dev/null @@ -1,17 +0,0 @@ -# Login Modal Bug - -## Bug Description -The login modal fails to show error messages when invalid input (or no input) is submitted. Specifically, in the nsec/key login flow, submitting without entering a key should display an error message "Paste an nsec, hex key, or mnemonic to continue.", but the error element remains hidden. This is observed in `tests/e2e/ui-modals.spec.ts`. - -## Steps to Reproduce -1. Install dependencies: `npm install` -2. Run the reproducer script: `npx playwright test examples/reproducers/login-modal-bug-repro.spec.ts` - -## Logs -``` -Error: expect(locator).toBeVisible() failed -Locator: locator('[data-nsec-error]') -Expected: visible -Received: hidden -Timeout: 5000ms -``` diff --git a/examples/reproducers/login-modal-bug-repro.spec.ts b/examples/reproducers/login-modal-bug-repro.spec.ts deleted file mode 100644 index 0d5dc990..00000000 --- a/examples/reproducers/login-modal-bug-repro.spec.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { expect, test } from "@playwright/test"; -import type { Page } from "@playwright/test"; - -// Helpers extracted from tests/e2e/helpers/uiTestUtils.ts to avoid relative import issues -export async function applyReducedMotion(page: Page) { - await page.evaluate(() => { - const style = document.createElement("style"); - style.innerHTML = ` - *, *::before, *::after { - transition-duration: 0.01ms !important; - animation-duration: 0.01ms !important; - animation-iteration-count: 1 !important; - scroll-behavior: auto !important; - } - `; - document.head.appendChild(style); - }); -} - -export function failOnConsoleErrors(page: Page) { - page.on("console", (message) => { - if (message.type() === "error") { - // Ignore known false positives or non-blocking errors if necessary - // For now, fail on any error - throw new Error(`Console error: ${message.text()}`); - } - }); -} - -test.describe("login modal bug reproduction", () => { - test.beforeEach(async ({ page }) => { - await applyReducedMotion(page); - failOnConsoleErrors(page); - await page.goto("/components/login-modal.html", { waitUntil: "networkidle" }); - - await page.addScriptTag({ - type: "module", - content: ` - import LoginModalController from "/js/ui/loginModalController.js"; - import { - prepareStaticModal, - openStaticModal, - closeStaticModal, - } from "/js/ui/components/staticModalAccessibility.js"; - - const modalElement = document.getElementById("loginModal"); - window.__testAuth = { - lastRequest: null, - }; - - const authService = { - requestLogin: async (options) => { - window.__testAuth.lastRequest = options; - return { pubkey: "npub-test" }; - }, - hydrateFromStorage: async () => null, - on: () => () => {}, - }; - - const providers = [ - { - id: "nsec", - label: "Secret key", - description: "Use a raw key", - login: async () => {}, - }, - ]; - - const prepareModal = (modal) => - prepareStaticModal({ root: modal }) || modal; - - const openModal = ({ modal, triggerElement } = {}) => - openStaticModal(modal, { triggerElement }); - - const closeModal = () => { - if (modalElement) { - closeStaticModal(modalElement); - } - }; - - window.__loginController = new LoginModalController({ - modalElement, - providers, - services: { - authService, - }, - helpers: { - prepareModal, - openModal, - closeModal, - setModalState: (name, isOpen) => { - window.__testModalState = { name, isOpen }; - }, - }, - }); - - window.__loginReady = true; - `, - }); - - await page.waitForFunction(() => window.__loginReady === true); - }); - - test("login modal error message should be visible", async ({ page }) => { - const modal = page.locator("#loginModal"); - - await page.evaluate(() => window.__loginController.openModal()); - await expect(modal).toHaveAttribute("data-open", "true"); - - await page - .locator('[data-provider-button][data-provider-id="nsec"]') - .click(); - await expect(page.locator("[data-nsec-secret]")).toBeVisible(); - - // Trigger the empty input error - await page.locator("[data-nsec-submit]").click(); - - const errorMessage = page.locator("[data-nsec-error]"); - - // This expectation fails in the current codebase - // The error message remains hidden - await expect(errorMessage).toBeVisible(); - await expect(errorMessage).toHaveText( - "Paste an nsec, hex key, or mnemonic to continue.", - ); - }); -}); diff --git a/examples/reproducers/moderation-bug-repro.md b/examples/reproducers/moderation-bug-repro.md deleted file mode 100644 index ec107ad6..00000000 --- a/examples/reproducers/moderation-bug-repro.md +++ /dev/null @@ -1,17 +0,0 @@ -# Moderation UI Bug - -## Bug Description -The "Show anyway" button in the moderation UI fails to render in the test environment, causing `toBeVisible()` assertions to fail. This is observed in `tests/visual/moderation.spec.ts`. The logic for creating the button appears correct (`allowOverride` is true), but the button element is not found in the DOM during the test execution. This might be due to a race condition in the test fixture's state initialization or an environment-specific rendering issue. - -## Steps to Reproduce -1. Install dependencies: `npm install` -2. Run the reproducer script: `npx playwright test examples/reproducers/moderation-bug-repro.spec.ts` - -## Logs -``` -Error: expect(locator).toBeVisible() failed -Locator: locator('[data-test-id="show-anyway"]').getByRole('button', { name: 'Show anyway' }) -Expected: visible -Timeout: 5000ms -Error: element(s) not found -``` diff --git a/examples/reproducers/moderation-bug-repro.spec.ts b/examples/reproducers/moderation-bug-repro.spec.ts deleted file mode 100644 index 47745458..00000000 --- a/examples/reproducers/moderation-bug-repro.spec.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { expect, test } from "@playwright/test"; - -const FIXTURE_URL = "/docs/moderation/fixtures/index.html"; -const STORAGE_KEY = "bitvid:moderation:fixture-overrides"; -const SENTINEL_KEY = "__moderationFixturesInit__"; - -function setupStorageReset(page) { - return page.addInitScript(({ storageKey, sentinelKey }) => { - try { - const session = window.sessionStorage; - const local = window.localStorage; - if (session && !session.getItem(sentinelKey)) { - local?.removeItem(storageKey); - session.setItem(sentinelKey, "1"); - } - } catch { - /* ignore reset failures in tests */ - } - }, { storageKey: STORAGE_KEY, sentinelKey: SENTINEL_KEY }); -} - -test.beforeEach(async ({ page }) => { - await setupStorageReset(page); -}); - -test.describe("moderation bug reproduction", () => { - test("show anyway override persists across reloads", async ({ page }) => { - await page.goto(FIXTURE_URL, { waitUntil: "networkidle" }); - - const overrideCard = page.locator('[data-test-id="show-anyway"]'); - const showAnywayButton = overrideCard.getByRole("button", { name: "Show anyway" }); - - // This expectation fails in the current codebase - // The "Show anyway" button should be visible but it's not found in the DOM - await expect(showAnywayButton).toBeVisible(); - }); -}); diff --git a/examples/reproducers/popover-bug-repro.md b/examples/reproducers/popover-bug-repro.md deleted file mode 100644 index 80e3da26..00000000 --- a/examples/reproducers/popover-bug-repro.md +++ /dev/null @@ -1,16 +0,0 @@ -# Popover ReferenceError Bug - -## Bug Description -The `tests/e2e/popover.spec.ts` test suite fails with `ReferenceError: getPanelWithTriggerMetrics is not defined`. This function appears to be defined in the test file, but for some reason, it is not accessible in the test scope where it is called. - -## Steps to Reproduce -1. Install dependencies: `npm install` -2. Run the reproducer script: `npx playwright test examples/reproducers/popover-bug-repro.spec.ts` - -Note: The provided reproducer script `examples/reproducers/popover-bug-repro.spec.ts` *fixes* the issue by correctly defining the function within the test file scope (or rather, copying the logic to where it works), effectively demonstrating what the code *should* do if the reference error didn't exist. To see the original error, run the original test suite: `npx playwright test tests/e2e/popover.spec.ts`. - -## Logs -``` -ReferenceError: getPanelWithTriggerMetrics is not defined - at /app/tests/e2e/popover.spec.ts:196:21 -``` diff --git a/examples/reproducers/popover-bug-repro.spec.ts b/examples/reproducers/popover-bug-repro.spec.ts deleted file mode 100644 index 4d00a191..00000000 --- a/examples/reproducers/popover-bug-repro.spec.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { expect, test } from "@playwright/test"; -import type { Locator } from "@playwright/test"; - -// Simplified helper functions extracted from tests/e2e/popover.spec.ts to reproduce the ReferenceError -// The error "ReferenceError: getPanelWithTriggerMetrics is not defined" occurs in the test context -// even though the function appears to be defined in the file. - -test.describe("popover bug reproduction", () => { - test.beforeEach(async ({ page }) => { - // Navigate to the demo page - await page.goto("/views/dev/popover-demo.html", { waitUntil: "networkidle" }); - }); - - // Re-declaration of the function, similar to how it is in the failing test file. - // Note: The original test seems to define this function in the outer scope, - // but something about how Playwright handles scope or compilation might be causing the issue. - // However, the error 'ReferenceError: getPanelWithTriggerMetrics is not defined' - // suggests it is not available when called. - - async function getPanelWithTriggerMetrics(panel: Locator, trigger: Locator) { - const triggerHandle = await trigger.elementHandle(); - if (!triggerHandle) { - throw new Error("Trigger element is not attached"); - } - - try { - return await panel.evaluate((node, triggerElement) => { - const rect = node.getBoundingClientRect(); - const triggerRect = triggerElement.getBoundingClientRect(); - const viewportWidth = window.innerWidth; - const viewportHeight = window.innerHeight; - - return { - rect: { - top: rect.top, - left: rect.left, - right: rect.right, - bottom: rect.bottom, - width: rect.width, - height: rect.height, - }, - triggerRect: { - top: triggerRect.top, - left: triggerRect.left, - right: triggerRect.right, - bottom: triggerRect.bottom, - width: triggerRect.width, - height: triggerRect.height, - }, - viewport: { width: viewportWidth, height: viewportHeight }, - placement: node.dataset.popoverPlacement || "", - state: node.dataset.popoverState || "", - }; - }, triggerHandle); - } finally { - await triggerHandle.dispose(); - } - } - - function assertWithinViewport(metrics: { - rect: { top: number; left: number; right: number; bottom: number }; - viewport: { width: number; height: number }; - }) { - const tolerance = 0.5; - expect(metrics.rect.left).toBeGreaterThanOrEqual(-tolerance); - expect(metrics.rect.top).toBeGreaterThanOrEqual(-tolerance); - expect(metrics.rect.right).toBeLessThanOrEqual(metrics.viewport.width + tolerance); - expect(metrics.rect.bottom).toBeLessThanOrEqual(metrics.viewport.height + tolerance); - } - - test("aligns the video card menu with the trigger's right edge", async ({ page }) => { - const trigger = page.locator("[data-demo-card-more]"); - await trigger.click(); - - const panel = page.locator('[data-component="overlay-root"] [data-popover-state="open"]'); - await expect(panel).toBeVisible(); - - // This is where the error happens in the original test - const metrics = await getPanelWithTriggerMetrics(panel, trigger); - - assertWithinViewport(metrics); - expect(metrics.state).toBe("open"); - expect(metrics.placement).toBe("bottom-end"); - - const tolerance = 1.5; - expect(Math.abs(metrics.rect.right - metrics.triggerRect.right)).toBeLessThanOrEqual(tolerance); - }); -}); diff --git a/examples/reproducers/sidebar-bug-repro.md b/examples/reproducers/sidebar-bug-repro.md deleted file mode 100644 index 1cfc3579..00000000 --- a/examples/reproducers/sidebar-bug-repro.md +++ /dev/null @@ -1,16 +0,0 @@ -# Mobile Sidebar Layout Bug - -## Bug Description -The `tests/visual/overlay-layers.spec.ts` test suite fails in the "mobile sidebar shares desktop rail behavior" test. There is a mismatch between the expected margin of the app container and the computed margin in the test environment (diff of ~45px or ~12.8px depending on environment). This indicates a layout issue where the mobile sidebar or app container margins are not being calculated or applied correctly according to the CSS variables. - -## Steps to Reproduce -1. Install dependencies: `npm install` -2. Run the reproducer script: `npx playwright test examples/reproducers/sidebar-bug-repro.spec.ts` - -## Logs -``` -Error: expect(received).toBeLessThan(expected) - -Expected: < 0.5 -Received: 12.799999999999997 -``` diff --git a/examples/reproducers/sidebar-bug-repro.spec.ts b/examples/reproducers/sidebar-bug-repro.spec.ts deleted file mode 100644 index 32abd0b1..00000000 --- a/examples/reproducers/sidebar-bug-repro.spec.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { expect, test } from "@playwright/test"; -import type { Page } from "@playwright/test"; - -test.describe("sidebar bug reproduction", () => { - async function dismissDisclaimerModal(page: Page) { - const modal = page.locator("#disclaimerModal"); - if ((await modal.count()) === 0) { - return; - } - - await page.evaluate(() => { - try { - window.localStorage?.setItem("hasSeenDisclaimer", "true"); - } catch (error) { - console.warn("Failed to persist disclaimer state", error); - } - document - .querySelectorAll("#disclaimerModal") - .forEach((node) => { - node.classList.add("hidden"); - node.setAttribute("data-open", "false"); - }); - document.documentElement?.classList.remove("modal-open"); - document.body?.classList.remove("modal-open"); - }); - } - - test("mobile sidebar margin mismatch", async ({ page }) => { - // Set viewport to mobile size - await page.setViewportSize({ width: 390, height: 844 }); - await page.goto("/index.html", { waitUntil: "networkidle" }); - - await dismissDisclaimerModal(page); - - const initialLayout = await page.evaluate(() => { - const sidebar = document.getElementById("sidebar"); - const app = document.getElementById("app"); - if (!sidebar || !app) { - throw new Error("Missing sidebar layout nodes"); - } - - // Helper to compute expected margins from CSS variables - const ensureProbeStyles = () => { - if (document.getElementById("sidebar-width-probe-style")) { - return; - } - const style = document.createElement("style"); - style.id = "sidebar-width-probe-style"; - style.textContent = ` - .sidebar-width-probe { - position: absolute; - visibility: hidden; - } - - .sidebar-width-probe--collapsed { - width: calc(var(--sidebar-width-collapsed) + var(--sidebar-content-gap)); - } - `; - document.head.appendChild(style); - }; - - ensureProbeStyles(); - - const computeMargin = () => { - const probe = document.createElement("div"); - probe.classList.add("sidebar-width-probe", "sidebar-width-probe--collapsed"); - document.body.appendChild(probe); - const width = parseFloat(window.getComputedStyle(probe).width); - probe.remove(); - return width; - }; - - const marginLeft = parseFloat(window.getComputedStyle(app).marginLeft); - const state = sidebar.classList.contains("sidebar-expanded") - ? "expanded" - : sidebar.classList.contains("sidebar-collapsed") - ? "collapsed" - : "unknown"; - - return { - state, - marginLeft, - collapsedMargin: computeMargin(), - }; - }); - - // Verify initial state - expect(initialLayout.state).toBe("collapsed"); - - // This expectation fails in the current codebase - // There is a mismatch between the actual margin and the calculated margin from CSS variables - expect(Math.abs(initialLayout.marginLeft - initialLayout.collapsedMargin)).toBeLessThan(0.5); - }); -}); diff --git a/js/app.js b/js/app.js index c689207e..37467795 100644 --- a/js/app.js +++ b/js/app.js @@ -6641,7 +6641,19 @@ class Application { } decorateVideoModeration(video, feedContext = {}) { - return this.moderationDecorator.decorateVideo(video, feedContext); + const decorated = this.moderationDecorator.decorateVideo(video, feedContext); + if ( + video && + video.pubkey && + this.isAuthorBlocked(video.pubkey) && + decorated && + decorated.moderation + ) { + decorated.moderation.viewerMuted = true; + decorated.moderation.hidden = true; + decorated.moderation.hideReason = "viewer-block"; + } + return decorated; } initializeModerationActionController() { diff --git a/js/disclaimer.js b/js/disclaimer.js index cf7f8c1f..8fa7df76 100644 --- a/js/disclaimer.js +++ b/js/disclaimer.js @@ -45,11 +45,6 @@ class DisclaimerModal { } show() { - // Check if Playwright automation requested to bypass the disclaimer - if (localStorage.getItem("playwright-bypass-disclaimer") === "true") { - return; - } - // In case the modal hasn't been initialized yet. if (!this.modal) { this.init(); diff --git a/js/index.js b/js/index.js index c6a1902e..55224106 100644 --- a/js/index.js +++ b/js/index.js @@ -1223,10 +1223,7 @@ async function bootstrapInterface() { if (closeNostrFormBtn) { closeNostrFormBtn.addEventListener("click", () => { closeStaticModal("nostrFormModal"); - if ( - !localStorage.getItem("hasSeenDisclaimer") && - localStorage.getItem("playwright-bypass-disclaimer") !== "true" - ) { + if (!localStorage.getItem("hasSeenDisclaimer")) { const disclaimerModal = document.getElementById("disclaimerModal"); if (disclaimerModal) { openStaticModal(disclaimerModal); @@ -1309,10 +1306,7 @@ function handleQueryParams() { if (closeAppealsBtn) { closeAppealsBtn.addEventListener("click", () => { closeStaticModal("contentAppealsModal"); - if ( - !localStorage.getItem("hasSeenDisclaimer") && - localStorage.getItem("playwright-bypass-disclaimer") !== "true" - ) { + if (!localStorage.getItem("hasSeenDisclaimer")) { const disclaimerModal = document.getElementById("disclaimerModal"); if (disclaimerModal) { openStaticModal(disclaimerModal); @@ -1329,10 +1323,7 @@ function handleQueryParams() { } } else { const hasSeenDisclaimer = localStorage.getItem("hasSeenDisclaimer"); - const bypassDisclaimer = - localStorage.getItem("playwright-bypass-disclaimer") === "true"; - - if (!hasSeenDisclaimer && !bypassDisclaimer) { + if (!hasSeenDisclaimer) { const disclaimerModal = document.getElementById("disclaimerModal"); if (disclaimerModal) { openStaticModal(disclaimerModal); diff --git a/js/nostrEventSchemas.js b/js/nostrEventSchemas.js index 09eae359..117ce77f 100644 --- a/js/nostrEventSchemas.js +++ b/js/nostrEventSchemas.js @@ -891,7 +891,7 @@ export function buildRepostEvent(params) { if (normalizedEventRelay) { tags.push(["e", normalizedEventId, normalizedEventRelay]); } else { - throw new Error("missing-event-relay"); + tags.push(["e", normalizedEventId]); } } diff --git a/js/services/moderationActionController.js b/js/services/moderationActionController.js index 5df72924..4ce95cfa 100644 --- a/js/services/moderationActionController.js +++ b/js/services/moderationActionController.js @@ -206,6 +206,12 @@ export default class ModerationActionController { this.clearHideBypass(target); this.decorateVideo(target); + if (target !== video) { + this.clearViewerOverride(video); + this.clearHideBypass(video); + this.decorateVideo(video); + } + const current = this.getCurrentVideo(); if (current && current.id === video.id) { this.clearViewerOverride(current); diff --git a/package-lock.json b/package-lock.json index 0fd69ca7..cdd0e7d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,7 @@ "playwright": "^1.57.0", "pngjs": "^7.0.0", "postcss": "^8.4.41", - "prettier": "^3.8.1", + "prettier": "^3.8.0", "prettier-plugin-tailwindcss": "^0.6.5", "stylelint": "^16.26.1", "stylelint-config-standard": "^36.0.1", @@ -3244,9 +3244,9 @@ "license": "MIT" }, "node_modules/prettier": { - "version": "3.8.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.1.tgz", - "integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==", + "version": "3.8.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.0.tgz", + "integrity": "sha512-yEPsovQfpxYfgWNhCfECjG5AQaO+K3dp6XERmOepyPDVqcJm+bjyCVO3pmU+nAPe0N5dDvekfGezt/EIiRe1TA==", "dev": true, "license": "MIT", "peer": true, diff --git a/package.json b/package.json index d87d02d3..049eb220 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "playwright": "^1.57.0", "pngjs": "^7.0.0", "postcss": "^8.4.41", - "prettier": "^3.8.1", + "prettier": "^3.8.0", "prettier-plugin-tailwindcss": "^0.6.5", "stylelint": "^16.26.1", "stylelint-config-standard": "^36.0.1", diff --git a/scripts/agent/interop-test.mjs b/scripts/agent/interop-test.mjs index 8f4d310a..40dbf9ac 100644 --- a/scripts/agent/interop-test.mjs +++ b/scripts/agent/interop-test.mjs @@ -1,308 +1,259 @@ -import { WebSocket } from 'ws'; -import crypto from 'node:crypto'; -import { spawn } from 'node:child_process'; -import { resolve } from 'node:path'; -import { setTimeout } from 'node:timers/promises'; +// Import environment setup FIRST to ensure polyfills are ready before other imports +import './setup-test-env.js'; -// --- Polyfills --- -if (!globalThis.WebSocket) { - globalThis.WebSocket = WebSocket; -} -if (!globalThis.crypto) { - globalThis.crypto = crypto; -} -if (!globalThis.window) { - globalThis.window = globalThis; -} +import * as NostrTools from 'nostr-tools'; +// Destructure what we need, handling potential version differences if necessary +const { generateSecretKey, getPublicKey, nip19, nip04, nip44 } = NostrTools; -// Ensure TextEncoder is available (it should be in Node > 11) -if (!globalThis.TextEncoder) { - const { TextEncoder, TextDecoder } = await import('util'); - globalThis.TextEncoder = TextEncoder; - globalThis.TextDecoder = TextDecoder; -} - -// --- Imports --- -// We need to import nostr-tools. Since it's an ESM module in node_modules, dynamic import should work. -const nostrTools = await import('nostr-tools'); -const { generateSecretKey, getPublicKey, finalizeEvent } = nostrTools; - -// Import Schema Builders +// --- Imports from Codebase --- +import { NostrClient } from '../../js/nostr/client.js'; +import { decryptDM } from '../../js/dmDecryptor.js'; import { buildVideoPostEvent, buildViewEvent, - buildLegacyDirectMessageEvent, - getNostrEventSchema, - NOTE_TYPES, + NOTE_TYPES } from '../../js/nostrEventSchemas.js'; -// Import DM Decryptor -import { decryptDM } from '../../js/dmDecryptor.js'; +// --- Configuration --- +// Using a small, respectful list of public relays for interop testing +const TEST_RELAYS = [ + 'wss://relay.damus.io', + 'wss://nos.lol' +]; // --- Helpers --- function bytesToHex(bytes) { return Buffer.from(bytes).toString('hex'); } -function hexToBytes(hex) { - if (typeof hex !== 'string') { - throw new TypeError('hexToBytes: expected string, got ' + typeof hex); - } - if (hex.length % 2) throw new Error('hexToBytes: received string with odd length'); - const array = new Uint8Array(hex.length / 2); - for (let i = 0; i < hex.length; i += 2) { - array[i / 2] = parseInt(hex.slice(i, i + 2), 16); - } - return array; -} - -// Minimal Nostr Client Wrapper for Testing -// We are manually constructing events and using a simple WebSocket connection or NostrClient if possible. -// Given the complexity of instantiating `NostrClient` in a pure Node environment without full DOM (IndexedDB, localStorage etc), -// we will verify the schema builders and use a lightweight interaction for publishing/subscribing. -// However, the prompt asks to use `nostrClientFacade` or `NostrClient`. -// The `NostrClient` class depends heavily on browser APIs (IndexedDB, localStorage). -// We will mock necessary browser APIs to instantiate `NostrClient` if possible, or use a simplified test client that respects the same logic. - -// Let's try to mock the minimal environment for `NostrClient` -class MockLocalStorage { - constructor() { this.store = {}; } - getItem(key) { return this.store[key] || null; } - setItem(key, value) { this.store[key] = String(value); } - removeItem(key) { delete this.store[key]; } - clear() { this.store = {}; } -} -globalThis.localStorage = new MockLocalStorage(); -// IndexedDB is more complex. We might mock `EventsCacheStore` methods if we can override them or just let them fail gracefully (NostrClient handles failures). -// Or we can mock `indexedDB` global. -import 'fake-indexeddb/auto'; // If available? No, not in dependencies list. -// Dependencies list has `fake-indexeddb` in devDependencies. -// Let's import it. -try { - await import('fake-indexeddb/auto'); -} catch (e) { - console.warn("fake-indexeddb not found, IndexedDB might fail."); -} - -// Now we can try to import NostrClient -// We need to handle `vendor/crypto-helpers.bundle.min.js` import in `js/nostr/client.js`. -// Since we are in Node, that relative import might fail or work depending on how Node handles it. -// It is a file in the repo. Node should be able to import it if it's ESM. -// `js/nostr/client.js` imports it as `../../vendor/crypto-helpers.bundle.min.js`. -// We are in `scripts/agent/interop-test.mjs`, so `../../js/nostr/client.js` is where we import from. -// `js/nostr/client.js` is relative to root. -// Let's see if we can import `NostrClient`. - -import { NostrClient } from '../../js/nostr/client.js'; - -// --- Test Setup --- -const RELAY_PORT = 8008; -const RELAY_URL = `ws://localhost:${RELAY_PORT}`; -let relayProcess; - -async function startRelay() { - console.log('Starting local relay...'); - const relayScript = resolve(process.cwd(), 'scripts/agent/simple-relay.mjs'); - relayProcess = spawn('node', [relayScript], { stdio: 'inherit' }); - - // Wait for relay to be ready - for (let i = 0; i < 20; i++) { - try { - const ws = new WebSocket(RELAY_URL); - await new Promise((resolve, reject) => { - ws.on('open', () => { ws.close(); resolve(); }); - ws.on('error', reject); - }); - console.log('Relay is ready.'); - return; - } catch (e) { - await setTimeout(500); - } - } - throw new Error('Relay failed to start'); -} - -function stopRelay() { - if (relayProcess) { - console.log('Stopping local relay...'); - relayProcess.kill(); - } -} - -// --- Main Test Logic --- +// --- Main Test --- async function runTests() { - let exitCode = 0; + console.log("=== Starting Protocol & Interop Tests ==="); + console.log(`[Config] Relays: ${TEST_RELAYS.join(', ')}`); + + // 1. Initialize Client + // Programmatic instantiation as requested + const client = new NostrClient(); + client.relays = [...TEST_RELAYS]; + client.readRelays = [...TEST_RELAYS]; + client.writeRelays = [...TEST_RELAYS]; + + // Initialize connection + await client.init(); + console.log("[Setup] NostrClient initialized and connecting..."); + + // Wait a bit for connections to establish + await new Promise(r => setTimeout(r, 1000)); + + // 2. Generate Ephemeral Keys + // Use ephemeral keys (generate locally) as requested + const aliceSkBytes = generateSecretKey(); + const aliceSk = bytesToHex(aliceSkBytes); + const alicePk = getPublicKey(aliceSkBytes); + console.log(`[Keys] Alice (Sender): ${alicePk}`); + + const bobSkBytes = generateSecretKey(); + const bobSk = bytesToHex(bobSkBytes); + const bobPk = getPublicKey(bobSkBytes); + console.log(`[Keys] Bob (Receiver): ${bobPk}`); + + // Register Alice as signer for the client + await client.registerPrivateKeySigner({ privateKey: aliceSk }); + console.log("[Setup] Alice registered as active signer."); + + // --- Test A: Publish VIDEO_POST --- + console.log("\n>>> Test A: Publish VIDEO_POST"); + const videoRootId = `test-${Date.now()}-${Math.random().toString(36).substring(2)}`; + const videoContent = { + version: 3, + title: `Interop Test Video ${Date.now()}`, + description: "Automated interop test video event.", + url: "https://example.com/video.mp4", + thumbnail: "https://example.com/thumb.jpg", + mimeType: "video/mp4", + isForKids: false, + isNsfw: false, + enableComments: false, + videoRootId, + mode: "live" + }; + + // Use schema builder + const videoEventTemplate = buildVideoPostEvent({ + pubkey: alicePk, + created_at: Math.floor(Date.now() / 1000), + dTagValue: videoRootId, + content: videoContent + }); + + let videoEvent; try { - await startRelay(); + const result = await client.signAndPublishEvent(videoEventTemplate); + videoEvent = result.signedEvent; - // 1. Generate keys - const sk = generateSecretKey(); - const pk = getPublicKey(sk); - const skHex = bytesToHex(sk); - const pkHex = pk; - console.log(`Ephemeral identity: ${pkHex}`); + // Check summary for acceptance + const summary = result.summary; + const acceptedCount = summary?.accepted?.length || 0; - // 2. Instantiate Client - const client = new NostrClient(); - // Override relays to use our local test relay - client.relays = [RELAY_URL]; - client.readRelays = [RELAY_URL]; - client.writeRelays = [RELAY_URL]; - - // Inject custom signer adapter for the test - const signer = { - pubkey: pkHex, - signEvent: async (event) => { - return finalizeEvent(event, sk); - }, - nip04Encrypt: async (pubkey, plaintext) => { - return await nostrTools.nip04.encrypt(sk, pubkey, plaintext); - }, - nip04Decrypt: async (pubkey, ciphertext) => { - return await nostrTools.nip04.decrypt(sk, pubkey, ciphertext); - } - }; - // We need to set this signer as active - // We can use the registry or just manually set it if the client exposes a way. - // Client.js has `setActiveSigner` exported? No, it's not a method of NostrClient instance, but exported from module. - // But `NostrClient` imports `setActiveSigner` from `../nostrClientRegistry.js`. - // We can try to import `setActiveSigner` from `nostrClientRegistry.js` and call it. - - const { setActiveSigner } = await import('../../js/nostrClientRegistry.js'); - setActiveSigner(signer); - - // Also we need to ensure the client is "logged in" or at least has a pubkey set for some operations - client.pubkey = pkHex; - - await client.init(); - console.log('Client initialized.'); - - // Test A: Video Post - console.log('\n--- Test A: Video Post ---'); - const videoData = { - title: "Test Video " + Date.now(), - description: "Integration test video", - magnet: "magnet:?xt=urn:btih:c917253504812345678901234567890123456789&dn=test", - isPrivate: false - }; - // Use schema builder - const videoEventUnsigned = buildVideoPostEvent({ - pubkey: pkHex, - created_at: Math.floor(Date.now() / 1000), - dTagValue: "test-video-" + Date.now(), - content: videoData - }); - // Sign and publish - const signedVideoEvent = await signer.signEvent(videoEventUnsigned); - console.log(`Publishing Video Event: ${signedVideoEvent.id}`); - - // Using client.publishEventToRelays or similar? - // Client has `publishVideo` but it does a lot of logic. - // Let's use `client.signAndPublishEvent` if possible, or low level pool publish. - // `client.pool` should be available. - await Promise.all(client.relays.map(url => { - const sub = client.pool.publish([url], signedVideoEvent); - return new Promise((resolve) => { - // We can just assume it sends for this test, or wait a bit. - // SimplePool publish returns a Promise in newer versions? - // In `js/nostr/toolkit.js` it seems to return an object with Promise? - // Actually `publishEventToRelay` in `js/nostrPublish.js` is what `client` uses. - // Let's use `client.signAndPublishEvent` if we can. - // `signAndPublishEvent` helper is used by `client`. - resolve(); - }); - })); - await setTimeout(200); // Wait for relay processing - - // Verify - const fetchedEvent = await client.getEventById(signedVideoEvent.id, { includeRaw: true }); - if (fetchedEvent && fetchedEvent.video && fetchedEvent.video.title === videoData.title) { - console.log('✅ Video Post verification successful'); - } else { - console.error('❌ Video Post verification failed'); - console.error('Fetched:', fetchedEvent); - exitCode = 1; + if (acceptedCount === 0) { + throw new Error(`Relays rejected the video event. Details: ${JSON.stringify(summary)}`); } - - // Test B: View Event - console.log('\n--- Test B: View Event ---'); - const viewEventUnsigned = buildViewEvent({ - pubkey: pkHex, - created_at: Math.floor(Date.now() / 1000), - pointerTag: ['a', `30078:${pkHex}:${videoData.title}`], // Simplification - content: "viewed" - }); - const signedViewEvent = await signer.signEvent(viewEventUnsigned); - // Publish - await Promise.all(client.relays.map(url => client.pool.publish([url], signedViewEvent))); - await setTimeout(200); - - // Verify by fetching - const fetchedView = await client.fetchRawEventById(signedViewEvent.id); - if (fetchedView && fetchedView.id === signedViewEvent.id) { - console.log('✅ View Event verification successful'); - } else { - console.error('❌ View Event verification failed'); - exitCode = 1; - } - - // Test C: DM - console.log('\n--- Test C: Direct Message ---'); - const dmMessage = "Hello from interop test"; - const recipientPubkey = pkHex; // Self-DM for simplicity - - // We can use `client.sendDirectMessage` or build manually. - // Let's use buildLegacyDirectMessageEvent + decryptDM to test the decryptor explicitly as requested. - - // Encrypt - const ciphertext = await signer.nip04Encrypt(recipientPubkey, dmMessage); - const dmEventUnsigned = buildLegacyDirectMessageEvent({ - pubkey: pkHex, - created_at: Math.floor(Date.now() / 1000), - recipientPubkey: recipientPubkey, - ciphertext: ciphertext - }); - const signedDmEvent = await signer.signEvent(dmEventUnsigned); - - // Publish - await Promise.all(client.relays.map(url => client.pool.publish([url], signedDmEvent))); - await setTimeout(200); - - // Fetch and Decrypt - const fetchedDm = await client.fetchRawEventById(signedDmEvent.id); - if (!fetchedDm) { - console.error('❌ DM Event fetch failed'); - exitCode = 1; - } else { - // Use decryptDM helper - // We need to provide a context with a decryptor - const decryptorContext = { - actorPubkey: pkHex, - decryptors: [{ - scheme: 'nip04', - decrypt: signer.nip04Decrypt, - source: 'test-signer' - }] - }; - - const decryptionResult = await decryptDM(fetchedDm, decryptorContext); - - if (decryptionResult.ok && decryptionResult.plaintext === dmMessage) { - console.log('✅ DM Decryption successful'); - } else { - console.error('❌ DM Decryption failed'); - console.error('Result:', decryptionResult); - exitCode = 1; - } - } - - } catch (error) { - console.error('Test failed with error:', error); - exitCode = 1; - } finally { - stopRelay(); - process.exit(exitCode); + console.log(`[Pass] Published Video Event ID: ${videoEvent.id} (Accepted by ${acceptedCount} relays)`); + } catch (e) { + console.error("[Fail] Video publish failed:", e); + process.exit(1); } + + // --- Test B: Fetch & Verify --- + console.log("\n>>> Test B: Fetch & Verify Roundtrip"); + // Allow propagation + await new Promise(r => setTimeout(r, 2000)); + + try { + const result = await client.getEventById(videoEvent.id, { includeRaw: true }); + + if (!result || !result.rawEvent) { + throw new Error("Could not fetch raw event back from relays."); + } + + const { video, rawEvent } = result; + + if (rawEvent.id !== videoEvent.id) { + throw new Error(`ID mismatch. Expected ${videoEvent.id}, got ${rawEvent.id}`); + } + + // Verify content shape + const content = JSON.parse(rawEvent.content); + if (content.title !== videoContent.title) { + throw new Error(`Content mismatch. Title: ${content.title} vs ${videoContent.title}`); + } + + // Verify signature (basic check) + const isSigValid = NostrTools.verifyEvent(rawEvent); + if (!isSigValid) { + throw new Error("Event signature verification failed."); + } + + console.log("[Pass] Event fetched, signature verified, and content matches."); + } catch (e) { + console.error(`[Fail] Fetch & Verify failed: ${e.message}`); + process.exit(1); + } + + // --- Test C: Publish VIEW_EVENT --- + console.log("\n>>> Test C: Publish VIEW_EVENT"); + const viewEventTemplate = buildViewEvent({ + pubkey: alicePk, + created_at: Math.floor(Date.now() / 1000), + pointerValue: videoEvent.id, + pointerTag: ['e', videoEvent.id], + dedupeTag: `view:${videoEvent.id}:${Date.now()}`, + includeSessionTag: false // Not a session actor + }); + + try { + const viewResult = await client.signAndPublishEvent(viewEventTemplate); + const acceptedCount = viewResult.summary?.accepted?.length || 0; + if (acceptedCount > 0) { + console.log(`[Pass] View Event published. ID: ${viewResult.signedEvent.id}`); + } else { + console.warn(`[Warn] View Event publish result not OK:`, viewResult); + } + } catch (e) { + console.error("[Fail] View Event publish threw:", e); + } + + // --- Test D: Encrypted DM --- + console.log("\n>>> Test D: Direct Message (Encryption/Decryption)"); + const dmMessage = `Secret message ${Date.now()}`; + + // We are Alice. Send to Bob. + // Note: client.sendDirectMessage uses the registered signer (Alice) + console.log(`[DM] Sending from Alice to Bob...`); + try { + const bobNpub = nip19.npubEncode(bobPk); + const sendRes = await client.sendDirectMessage(bobNpub, dmMessage); + if (!sendRes.ok) { + throw new Error(sendRes.error || "Unknown error sending DM"); + } + console.log("[DM] Message sent."); + } catch (e) { + console.error("[Fail] DM Send failed:", e); + process.exit(1); + } + + // Wait for propagation + await new Promise(r => setTimeout(r, 2000)); + + // Verify by fetching as Bob + // We need to use the pool directly to fetch, as 'client' is configured as Alice + console.log(`[DM] Fetching events for Bob (p=${bobPk})...`); + const filter = { + kinds: [4, 1059], // Legacy and GiftWrap + '#p': [bobPk], + authors: [alicePk], // Sent by Alice + limit: 1 + }; + + const dmEvents = await client.pool.list(TEST_RELAYS, [filter]); + console.log(`[DM] Found ${dmEvents.length} events for Bob.`); + + if (dmEvents.length === 0) { + console.error("[Fail] No DM events found on relays."); + process.exit(1); + } + + const targetEvent = dmEvents[0]; + console.log(`[DM] Decrypting event kind ${targetEvent.kind}...`); + + // Setup Bob's decryptors + const bobDecryptors = []; + + // NIP-44 + if (nip44) { + bobDecryptors.push({ + scheme: 'nip44', + decrypt: (pk, ct) => nip44.decrypt(bobSk, pk, ct), + priority: 1, + supportsGiftWrap: true + }); + } + // NIP-04 + if (nip04) { + bobDecryptors.push({ + scheme: 'nip04', + decrypt: async (pk, ct) => nip04.decrypt(bobSk, pk, ct), + priority: 0 + }); + } + + const decryptContext = { + actorPubkey: bobPk, + decryptors: bobDecryptors + }; + + const decryptRes = await decryptDM(targetEvent, decryptContext); + + if (!decryptRes.ok) { + console.error("[Fail] Decryption failed:", decryptRes.errors); + process.exit(1); + } + + if (decryptRes.plaintext !== dmMessage) { + console.error(`[Fail] Message mismatch. Got: "${decryptRes.plaintext}", Expected: "${dmMessage}"`); + process.exit(1); + } + + console.log(`[Pass] Decryption verified. Scheme: ${decryptRes.scheme}, Message: "${decryptRes.plaintext}"`); + + // Cleanup + // In a real scenario we might delete the events, but these are ephemeral and test relays usually clean up or we accept the dust for this test. + + console.log("\n=== All Tests Passed ==="); + process.exit(0); } -runTests(); +runTests().catch(e => { + console.error("Unhandled Error:", e); + process.exit(1); +}); diff --git a/scripts/agent/validate-events.mjs b/scripts/agent/validate-events.mjs deleted file mode 100644 index 65a695d9..00000000 --- a/scripts/agent/validate-events.mjs +++ /dev/null @@ -1,350 +0,0 @@ - -import { - getNostrEventSchema, - NOTE_TYPES, - buildVideoPostEvent, - buildVideoMirrorEvent, - buildRepostEvent, - buildRelayListEvent, - buildDmRelayListEvent, - buildProfileMetadataEvent, - buildMuteListEvent, - buildDeletionEvent, - buildLegacyDirectMessageEvent, - buildDmAttachmentEvent, - buildDmReadReceiptEvent, - buildDmTypingIndicatorEvent, - buildViewEvent, - buildZapRequestEvent, - buildReactionEvent, - buildCommentEvent, - buildWatchHistoryEvent, - buildSubscriptionListEvent, - buildBlockListEvent, - buildHashtagPreferenceEvent, - buildAdminListEvent -} from '../../js/nostrEventSchemas.js'; - -const COLORS = { - RESET: "\x1b[0m", - RED: "\x1b[31m", - GREEN: "\x1b[32m", - YELLOW: "\x1b[33m", - BLUE: "\x1b[34m", -}; - -let errorCount = 0; -let successCount = 0; - -function logError(msg) { - console.error(`${COLORS.RED}[FAIL] ${msg}${COLORS.RESET}`); - errorCount++; -} - -function logSuccess(msg) { - console.log(`${COLORS.GREEN}[PASS] ${msg}${COLORS.RESET}`); - successCount++; -} - -function logInfo(msg) { - console.log(`${COLORS.BLUE}[INFO] ${msg}${COLORS.RESET}`); -} - -function hasTag(tags, tagName, tagValue = null) { - if (!Array.isArray(tags)) return false; - return tags.some( - (tag) => - Array.isArray(tag) && - tag[0] === tagName && - (tagValue === null || tag[1] === tagValue) - ); -} - -function validateEvent(type, event, description) { - const schema = getNostrEventSchema(type); - if (!schema) { - logError(`${description}: No schema found for type ${type}`); - return; - } - - let valid = true; - const errors = []; - - // Validate Kind - if (event.kind !== schema.kind) { - errors.push(`Kind mismatch: expected ${schema.kind}, got ${event.kind}`); - valid = false; - } - - // Validate Topic Tag - if (schema.topicTag) { - if (!hasTag(event.tags, schema.topicTag.name, schema.topicTag.value)) { - errors.push(`Missing topic tag: ${schema.topicTag.name}=${schema.topicTag.value}`); - valid = false; - } - } - - // Validate Identifier Tag - if (schema.identifierTag) { - const expectedValue = schema.identifierTag.value; - if (expectedValue) { - if (!hasTag(event.tags, schema.identifierTag.name, expectedValue)) { - errors.push(`Missing identifier tag: ${schema.identifierTag.name}=${expectedValue}`); - valid = false; - } - } else { - // If value is not fixed in schema, just check tag name presence if it's supposed to be there. - // Usually d-tags are present. - if (!hasTag(event.tags, schema.identifierTag.name)) { - errors.push(`Missing identifier tag: ${schema.identifierTag.name}`); - valid = false; - } - } - } - - // Validate Append Tags - if (schema.appendTags) { - schema.appendTags.forEach((appendTag) => { - if (Array.isArray(appendTag) && appendTag.length >= 2) { - const tagName = appendTag[0]; - const tagValue = appendTag[1]; - if (!hasTag(event.tags, tagName, tagValue)) { - errors.push(`Missing append tag: ${tagName}=${tagValue}`); - valid = false; - } - } - }); - } - - // Validate Content Format - if (schema.content) { - if (schema.content.format === "json") { - try { - JSON.parse(event.content); - } catch (e) { - errors.push(`Content is not valid JSON`); - valid = false; - } - } else if (schema.content.format === "empty") { - if (event.content !== "") { - errors.push(`Content should be empty, got: "${event.content}"`); - valid = false; - } - } - } - - if (valid) { - logSuccess(`${description}`); - } else { - logError(`${description}`); - errors.forEach(e => console.error(` - ${e}`)); - console.log(" Event:", JSON.stringify(event, null, 2)); - } -} - -async function main() { - logInfo("Starting Event Validation..."); - - // 1. VIDEO_POST - validateEvent(NOTE_TYPES.VIDEO_POST, buildVideoPostEvent({ - pubkey: "pubkey", - created_at: 1234567890, - dTagValue: "video-id", - content: { title: "Test Video", videoRootId: "root-id" } - }), "buildVideoPostEvent (standard)"); - - validateEvent(NOTE_TYPES.VIDEO_POST, buildVideoPostEvent({ - pubkey: "pubkey", - created_at: 1234567890, - dTagValue: "video-id-minimal", - content: { version: 1, title: "Minimal", videoRootId: "root" } - }), "buildVideoPostEvent (minimal)"); - - // 2. VIDEO_MIRROR - validateEvent(NOTE_TYPES.VIDEO_MIRROR, buildVideoMirrorEvent({ - pubkey: "pubkey", - created_at: 1234567890, - content: "Alt text" - }), "buildVideoMirrorEvent"); - - // 3. REPOST - validateEvent(NOTE_TYPES.REPOST, buildRepostEvent({ - pubkey: "pubkey", - created_at: 1234567890, - eventId: "hex-id", - eventRelay: "wss://relay.example.com", - targetKind: 1, - serializedEvent: JSON.stringify({ kind: 1, content: "test" }) - }), "buildRepostEvent (event pointer)"); - - // 4. RELAY_LIST - validateEvent(NOTE_TYPES.RELAY_LIST, buildRelayListEvent({ - pubkey: "pubkey", - created_at: 1234567890, - relays: ["wss://relay.example.com"] - }), "buildRelayListEvent"); - - // 5. DM_RELAY_LIST - validateEvent(NOTE_TYPES.DM_RELAY_LIST, buildDmRelayListEvent({ - pubkey: "pubkey", - created_at: 1234567890, - relays: ["wss://relay.example.com"] - }), "buildDmRelayListEvent"); - - // 6. PROFILE_METADATA - validateEvent(NOTE_TYPES.PROFILE_METADATA, buildProfileMetadataEvent({ - pubkey: "pubkey", - created_at: 1234567890, - metadata: { name: "Test User" } - }), "buildProfileMetadataEvent"); - - // 7. MUTE_LIST - validateEvent(NOTE_TYPES.MUTE_LIST, buildMuteListEvent({ - pubkey: "pubkey", - created_at: 1234567890, - pTags: ["hex-pubkey"] - }), "buildMuteListEvent"); - - // 8. DELETION - validateEvent(NOTE_TYPES.DELETION, buildDeletionEvent({ - pubkey: "pubkey", - created_at: 1234567890, - eventIds: ["hex-id"] - }), "buildDeletionEvent"); - - // 9. LEGACY_DM - validateEvent(NOTE_TYPES.LEGACY_DM, buildLegacyDirectMessageEvent({ - pubkey: "pubkey", - created_at: 1234567890, - recipientPubkey: "hex-recipient", - ciphertext: "encrypted-content" - }), "buildLegacyDirectMessageEvent"); - - // 10. DM_ATTACHMENT - validateEvent(NOTE_TYPES.DM_ATTACHMENT, buildDmAttachmentEvent({ - pubkey: "pubkey", - created_at: 1234567890, - recipientPubkey: "hex-recipient", - attachment: { url: "https://example.com/file", x: "hash" } - }), "buildDmAttachmentEvent"); - - // 11. DM_READ_RECEIPT - validateEvent(NOTE_TYPES.DM_READ_RECEIPT, buildDmReadReceiptEvent({ - pubkey: "pubkey", - created_at: 1234567890, - recipientPubkey: "hex-recipient", - eventId: "hex-event-id" - }), "buildDmReadReceiptEvent"); - - // 12. DM_TYPING - validateEvent(NOTE_TYPES.DM_TYPING, buildDmTypingIndicatorEvent({ - pubkey: "pubkey", - created_at: 1234567890, - recipientPubkey: "hex-recipient", - eventId: "hex-event-id", - expiresAt: 1234567899 - }), "buildDmTypingIndicatorEvent"); - - // 13. VIEW_EVENT - validateEvent(NOTE_TYPES.VIEW_EVENT, buildViewEvent({ - pubkey: "pubkey", - created_at: 1234567890, - pointerValue: "video-d-tag", - dedupeTag: "unique-session-view-id", - includeSessionTag: true - }), "buildViewEvent"); - - // 14. ZAP_REQUEST - validateEvent(NOTE_TYPES.ZAP_REQUEST, buildZapRequestEvent({ - pubkey: "pubkey", - created_at: 1234567890, - recipientPubkey: "hex-recipient", - amountSats: 100, - relays: ["wss://relay.example.com"] - }), "buildZapRequestEvent"); - - // 15. VIDEO_REACTION - validateEvent(NOTE_TYPES.VIDEO_REACTION, buildReactionEvent({ - pubkey: "pubkey", - created_at: 1234567890, - targetPointer: { type: "e", value: "hex-id" }, - content: "+" - }), "buildReactionEvent"); - - // 16. VIDEO_COMMENT - validateEvent(NOTE_TYPES.VIDEO_COMMENT, buildCommentEvent({ - pubkey: "pubkey", - created_at: 1234567890, - content: "This is a comment", - videoEventId: "hex-video-id" - }), "buildCommentEvent (root)"); - - validateEvent(NOTE_TYPES.VIDEO_COMMENT, buildCommentEvent({ - pubkey: "pubkey", - created_at: 1234567890, - content: "This is a reply", - videoEventId: "hex-video-id", - rootIdentifier: "root-id", - parentCommentId: "parent-id" - }), "buildCommentEvent (reply)"); - - // 17. WATCH_HISTORY - validateEvent(NOTE_TYPES.WATCH_HISTORY, buildWatchHistoryEvent({ - pubkey: "pubkey", - created_at: 1234567890, - monthIdentifier: "2023-10", - content: "[]" - }), "buildWatchHistoryEvent"); - - // 18. SUBSCRIPTION_LIST - validateEvent(NOTE_TYPES.SUBSCRIPTION_LIST, buildSubscriptionListEvent({ - pubkey: "pubkey", - created_at: 1234567890, - content: "encrypted-json" - }), "buildSubscriptionListEvent"); - - // 19. USER_BLOCK_LIST - validateEvent(NOTE_TYPES.USER_BLOCK_LIST, buildBlockListEvent({ - pubkey: "pubkey", - created_at: 1234567890, - content: "encrypted-json" - }), "buildBlockListEvent"); - - // 20. HASHTAG_PREFERENCES - validateEvent(NOTE_TYPES.HASHTAG_PREFERENCES, buildHashtagPreferenceEvent({ - pubkey: "pubkey", - created_at: 1234567890, - content: "encrypted-json" - }), "buildHashtagPreferenceEvent"); - - // 21. ADMIN_MODERATION_LIST - validateEvent(NOTE_TYPES.ADMIN_MODERATION_LIST, buildAdminListEvent("moderation", { - pubkey: "pubkey", - created_at: 1234567890, - hexPubkeys: ["hex-pubkey"] - }), "buildAdminListEvent (moderation)"); - - // 22. ADMIN_BLACKLIST - validateEvent(NOTE_TYPES.ADMIN_BLACKLIST, buildAdminListEvent("blacklist", { - pubkey: "pubkey", - created_at: 1234567890, - hexPubkeys: ["hex-pubkey"] - }), "buildAdminListEvent (blacklist)"); - - // 23. ADMIN_WHITELIST - validateEvent(NOTE_TYPES.ADMIN_WHITELIST, buildAdminListEvent("whitelist", { - pubkey: "pubkey", - created_at: 1234567890, - hexPubkeys: ["hex-pubkey"] - }), "buildAdminListEvent (whitelist)"); - - console.log(`\nResults: ${successCount} passed, ${errorCount} failed.`); - if (errorCount > 0) { - process.exit(1); - } -} - -main().catch(err => { - console.error(err); - process.exit(1); -}); diff --git a/scripts/telemetry-aggregator.mjs b/scripts/telemetry-aggregator.mjs index e187611c..18eafe25 100644 --- a/scripts/telemetry-aggregator.mjs +++ b/scripts/telemetry-aggregator.mjs @@ -5,8 +5,7 @@ import crypto from 'node:crypto'; // Configuration const ARTIFACTS_DIR = 'artifacts'; const REPORTS_DIR = 'ai/reports'; -// We will dynamically discover log files in main(), but these are explicit targets if found. -const EXPLICIT_LOG_FILES = ['python_server.log', 'server.log', 'npm_output.log']; +const LOG_FILES = ['test_output.log', 'server.log', 'python_server.log', 'npm_output.log']; // Regex patterns for PII sanitization const PII_PATTERNS = [ @@ -57,11 +56,11 @@ function suggestOwner(stack) { // Collectors // ----------------------------------------------------------------------------- -function collectUnitTests(logFile) { +function collectUnitTests() { const errors = []; + const logFile = 'test_output.log'; if (!fs.existsSync(logFile)) return errors; - console.log(`Parsing unit test log: ${logFile}`); const content = fs.readFileSync(logFile, 'utf8'); const lines = content.split('\n'); let currentError = null; @@ -77,7 +76,7 @@ function collectUnitTests(logFile) { } const title = line.replace(/^not ok \d+ - /, '').trim(); currentError = { - source: `unit-test:${path.basename(logFile)}`, + source: 'unit-test', title: sanitize(title), details: [], severity: 'High' @@ -86,36 +85,18 @@ function collectUnitTests(logFile) { continue; } - // Detect generic JS errors in output that aren't strictly TAP failures but cause crashes - if (line.match(/^Error:/) || line.match(/^TypeError:/) || line.match(/^ReferenceError:/)) { - // If we are already inside a TAP failure block, this is likely detail - if (insideErrorBlock && currentError) { - currentError.details.push(sanitize(line)); - } else { - // Independent error (crash?) - if (currentError) { - errors.push(currentError); - } - currentError = { - source: `unit-test-crash:${path.basename(logFile)}`, - title: sanitize(line.trim()), - details: [sanitize(line.trim())], - severity: 'Critical' - }; - insideErrorBlock = true; // Treat as error block - } - continue; - } - // End of failure block detection (heuristic) + // TAP ok line, or start of a new test section, or unexpected unindented line that looks like a header if (insideErrorBlock) { - // TAP ok line, or start of a new test section (Running tests/...), or TAP version - if (line.match(/^(ok \d+|# tests \d+|1\.\.\d+|TAP version|→ Running )/)) { + if (line.match(/^(ok \d+|# tests \d+|1\.\.\d+|TAP version)/)) { insideErrorBlock = false; errors.push(currentError); currentError = null; } else { // Capture detail lines + // If line is empty or indented, it's likely part of the error + // If it's unindented but not a TAP command, it might still be part of it (console logs) + // We'll be greedy here. currentError.details.push(sanitize(line)); } } @@ -129,7 +110,7 @@ function collectUnitTests(logFile) { errors.forEach(e => { const fullText = e.details.join('\n'); // Try to extract stack trace - const stackMatch = fullText.match(/stack: \|-([\s\S]+?)(?:\n\s*\w+:|$)/) || fullText.match(/(?:Error|Exception):([\s\S]+)/); + const stackMatch = fullText.match(/stack: \|-([\s\S]+?)(?:\n\s*\w+:|$)/) || fullText.match(/Error:([\s\S]+)/); if (stackMatch) { e.stack = stackMatch[1].trim(); } else { @@ -151,9 +132,11 @@ function collectGenericLogs(filepath, sourceName) { let currentError = null; for (const line of lines) { + // Simple heuristic for generic logs: look for "Error:" or "Exception:" + // and capture following lines that look like stack traces (start with 'at ' or are indented) + const isErrorStart = /Error:|Exception:|FAIL|CRITICAL/.test(line); - // Indented lines or lines starting with 'at ' often follow errors - const isStackLine = /^\s+at /.test(line) || /^\s+/.test(line); + const isStackLine = /^\s+at /.test(line) || /^\s+/.test(line); // Indented lines often follow errors if (isErrorStart) { if (currentError) { @@ -168,7 +151,7 @@ function collectGenericLogs(filepath, sourceName) { } else if (currentError && isStackLine) { currentError.details.push(sanitize(line.trim())); } else if (currentError) { - // End of error block if we hit a non-indented line + // End of error block errors.push(currentError); currentError = null; } @@ -256,6 +239,8 @@ function aggregate(errors) { grouped[key].sources.add(err.source); // Escalating severity if we see critical if (err.severity === 'Critical') grouped[key].severity = 'Critical'; + + // Update title if new one is longer/better (optional, keeping first for now) } return Object.values(grouped).sort((a, b) => { @@ -346,18 +331,14 @@ async function main() { // 1. Collect const errors = []; - // Unit Tests - Dynamically find test_unit*.log - const files = fs.readdirSync('.'); - const testLogs = files.filter(f => f.startsWith('test_unit') && f.endsWith('.log')); + // Unit Tests + const unitTestErrors = collectUnitTests(); + console.log(`Collected ${unitTestErrors.length} unit test errors.`); + errors.push(...unitTestErrors); - for (const logFile of testLogs) { - const unitTestErrors = collectUnitTests(logFile); - console.log(`Collected ${unitTestErrors.length} unit test errors from ${logFile}.`); - errors.push(...unitTestErrors); - } - - // Explicit generic logs - for (const logFile of EXPLICIT_LOG_FILES) { + // Server Logs + for (const logFile of LOG_FILES) { + if (logFile === 'test_output.log') continue; // Handled by collectUnitTests const genericErrors = collectGenericLogs(logFile, logFile); console.log(`Collected ${genericErrors.length} errors from ${logFile}.`); errors.push(...genericErrors); diff --git a/tests/app/moderation-overrides.test.mjs b/tests/app/moderation-overrides.test.mjs index 30fcec5f..e700197a 100644 --- a/tests/app/moderation-overrides.test.mjs +++ b/tests/app/moderation-overrides.test.mjs @@ -184,8 +184,12 @@ test("handleModerationBlock requests a block, clears overrides, and refreshes hi const originalIsBlocked = userBlocks.isBlocked; const ensureLoadedMock = mock.fn(async () => {}); - const addBlockMock = mock.fn(async () => ({ ok: true })); - const isBlockedMock = mock.fn(() => false); + let blockedState = false; + const addBlockMock = mock.fn(async () => { + blockedState = true; + return { ok: true }; + }); + const isBlockedMock = mock.fn(() => blockedState); userBlocks.ensureLoaded = ensureLoadedMock; userBlocks.addBlock = addBlockMock; diff --git a/tests/services/storage-service.test.mjs b/tests/services/storage-service.test.mjs deleted file mode 100644 index 39197026..00000000 --- a/tests/services/storage-service.test.mjs +++ /dev/null @@ -1,243 +0,0 @@ -// Run with: node tests/services/storage-service.test.mjs - -import "../test-helpers/setup-localstorage.mjs"; -import "fake-indexeddb/auto"; -import assert from "node:assert/strict"; - -// Fix for fake-indexeddb in Node env with window mock: -// fake-indexeddb might attach to window.indexedDB because window exists, -// but in Node, window is not the global scope, so 'indexedDB' global lookup fails. -if (typeof globalThis.indexedDB === "undefined" && globalThis.window?.indexedDB) { - globalThis.indexedDB = globalThis.window.indexedDB; - globalThis.IDBKeyRange = globalThis.window.IDBKeyRange; -} -import { StorageService, PROVIDERS } from "../../js/services/storageService.js"; - -// Ensure global crypto is available (Node.js 19+) -if (!globalThis.crypto) { - throw new Error("Crypto API is not available in this environment."); -} - -// Mock userLogger to avoid console noise during tests -// We can't easily mock the module import, so we'll just suppress console output if needed. -// For now, we'll let it log, as it helps debugging. - -const SAMPLE_PUBKEY = "0000000000000000000000000000000000000000000000000000000000000001"; - -// Helper to create a mock signer -function createMockSigner() { - return { - nip04Encrypt: async (pubkey, plaintext) => { - return `nip04_enc:${plaintext}`; - }, - nip04Decrypt: async (pubkey, ciphertext) => { - if (!ciphertext.startsWith("nip04_enc:")) { - throw new Error("Decryption failed"); - } - return ciphertext.replace("nip04_enc:", ""); - }, - nip44Encrypt: async (pubkey, plaintext) => { - return `nip44_enc:${plaintext}`; - }, - nip44Decrypt: async (pubkey, ciphertext) => { - if (!ciphertext.startsWith("nip44_enc:")) { - throw new Error("Decryption failed"); - } - return ciphertext.replace("nip44_enc:", ""); - } - }; -} - -// Helper: Delete DB to ensure clean state -async function deleteDatabase() { - const idb = globalThis.indexedDB || globalThis.window?.indexedDB; - if (!idb) { - throw new Error("IndexedDB is not available in global scope"); - } - return new Promise((resolve, reject) => { - const request = idb.deleteDatabase("bitvid-storage"); - request.onsuccess = resolve; - request.onerror = reject; - request.onblocked = resolve; // Just proceed - }); -} - -// Tests - -// 1. Initialization -await (async () => { - console.log("Test: Initialization"); - await deleteDatabase(); - const storage = new StorageService(); - const db = await storage.init(); - assert.ok(db, "Database should be initialized"); - assert.equal(db.name, "bitvid-storage"); - assert.ok(db.objectStoreNames.contains("accounts"), "Should contain 'accounts' store"); - db.close(); -})(); - -// 2. Unlock with NIP-04 (Legacy) -await (async () => { - console.log("Test: Unlock with NIP-04"); - await deleteDatabase(); - const storage = new StorageService(); - const signer = createMockSigner(); - - try { - // Remove nip44 methods to force NIP-04 - const legacySigner = { - nip04Encrypt: signer.nip04Encrypt, - nip04Decrypt: signer.nip04Decrypt - }; - - await storage.unlock(SAMPLE_PUBKEY, { signer: legacySigner }); - assert.ok(storage.isUnlocked(SAMPLE_PUBKEY), "Storage should be unlocked"); - - // Verify account created - const account = await storage._getAccount(SAMPLE_PUBKEY); - assert.ok(account, "Account should be created"); - assert.equal(account.pubkey, SAMPLE_PUBKEY); - assert.equal(account.encryptedMasterKey.method, "nip04"); - assert.ok(account.encryptedMasterKey.ciphertext.startsWith("nip04_enc:"), "Master key should be encrypted with NIP-04"); - } finally { - if (storage.db) storage.db.close(); - } -})(); - -// 3. Unlock with NIP-44 -await (async () => { - console.log("Test: Unlock with NIP-44"); - await deleteDatabase(); - const storage = new StorageService(); - const signer = createMockSigner(); - - try { - await storage.unlock(SAMPLE_PUBKEY, { signer }); // Has nip44 methods - assert.ok(storage.isUnlocked(SAMPLE_PUBKEY)); - - const account = await storage._getAccount(SAMPLE_PUBKEY); - assert.equal(account.encryptedMasterKey.method, "nip44"); - assert.ok(account.encryptedMasterKey.ciphertext.startsWith("nip44_enc:"), "Master key should be encrypted with NIP-44"); - } finally { - if (storage.db) storage.db.close(); - } -})(); - -// 4. Save and Get Connection (Encryption/Decryption) -await (async () => { - console.log("Test: Save and Get Connection"); - await deleteDatabase(); - const storage = new StorageService(); - const signer = createMockSigner(); - - try { - await storage.unlock(SAMPLE_PUBKEY, { signer }); - - const connectionId = "conn_1"; - const payload = { - provider: PROVIDERS.R2, - accessKeyId: "TEST_ACCESS_KEY", - secretAccessKey: "TEST_SECRET_KEY", - endpoint: "https://example.com" - }; - const meta = { - label: "My R2 Bucket" - }; - - await storage.saveConnection(SAMPLE_PUBKEY, connectionId, payload, meta); - - // Verify raw storage is encrypted - const account = await storage._getAccount(SAMPLE_PUBKEY); - const connStored = account.connections[connectionId]; - assert.ok(connStored.encrypted.cipher, "Payload should be encrypted"); - assert.notEqual(connStored.encrypted.cipher, JSON.stringify(payload), "Cipher should not match plaintext"); - - // Retrieve and decrypt - const retrieved = await storage.getConnection(SAMPLE_PUBKEY, connectionId); - assert.deepEqual(retrieved.accessKeyId, payload.accessKeyId); - assert.deepEqual(retrieved.secretAccessKey, payload.secretAccessKey); - assert.equal(retrieved.meta.label, meta.label); - } finally { - if (storage.db) storage.db.close(); - } -})(); - -// 5. Locking -await (async () => { - console.log("Test: Locking"); - await deleteDatabase(); - const storage = new StorageService(); - const signer = createMockSigner(); - - try { - await storage.unlock(SAMPLE_PUBKEY, { signer }); - assert.ok(storage.isUnlocked(SAMPLE_PUBKEY)); - - storage.lock(SAMPLE_PUBKEY); - assert.ok(!storage.isUnlocked(SAMPLE_PUBKEY)); - - // Try to access without unlock - await assert.rejects( - async () => await storage.getConnection(SAMPLE_PUBKEY, "conn_1"), - /Storage is locked/, - "Should reject access when locked" - ); - } finally { - if (storage.db) storage.db.close(); - } -})(); - -// 6. List and Delete Connections -await (async () => { - console.log("Test: List and Delete Connections"); - await deleteDatabase(); - const storage = new StorageService(); - const signer = createMockSigner(); - - try { - await storage.unlock(SAMPLE_PUBKEY, { signer }); - - await storage.saveConnection(SAMPLE_PUBKEY, "c1", { provider: "p1" }, { label: "C1" }); - await storage.saveConnection(SAMPLE_PUBKEY, "c2", { provider: "p2" }, { label: "C2" }); - - const list = await storage.listConnections(SAMPLE_PUBKEY); - assert.equal(list.length, 2); - assert.ok(list.find(c => c.id === "c1")); - assert.ok(list.find(c => c.id === "c2")); - - await storage.deleteConnection(SAMPLE_PUBKEY, "c1"); - const listAfter = await storage.listConnections(SAMPLE_PUBKEY); - assert.equal(listAfter.length, 1); - assert.equal(listAfter[0].id, "c2"); - } finally { - if (storage.db) storage.db.close(); - } -})(); - -// 7. Relock and Unlock (Persistence) -await (async () => { - console.log("Test: Persistence (Unlock -> Lock -> Unlock)"); - await deleteDatabase(); - const storage = new StorageService(); - const signer = createMockSigner(); - - let storage2; - try { - // Initial unlock and save - await storage.unlock(SAMPLE_PUBKEY, { signer }); - await storage.saveConnection(SAMPLE_PUBKEY, "c1", { secret: "secret123" }, { label: "Persistent" }); - storage.lock(SAMPLE_PUBKEY); - - // Re-unlock - storage2 = new StorageService(); // Simulate app restart or new service instance - await storage2.unlock(SAMPLE_PUBKEY, { signer }); - - const conn = await storage2.getConnection(SAMPLE_PUBKEY, "c1"); - assert.equal(conn.secret, "secret123", "Should be able to decrypt data persisted in DB"); - } finally { - if (storage.db) storage.db.close(); - if (storage2 && storage2.db) storage2.db.close(); - } -})(); - -console.log("All tests passed!"); diff --git a/torrent/dist/beacon.js b/torrent/dist/beacon.js index 2005f96d..7c59bfb1 100644 --- a/torrent/dist/beacon.js +++ b/torrent/dist/beacon.js @@ -1 +1 @@ -var ye=Object.freeze([]);var k=Object.freeze({light:Object.freeze({accent:"#fe0032",accentStrong:"#540011",accentPressed:"#a90021"}),dark:Object.freeze({accent:"#fe0032",accentStrong:"#540011",accentPressed:"#a90021"})});var be=Object.freeze(["watch-history:v2:index"]);var ge=1440*60*1e3;var _e=300*1e3;var F=!!!0;typeof window<"u"&&(window.__BITVID_DEV_MODE__=F);var P=()=>{};function Ae(){return typeof console>"u"||console===null?null:console}function Ce(){let e=Ae();if(!e)return{log:P,info:P,warn:P,error:P};let t=typeof e.log=="function"?e.log.bind(e):P;return{log:typeof e.log=="function"?e.log.bind(e):t,info:typeof e.info=="function"?e.info.bind(e):t,warn:typeof e.warn=="function"?e.warn.bind(e):t,error:typeof e.error=="function"?e.error.bind(e):t}}var S=Ce(),M="[bitvid]";function ee(e){if(e.length===0)return{args:e,force:!1};let t=e[e.length-1];if(t&&typeof t=="object"&&!Array.isArray(t)&&Object.prototype.hasOwnProperty.call(t,"force")){let r=[...e],o=r.pop();return{args:r,force:!!o.force}}return{args:e,force:!1}}var L=Object.freeze({log:(...e)=>{F&&S.log(...e)},info:(...e)=>{F&&S.info(...e)},warn:(...e)=>{F&&S.warn(...e)},error:(...e)=>{F&&S.error(...e)}}),E=Object.freeze({log:(...e)=>{let{args:t,force:r}=ee(e);if(!r&&t.length===0)return;if(r){S.log(M,...t);return}let[o,...n]=t;if(typeof o=="string"&&n.length===0){S.log(`${M} ${o}`);return}o!==void 0&&S.log(M,o)},info:(...e)=>{let{args:t,force:r}=ee(e);if(!r&&t.length===0)return;if(r){S.info(M,...t);return}let[o,...n]=t;if(typeof o=="string"&&n.length===0){S.info(`${M} ${o}`);return}o!==void 0&&S.info(M,o)},warn:(...e)=>{S.warn(M,...e)},error:(...e)=>{S.error(M,...e)}}),te=Object.freeze({dev:L,user:E});function we(e){if(!e||typeof e!="object")return!1;let t=["dev","user"],r=["log","info","warn","error"];return t.every(o=>{let n=e[o];return n&&typeof n=="object"&&r.every(l=>typeof n[l]=="function")})}typeof window<"u"&&(we(window.bitvidLogger)||(window.bitvidLogger=te));Object.freeze(te);var G="bitvid:theme",N="dark",Le=new Set(["light","dark"]),re={dark:"#0f172a",light:"#f8fafc"},ne=["[data-theme-toggle]",'[data-action="toggle-theme"]',".js-theme-toggle","#themeToggle"],xe=Object.freeze({accent:"--color-accent",accentStrong:"--color-accent-strong",accentPressed:"--color-accent-pressed"}),oe=new WeakSet,V=new Set,R=null,ae=!1,I=()=>typeof window<"u"&&typeof document<"u",q=e=>{if(typeof e!="string")return null;let t=e.trim().toLowerCase();return t==="default"||t===""?N:Le.has(t)?t:null},Ie=()=>{if(!I())return null;try{let e=window.localStorage.getItem(G);return q(e)}catch(e){return E.warn("Unable to read stored theme preference:",e),null}},ve=e=>{if(I())try{window.localStorage.setItem(G,e)}catch(t){E.warn("Unable to persist theme preference:",t)}},K=()=>{if(!I())return null;let{documentElement:e}=document;return e instanceof HTMLElement?e:null},Ne=e=>typeof e=="string"?e.trim():"",Oe=(e,t)=>{if(!e)return;let r=k&&typeof k=="object"&&k[t]&&typeof k[t]=="object"?k[t]:null;Object.entries(xe).forEach(([o,n])=>{let l=r?r[o]:null,a=Ne(l);a?e.style.setProperty(n,a):e.style.removeProperty(n)})},Be=e=>{if(!I())return;let t=document.querySelector('meta[name="theme-color"]');if(!t)return;let r=K();if(!r)return;let o="";try{typeof window.getComputedStyle=="function"&&(o=getComputedStyle(r).getPropertyValue("--color-page").trim())}catch(a){E.warn("Unable to compute theme color token:",a)}let n=re[e]||re[N],l=o||n;l&&t.setAttribute("content",l)},se=(e,t)=>{if(!(e instanceof HTMLElement))return;e.dataset.themeState=t,e.setAttribute("aria-pressed",t==="dark"?"true":"false");let r=t==="dark"?"Switch to light theme":"Switch to dark theme";e.setAttribute("aria-label",r),e.setAttribute("title",r);let o=e.querySelector("[data-theme-toggle-label]")||e.querySelector(".sr-only");o&&(o.textContent=r);let n=e.querySelector("[data-theme-toggle-icon]")||e.querySelector('[aria-hidden="true"]');if(n){let l=typeof SVGElement<"u"?SVGElement:null;if(l&&n instanceof l||n instanceof Element&&!!n.querySelector("svg"))n instanceof Element&&n.setAttribute("data-theme-icon-state",t);else{let p=e.dataset.iconLight||"\u2600\uFE0F",d=e.dataset.iconDark||"\u{1F319}";n.textContent=t==="dark"?p:d}}},Me=()=>{if(!I()){V.clear();return}V.forEach(e=>{(!(e instanceof HTMLElement)||!document.contains(e))&&V.delete(e)})},He=e=>{Me(),V.forEach(t=>{se(t,e)})},Re=e=>{!(e instanceof HTMLElement)||oe.has(e)||(e.addEventListener("click",t=>{t.preventDefault(),ke(R==="dark"?"light":"dark")}),e.dataset.themeToggleBound="true",oe.add(e))},De=(e=document)=>{if(!I())return[];let t=new Set,r=e&&typeof e.querySelectorAll=="function"?e:document;return ne.forEach(o=>{try{r.querySelectorAll(o).forEach(n=>{n instanceof HTMLElement&&t.add(n)})}catch(n){E.warn(`Invalid selector for theme toggle: ${o}`,n)}}),e instanceof HTMLElement&&ne.forEach(o=>{e.matches(o)&&t.add(e)}),Array.from(t)},X=(e,{persist:t=!0}={})=>{let r=q(e)||N;R=r;let o=K();return o&&(o.dataset.theme=r,Oe(o,r)),t&&ve(r),Be(r),He(r),r},ke=e=>I()?X(e,{persist:!0}):(R=q(e)||N,R),Fe=(e=document)=>{if(!I())return;De(e).forEach(r=>{V.add(r),Re(r),se(r,R||N)})},Ue=()=>{if(!I())return N;let e=K();return(e?q(e.dataset.theme):null)||Ie()||N},We=e=>{if(!e||e.key!==G)return;let t=q(e.newValue)||N;t!==R&&X(t,{persist:!1})},ie=()=>{if(!I()){R=N;return}ae||(window.addEventListener("storage",We),ae=!0);let e=Ue();X(e,{persist:!0}),Fe(document)};var ce={info:"torrent-toast torrent-toast--info",success:"torrent-toast torrent-toast--success",error:"torrent-toast torrent-toast--error",warn:"torrent-toast torrent-toast--warn"},le="transition duration-200 ease-out",Pe="beacon-toast-motion",Z="data-beacon-motion";function Ve(e,t){let r=e?.defaultView;if(r&&typeof r.requestAnimationFrame=="function"){r.requestAnimationFrame(t);return}setTimeout(t,16)}function qe(e){let t=e||(typeof document<"u"?document:null);if(!t)return null;let r=t.getElementById("beacon-toast-container");if(!r){r=t.createElement("div"),r.id="beacon-toast-container",r.setAttribute("role","region"),r.setAttribute("aria-live","polite"),r.className=["pointer-events-none","fixed","inset-x-0","top-0","z-overlay-toast","flex","w-full","flex-col","items-center","gap-3","px-4","pt-6","sm:items-end","sm:px-6","sm:pt-8"].join(" ");let o=t.body||t.getElementsByTagName?.("body")?.[0];o&&o.appendChild(r)}return r}function Ye(e,t,r){let o=e.createElement("div");o.className=[le,r].join(" "),o.setAttribute("role","status"),o.setAttribute("tabindex","0"),o.textContent=t;let n=e.createElement("button");n.type="button",n.className=["notification-dismiss","focus-ring","absolute","right-3","top-3"].join(" "),n.setAttribute("aria-label","Dismiss notification"),n.innerHTML='';let l=e.createElement("div");return l.className=["relative","w-full",le,Pe].join(" "),l.setAttribute(Z,"exit"),l.appendChild(o),o.appendChild(n),{wrapper:l,toast:o,closeButton:n}}function je(e,t=setTimeout,r=clearTimeout){return{show(o,n={}){if(!o)return;let l=n.type&&ce[n.type]?n.type:"info",a=qe(e);if(!a)return;let{wrapper:p,toast:d,closeButton:A}=Ye(e,o,ce[l]);a.appendChild(p),Ve(e,()=>{p.setAttribute(Z,"enter")});let _=typeof n.duration=="number"?n.duration:3500,m=()=>{p.setAttribute(Z,"exit");let B=t(()=>{p.parentNode&&p.parentNode.removeChild(p)},200);return A.removeEventListener("click",C),d.removeEventListener("keydown",T),A.removeEventListener("keydown",T),B},C=()=>m(),T=B=>{B.key==="Escape"&&(B.stopPropagation(),m())};A.addEventListener("click",C),d.addEventListener("keydown",T),A.addEventListener("keydown",T);let O=null;return n.sticky||(O=t(()=>{m()},_)),{dismiss(){O&&r(O),m()}}},set(o,n){return typeof n=="string"?this.show(o,{type:n}):this.show(o,n)},info(o,n={}){return this.show(o,{...n,type:"info"})},success(o,n={}){return this.show(o,{...n,type:"success"})},error(o,n={}){return this.show(o,{...n,type:"error"})},warn(o,n={}){return this.show(o,{...n,type:"warn"})}}}function de(e,t=setTimeout,r=clearTimeout){return je(e,t,r)}var J=[{key:"name",label:"Name",headerClass:"w-full sm:w-1/3 text-left",cellClass:"font-medium text-text truncate"},{key:"progress",label:"Progress",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"progress"},{key:"downloadSpeed",label:"\u2193 Speed",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"bytesPerSecond"},{key:"uploadSpeed",label:"\u2191 Speed",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"bytesPerSecond"},{key:"numPeers",label:"Peers",headerClass:"w-20 text-right",cellClass:"tabular-nums text-right",formatter:"integer"},{key:"ratio",label:"Ratio",headerClass:"w-20 text-right",cellClass:"tabular-nums text-right",formatter:"ratio"}];function $e(e){return e?e.infoHash||e.magnetURI||e.name||String(Math.random()):""}function ze(e,t){return!e||!t?!1:e===t||e.magnetURI&&e.magnetURI===t.magnetURI||e.infoHash&&e.infoHash===t.infoHash}function Ge(e){return typeof e!="number"||Number.isNaN(e)?"":`${(e*100).toFixed(1)}%`}function Ke(e){return typeof e!="number"||!Number.isFinite(e)?"0.00":e.toFixed(2)}function Xe(e){return typeof e!="number"||!Number.isFinite(e)?"0":Math.max(0,Math.floor(e)).toString()}function Ze(e,t){if(typeof t=="function")return t(e,!0)||"";if(typeof e!="number"||Number.isNaN(e)||e<1)return"";let r=["B","kB","MB","GB","TB"],o=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);return`${(e/Math.pow(1e3,o)).toFixed(1)} ${r[o]}/s`}function Je(e,t){let r=e.formatter;if(typeof r=="function")return r;let o=t?.[r];if(typeof o=="function")return o;switch(r){case"progress":return Ge;case"ratio":return Ke;case"integer":return Xe;case"bytesPerSecond":return n=>Ze(n,t?.bytes);default:return n=>n}}function ue({documentRef:e=typeof document<"u"?document:null,root:t,onSelect:r,formatters:o={}}={}){if(!e)throw new Error("createTorrentTable requires a document reference");if(!(t instanceof e.defaultView.HTMLElement))throw new Error("createTorrentTable requires a valid root element");let n=e;t.innerHTML="";let l=n.createElement("div");l.className="overflow-hidden rounded-xl border border-border-translucent bg-surface shadow-sm";let a=n.createElement("div");a.className="overflow-x-auto",l.appendChild(a);let p=n.createElement("table");p.className="min-w-full divide-y divide-border-translucent text-sm",a.appendChild(p);let d=n.createElement("thead");d.className="bg-surface-alt text-muted-strong";let A=n.createElement("tr");J.forEach(y=>{let x=n.createElement("th");x.setAttribute("scope","col"),x.className="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide",y.headerClass&&(x.className+=` ${y.headerClass}`),x.textContent=y.label,A.appendChild(x)}),d.appendChild(A),p.appendChild(d);let _=n.createElement("tbody");_.className="divide-y divide-border-translucent",p.appendChild(_);let m=n.createElement("tbody"),C=n.createElement("tr"),T=n.createElement("td");T.className="px-4 py-8 text-center text-sm text-muted",T.colSpan=J.length,T.textContent="No active torrents yet. Add a magnet link to get started.",C.appendChild(T),m.appendChild(C),p.appendChild(m),t.appendChild(l);function O(y=[],x=null){let U=Array.isArray(y)?y:[];if(_.innerHTML="",!U.length){_.hidden=!0,m.hidden=!1;return}_.hidden=!1,m.hidden=!0;for(let v of U){let g=n.createElement("tr");g.className=["group","align-middle","cursor-pointer","transition","hover:bg-panel-hover","focus-visible:outline","focus-visible:outline-2","focus-visible:outline-offset-2","focus-visible:outline-info"].join(" "),g.setAttribute("role","button"),g.setAttribute("tabindex","0"),g.dataset.torrentId=$e(v),ze(x,v)?(g.classList.add("bg-info/10","text-text"),g.setAttribute("aria-pressed","true")):g.setAttribute("aria-pressed","false"),g.addEventListener("click",()=>{typeof r=="function"&&r(v)}),g.addEventListener("keydown",w=>{(w.key==="Enter"||w.key===" ")&&(w.preventDefault(),typeof r=="function"&&r(v))}),J.forEach(w=>{let D=n.createElement("td");D.className="px-4 py-3",w.cellClass&&(D.className+=` ${w.cellClass}`);let z=v?v[w.key]:void 0,s=Je(w,o)(z,v,w),i=n.createElement("span");i.textContent=s==null?"":String(s),D.appendChild(i),g.appendChild(D)}),_.appendChild(g)}}function B(){t.innerHTML=""}return{render:O,destroy:B}}var Qe="2.0",he=["wss://tracker.btorrent.xyz","wss://tracker.openwebtorrent.com"],fe={announce:he},et={announce:he};function Y(e,t=!1){if(typeof e!="number"||Number.isNaN(e))return"";if(e<1)return t?"":"0 B";let r=["B","kB","MB","GB","TB"],o=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);return`${(e/Math.pow(1e3,o)).toFixed(1)} ${r[o]}${t?"/s":""}`}function tt(e){return typeof e!="number"||Number.isNaN(e)?"":`${(e*100).toFixed(1)}%`}function pe(e){return typeof e!="number"||!Number.isFinite(e)?"0.00":e.toFixed(2)}function rt(e,t){return!e||!t?!1:!!(e===t||e.infoHash&&t.infoHash&&e.infoHash===t.infoHash||e.magnetURI&&t.magnetURI&&e.magnetURI===t.magnetURI)}function nt(e){if(!(!e||!Array.isArray(e.files))&&(e.files.forEach(t=>{typeof t.priority!="string"&&(t.priority="0"),typeof t.getBlobURL=="function"&&t.getBlobURL((r,o)=>{r||!o||(t.url=o)})}),e.torrentFile&&typeof Blob=="function"&&typeof URL<"u"))try{let t=new Blob([e.torrentFile],{type:"application/x-bittorrent"});if(e.torrentFileBlobURL=URL.createObjectURL(t),!e.fileName){let r=e.name||"download";e.fileName=`${r}.torrent`}}catch(t){L.warn("[beacon] Failed to create torrent blob URL",t)}}function ot(e,t){if(!e)return;let r=typeof t=="string"?t:String(t??"0");if(e.priority=r,r==="-1"){typeof e.deselect=="function"&&e.deselect();return}let o=Number.parseInt(r,10);Number.isFinite(o)&&typeof e.select=="function"&&e.select(o)}function me(e,t){if(!Array.isArray(t)||t.length===0)return null;if(!e)return t[0];let r=t.find(o=>rt(o,e));return r||t[0]}function Ee({documentRef:e=typeof document<"u"?document:null,WebTorrentCtor:t}={}){if(!e)throw new Error("createBeaconApp requires a document reference");let r=e.defaultView||globalThis,o=typeof t=="function"?t:typeof r?.WebTorrent=="function"?r.WebTorrent:null;if(!o)throw new Error("createBeaconApp requires a WebTorrent constructor");let n=de(e),l=new o({tracker:et}),a={form:e.querySelector('[data-beacon="magnet-form"]'),magnetInput:e.querySelector('[data-beacon="magnet-input"]'),seedButton:e.querySelector('[data-beacon="seed-button"]'),seedInput:e.querySelector('[data-beacon="seed-input"]'),tableRoot:e.querySelector('[data-beacon="torrent-table"]'),selectedSection:e.querySelector('[data-beacon="selected"]'),selectedName:e.querySelector('[data-beacon="selected-name"]'),pauseButton:e.querySelector('[data-beacon-action="pause"]'),resumeButton:e.querySelector('[data-beacon-action="resume"]'),downloadAllButton:e.querySelector('[data-beacon-action="download-all"]'),removeButton:e.querySelector('[data-beacon-action="remove"]'),copyMagnetButton:e.querySelector('[data-beacon-action="copy-magnet"]'),infoHashValue:e.querySelector('[data-beacon="info-hash"]'),fileList:e.querySelector('[data-beacon="file-list"]'),stats:e.querySelector('[data-beacon="client-stats"]'),overlay:e.querySelector('[data-beacon="processing-overlay"]')},p=a.tableRoot?ue({documentRef:e,root:a.tableRoot,formatters:{bytes:(s,i=!1)=>Y(s,i),progress:s=>tt(s),ratio:s=>pe(s),integer:s=>typeof s=="number"&&Number.isFinite(s)?Math.floor(s).toString():"0",bytesPerSecond:s=>Y(s,!0)},onSelect(s){d.selectedTorrent=s,y()}}):null,d={selectedTorrent:null,processing:!1},A=[],_=[];function m(s){A.push(s)}function C(s){if(d.processing=!!s,!a.overlay)return;let i=d.processing;a.overlay.hidden=!i,a.overlay.setAttribute("aria-hidden",i?"false":"true"),i?a.overlay.style.removeProperty("display"):a.overlay.style.display="none"}function T(){return Array.isArray(l.torrents)?[...l.torrents]:[]}function O(){if(!a.stats)return;let s=Y(l.downloadSpeed,!0)||"0 B/s",i=Y(l.uploadSpeed,!0)||"0 B/s",f=pe(l.ratio);a.stats.textContent=`Client stats: \u2193 ${s} \xB7 \u2191 ${i} \xB7 Ratio: ${f}`}function B(s){a.fileList&&(a.fileList.innerHTML="",!(!s||!Array.isArray(s.files)||!s.files.length)&&s.files.forEach((i,f)=>{let u=e.createElement("tr");u.className="align-top";let c=e.createElement("td");if(c.className="px-3 py-2",i.done&&i.url){let b=e.createElement("a");b.className="text-info hover:text-info-strong",b.href=i.url,b.download=i.name||`file-${f+1}`,b.textContent=i.name||`File ${f+1}`,c.appendChild(b)}else{let b=e.createElement("span");b.textContent=i.name||`File ${f+1}`,c.appendChild(b)}u.appendChild(c);let h=e.createElement("td");h.className="px-3 py-2 text-muted-strong",h.textContent=Y(i.length)||"0 B",u.appendChild(h);let W=e.createElement("td");W.className="px-3 py-2";let H=e.createElement("select");H.className="select",H.name=`${i.name||`file-${f+1}`}-priority`,H.setAttribute("aria-label",`Set priority for ${i.name||`file ${f+1}`}`),[{value:"1",label:"High priority"},{value:"0",label:"Low priority"},{value:"-1",label:"Don't download"}].forEach(b=>{let $=e.createElement("option");$.value=b.value,$.textContent=b.label,b.value===i.priority&&($.selected=!0),H.appendChild($)}),H.value=i.priority??"0",H.addEventListener("change",b=>{ot(i,b.target.value)}),W.appendChild(H),u.appendChild(W),a.fileList.appendChild(u)}))}function y(){if(!a.selectedSection)return;let s=T();d.selectedTorrent=me(d.selectedTorrent,s);let i=d.selectedTorrent;a.selectedSection.hidden=!i,i&&(a.selectedName&&(a.selectedName.textContent=i.name||"Untitled torrent"),a.pauseButton&&(a.pauseButton.hidden=i.paused===!0,a.pauseButton.disabled=i.paused===!0),a.resumeButton&&(a.resumeButton.hidden=i.paused!==!0,a.resumeButton.disabled=i.paused!==!0),a.downloadAllButton&&(a.downloadAllButton.disabled=!(i.progress>=1)),a.infoHashValue&&(a.infoHashValue.textContent=i.infoHash||""),B(i))}function x(s,{isSeed:i=!1}={}){C(!1),nt(s),i||n.info(`Received ${s.name||"torrent"} metadata`),d.selectedTorrent||(d.selectedTorrent=s),y(),p?.render(T(),d.selectedTorrent)}function U(s){let i=typeof s=="string"?s.trim():"";i&&(C(!0),l.add(i,fe,f=>{x(f,{isSeed:!1})}))}function v(s){!Array.isArray(s)||s.length===0||(C(!0),l.seed(s,fe,i=>{x(i,{isSeed:!0}),n.success(`Seeding ${i.files.length} file(s)`)}))}function g(s){if(s){if(s.progress<1){n.warn("Torrent is not finished downloading yet.");return}s.files.forEach(i=>{typeof i.getBlob=="function"&&i.getBlob((f,u)=>{if(f||!u){E.error("[beacon] Failed to get blob for file",i?.name,f),n.error("Failed to prepare file for download.");return}let c=URL.createObjectURL(u),h=e.createElement("a");h.className="beacon-hidden-download",h.setAttribute("data-beacon-hidden-download","true"),h.href=c,h.download=i.name||"download",e.body.appendChild(h),h.click(),e.body.removeChild(h),URL.revokeObjectURL(c)})})}}function w(s){if(!s?.magnetURI)return;let i=s.magnetURI,f=r?.navigator?.clipboard;if(f&&typeof f.writeText=="function"){f.writeText(i).then(()=>{n.success("Magnet URI copied to clipboard!")}).catch(u=>{E.error("[beacon] Clipboard error",u),n.error("Failed to copy magnet URI")});return}try{let u=e.createElement("textarea");u.value=i,u.className="beacon-clipboard-offscreen",u.setAttribute("data-beacon-clipboard-state","offscreen"),e.body.appendChild(u),u.select(),e.execCommand?.("copy"),e.body.removeChild(u),n.success("Magnet URI copied (fallback)!")}catch(u){E.error("[beacon] Clipboard fallback error",u),n.error("Failed to copy magnet URI")}}function D(s){s&&s.destroy(i=>{i&&(E.error("[beacon] Failed to destroy torrent",i),n.error("Failed to remove torrent"));let f=T();d.selectedTorrent=me(null,f),p?.render(f,d.selectedTorrent),y()})}function z(){if(L.info(`[beacon] Starting beacon runtime v${Qe}`),n||L.warn("[beacon] Toast service unavailable"),o.WEBRTC_SUPPORT||n?.error("Please use a browser with WebRTC support.",{sticky:!0}),a.form){let c=h=>{h.preventDefault(),U(a.magnetInput?.value||""),a.magnetInput&&(a.magnetInput.value="")};a.form.addEventListener("submit",c),m(()=>a.form.removeEventListener("submit",c))}if(a.seedButton&&a.seedInput){let c=()=>{a.seedInput.click()},h=()=>{let W=Array.from(a.seedInput.files||[]);a.seedInput.value="",v(W)};a.seedButton.addEventListener("click",c),a.seedInput.addEventListener("change",h),m(()=>{a.seedButton.removeEventListener("click",c),a.seedInput.removeEventListener("change",h)})}if(a.pauseButton){let c=()=>{d.selectedTorrent&&typeof d.selectedTorrent.pause=="function"&&(d.selectedTorrent.pause(),y())};a.pauseButton.addEventListener("click",c),m(()=>a.pauseButton.removeEventListener("click",c))}if(a.resumeButton){let c=()=>{d.selectedTorrent&&typeof d.selectedTorrent.resume=="function"&&(d.selectedTorrent.resume(),y())};a.resumeButton.addEventListener("click",c),m(()=>a.resumeButton.removeEventListener("click",c))}if(a.downloadAllButton){let c=()=>g(d.selectedTorrent);a.downloadAllButton.addEventListener("click",c),m(()=>a.downloadAllButton.removeEventListener("click",c))}if(a.removeButton){let c=()=>D(d.selectedTorrent);a.removeButton.addEventListener("click",c),m(()=>a.removeButton.removeEventListener("click",c))}if(a.copyMagnetButton){let c=()=>w(d.selectedTorrent);a.copyMagnetButton.addEventListener("click",c),m(()=>a.copyMagnetButton.removeEventListener("click",c))}let s=c=>{E.error("[beacon] Torrent client error",c),n?.error(c?.message||String(c)),C(!1)};l.on("error",s),m(()=>l.removeListener("error",s));let i=c=>{if(l.torrents&&l.torrents.length>0)return c.preventDefault(),c.returnValue="Transfers are in progress. Are you sure you want to leave or refresh?",c.returnValue};r&&typeof r.addEventListener=="function"&&(r.addEventListener("beforeunload",i),m(()=>r.removeEventListener("beforeunload",i))),p?.render(T(),d.selectedTorrent),y(),O(),C(!1);let f=r.setInterval(()=>{p?.render(T(),d.selectedTorrent),y(),O()},1e3);_.push(f);let u=r?.location?.hash;typeof u=="string"&&u.length>1&&U(u.slice(1))}function Q(){_.forEach(s=>r.clearInterval(s)),_.length=0,A.forEach(s=>{try{s()}catch(i){L.warn("[beacon] Cleanup task failed",i)}}),A.length=0,p?.destroy();try{l.destroy()}catch(s){L.warn("[beacon] Failed to destroy client",s)}}return{mount:z,destroy:Q}}function at(){return typeof window<"u"&&window||typeof globalThis<"u"&&globalThis||null}function st(){let e=at();if(e&&typeof e.WebTorrent=="function")return e.WebTorrent;throw new Error("WebTorrent runtime is not available on the global scope")}var j=null,it=()=>{try{ie()}catch(e){L.warn("[beacon] Failed to initialize theme controller",e)}};it();function Te(){if(j)return L.info("[beacon] Reusing existing beacon app instance"),j;if(typeof document>"u")throw new Error("Beacon runtime requires a document environment");let e=st();return L.info("[beacon] Creating beacon app instance"),j=Ee({documentRef:document,WebTorrentCtor:e}),j.mount(),j}if(typeof document<"u")if(document.readyState==="loading")document.addEventListener("DOMContentLoaded",()=>{try{Te()}catch(e){E.error("[beacon] Failed to mount app",e)}});else try{Te()}catch(e){E.error("[beacon] Failed to mount app",e)}export{Ee as createBeaconApp,Te as mountBeaconApp}; +import{IS_DEV_MODE as Le,IS_VERBOSE_DEV_MODE as Ce,IS_LOCKDOWN_MODE as Ae,LOCKDOWN_TITLE as bt,LOCKDOWN_MESSAGE as Et,ADMIN_SUPER_NPUB as Tt,ADMIN_DM_IMAGE_URL as gt,BITVID_WEBSITE_URL as _t,BLOG_URL as St,COMMUNITY_URL as wt,NOSTR_URL as Lt,GITHUB_URL as Ct,BETA_URL as At,DNS_URL as xt,TIP_JAR_URL as vt,MAX_WALLET_DEFAULT_ZAP as It,PLATFORM_FEE_PERCENT as Ot,PLATFORM_LUD16_OVERRIDE as Nt,DEFAULT_RELAY_URLS_OVERRIDE as Bt,ADMIN_WHITELIST_MODE_STORAGE_KEY as Mt,DEFAULT_WHITELIST_MODE_ENABLED as Dt,ALLOW_NSFW_CONTENT as Rt,DEFAULT_TRUST_SEED_NPUBS as Ht,DEFAULT_BLUR_THRESHOLD as Ut,DEFAULT_AUTOPLAY_BLOCK_THRESHOLD as kt,DEFAULT_TRUSTED_MUTE_HIDE_THRESHOLD as Ft,DEFAULT_TRUSTED_SPAM_HIDE_THRESHOLD as Pt,WATCH_HISTORY_KIND as Vt,WATCH_HISTORY_LIST_IDENTIFIER as Wt,WATCH_HISTORY_MAX_ITEMS as qt,WATCH_HISTORY_BATCH_RESOLVE as jt,WATCH_HISTORY_BATCH_PAGE_SIZE as $t,WATCH_HISTORY_PAYLOAD_MAX_BYTES as Yt,WATCH_HISTORY_FETCH_EVENT_LIMIT as Gt,WATCH_HISTORY_CACHE_TTL_MS as zt,VIEW_COUNT_DEDUPE_WINDOW_SECONDS as Kt,VIEW_COUNT_BACKFILL_MAX_DAYS as Xt,VIEW_COUNT_CACHE_TTL_MS as Zt,ENSURE_PRESENCE_REBROADCAST_COOLDOWN_SECONDS as Jt}from"../config/instance-config.js";var H=!!Le;var xe=!!Ce;var ve=!!Ae;typeof window<"u"&&(window.__BITVID_DEV_MODE__=H,window.__BITVID_VERBOSE_DEV_MODE__=xe,window.__BITVID_LOCKDOWN_MODE__=ve);var k=()=>{};function Ie(){return typeof console>"u"||console===null?null:console}function Oe(){let e=Ie();if(!e)return{log:k,info:k,debug:k,warn:k,error:k};let t=typeof e.log=="function"?e.log.bind(e):k;return{log:typeof e.log=="function"?e.log.bind(e):t,info:typeof e.info=="function"?e.info.bind(e):t,debug:typeof e.debug=="function"?e.debug.bind(e):t,warn:typeof e.warn=="function"?e.warn.bind(e):t,error:typeof e.error=="function"?e.error.bind(e):t}}var _=Oe(),M="[bitvid]";function ee(e){if(e.length===0)return{args:e,force:!1};let t=e[e.length-1];if(t&&typeof t=="object"&&!Array.isArray(t)&&Object.prototype.hasOwnProperty.call(t,"force")){let r=[...e],o=r.pop();return{args:r,force:!!o.force}}return{args:e,force:!1}}var S=Object.freeze({log:(...e)=>{H&&_.log(...e)},info:(...e)=>{H&&_.info(...e)},debug:(...e)=>{H&&_.debug(...e)},warn:(...e)=>{H&&_.warn(...e)},error:(...e)=>{H&&_.error(...e)}}),y=Object.freeze({log:(...e)=>{let{args:t,force:r}=ee(e);if(!r&&t.length===0)return;if(r){_.log(M,...t);return}let[o,...n]=t;if(typeof o=="string"&&n.length===0){_.log(`${M} ${o}`);return}o!==void 0&&_.log(M,o)},info:(...e)=>{let{args:t,force:r}=ee(e);if(!r&&t.length===0)return;if(r){_.info(M,...t);return}let[o,...n]=t;if(typeof o=="string"&&n.length===0){_.info(`${M} ${o}`);return}o!==void 0&&_.info(M,o)},warn:(...e)=>{_.warn(M,...e)},error:(...e)=>{_.error(M,...e)}}),te=Object.freeze({dev:S,user:y});function Ne(e){return!e||typeof e!="object"?!1:Object.entries({dev:["log","info","debug","warn","error"],user:["log","info","warn","error"]}).every(([r,o])=>{let n=e[r];return!n||typeof n!="object"?!1:o.every(c=>typeof n[c]=="function")})}typeof window<"u"&&(Ne(window.bitvidLogger)||(window.bitvidLogger=te));Object.freeze(te);import{THEME_ACCENT_OVERRIDES as re}from"../config/instance-config.js";var K="bitvid:theme",v="dark",Be=new Set(["light","dark"]),ne={dark:"#0f172a",light:"#f8fafc"},oe=["[data-theme-toggle]",'[data-action="toggle-theme"]',".js-theme-toggle","#themeToggle"],ie=Object.freeze({accent:"--color-accent",accentStrong:"--color-accent-strong",accentPressed:"--color-accent-pressed"}),ae=new WeakSet,W=new Set,D=null,se=!1,F=null,I=()=>typeof window<"u"&&typeof document<"u",q=e=>{if(typeof e!="string")return null;let t=e.trim().toLowerCase();return t==="default"||t===""?v:Be.has(t)?t:null},Me=()=>{if(!I())return null;try{let e=window.localStorage.getItem(K);return q(e)}catch(e){return y.warn("Unable to read stored theme preference:",e),null}},De=e=>{if(I())try{window.localStorage.setItem(K,e)}catch(t){y.warn("Unable to persist theme preference:",t)}},G=()=>{if(!I())return null;let{documentElement:e}=document;return e instanceof HTMLElement?e:null},Re=e=>typeof e=="string"?e.trim():"",He=e=>{if(!e||typeof e!="object")return null;let t=Object.entries(e).reduce((r,[o,n])=>{if(!n||typeof n!="object")return r;let c=Object.entries(n).reduce((a,[u,d])=>(Object.prototype.hasOwnProperty.call(ie,u)&&(a[u]=d),a),{});return Object.keys(c).length>0&&(r[o]=c),r},{});return Object.keys(t).length>0?t:null},ce=e=>{F=He(e)};ce(typeof re<"u"?re:null);var le=(e,t)=>{if(!e)return;let r=F&&typeof F=="object"&&F[t]&&typeof F[t]=="object"?F[t]:null,o=!1;Object.entries(ie).forEach(([n,c])=>{let a=r?r[n]:null,u=Re(a);u?(e.style.setProperty(c,u),o=!0):e.style.removeProperty(c)}),o?e.dataset.themeAccent="override":e.removeAttribute("data-theme-accent")},Ue=e=>{if(!I())return;let t=document.querySelector('meta[name="theme-color"]');if(!t)return;let r=G();if(!r)return;let o="";try{typeof window.getComputedStyle=="function"&&(o=getComputedStyle(r).getPropertyValue("--color-page").trim())}catch(a){y.warn("Unable to compute theme color token:",a)}let n=ne[e]||ne[v],c=o||n;c&&t.setAttribute("content",c)},de=(e,t)=>{if(!(e instanceof HTMLElement))return;e.dataset.themeState=t,e.setAttribute("aria-pressed",t==="dark"?"true":"false");let r=t==="dark"?"Switch to light theme":"Switch to dark theme";e.setAttribute("aria-label",r),e.setAttribute("title",r);let o=e.querySelector("[data-theme-toggle-label]")||e.querySelector(".sr-only");o&&(o.textContent=r);let n=e.querySelector("[data-theme-toggle-icon]")||e.querySelector('[aria-hidden="true"]');if(n){let c=typeof SVGElement<"u"?SVGElement:null;if(c&&n instanceof c||n instanceof Element&&!!n.querySelector("svg"))n instanceof Element&&n.setAttribute("data-theme-icon-state",t);else{let u=e.dataset.iconLight||"\u2600\uFE0F",d=e.dataset.iconDark||"\u{1F319}";n.textContent=t==="dark"?u:d}}},ke=()=>{if(!I()){W.clear();return}W.forEach(e=>{(!(e instanceof HTMLElement)||!document.contains(e))&&W.delete(e)})},Fe=e=>{ke(),W.forEach(t=>{de(t,e)})},Pe=e=>{!(e instanceof HTMLElement)||ae.has(e)||(e.addEventListener("click",t=>{t.preventDefault(),We(D==="dark"?"light":"dark")}),e.dataset.themeToggleBound="true",ae.add(e))},Ve=(e=document)=>{if(!I())return[];let t=new Set,r=e&&typeof e.querySelectorAll=="function"?e:document;return oe.forEach(o=>{try{r.querySelectorAll(o).forEach(n=>{n instanceof HTMLElement&&t.add(n)})}catch(n){y.warn(`Invalid selector for theme toggle: ${o}`,n)}}),e instanceof HTMLElement&&oe.forEach(o=>{e.matches(o)&&t.add(e)}),Array.from(t)},X=(e,{persist:t=!0}={})=>{let r=q(e)||v;D=r;let o=G();return o&&(o.dataset.theme=r,le(o,r)),t&&De(r),Ue(r),Fe(r),r},ue=e=>{ce(e);let t=G();if(!t)return;le(t,D||v)};var We=e=>I()?X(e,{persist:!0}):(D=q(e)||v,D),qe=(e=document)=>{if(!I())return;Ve(e).forEach(r=>{W.add(r),Pe(r),de(r,D||v)})},je=()=>{if(!I())return v;let e=G();return(e?q(e.dataset.theme):null)||Me()||v},$e=e=>{if(!e||e.key!==K)return;let t=q(e.newValue)||v;t!==D&&X(t,{persist:!1})},fe=()=>{if(!I()){D=v;return}se||(window.addEventListener("storage",$e),se=!0);let e=je();X(e,{persist:!0}),qe(document)};var pe={info:"torrent-toast torrent-toast--info",success:"torrent-toast torrent-toast--success",error:"torrent-toast torrent-toast--error",warn:"torrent-toast torrent-toast--warn"},me="transition duration-200 ease-out",Ye="beacon-toast-motion",Z="data-beacon-motion";function Ge(e,t){let r=e?.defaultView;if(r&&typeof r.requestAnimationFrame=="function"){r.requestAnimationFrame(t);return}setTimeout(t,16)}function ze(e){let t=e||(typeof document<"u"?document:null);if(!t)return null;let r=t.getElementById("beacon-toast-container");if(!r){r=t.createElement("div"),r.id="beacon-toast-container",r.setAttribute("role","region"),r.setAttribute("aria-live","polite"),r.className=["pointer-events-none","fixed","inset-x-0","top-0","z-overlay-toast","flex","w-full","flex-col","items-center","gap-3","px-4","pt-6","sm:items-end","sm:px-6","sm:pt-8"].join(" ");let o=t.body||t.getElementsByTagName?.("body")?.[0];o&&o.appendChild(r)}return r}function Ke(e,t,r){let o=e.createElement("div");o.className=[me,r].join(" "),o.setAttribute("role","status"),o.setAttribute("tabindex","0"),o.textContent=t;let n=e.createElement("button");n.type="button",n.className=["notification-dismiss","focus-ring","absolute","right-3","top-3"].join(" "),n.setAttribute("aria-label","Dismiss notification"),n.innerHTML='';let c=e.createElement("div");return c.className=["relative","w-full",me,Ye].join(" "),c.setAttribute(Z,"exit"),c.appendChild(o),o.appendChild(n),{wrapper:c,toast:o,closeButton:n}}function Xe(e,t=setTimeout,r=clearTimeout){return{show(o,n={}){if(!o)return;let c=n.type&&pe[n.type]?n.type:"info",a=ze(e);if(!a)return;let{wrapper:u,toast:d,closeButton:L}=Ke(e,o,pe[c]);a.appendChild(u),Ge(e,()=>{u.setAttribute(Z,"enter")});let w=typeof n.duration=="number"?n.duration:3500,m=()=>{u.setAttribute(Z,"exit");let B=t(()=>{u.parentNode&&u.parentNode.removeChild(u)},200);return L.removeEventListener("click",C),d.removeEventListener("keydown",b),L.removeEventListener("keydown",b),B},C=()=>m(),b=B=>{B.key==="Escape"&&(B.stopPropagation(),m())};L.addEventListener("click",C),d.addEventListener("keydown",b),L.addEventListener("keydown",b);let N=null;return n.sticky||(N=t(()=>{m()},w)),{dismiss(){N&&r(N),m()}}},set(o,n){return typeof n=="string"?this.show(o,{type:n}):this.show(o,n)},info(o,n={}){return this.show(o,{...n,type:"info"})},success(o,n={}){return this.show(o,{...n,type:"success"})},error(o,n={}){return this.show(o,{...n,type:"error"})},warn(o,n={}){return this.show(o,{...n,type:"warn"})}}}function he(e,t=setTimeout,r=clearTimeout){return Xe(e,t,r)}var J=[{key:"name",label:"Name",headerClass:"w-full sm:w-1/3 text-left",cellClass:"font-medium text-text truncate"},{key:"progress",label:"Progress",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"progress"},{key:"downloadSpeed",label:"\u2193 Speed",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"bytesPerSecond"},{key:"uploadSpeed",label:"\u2191 Speed",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"bytesPerSecond"},{key:"numPeers",label:"Peers",headerClass:"w-20 text-right",cellClass:"tabular-nums text-right",formatter:"integer"},{key:"ratio",label:"Ratio",headerClass:"w-20 text-right",cellClass:"tabular-nums text-right",formatter:"ratio"}];function Ze(e){return e?e.infoHash||e.magnetURI||e.name||String(Math.random()):""}function Je(e,t){return!e||!t?!1:e===t||e.magnetURI&&e.magnetURI===t.magnetURI||e.infoHash&&e.infoHash===t.infoHash}function Qe(e){return typeof e!="number"||Number.isNaN(e)?"":`${(e*100).toFixed(1)}%`}function et(e){return typeof e!="number"||!Number.isFinite(e)?"0.00":e.toFixed(2)}function tt(e){return typeof e!="number"||!Number.isFinite(e)?"0":Math.max(0,Math.floor(e)).toString()}function rt(e,t){if(typeof t=="function")return t(e,!0)||"";if(typeof e!="number"||Number.isNaN(e)||e<1)return"";let r=["B","kB","MB","GB","TB"],o=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);return`${(e/Math.pow(1e3,o)).toFixed(1)} ${r[o]}/s`}function nt(e,t){let r=e.formatter;if(typeof r=="function")return r;let o=t?.[r];if(typeof o=="function")return o;switch(r){case"progress":return Qe;case"ratio":return et;case"integer":return tt;case"bytesPerSecond":return n=>rt(n,t?.bytes);default:return n=>n}}function ye({documentRef:e=typeof document<"u"?document:null,root:t,onSelect:r,formatters:o={}}={}){if(!e)throw new Error("createTorrentTable requires a document reference");if(!(t instanceof e.defaultView.HTMLElement))throw new Error("createTorrentTable requires a valid root element");let n=e;t.innerHTML="";let c=n.createElement("div");c.className="overflow-hidden rounded-xl border border-border-translucent bg-surface shadow-sm";let a=n.createElement("div");a.className="overflow-x-auto",c.appendChild(a);let u=n.createElement("table");u.className="min-w-full divide-y divide-border-translucent text-sm",a.appendChild(u);let d=n.createElement("thead");d.className="bg-surface-alt text-muted-strong";let L=n.createElement("tr");J.forEach(E=>{let x=n.createElement("th");x.setAttribute("scope","col"),x.className="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide",E.headerClass&&(x.className+=` ${E.headerClass}`),x.textContent=E.label,L.appendChild(x)}),d.appendChild(L),u.appendChild(d);let w=n.createElement("tbody");w.className="divide-y divide-border-translucent",u.appendChild(w);let m=n.createElement("tbody"),C=n.createElement("tr"),b=n.createElement("td");b.className="px-4 py-8 text-center text-sm text-muted",b.colSpan=J.length,b.textContent="No active torrents yet. Add a magnet link to get started.",C.appendChild(b),m.appendChild(C),u.appendChild(m),t.appendChild(c);function N(E=[],x=null){let P=Array.isArray(E)?E:[];if(w.innerHTML="",!P.length){w.hidden=!0,m.hidden=!1;return}w.hidden=!1,m.hidden=!0;for(let O of P){let g=n.createElement("tr");g.className=["group","align-middle","cursor-pointer","transition","hover:bg-panel-hover","focus-visible:outline","focus-visible:outline-2","focus-visible:outline-offset-2","focus-visible:outline-info"].join(" "),g.setAttribute("role","button"),g.setAttribute("tabindex","0"),g.dataset.torrentId=Ze(O),Je(x,O)?(g.classList.add("bg-info/10","text-text"),g.setAttribute("aria-pressed","true")):g.setAttribute("aria-pressed","false"),g.addEventListener("click",()=>{typeof r=="function"&&r(O)}),g.addEventListener("keydown",A=>{(A.key==="Enter"||A.key===" ")&&(A.preventDefault(),typeof r=="function"&&r(O))}),J.forEach(A=>{let U=n.createElement("td");U.className="px-4 py-3",A.cellClass&&(U.className+=` ${A.cellClass}`);let z=O?O[A.key]:void 0,s=nt(A,o)(z,O,A),i=n.createElement("span");i.textContent=s==null?"":String(s),U.appendChild(i),g.appendChild(U)}),w.appendChild(g)}}function B(){t.innerHTML=""}return{render:N,destroy:B}}var ot="2.0",ge=["wss://tracker.btorrent.xyz","wss://tracker.openwebtorrent.com"],be={announce:ge},at={announce:ge};function j(e,t=!1){if(typeof e!="number"||Number.isNaN(e))return"";if(e<1)return t?"":"0 B";let r=["B","kB","MB","GB","TB"],o=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);return`${(e/Math.pow(1e3,o)).toFixed(1)} ${r[o]}${t?"/s":""}`}function st(e){return typeof e!="number"||Number.isNaN(e)?"":`${(e*100).toFixed(1)}%`}function Ee(e){return typeof e!="number"||!Number.isFinite(e)?"0.00":e.toFixed(2)}function it(e,t){return!e||!t?!1:!!(e===t||e.infoHash&&t.infoHash&&e.infoHash===t.infoHash||e.magnetURI&&t.magnetURI&&e.magnetURI===t.magnetURI)}function ct(e){if(!(!e||!Array.isArray(e.files))&&(e.files.forEach(t=>{typeof t.priority!="string"&&(t.priority="0"),typeof t.getBlobURL=="function"&&t.getBlobURL((r,o)=>{r||!o||(t.url=o)})}),e.torrentFile&&typeof Blob=="function"&&typeof URL<"u"))try{let t=new Blob([e.torrentFile],{type:"application/x-bittorrent"});if(e.torrentFileBlobURL=URL.createObjectURL(t),!e.fileName){let r=e.name||"download";e.fileName=`${r}.torrent`}}catch(t){S.warn("[beacon] Failed to create torrent blob URL",t)}}function lt(e,t){if(!e)return;let r=typeof t=="string"?t:String(t??"0");if(e.priority=r,r==="-1"){typeof e.deselect=="function"&&e.deselect();return}let o=Number.parseInt(r,10);Number.isFinite(o)&&typeof e.select=="function"&&e.select(o)}function Te(e,t){if(!Array.isArray(t)||t.length===0)return null;if(!e)return t[0];let r=t.find(o=>it(o,e));return r||t[0]}function _e({documentRef:e=typeof document<"u"?document:null,WebTorrentCtor:t}={}){if(!e)throw new Error("createBeaconApp requires a document reference");let r=e.defaultView||globalThis,o=typeof t=="function"?t:typeof r?.WebTorrent=="function"?r.WebTorrent:null;if(!o)throw new Error("createBeaconApp requires a WebTorrent constructor");let n=he(e),c=new o({tracker:at}),a={form:e.querySelector('[data-beacon="magnet-form"]'),magnetInput:e.querySelector('[data-beacon="magnet-input"]'),seedButton:e.querySelector('[data-beacon="seed-button"]'),seedInput:e.querySelector('[data-beacon="seed-input"]'),tableRoot:e.querySelector('[data-beacon="torrent-table"]'),selectedSection:e.querySelector('[data-beacon="selected"]'),selectedName:e.querySelector('[data-beacon="selected-name"]'),pauseButton:e.querySelector('[data-beacon-action="pause"]'),resumeButton:e.querySelector('[data-beacon-action="resume"]'),downloadAllButton:e.querySelector('[data-beacon-action="download-all"]'),removeButton:e.querySelector('[data-beacon-action="remove"]'),copyMagnetButton:e.querySelector('[data-beacon-action="copy-magnet"]'),infoHashValue:e.querySelector('[data-beacon="info-hash"]'),fileList:e.querySelector('[data-beacon="file-list"]'),stats:e.querySelector('[data-beacon="client-stats"]'),overlay:e.querySelector('[data-beacon="processing-overlay"]')},u=a.tableRoot?ye({documentRef:e,root:a.tableRoot,formatters:{bytes:(s,i=!1)=>j(s,i),progress:s=>st(s),ratio:s=>Ee(s),integer:s=>typeof s=="number"&&Number.isFinite(s)?Math.floor(s).toString():"0",bytesPerSecond:s=>j(s,!0)},onSelect(s){d.selectedTorrent=s,E()}}):null,d={selectedTorrent:null,processing:!1},L=[],w=[];function m(s){L.push(s)}function C(s){if(d.processing=!!s,!a.overlay)return;let i=d.processing;a.overlay.hidden=!i,a.overlay.setAttribute("aria-hidden",i?"false":"true"),i?a.overlay.style.removeProperty("display"):a.overlay.style.display="none"}function b(){return Array.isArray(c.torrents)?[...c.torrents]:[]}function N(){if(!a.stats)return;let s=j(c.downloadSpeed,!0)||"0 B/s",i=j(c.uploadSpeed,!0)||"0 B/s",p=Ee(c.ratio);a.stats.textContent=`Client stats: \u2193 ${s} \xB7 \u2191 ${i} \xB7 Ratio: ${p}`}function B(s){a.fileList&&(a.fileList.innerHTML="",!(!s||!Array.isArray(s.files)||!s.files.length)&&s.files.forEach((i,p)=>{let f=e.createElement("tr");f.className="align-top";let l=e.createElement("td");if(l.className="px-3 py-2",i.done&&i.url){let T=e.createElement("a");T.className="text-info hover:text-info-strong",T.href=i.url,T.download=i.name||`file-${p+1}`,T.textContent=i.name||`File ${p+1}`,l.appendChild(T)}else{let T=e.createElement("span");T.textContent=i.name||`File ${p+1}`,l.appendChild(T)}f.appendChild(l);let h=e.createElement("td");h.className="px-3 py-2 text-muted-strong",h.textContent=j(i.length)||"0 B",f.appendChild(h);let V=e.createElement("td");V.className="px-3 py-2";let R=e.createElement("select");R.className="select",R.name=`${i.name||`file-${p+1}`}-priority`,R.setAttribute("aria-label",`Set priority for ${i.name||`file ${p+1}`}`),[{value:"1",label:"High priority"},{value:"0",label:"Low priority"},{value:"-1",label:"Don't download"}].forEach(T=>{let Y=e.createElement("option");Y.value=T.value,Y.textContent=T.label,T.value===i.priority&&(Y.selected=!0),R.appendChild(Y)}),R.value=i.priority??"0",R.addEventListener("change",T=>{lt(i,T.target.value)}),V.appendChild(R),f.appendChild(V),a.fileList.appendChild(f)}))}function E(){if(!a.selectedSection)return;let s=b();d.selectedTorrent=Te(d.selectedTorrent,s);let i=d.selectedTorrent;a.selectedSection.hidden=!i,i&&(a.selectedName&&(a.selectedName.textContent=i.name||"Untitled torrent"),a.pauseButton&&(a.pauseButton.hidden=i.paused===!0,a.pauseButton.disabled=i.paused===!0),a.resumeButton&&(a.resumeButton.hidden=i.paused!==!0,a.resumeButton.disabled=i.paused!==!0),a.downloadAllButton&&(a.downloadAllButton.disabled=!(i.progress>=1)),a.infoHashValue&&(a.infoHashValue.textContent=i.infoHash||""),B(i))}function x(s,{isSeed:i=!1}={}){C(!1),ct(s),i||n.info(`Received ${s.name||"torrent"} metadata`),d.selectedTorrent||(d.selectedTorrent=s),E(),u?.render(b(),d.selectedTorrent)}function P(s){let i=typeof s=="string"?s.trim():"";i&&(C(!0),c.add(i,be,p=>{x(p,{isSeed:!1})}))}function O(s){!Array.isArray(s)||s.length===0||(C(!0),c.seed(s,be,i=>{x(i,{isSeed:!0}),n.success(`Seeding ${i.files.length} file(s)`)}))}function g(s){if(s){if(s.progress<1){n.warn("Torrent is not finished downloading yet.");return}s.files.forEach(i=>{typeof i.getBlob=="function"&&i.getBlob((p,f)=>{if(p||!f){y.error("[beacon] Failed to get blob for file",i?.name,p),n.error("Failed to prepare file for download.");return}let l=URL.createObjectURL(f),h=e.createElement("a");h.className="beacon-hidden-download",h.setAttribute("data-beacon-hidden-download","true"),h.href=l,h.download=i.name||"download",e.body.appendChild(h),h.click(),e.body.removeChild(h),URL.revokeObjectURL(l)})})}}function A(s){if(!s?.magnetURI)return;let i=s.magnetURI,p=r?.navigator?.clipboard;if(p&&typeof p.writeText=="function"){p.writeText(i).then(()=>{n.success("Magnet URI copied to clipboard!")}).catch(f=>{y.error("[beacon] Clipboard error",f),n.error("Failed to copy magnet URI")});return}try{let f=e.createElement("textarea");f.value=i,f.className="beacon-clipboard-offscreen",f.setAttribute("data-beacon-clipboard-state","offscreen"),e.body.appendChild(f),f.select(),e.execCommand?.("copy"),e.body.removeChild(f),n.success("Magnet URI copied (fallback)!")}catch(f){y.error("[beacon] Clipboard fallback error",f),n.error("Failed to copy magnet URI")}}function U(s){s&&s.destroy(i=>{i&&(y.error("[beacon] Failed to destroy torrent",i),n.error("Failed to remove torrent"));let p=b();d.selectedTorrent=Te(null,p),u?.render(p,d.selectedTorrent),E()})}function z(){if(S.info(`[beacon] Starting beacon runtime v${ot}`),n||S.warn("[beacon] Toast service unavailable"),o.WEBRTC_SUPPORT||n?.error("Please use a browser with WebRTC support.",{sticky:!0}),a.form){let l=h=>{h.preventDefault(),P(a.magnetInput?.value||""),a.magnetInput&&(a.magnetInput.value="")};a.form.addEventListener("submit",l),m(()=>a.form.removeEventListener("submit",l))}if(a.seedButton&&a.seedInput){let l=()=>{a.seedInput.click()},h=()=>{let V=Array.from(a.seedInput.files||[]);a.seedInput.value="",O(V)};a.seedButton.addEventListener("click",l),a.seedInput.addEventListener("change",h),m(()=>{a.seedButton.removeEventListener("click",l),a.seedInput.removeEventListener("change",h)})}if(a.pauseButton){let l=()=>{d.selectedTorrent&&typeof d.selectedTorrent.pause=="function"&&(d.selectedTorrent.pause(),E())};a.pauseButton.addEventListener("click",l),m(()=>a.pauseButton.removeEventListener("click",l))}if(a.resumeButton){let l=()=>{d.selectedTorrent&&typeof d.selectedTorrent.resume=="function"&&(d.selectedTorrent.resume(),E())};a.resumeButton.addEventListener("click",l),m(()=>a.resumeButton.removeEventListener("click",l))}if(a.downloadAllButton){let l=()=>g(d.selectedTorrent);a.downloadAllButton.addEventListener("click",l),m(()=>a.downloadAllButton.removeEventListener("click",l))}if(a.removeButton){let l=()=>U(d.selectedTorrent);a.removeButton.addEventListener("click",l),m(()=>a.removeButton.removeEventListener("click",l))}if(a.copyMagnetButton){let l=()=>A(d.selectedTorrent);a.copyMagnetButton.addEventListener("click",l),m(()=>a.copyMagnetButton.removeEventListener("click",l))}let s=l=>{y.error("[beacon] Torrent client error",l),n?.error(l?.message||String(l)),C(!1)};c.on("error",s),m(()=>c.removeListener("error",s));let i=l=>{if(c.torrents&&c.torrents.length>0)return l.preventDefault(),l.returnValue="Transfers are in progress. Are you sure you want to leave or refresh?",l.returnValue};r&&typeof r.addEventListener=="function"&&(r.addEventListener("beforeunload",i),m(()=>r.removeEventListener("beforeunload",i))),u?.render(b(),d.selectedTorrent),E(),N(),C(!1);let p=r.setInterval(()=>{u?.render(b(),d.selectedTorrent),E(),N()},1e3);w.push(p);let f=r?.location?.hash;typeof f=="string"&&f.length>1&&P(f.slice(1))}function Q(){w.forEach(s=>r.clearInterval(s)),w.length=0,L.forEach(s=>{try{s()}catch(i){S.warn("[beacon] Cleanup task failed",i)}}),L.length=0,u?.destroy();try{c.destroy()}catch(s){S.warn("[beacon] Failed to destroy client",s)}}return{mount:z,destroy:Q}}var Se="../../config/instance-config.js";function dt(){return typeof window<"u"&&window||typeof globalThis<"u"&&globalThis||null}function ut(){let e=dt();if(e&&typeof e.WebTorrent=="function")return e.WebTorrent;throw new Error("WebTorrent runtime is not available on the global scope")}var $=null,ft=async()=>{try{let e=await import(Se);if(e&&typeof e.THEME_ACCENT_OVERRIDES<"u")return e.THEME_ACCENT_OVERRIDES}catch(e){S.warn(`[beacon] Failed to load theme accent overrides from ${Se}`,e)}return null},pt=async()=>{try{let e=await ft();e&&ue(e)}catch(e){S.warn("[beacon] Unable to hydrate theme accent overrides",e)}try{fe()}catch(e){S.warn("[beacon] Failed to initialize theme controller",e)}};pt();function we(){if($)return S.info("[beacon] Reusing existing beacon app instance"),$;if(typeof document>"u")throw new Error("Beacon runtime requires a document environment");let e=ut();return S.info("[beacon] Creating beacon app instance"),$=_e({documentRef:document,WebTorrentCtor:e}),$.mount(),$}if(typeof document<"u")if(document.readyState==="loading")document.addEventListener("DOMContentLoaded",()=>{try{we()}catch(e){y.error("[beacon] Failed to mount app",e)}});else try{we()}catch(e){y.error("[beacon] Failed to mount app",e)}export{_e as createBeaconApp,we as mountBeaconApp};