// FILE: _St/IG_StYellow/test_ext_side_reread_restart_ui.js | ROLE: IG_StYellow 회귀 테스트
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 content = fs.readFileSync(path.join(root, 'content_instagram.js'), 'utf8');
const inject = fs.readFileSync(path.join(root, 'js', 'instagram_inject.js'), 'utf8');
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'manifest.json'), 'utf8'));

assert.strictEqual(manifest.version, '2.3.8.2870');
assert.strictEqual(manifest.version_name, '2.3.8.287');
assert.ok(html.includes('id="mini_reread"') && html.includes('탭 새로읽기'));
assert.ok(side.includes('type: "ndy_reread"') && content.includes('igblue_reread_current_tab'));
assert.ok(inject.includes('teStatsLastDispatchSignature=""') && inject.includes('manual_reread'));
assert.ok(html.indexOf('다운로드 방식 2 · 필터 결과') < html.indexOf('다운로드 방식 3 · Top sorted 자동'));
assert.ok(!html.includes('ID-date filename = username_YYYYMMDD_HHMMSS.ext.'));
assert.ok(side.includes('teUnifiedCurrentAccountName') && side.includes('현재 주소  ${currentAccountName}'));

// v263: one Auto Start button handles fresh start and saved-progress continuation.
assert.ok(html.includes('id="igblue_auto_py_start"'));
assert.ok(html.includes('id="igblue_auto_progress_reset"') && html.includes('진행상태 리셋'));
assert.ok(!html.includes('id="igblue_auto_restart"'));
assert.ok(!side.includes('teAutoPyRestartFromStop'));
assert.ok(!side.includes('teAutoPySyncRestartButtonState'));
assert.ok(side.includes('function teAutoPySaveProgressState(reason)'));
assert.ok(side.includes('function teAutoPyLoadProgressState()'));
assert.ok(side.includes('async function teAutoPyStartOrContinueFromUi()'));
assert.ok(side.includes('resume_progress: !!state'));
assert.ok(side.includes('new_session: !state'));
assert.ok(side.includes('function teAutoPyResetProgressState(reason, options)'));
assert.ok(side.includes('teAutoPySetAccountQueueIndex(0)'));
assert.ok(side.includes('teAutoPySetLastRank(0, String(reason || "progress_reset"))'));
assert.ok(side.includes('teAutoPySaveProgressState(reason || "user_stop")'));
assert.ok(side.includes('teAutoPyRestoreProgressNotice()'));
assert.ok(side.includes('시작을 누르면 보존된 진행상태에서 이어서 실행합니다.'));

// Stop becomes available before tab acquisition/wait so a stuck preparation can be cancelled.
const runningPos = side.indexOf('teAutoPyRunning = true;', side.indexOf('async function teAutoPyStartOnceFromUi'));
const buttonUpdatePos = side.indexOf('teAutoPyUpdateButtons(teAutoPyLastPyOk || true);', runningPos);
const acquirePos = side.indexOf('let sessionReady =', runningPos);
assert.ok(runningPos >= 0 && buttonUpdatePos > runningPos && acquirePos > buttonUpdatePos);

// Feature logs distinguish fresh start/continuation and the restored queue position.
for (const token of ['"AS-01"', '"AS-02"', '"AS-03"', '실행방식=${runMode}', '진행상태=${Math.max']) {
  assert.ok(side.includes(token), `missing start progress log: ${token}`);
}

// Successful automatic end clears the saved progress; user stop preserves it.
const finishPos = side.indexOf('function teAutoPyFinalizeAutomaticEnd');
const clearPos = side.indexOf('teAutoPyClearProgressState();', finishPos);
assert.ok(finishPos >= 0 && clearPos > finishPos);

console.log('OK test_ext_side_reread_restart_ui v272');
