/**
 * solar-core.test.js — solar-core.js 自检脚本（Node 直接运行）
 * 用法：node solar-core.test.js
 * 退出码：0 全部通过 / 1 存在失败
 */
'use strict';

var SolarCore = require('./solar-core.js');

var passed = 0;
var failed = 0;
var lines = [];

function check(name, cond, detail) {
  if (cond) {
    passed++;
    lines.push('  PASS  ' + name);
  } else {
    failed++;
    lines.push('  FAIL  ' + name + (detail ? '   ' + detail : ''));
  }
}

function close(a, b, tol) {
  tol = (tol === undefined) ? 1e-6 : tol;
  return Math.abs(a - b) <= tol;
}

function fmt(n, d) {
  return Number(n).toFixed(d === undefined ? 2 : d);
}

var C = SolarCore;
var TILT = C.AXIAL_TILT_DEG; // 23.44

lines.push('==========================================================');
lines.push(' solar-core.js 自检（教学近似模型，非精密历算）');
lines.push(' 公式: H = acos(-tan(lat)*tan(dec))，时长 = 2H/15 小时');
lines.push('==========================================================');

/* ---------- 1. 赤纬映射 ---------- */
lines.push('\n[1] 季节 → 太阳赤纬');
check('春分 δ = 0°', close(C.declinationForSeason('春分'), 0));
check('夏至 δ = +23.44°', close(C.declinationForSeason('夏至'), 23.44));
check('秋分 δ = 0°', close(C.declinationForSeason('秋分'), 0));
check('冬至 δ = -23.44°', close(C.declinationForSeason('冬至'), -23.44));
check('未知季节回落 0°', close(C.declinationForSeason('???'), 0));

/* ---------- 2. 赤道恒 12 小时 ---------- */
lines.push('\n[2] 赤道（φ=0°）各季节日照 ≈ 12 小时（关键分支）');
C.SEASONS.forEach(function (s) {
  var r = C.daylightForSeason(0, s);
  check('赤道 · ' + s + ' → ' + fmt(r.hours) + ' h (' + C.modeLabel(r.mode) + ')',
    r.mode === 'normal' && close(r.hours, 12, 1e-9),
    '期望 12 h / normal，实际 ' + fmt(r.hours) + ' / ' + r.mode);
});

/* ---------- 3. 二分日恒 12 小时 ---------- */
lines.push('\n[3] 二分日（δ=0°）各纬度日照 ≈ 12 小时（关键分支）');
[0, 20, 40, 60, 66.56, 80, -40, -80].forEach(function (lat) {
  var r = C.daylight(lat, 0);
  check('δ=0° · φ=' + lat + '° → ' + fmt(r.hours) + ' h',
    r.mode === 'normal' && close(r.hours, 12, 1e-9),
    '期望 12 h / normal，实际 ' + fmt(r.hours) + ' / ' + r.mode);
});

/* ---------- 4. 极昼 / 极夜分支 ---------- */
lines.push('\n[4] 极昼 / 极夜分支（关键分支）');
var pSummer = C.daylight(80, TILT);            // 北纬80° 夏至
var pWinter = C.daylight(80, -TILT);           // 北纬80° 冬至
var pEq = C.daylight(80, 0);                   // 北纬80° 二分
check('北纬80° 夏至 → 极昼 24 h', pSummer.mode === 'polarDay' && close(pSummer.hours, 24),
  '实际 ' + pSummer.mode + ' / ' + fmt(pSummer.hours));
check('北纬80° 冬至 → 极夜 0 h', pWinter.mode === 'polarNight' && close(pWinter.hours, 0),
  '实际 ' + pWinter.mode + ' / ' + fmt(pWinter.hours));
check('北纬80° 二分 → 12 h 正常', pEq.mode === 'normal' && close(pEq.hours, 12),
  '实际 ' + pEq.mode + ' / ' + fmt(pEq.hours));

check('南纬80° 夏至(δ=+23.44) → 极夜 0 h',
  C.daylight(-80, TILT).mode === 'polarNight' && close(C.daylight(-80, TILT).hours, 0));
check('南纬80° 冬至(δ=-23.44) → 极昼 24 h',
  C.daylight(-80, -TILT).mode === 'polarDay' && close(C.daylight(-80, -TILT).hours, 24));

// 北极圈边界（90-23.44 = 66.56°）夏至应为极昼（或恰好 24 h）
var arc = C.daylight(90 - TILT, TILT);
check('北极圈 66.56° 夏至 → 极昼/24 h 边界', arc.hours >= 24 - 1e-6 && (arc.mode === 'polarDay' || close(arc.hours, 24, 1e-6)),
  '实际 ' + arc.mode + ' / ' + fmt(arc.hours, 6));

// 极点
check('北极(90°) 夏至 → 极昼 24 h', C.daylight(90, TILT).hours === 24 && C.daylight(90, TILT).mode === 'polarDay');
check('北极(90°) 冬至 → 极夜 0 h', C.daylight(90, -TILT).hours === 0 && C.daylight(90, -TILT).mode === 'polarNight');
check('北极(90°) 二分 → 12 h', close(C.daylight(90, 0).hours, 12));
check('南极(-90°) 夏至 → 极夜 0 h', C.daylight(-90, TILT).hours === 0 && C.daylight(-90, TILT).mode === 'polarNight');
check('南极(-90°) 冬至 → 极昼 24 h', C.daylight(-90, -TILT).hours === 24 && C.daylight(-90, -TILT).mode === 'polarDay');
check('南极(-90°) 二分 → 12 h', close(C.daylight(-90, 0).hours, 12));

/* ---------- 5. 数值锚点 ---------- */
lines.push('\n[5] 数值锚点（与独立手算对照）');
// φ=40° δ=+23.44°：x=-tan40*tan23.44≈-0.36401，acos→111.34°，t≈14.85 h
var s40 = C.daylight(40, TILT);
check('北纬40° 夏至 ≈ 14.85 h', close(s40.hours, 14.85, 0.05),
  '实际 ' + fmt(s40.hours, 4) + ' h, H=' + fmt(s40.H_deg, 3) + '°');
// φ=40° δ=-23.44°：t≈9.15 h
var w40 = C.daylight(40, -TILT);
check('北纬40° 冬至 ≈ 9.15 h', close(w40.hours, 9.15, 0.05),
  '实际 ' + fmt(w40.hours, 4) + ' h, H=' + fmt(w40.H_deg, 3) + '°');
check('北纬40° 夏至 + 冬至 = 24 h', close(s40.hours + w40.hours, 24, 1e-6));
check('公式自洽 t = 2H/15', close(s40.hours, 2 * s40.H_deg / 15, 1e-9));

/* ---------- 6. 南北半球对称 ---------- */
lines.push('\n[6] 南北半球对称性  t(-φ, δ) = 24 - t(φ, δ)');
[10, 23.44, 40, 55, 70].forEach(function (lat) {
  [0, TILT, -TILT].forEach(function (dec) {
    var a = C.daylightHours(lat, dec);
    var b = C.daylightHours(-lat, dec);
    check('φ=' + lat + ' δ=' + dec + ' ↔ φ=' + (-lat),
      close(a + b, 24, 1e-6), fmt(a) + ' + ' + fmt(b) + ' = ' + fmt(a + b));
  });
});

/* ---------- 7. spec.json 全矩阵（3 地点 × 4 季节） ---------- */
lines.push('\n[7] inputs/spec.json 全矩阵（3 地点 × 4 季节）');
lines.push('  --------------------------------------------------');
lines.push('  地点            春分     夏至       秋分     冬至');
lines.push('  --------------------------------------------------');
C.LOCATIONS.forEach(function (loc) {
  var cells = C.SEASONS.map(function (s) {
    var r = C.daylightForSeason(loc.latitude, s);
    if (r.mode === 'polarDay') return '24.00(极昼)';
    if (r.mode === 'polarNight') return '0.00(极夜)';
    return fmt(r.hours);
  });
  var name = (loc.name + '            ').slice(0, 12);
  lines.push('  ' + name + '  ' + cells.join('  '));
  C.SEASONS.forEach(function (s) {
    var r = C.daylightForSeason(loc.latitude, s);
    check(loc.name + ' · ' + s + ' → ' + fmt(r.hours) + ' h (' + C.modeLabel(r.mode) + ')',
      isFinite(r.hours) && r.hours >= 0 && r.hours <= 24);
  });
});
lines.push('  --------------------------------------------------');

/* ---------- 8. 关键锚点复核（赤道12h / 极昼极夜） ---------- */
lines.push('\n[8] 关键分支复核');
var eqAllOk = C.SEASONS.every(function (s) {
  var r = C.daylightForSeason(0, s);
  return r.mode === 'normal' && close(r.hours, 12, 1e-9);
});
check('赤道四季全部 12.00 h ✓', eqAllOk);

var polarOk = (C.daylightForSeason(80, '夏至').hours === 24)
  && (C.daylightForSeason(80, '夏至').mode === 'polarDay')
  && (C.daylightForSeason(80, '冬至').hours === 0)
  && (C.daylightForSeason(80, '冬至').mode === 'polarNight');
check('北纬80° 夏至极昼 24h / 冬至极夜 0h ✓', polarOk);

var eqDecOk = [0, 40, 80].every(function (lat) {
  return close(C.daylightHours(lat, 0), 12, 1e-9);
});
check('二分日三纬度全部 12.00 h ✓', eqDecOk);

/* ---------- 汇总 ---------- */
lines.push('\n==========================================================');
lines.push(' 汇总: ' + passed + ' 通过, ' + failed + ' 失败, 共 ' + (passed + failed) + ' 项');
lines.push(' 结论: ' + (failed === 0 ? '全部通过 ✓' : '存在失败 ✗'));
lines.push('==========================================================');

var out = lines.join('\n');
console.log(out);
process.exit(failed === 0 ? 0 : 1);
