import { afterEach, describe, expect, it, vi } from "vitest";
import {
  getMnotePublicRuntimeConfig,
  getMnoteRuntimeConfig,
} from "@/lib/runtime-config";

describe("runtime-config public projection", () => {
  it("不再暴露 legacy mnote-web runtime 字段", () => {
    const runtime = getMnotePublicRuntimeConfig();
    expect("mnoteWebBaseUrl" in (runtime as Record<string, unknown>)).toBe(false);
    expect("mnoteWebTreeShellEnabled" in (runtime as Record<string, unknown>)).toBe(false);
  });

  it("允许通过新 runtime 配置显式选择树 renderer family", () => {
    window.__MNOTE_RUNTIME_CONFIG__ = {
      treeRendererFamily: "rust_family",
    };

    const runtime = getMnoteRuntimeConfig();

    expect(runtime.treeRendererFamily).toBe("rust_family");
  });

  it("树 renderer family 别名应归一到 rust_family", () => {
    window.__MNOTE_RUNTIME_CONFIG__ = {
      treeRendererFamily: "tree_shell" as "rust_family",
    };

    const runtime = getMnoteRuntimeConfig();

    expect(runtime.treeRendererFamily).toBe("rust_family");
  });

  it("树 renderer family 缺省时应回落到 react", () => {
    const runtime = getMnoteRuntimeConfig();

    expect(runtime.treeRendererFamily).toBe("react");
  });

  afterEach(() => {
    vi.unstubAllGlobals();
    delete process.env.NEXT_PUBLIC_MNOTE_WEB_BASE_URL;
    delete process.env.MNOTE_WEB_BASE_URL;
    delete process.env.NEXT_PUBLIC_MNOTE_WEB_TREE_SHELL_ENABLED;
    delete process.env.MNOTE_WEB_TREE_SHELL_ENABLED;
    delete process.env.NEXT_PUBLIC_TREE_RENDERER_FAMILY;
    delete process.env.TREE_RENDERER_FAMILY;
    delete window.__MNOTE_RUNTIME_CONFIG__;
  });

  it("即使保留 legacy env 也不应回注 mnote-web runtime", () => {
    process.env.NEXT_PUBLIC_MNOTE_WEB_BASE_URL = "http://127.0.0.1:3104";
    process.env.MNOTE_WEB_BASE_URL = "http://127.0.0.1:3104";
    process.env.NEXT_PUBLIC_MNOTE_WEB_TREE_SHELL_ENABLED = "1";
    process.env.MNOTE_WEB_TREE_SHELL_ENABLED = "1";

    const runtime = getMnoteRuntimeConfig();

    expect("mnoteWebBaseUrl" in (runtime as Record<string, unknown>)).toBe(false);
    expect("mnoteWebTreeShellEnabled" in (runtime as Record<string, unknown>)).toBe(false);
  });
});
