const fs = require('fs');
const path = require('path');
const assert = require('assert');

const root = __dirname;
const read = (name) => fs.readFileSync(path.join(root, name), 'utf8');
const manifest = JSON.parse(read('manifest.json'));
const bg = read('background.js');
const side = read('side.js');

assert.strictEqual(manifest.version, '2.3.8.252');
assert.strictEqual(manifest.version_name, '2.3.8.252');

// Manual mode: read only the single current active tab. Never search/fallback among Instagram tabs.
assert.ok(bg.includes('function igblueReadCurrentActiveState(callback)'));
assert.ok(bg.includes('chrome.tabs.query({active:true, lastFocusedWindow:true}'));
assert.ok(!bg.includes('function igblueQueryBestInstagramTab'));
assert.ok(!bg.includes('chrome.tabs.query({url:['));
assert.ok(!bg.includes('igblueGetConnectedState'));
assert.ok(!bg.includes('igblueGetBestState'));
assert.ok(bg.includes('if(!tab || !tab.id || !igblueIsInstagramUrl(tab.url || ""))'));
assert.ok(bg.includes('igblueActiveInstagramTabId = 0;'));

// Auto start: fix exactly the current active state tab; no remembered-tab fallback.
assert.ok(side.includes('const tabId = Number(state && state.tabId || 0) || 0;'));
assert.ok(side.includes('const url = String(state && state.url || "");'));
assert.ok(!side.includes('Number(teLastConnectedTargetTabId || state && state.tabId'));
assert.ok(!side.includes('(tabId === teLastConnectedTargetTabId && teLastConnectedTargetUrl)'));

// Auto run: all target commands continue to use the fixed id until release.
assert.ok(side.includes('if (teAutoRunSessionActive) {'));
assert.ok(side.includes('const fixedId = Number(teAutoRunTabId || 0) || 0;'));
assert.ok(side.includes('chrome.tabs.get(fixedId'));
assert.ok(side.includes('teAutoRunSessionActive = true;'));
assert.ok(side.includes('teAutoRunTabId = tabId;'));
assert.ok(side.includes('teAutoRunSessionActive = false;'));
assert.ok(side.includes('teAutoRunTabId = 0;'));
assert.ok(side.includes('teRequestTargetState(teIsPopupMode())'));

console.log('OK test_ext_active_tab v252');
