const fs = require('fs');
const path = require('path');
const assert = require('assert');
const root = __dirname;
const read = (name) => fs.readFileSync(path.join(root, name), 'utf8');
const content = read('content_instagram.js');
const inject = read('js/instagram_inject.js');
const side = read('side.js');
const manifest = JSON.parse(read('manifest.json'));

assert.strictEqual(manifest.version, '2.3.8.239');
assert.strictEqual(manifest.version_name, '2.3.8.239');

// Right-side origin/status box is no longer shown directly.
assert.ok(content.includes('#IGBLUE_NoTop > .igblue-origin-panel'));
assert.ok(content.includes('#IGBLUE_NoTop > .igblue-status-box'));
assert.ok(content.includes('display:none !important'));

// A compact square diagnostic button lives next to the existing CDP gate.
assert.ok(content.includes("toggle.id = 'igblue_main_diag_toggle'"));
assert.ok(content.includes("toggle.textContent = '⋮'"));
assert.ok(content.includes("const r = cdp && cdp.getBoundingClientRect"));
assert.ok(content.includes("let left = r ? r.right + gap"));
assert.ok(content.includes("panel.id = 'igblue_main_diag_panel'"));
assert.ok(content.includes("width:174px"));
assert.ok(content.includes("max-height:58vh"));
assert.ok(content.includes("font:700 9px/1.26"));

// Hidden diagnostics still mirror the live original values.
for (const token of ['IGBLUE_OriginInfo', 'IGBLUE_SyncLog', 'IGBLUE_HoverStatus', 'IGBLUE_DevStatus']) {
  assert.ok(content.includes(`text('${token}'`), `missing diagnostic source: ${token}`);
}

// Stored visual settings also keep MAIN equal to Side after load/save.
assert.ok(side.includes('e.items[r].main = e.items[r].side;'));
assert.ok(side.includes('main: i?.side !== false'));
assert.ok(side.includes('e.levels[r].main = e.levels[r].side;'));
assert.ok(side.includes('e.main = e.side;'));

// MAIN uses the same Side visibility rules for date colors and per-metric bars.
assert.ok(inject.includes('if(!i||!1===i.enabled||i.side===false)return;'));
assert.ok(inject.includes('a&&!1!==a.enabled&&a.side!==false&&('));

// No label survives later account-row rewrites and Prev/Next always has a visible current marker.
assert.ok(inject.includes('installIgblueMainIdentitySplitV239'));
assert.ok(inject.includes('const rankInfo=code&&teMainRankLabelMap'));
assert.ok(inject.includes('rank=teMainRankNoText(Number(rankInfo.no||0))'));
assert.ok(inject.includes('.ndy_ins_info[data-igblue-active="1"]'));
assert.ok(inject.includes('background:rgba(255,213,74,.92)'));

console.log('OK test_ext_main_ui v239');
