// FILE: _St/IG_StYellow/test_ext_manual_offline_original_download_v316.js | ROLE: PY/WS 미연결 수동 다운로드 원래 injection shortcode.mp4 fallback 회귀
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const root = __dirname;
const inject = fs.readFileSync(path.join(root,'js','instagram_inject.js'),'utf8');
const content = fs.readFileSync(path.join(root,'content_instagram.js'),'utf8');
const bg = fs.readFileSync(path.join(root,'background.js'),'utf8');

const fnStart = inject.indexOf('function teMainOriginalManualDownload(');
const fnEnd = inject.indexOf('window.addEventListener("message"', fnStart);
assert.ok(fnStart >= 0 && fnEnd > fnStart, '원래 수동 downloader 함수 없음');
const fn = inject.slice(fnStart, fnEnd);
for (const token of ['fetch(e,{})','window.URL.createObjectURL(e)','document.createElement("a")','n.download=`${stem}.${ext}`','window.URL.revokeObjectURL(t)']) {
  assert.ok(fn.includes(token), token);
}
assert.ok(fn.includes('"video/mp4"'), 'Content-Type 기본 video/mp4 없음');
assert.ok(inject.includes('teMainOriginalManualDownload(e,o||t,mediaIndex,totalMedia,"video",false);return'), '미연결 수동 MP4 클릭 즉시 원래 downloader 호출 없음');
assert.ok(content.includes('res.error_code === "IGBLUE_MANUAL_USE_MAIN_DOWNLOADER"'), '실제 WS 요청 실패 후 MAIN fallback 브리지 없음');
const bgStart = bg.indexOf('function igblueDownloadWithChromeFallback');
const bgEnd = bg.indexOf('function igblueDownloadWithApi', bgStart);
const fallback = bg.slice(bgStart,bgEnd);
assert.ok(!fallback.includes('chrome.downloads.download'), '미연결 수동 fallback에서 Chrome downloads API 사용 금지');
console.log('test_ext_manual_offline_original_download_v316: OK');
