import { afterEach, beforeEach, describe, expect, test } from "vitest";
import "../gui/EFPreview.js";
import "./EFCaptions.js";
import "./EFVideo.js";
import { v4 } from "uuid";

describe("EFCaptions", () => {
  describe("when rendering", () => {
    beforeEach(() => {
      // @ts-ignore
      window.FRAMEGEN_BRIDGE = true;
    });
    afterEach(() => {
      // @ts-ignore
      delete window.FRAMEGEN_BRIDGE;
    });
    test("captionsPath uses http:// protocol", () => {
      const id = v4();
      const workbench = document.createElement("ef-workbench");

      const target = document.createElement("ef-video");
      target.setAttribute("id", id);
      target.assetId = "550e8400-e29b-41d4-a716-446655440000";
      document.body.appendChild(target);
      const captions = document.createElement("ef-captions");
      captions.setAttribute("target", id);
      document.body.appendChild(captions);
      workbench.appendChild(captions);
      expect(captions.captionsPath()).toBe(
        "https://editframe.dev/api/v1/caption_files/550e8400-e29b-41d4-a716-446655440000",
      );
    });
  });

  describe("attribute: asset-id", () => {
    test("determines assetPath", () => {
      const id = v4();
      const target = document.createElement("ef-video");
      target.setAttribute("id", id);
      target.assetId = id;
      document.body.appendChild(target);
      const captions = document.createElement("ef-captions");
      captions.setAttribute("target", id);
      document.body.appendChild(captions);
      expect(captions.captionsPath()).toBe(
        `https://editframe.dev/api/v1/caption_files/${id}`,
      );
    });

    test("Honors provided apiHost", () => {
      const preview = document.createElement("ef-preview");

      const id = v4();
      const target = document.createElement("ef-video");
      target.setAttribute("id", id);
      target.assetId = id;
      document.body.appendChild(target);
      const captions = document.createElement("ef-captions");
      captions.setAttribute("target", id);
      preview.appendChild(captions);
      document.body.appendChild(preview);
      preview.apiHost = "test://";
      expect(captions.captionsPath()).toBe(
        `test:///api/v1/caption_files/${id}`,
      );
    });
  });
});
