const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");

const {
  shouldShowCustomWindowControls,
  buildLocalFontsFeedback,
  normalizeLocalFonts,
} = require("../static/desktop-runtime.js");

{
  const source = fs.readFileSync(path.join(__dirname, "../static/desktop-runtime.js"), "utf8");
  const context = {
    module: { exports: {} },
    exports: {},
    require,
    console,
  };

  vm.runInNewContext(source, context, { filename: "desktop-runtime.js" });
  assert(context.module.exports.normalizeLocalFonts, "module export should load without globalThis");
}

{
  assert.strictEqual(
    shouldShowCustomWindowControls({ isQt: true }),
    false,
    "Qt shell should hide in-page window controls"
  );
  assert.strictEqual(
    shouldShowCustomWindowControls({ isQt: false }),
    true,
    "browser mode should keep in-page window controls"
  );
}

{
  const feedback = buildLocalFontsFeedback(0);
  assert.strictEqual(feedback.type, "info");
  assert.strictEqual(feedback.message, "未发现可用的本地字体");
}

{
  const feedback = buildLocalFontsFeedback(12);
  assert.strictEqual(feedback.type, "success");
  assert.strictEqual(feedback.message, "成功加载 12 款本地字体");
}

{
  const fonts = normalizeLocalFonts([
    { name: "黑体", value: "SimHei" },
    { postscriptName: "MicrosoftYaHei", fullName: "微软雅黑" },
    { name: "黑体", value: "SimHei" },
    { fullName: "宋体", postscriptName: "SimSun" },
  ]);

  assert.deepStrictEqual(fonts, [
    { name: "宋体", value: "SimSun" },
    { name: "微软雅黑", value: "MicrosoftYaHei" },
    { name: "黑体", value: "SimHei" },
  ]);
}

console.log("desktop runtime tests passed");
