// FILE: _St/IG_StYellow/test_ext_cache_storage_v300.js | ROLE: v300 자동 Cache·Video 게이트·간소화 저장 회귀
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const root = __dirname;
const side = fs.readFileSync(path.join(root, 'side.js'), 'utf8');
const html = fs.readFileSync(path.join(root, 'side.html'), 'utf8');
const background = fs.readFileSync(path.join(root, 'background.js'), 'utf8');
const ws = fs.readFileSync(path.join(root, '..', 'WS8771', 'ws8771_main.py'), 'utf8');

assert.ok(side.includes('function teCache8701Submit'));
assert.ok(side.includes('http://127.0.0.1:8701/api/meta'));
assert.ok(side.includes('http://127.0.0.1:8701/api/status?run_id='));
for (const token of ['Cache Image · 대상', '신규저장', '중복스킵', '실패']) assert.ok(side.includes(token), token);
assert.ok(side.includes('const items = teCache8701Items(list);'));
assert.ok(side.includes('const shortcode = String(media && (media.code || media.shortcode)'));

const kindsStart = side.indexOf('function teAutoPyDownloadableKindsFromCache(media)');
const kindsEnd = side.indexOf('function teAutoPyCacheTargetForMedia', kindsStart);
const kinds = side.slice(kindsStart, kindsEnd);
assert.ok(kinds.includes('teDownloadVideoSave()'));
assert.ok(kinds.includes('out.push("mp4")'));
assert.ok(!kinds.includes('out.push("jpg")'));
const directStart = side.indexOf('async function teAutoPyDownloadMediaPairDirect');
const directEnd = side.indexOf('async function teAutoPyStopCycle', directStart);
const direct = side.slice(directStart, directEnd);
assert.ok(direct.includes('if (!teDownloadVideoSave())'));
assert.ok(direct.includes('ig_media_metadata_collect'));
assert.ok(direct.includes('for (const kind of ["mp4"])'));
assert.ok(!direct.includes('for (const kind of ["mp4", "jpg"])'));

assert.ok(side.includes('source_mode: "MANUAL_SINGLE"'), 'manual single download must remain');
assert.ok(side.includes('function teNotifyManualDownloadStart'), 'manual JPG/MP4 entry must remain');
const queueStart = side.indexOf('function q(e, accountFolderMergeOverride, flowMeta)');
const queueEnd = side.indexOf('async function teCollectMetadataOnlyEntry', queueStart);
const queue = side.slice(queueStart, queueEnd);
assert.ok(queue.includes('const manualSingle = sourceMode === "MANUAL_SINGLE";'));
assert.ok(queue.includes('if (manualSingle || teDownloadVideoSave()) urls.video'));
assert.ok(queue.includes('if (manualSingle) urls.image'));
assert.ok(html.includes('data-fold-icon'));
assert.ok(side.includes('if (icon && (ev.target === icon || icon.contains(ev.target)))'));
assert.ok(side.includes('else {\n            ev.preventDefault();'));
assert.ok(side.includes('teAutoPySelectProfileAccountAfterInjection'));
assert.ok(side.includes('teAutoPySetCurrentAccountAddTarget(username, "profile_injection", "")'));
assert.ok(!html.includes('보고서 재생성'));
assert.ok(!side.includes('ig_report_rebuild_all'));

assert.ok(background.includes('const stableFileName = shortcode ? `${igblueSafePath(shortcode, "post")}.${ext}` : originalFileName;'));
assert.ok(ws.includes('build_instagram_media_path(ST_ROOT, account, shortcode, extension)'));
for (const token of ['def file_hashes', 'def generate_account_html_report', 'def write_sidecar_md5', 'def upsert_folder_json']) assert.ok(!ws.includes(token), token);
for (const file of ['ws8771_html_report.py', 'ws8771_folder_reconcile.py', 'assets/echarts.min.js']) {
  assert.strictEqual(fs.statSync(path.join(root, '..', 'WS8771', file)).size, 0, `${file} must be deletion placeholder`);
}
console.log('OK test_ext_cache_storage_v300');
