// FILE: _St/IG_StYellow/test_ext_rank_visual_removal.js | ROLE: IG_StYellow 회귀 테스트
const fs=require("fs"),path=require("path"),assert=require("assert");
const read=n=>fs.readFileSync(path.join(__dirname,n),"utf8");
const side=read("side.js"),html=read("side.html"),inject=read("js/instagram_inject.js"),content=read("content_instagram.js"),manifest=JSON.parse(read("manifest.json"));
assert.strictEqual(manifest.version,"2.3.8.3130");
assert.strictEqual(manifest.version_name,"2.3.8.313");

// Side No/rank is a completed Side feature and must remain available.
for(const token of ["Side Rank badge / No 000","teCreateRankBadgeSettings","teRankBadgeSettings","teSaveRankBadgeSettings","teApplySideRankLabel","igblue-side-rank","igblue-rank-badge",'{ title: "순위"']) {
  assert.ok(side.includes(token)||html.includes(token),`Side rank feature missing: ${token}`);
}
assert.ok(side.includes('desc.textContent = "Side 카드에만 순위를 표시합니다. Main DOM에는 순위 UI를 만들지 않습니다."'));
const rankFnStart=side.indexOf('function teCreateRankBadgeSettings()');
const rankFnEnd=side.indexOf('function teCreateActiveDownloadSettings()',rankFnStart);
assert.ok(rankFnStart>=0&&rankFnEnd>rankFnStart,'Side rank settings function block missing');
const rankFn=side.slice(rankFnStart,rankFnEnd);
assert.ok(!rankFn.includes('mkCheck("Main"'),'Side rank settings must not expose Main');
assert.ok(!rankFn.includes('main:'),'Side rank settings must not save Main payload');

// Main rank visual UI remains removed.
for(const token of ["igblue-main-rank-row","igblue-main-kind-row","IGBLUE_NoNav_First","IGBLUE_NoNav_Prev","IGBLUE_NoNav_Current","IGBLUE_NoNav_Next","IGBLUE_NoNav_Last","igblue-nav-box","igblue-nav-row","data-igblue-rank-id","teMainAnchorId","teMainDownloadButtonForRankEl","teMainKindText","teMainRankTotal"]) {
  assert.ok(!inject.includes(token),`Main rank visual code survived: ${token}`);
}
assert.ok(!content.includes('querySelector("#IGBLUE_NoNav_'));
assert.ok(inject.includes('function teMainNoNavCurrent'));
assert.ok(inject.includes('function teMainNoNavNext'));
assert.ok(inject.includes('IGBLUE/internal_no_nav_state'));
assert.ok(inject.includes('document.querySelectorAll(".ndy_ins_info[data-code]")'));
assert.ok(content.includes('igblueReadInternalNoNavState'));
assert.ok(content.includes('internal_handler_found: true'));
assert.ok(side.includes('st.internal_handler_found && st.labels_ready'));
assert.ok(side.includes('build_internal_shortcode_snapshot_index'));
assert.ok(!side.includes('build_No000_shortcode_snapshot_index'));
assert.ok(inject.includes('schema:"ig_active_target_v266_internal_no"'));
assert.ok(inject.includes('internal_no:Number(e)||0'));
assert.ok(inject.includes('function teMainDownloadButtonForCardEl'));

// Side rank is never sent back to Main as a visual payload.
const payloadStart=side.indexOf('function teVisualPayload(');
const payloadEnd=side.indexOf('function teSendVisualSettingsToMain',payloadStart);
const payloadBlock=side.slice(payloadStart,payloadEnd);
assert.ok(payloadBlock&&!payloadBlock.includes('rank'),'Main visual payload must not contain rank settings');
console.log("OK test_ext_rank_visual_removal v272 Side-restored Main-removed");
