// FILE: _St/IG_StYellow/test_ext_explore_clips_scroll_v312.js | ROLE: Explorer swipe/scroll 후 clips medias 응답 수집 회귀
const fs = require('fs');
const path = require('path');
const assert = require('assert');

const src = fs.readFileSync(path.join(__dirname, 'js', 'instagram_inject.js'), 'utf8');
const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, 'manifest.json'), 'utf8'));
assert.strictEqual(manifest.version, '2.3.8.3140');
assert.strictEqual(manifest.version_name, '2.3.8.314');

const start = src.indexOf('function z(e){');
const end = src.indexOf('function $(', start);
assert.ok(start >= 0 && end > start, 'Explorer parser z() must exist');
const zSource = src.slice(start, end);
assert.ok(zSource.includes('layout_content?.fill_items'), 'existing fill_items path preserved');
assert.ok(zSource.includes('layout_content?.medias'), '2.4.1 medias path added');
assert.ok(zSource.includes('e?.clips?.items'), '2.4.1 nested clips.items path added');
assert.ok(zSource.includes('one_by_two_item'), 'existing one_by_two_item path preserved');

const collected = [];
const g = (m) => { if (m && m.code) collected.push(m.code); };
const z = new Function('g', `${zSource}; return z;`)(g);

z({sectional_items:[
  {feed_type:'media', layout_content:{medias:[{media:{code:'MEDIA'}}]}},
  {feed_type:'clips', layout_content:{
    fill_items:[{media:{code:'FILL'}}],
    medias:[
      {media:{code:'DIRECT'}},
      {clips:{items:[{media:{code:'NESTED1'}},{media:{code:'NESTED2'}}]}}
    ],
    one_by_two_item:{clips:{items:[{media:{code:'ONE2'}}]}}
  }},
  {feed_type:'other', layout_content:{fill_items:[{media:{code:'OTHER'}}]}}
]});

for (const code of ['MEDIA','FILL','DIRECT','NESTED1','NESTED2','ONE2','OTHER']) {
  assert.ok(collected.includes(code), `must collect ${code}`);
}
assert.doesNotThrow(() => z({sectional_items:[{feed_type:'clips',layout_content:{medias:[{}, {clips:{}}]}}]}));
assert.doesNotThrow(() => z({sectional_items:[]}));
console.log('test_ext_explore_clips_scroll_v312: OK');
