import { describe, expect, test } from "bun:test";

import { nativeViteCacheDir, parseWorkbenchNativeRuntimeConfig } from "../vite.config";

const validConfig = {
  displayTime: { timeZone: "Asia/Shanghai", locale: "zh-CN", label: "北京时间" },
  workbench: {
    realtimeFeatures: { liveKafkaSse: false, kafkaRefreshReplay: false, projectionRealtime: true },
    debugCapabilities: { isolatedKafka: false, rawHwlabEventWindow: { enabled: false, maxEntries: 1, maxRetainedBytes: 1 } },
    traceTimeline: { autoExpandRunning: false, autoCollapseTerminal: false }
  }
};

describe("Workbench native Vite runtime config", () => {
  test("accepts the YAML-rendered runtime contract", () => {
    expect(parseWorkbenchNativeRuntimeConfig(JSON.stringify(validConfig))).toEqual(validConfig);
  });

  test("fails closed when required runtime capabilities are missing", () => {
    expect(() => parseWorkbenchNativeRuntimeConfig(JSON.stringify({ displayTime: validConfig.displayTime, workbench: {} }))).toThrow("workbench.realtimeFeatures");
  });

  test("isolates Vite optimizer caches by native runtime", () => {
    const workbench = nativeViteCacheDir({ nativeApiUrl: "http://127.0.0.1:6677", nativeCaseRunEnabled: false });
    const caserun = nativeViteCacheDir({ nativeApiUrl: "", nativeCaseRunEnabled: true });
    const cloudWeb = nativeViteCacheDir({ nativeApiUrl: "", nativeCaseRunEnabled: false });

    expect(new Set([workbench, caserun, cloudWeb]).size).toBe(3);
    expect(workbench).toBe(".state/vite/workbench");
  });
});
