// FILE: _St/IG_StYellow/test_ext_single_tab_chrome_download_v284.js | ROLE: v284 단일 탭·downloadId 실제 경로·공통 다운로드 회귀 검사
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 bg = read('background.js');
const side = read('side.js');
const content = read('content_instagram.js');
const ws = fs.readFileSync(path.join(root, '..', 'WS8771', 'ws8771_main.py'), 'utf8');

assert.strictEqual(manifest.version, '2.3.8.2840');
assert.strictEqual(manifest.version_name, '2.3.8.284');
assert.ok(manifest.description.includes('one fixed Instagram tab'));
assert.ok(side.includes('const IGBLUE_PACKAGE_VERSION = "v284";'));

for (const token of [
  'const IGBLUE_SINGLE_TAB_STORAGE_KEY = "YELLOW/single_tab_v284"',
  'function igblueNavigateActiveWebTab(url, preferredTabId, navigationMeta, callback)',
  'chrome.tabs.update(tabId, {url:targetUrl, active:true}',
  '"ST-01", "[체크완료]", "자동 실행 탭 고정"',
  '[단일탭전환][ST-02][체크완료]',
  '[단일탭전환][ST-03][체크완료]',
  'type: "IGBLUE_CAPTURE_ACTIVE_SINGLE_TAB"',
  'type: "IGBLUE_NAVIGATE_ACTIVE_TAB"',
]) assert.ok(bg.includes(token) || side.includes(token), `missing single-tab token: ${token}`);

for (const retired of [
  'HANDOFF_READY', 'OWNER_COMMITTED', 'OLD_TAB_CLOSED', 'RETIRED',
  'IGBLUE_COMMIT_TAB_HANDOFF', 'IGBLUE_CLAIM_TAB_OWNER',
  'pending_handoff', 'handoff_token', 'YELLOW_TEMP',
  'igblueClosePreviousTabAfterReplacement',
]) assert.ok(!bg.includes(retired) && !side.includes(retired) && !content.includes(retired), `retired token remains: ${retired}`);
assert.ok(!bg.includes('chrome.tabs.remove('), 'account cycle must not close any tab');
assert.ok(!bg.includes('chrome.tabs.create('), 'v284 background must not create Instagram tabs');

for (const token of [
  'const igblueChromeDownloadJobs = new Map()',
  'chrome.downloads.onChanged.addListener',
  'chrome.downloads.search({id:id}',
  'item.state !== "complete"',
  'const actualPath = String(item.filename || "").trim()',
  '/\\.crdownload$/i.test(actualPath)',
  'chrome.downloads.download({url:url, saveAs:false, conflictAction:"uniquify"}',
  'function igblueStartChromeSessionDownload(msg, options)',
  'function igblueDownloadWithApi(msg, sendResponse)',
  'function igblueDownloadWithChromeFallback(msg, finish, reason)',
  'igblueLocalWsRequest("ig_media_downloaded"',
]) assert.ok(bg.includes(token), `missing downloadId token: ${token}`);

for (const field of ['downloadId:0', 'account:account', 'shortcode:shortcode', 'asset:asset', 'original_filename:originalFileName', 'post_url:', 'media_url:url', 'tab_id:', 'run_seq:', 'final_relative_file:']) {
  assert.ok(bg.includes(field), `download task field missing: ${field}`);
}
assert.ok(bg.includes('igblueChromeDownloadJobs.set(id, {'));
assert.ok(bg.includes('const job = igblueChromeDownloadJobs.get(id)'));
assert.ok(!bg.includes('filename:YELLOW_TEMP') && !bg.includes('filename:tempFile'));

assert.ok(ws.includes('return True, "default_enter"'));
assert.ok(!ws.includes('WM_SETTEXT') && !ws.includes('def write_control_text') && !ws.includes('def save_dialog_target_path'));
for (const token of ['download_incomplete_crdownload','source_file_empty','final_file_empty','source_file_remains_after_move','[Chrome다운로드][CD-03][체크완료]','[Chrome다운로드][CD-04][체크완료]']) {
  assert.ok(ws.includes(token), `missing WS finalization token: ${token}`);
}
assert.ok(content.includes("return `CDP:${cdp} · tab ${tabId || '-'}`"));
assert.ok(content.includes("'단일 탭 제어 v284'"));

console.log('OK test_ext_single_tab_chrome_download_v284');

assert.ok(side.includes('reason: "auto_start_button"'));
assert.ok(bg.includes('status:"single_tab_captured"'));
