/**
 * KRPricing 会员档 / 案件规定自测（Node，无浏览器）
 * 运行：node website/app/js/kr_pricing_selftest.js
 */
(function () {
  "use strict";
  var fails = 0;
  function eq(name, a, b) {
    var ok = JSON.stringify(a) === JSON.stringify(b);
    if (!ok) { fails++; console.error("FAIL", name, "got", a, "want", b); }
    else console.log("ok", name);
  }
  global.KRAuthor = { unlocked: false, ensure: function () { return this.unlocked; } };
  require("./kr_pricing.js");
  var P = global.KRPricing;

  eq("guest tier", P.currentTier(), "free");
  eq("guest no pro", P.hasPro(), false);
  eq("guest quota", P.casePolicy().quota, 3);
  eq("guest no events", P.casePolicy().events, false);
  eq("guest no json", P.casePolicy().exportJson, false);
  eq("guest no judgment", P.casePolicy().exportJudgment, false);
  eq("guest no proDetail", P.allows("proDetail"), false);

  P.setPreviewTier("pro");
  eq("preview ignored without author", P.currentTier(), "free");
  P.setPreviewTier(null);

  global.KRAuthor.unlocked = true;
  eq("author = resonance", P.currentTier(), "resonance");
  eq("author quota", P.casePolicy().quota, 300);
  eq("author events", P.casePolicy().events, true);
  eq("author judgment", P.allows("judgmentReport"), true);

  P.setPreviewTier("free");
  eq("author preview free", P.currentTier(), "free");
  eq("preview free quota", P.casePolicy().quota, 3);
  eq("preview free no pro tab", P.hasPro(), false);
  eq("preview free no key tab", P.hasResonance(), false);

  P.setPreviewTier("pro");
  eq("author preview pro", P.currentTier(), "pro");
  eq("pro quota", P.casePolicy().quota, 30);
  eq("pro note", P.casePolicy().note, true);
  eq("pro no events", P.casePolicy().events, false);
  eq("pro json", P.casePolicy().exportJson, true);
  eq("pro no intensity", P.casePolicy().showIntensity, false);
  eq("pro no judgment", P.casePolicy().exportJudgment, false);
  eq("pro hasPro", P.hasPro(), true);
  eq("pro not resonance", P.hasResonance(), false);

  P.setPreviewSingle(true);
  eq("24h does not raise tier", P.currentTier(), "pro");
  eq("24h opens judgment", P.casePolicy().exportJudgment, true);
  eq("24h allows judgmentReport", P.allows("judgmentReport"), true);
  eq("24h still no keyResonanceFull", P.allows("keyResonanceFull"), false);

  var pay = P.checkout("pro");
  eq("checkout not connected", pay.ok, false);
  eq("checkout message", pay.message, "结账链接未接通");
  eq("checkoutConnected flag", P.checkoutConnected, false);

  P.setPreviewTier("resonance");
  P.setPreviewSingle(false);
  var cs = { name: "甲", pillars: "癸丑 壬戌 甲申 庚午", intensity: "2.40", events: [{ date: "2024", text: "x" }], pattern: "正格", yong: "水", ji: "火" };
  var red = P.sanitizeCase(cs, false);
  eq("sanitize(full=false) drops intensity", red.intensity, undefined);
  P.setPreviewTier("pro");
  red = P.sanitizeCase(cs, false);
  eq("pro sanitize keeps pattern", red.pattern, "正格");
  eq("pro sanitize drops events", red.events, undefined);

  if (fails) { console.error("FAILED", fails); process.exit(1); }
  console.log("ALL_OK");
})();
