import { describe, expect, test } from "vitest";
import type { MediaAsset } from "@/types/media";
import { isRealFileAsset, parseSupabaseStorageObjectUrl } from "./asset";

describe("file-tree/asset", () => {
  test("parseSupabaseStorageObjectUrl", () => {
    expect(
      parseSupabaseStorageObjectUrl(
        "https://xxx.supabase.co/storage/v1/object/public/documents/a/b/c.txt",
      ),
    ).toEqual({ bucket: "documents", path: "a/b/c.txt" });
    expect(
      parseSupabaseStorageObjectUrl(
        "https://xxx.supabase.co/storage/v1/object/sign/documents/a/b/c.txt?token=abc",
      ),
    ).toEqual({ bucket: "documents", path: "a/b/c.txt" });
    expect(parseSupabaseStorageObjectUrl("/documents/123")).toBe(null);
  });

  test("isRealFileAsset", () => {
    const base = { id: "1" } as MediaAsset;
    expect(isRealFileAsset({ ...base, storage_path: "a/b", file_url: null } as any)).toBe(true);
    expect(isRealFileAsset({ ...base, storage_path: null, file_url: "/documents/123" } as any)).toBe(false);
    expect(
      isRealFileAsset({
        ...base,
        storage_path: null,
        file_url: "https://xxx.supabase.co/storage/v1/object/sign/documents/a/b/c.txt?token=abc",
      } as any),
    ).toBe(true);
  });
});

