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

let runtimeSelection: Record<string, unknown> = {};

beforeAll(async () => {
  try {
    runtimeSelection = (await import("@/lib/documents/rust-runtime-selection")) as Record<string, unknown>;
  } catch {
    runtimeSelection = {};
  }
});

describe("shouldUseBuiltBridgeRuntimeBinary", () => {
  it("当源码比已编译二进制更新时应放弃旧二进制", () => {
    const decide = runtimeSelection.shouldUseBuiltBridgeRuntimeBinary;
    expect(typeof decide).toBe("function");
    if (typeof decide !== "function") {
      return;
    }

    expect(
      decide({
        builtBinaryMtimeMs: 100,
        latestSourceMtimeMs: 200,
      }),
    ).toBe(false);
  });

  it("当二进制不旧于源码时仍可直接复用", () => {
    const decide = runtimeSelection.shouldUseBuiltBridgeRuntimeBinary;
    expect(typeof decide).toBe("function");
    if (typeof decide !== "function") {
      return;
    }

    expect(
      decide({
        builtBinaryMtimeMs: 300,
        latestSourceMtimeMs: 200,
      }),
    ).toBe(true);
  });
});
