import { describe, expect, it } from "vitest";

import {
  isBrowserLocalChatImageSource,
  isCacheableRemoteChatMediaUrl,
  normalizeChatImageSource,
} from "../chat_media_url";

describe("chat media url helpers", () => {
  it("detects cacheable remote media urls", () => {
    expect(isCacheableRemoteChatMediaUrl("https://example.com/a.jpg")).toBe(
      true,
    );
    expect(isCacheableRemoteChatMediaUrl("http://example.com/a.jpg")).toBe(
      true,
    );
    expect(isCacheableRemoteChatMediaUrl("blob:http://app/media")).toBe(false);
    expect(isCacheableRemoteChatMediaUrl("data:image/png;base64,abc")).toBe(
      false,
    );
  });

  it("normalizes bare base64 image payloads", () => {
    expect(normalizeChatImageSource("abc123")).toBe(
      "data:image/png;base64,abc123",
    );
    expect(normalizeChatImageSource("blob:http://app/media")).toBe(
      "blob:http://app/media",
    );
    expect(normalizeChatImageSource("https://example.com/a.jpg")).toBe(
      "https://example.com/a.jpg",
    );
  });

  it("detects image sources that should bypass Next image optimization", () => {
    expect(isBrowserLocalChatImageSource("blob:http://app/media")).toBe(true);
    expect(isBrowserLocalChatImageSource("data:image/png;base64,abc")).toBe(
      true,
    );
    expect(isBrowserLocalChatImageSource("https://example.com/a.jpg")).toBe(
      false,
    );
  });
});
