// FILE: _St/IG_StYellow/test_ext_manual_download_global_highlight_v318.js | ROLE: Side 닫힘 상태 수동 다운로드 강조·Chrome storage·수동 편집폴더 회귀
const fs=require('fs'), path=require('path'), assert=require('assert');
const read=(f)=>fs.readFileSync(path.join(__dirname,f),'utf8');
const side=read('side.js'), bg=read('background.js'), content=read('content_instagram.js'), inject=read(path.join('js','instagram_inject.js'));

// 다운로드 강조 설정은 Instagram localStorage가 아니라 extension chrome.storage.local 단일 기준.
assert.ok(side.includes('const TE_VISUAL_SETTINGS_STORAGE_KEY = "ndy_ig_visual_settings"'));
assert.ok(side.includes('chrome.storage.local.get(TE_VISUAL_SETTINGS_STORAGE_KEY'));
assert.ok(side.includes('chrome.storage.local.set({ [TE_VISUAL_SETTINGS_STORAGE_KEY]: current }'));
assert.ok(!side.includes('JSON.parse(teLocalGet(teActiveDownloadStoreKey'));
assert.ok(!side.includes('teLocalSet(teActiveDownloadStoreKey, JSON.stringify'));
assert.ok(content.includes('chrome.storage.local.get(VISUAL_SETTINGS_KEY'));
assert.ok(content.includes('chrome.storage.onChanged.addListener'));
assert.ok(content.includes('postVisualSettings(data[VISUAL_SETTINGS_KEY] || {})'));

// WS 수동 다운로드 시작부터 shortcode/asset을 보내 MAIN이 즉시 어느 카드를 강조할지 안다.
const apiStart=bg.indexOf('function igblueDownloadWithApi');
const apiEnd=bg.indexOf('function igblueIsInstagramUrl', apiStart);
const api=bg.slice(apiStart, apiEnd);
assert.ok(api.includes('status:"start"'));
assert.ok(api.includes('shortcode:igblueMetaShortcode(msg && msg.meta)'));
assert.ok(api.includes('asset_kind:igblueAssetKindFromMeta(msg && msg.meta, url)'));
assert.ok(content.includes('msg.type === "ndy_down_progress"'));
assert.ok(content.includes('type: "igblue_download_progress"'));

// WS 미연결 원래 인젝션도 start/done/error를 MAIN 상태에 직접 넣는다.
const offStart=inject.indexOf('function teMainOriginalManualDownload(');
const offEnd=inject.indexOf('window.addEventListener("message"', offStart);
const off=inject.slice(offStart,offEnd);
assert.ok(off.includes('type:"igblue_download_progress"'));
assert.ok(off.includes('type:"igblue_download_result"'));
assert.ok(off.includes('status:"start"'));
assert.ok(off.includes('status:"done"'));
assert.ok(off.includes('status:"error"'));

// 완료 표시는 예전처럼 버튼에서 "다운"이 보이고 상태맵은 DOM 재생성 뒤에도 재적용 가능.
assert.ok(inject.includes('compact ? "다운"'));
assert.ok(inject.includes('content:"다운"'));
assert.ok(inject.includes('const stateByCode = new Map()'));
assert.ok(inject.includes('const observer = new MutationObserver'));

// 편집폴더는 자동 여러 개가 아니라 수동 MP4 1건만 Background에서 처리한다.
assert.ok(bg.includes('function igblueManualSingleEditCopyEligible'));
assert.ok(bg.includes('sourceMode === "MANUAL_SINGLE"'));
assert.ok(bg.includes('file_paths:[String(finalPath)]'));
assert.ok(!side.includes('teEditCopyTrackFile(result.file_path)'));
console.log('test_ext_manual_download_global_highlight_v318: OK');
