const assert = require('assert');
const fs = require('fs');
const path = require('path');

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

assert.strictEqual(manifest.version, '2.3.8.276');
assert.strictEqual(manifest.version_name, '2.3.8.276');
assert.ok(side.includes('const IGBLUE_PACKAGE_VERSION = "v276";'));
assert.ok(inject.includes('const TE_RUNTIME_DIAG_KEY_V276="IGBLUE/runtime_diag_v276"'));
assert.ok(inject.includes('window.__IGBLUE_RUNTIME_DIAG_V276__=api'));
assert.ok(inject.includes('PerformanceObserver.supportedEntryTypes.includes("longtask")'));
assert.ok(inject.includes('setInterval((()=>sample("interval_10s")),10000)'));
assert.ok(inject.includes('dom_total:Number(document.getElementsByTagName("*").length||0)'));
assert.ok(inject.includes('used_js_heap_bytes'));
assert.ok(inject.includes('media_map_count:Object.keys(n||{}).length'));
assert.ok(inject.includes('spa_retry_timer_count'));
assert.ok(inject.includes('teRuntimeDiagV276.markPhase("DOM_SCAN_BEGIN"'));
assert.ok(inject.includes('teRuntimeDiagV276.markDomScan(elapsed,!error,error)'));
assert.ok(inject.includes('teRuntimeDiagV276.markObserver("spa_dom")'));
assert.ok(inject.includes('teRuntimeDiagV276.markObserver("download_status")'));
assert.ok(!inject.includes('addEventListener("unload"'));
assert.ok(!inject.includes("addEventListener('unload'"));
assert.ok(content.includes('const IGBLUE_RUNTIME_DIAG_KEY_V276 = "IGBLUE/runtime_diag_v276"'));
assert.ok(content.includes('function igblueRuntimeDiagTextV276()'));
assert.ok(content.includes('globalThis.__IGBLUE_CONTENT_RUNTIME_DIAG_V276__'));
assert.ok(content.includes('msg.type === "igblue_runtime_diag_v276"'));
assert.ok(content.includes('localStorage.setItem(IGBLUE_AUTO_RUNTIME_STATUS_KEY_V276'));
assert.ok(content.includes('igblueRuntimeDiagTextV276()'));
for (const token of ['phase:', 'run_seq:', 'account_index:', 'account_total:', 'account_username:']) {
  assert.ok(side.includes(token), `missing auto correlation field: ${token}`);
}
assert.ok(side.includes('type: "igblue_auto_status_update"'));
assert.ok(!side.includes('igblue_runtime_auto_reload_v276'));
console.log('OK test_ext_runtime_diagnostic_v276');
