// FILE: _St/IG_StYellow/test_ext_download_v262_bridge_v279a.js | ROLE: IG_StYellow 회귀 테스트
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 manifest = JSON.parse(read('manifest.json'));
const bg = read('background.js');
const content = read('content_instagram.js');
const side = read('side.js');
const inject = read(path.join('js', 'instagram_inject.js'));

assert.strictEqual(manifest.version, '2.3.8.2810');
assert.strictEqual(manifest.version_name, '2.3.8.281');
assert.ok(side.includes('const IGBLUE_PACKAGE_VERSION = "v281";'));

// v262 download transfer/storage core.
assert.ok(bg.includes('function igblueDownloadWithApi(msg, sendResponse)'));
assert.ok(bg.includes('igblueSendDownloadProgress({status:"start", file:relativeFile, loaded:0, total:0, percent:0, message:"WS8771 direct download"});'));
assert.ok(bg.includes('igblueLocalWsRequest("ig_media_download_direct"'));
assert.ok(!bg.includes('const progressIdentity ='));
assert.ok(content.includes('function sendMainBackgroundDownload(evt)'));
assert.ok(content.includes('source_mode: "MANUAL_SINGLE"'));
assert.ok(!content.includes('manual_click: true'));

// v276 heavy runtime/pagehide diagnostic removed.
for (const token of ['IGBLUE/runtime_diag_v276', '__IGBLUE_RUNTIME_DIAG_V276__', 'igblue_runtime_diag_v276', 'sample("pagehide")']) {
  assert.ok(!content.includes(token), `content must not contain ${token}`);
  assert.ok(!inject.includes(token), `inject must not contain ${token}`);
}
assert.ok(!inject.includes('addEventListener("pagehide"'));
assert.ok(!inject.includes('teRuntimeDiagV276'));

// Latest non-download behavior remains.
for (const token of ['__IGBLUE_SPA_MONITOR_V264__', 'TE_MAIN_HOVER_CONFIRM_MS=200', 'IGBLUE/internal_no_nav_state']) {
  assert.ok(inject.includes(token), `latest MAIN feature missing: ${token}`);
}
for (const token of ['download_video_save', 'download_image_save', 'TE_AUTO_PY_PROGRESS_KEY', 'IGBLUE_CDP_REQUIRED_TEXT']) {
  assert.ok(side.includes(token), `latest Side feature missing: ${token}`);
}

console.log('test_ext_download_v262_bridge_v279a: OK');
