// FILE: _St/IG_StYellow/test_ext_navigation_unload_guard_v279cc.js | ROLE: 이전 탭 종료 직전 unload 전용 guard 회귀 검사
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');

const root = __dirname;
const background = fs.readFileSync(path.join(root, 'background.js'), 'utf8');
const inject = fs.readFileSync(path.join(root, 'js', 'instagram_inject.js'), 'utf8');

for (const token of [
  'function igblueUnloadGuardMainWorld(context)',
  '__YELLOW_UNLOAD_GUARD_V279D_CLOSE__',
  'CALL_BEFORE_TAB_CLOSE',
  'String(type || "").toLowerCase() === "unload"',
  'return original.call(this, type, listener, options);',
  'Object.defineProperty(window, "onunload"',
  'function igblueInstallUnloadGuard(tabId, context, callback)',
  'function igblueCloseManagedAutoTab(tabId, replacementTabId, reason, callback)',
  'function igblueRequestCdpTargetClose(targetId, expectedUrl, reason, callback)',
  'meta.auto_created !== true',
  'YELLOW_CDP_CLOSE_TARGET_URL',
]) assert.ok(background.includes(token), `missing close-guard token: ${token}`);
assert.ok(!background.includes('String(type || "").toLowerCase() === "beforeunload"'));
assert.ok(!inject.includes('yellowDocumentStartGuardV279ccc'));
assert.ok(!inject.includes('yellowDocumentStartGuardV279d'));
assert.ok(inject.includes('data-yellow-main-injection-ready","v288"'));

const start = background.indexOf('function igblueUnloadGuardMainWorld(context)');
const end = background.indexOf('\n\n  function igblueInstallUnloadGuard', start);
assert.ok(start >= 0 && end > start, 'guard helper block missing');
const code = background.slice(start, end) + '\nthis.guard = igblueUnloadGuardMainWorld;';
const calls = [];
const logs = [];
function EventTargetMock() {}
EventTargetMock.prototype.addEventListener = function(type, listener, options) {
  calls.push({target:this, type, listener, options});
  return 'delegated';
};
const windowObject = {};
const context = {
  window: windowObject,
  location: {href: 'https://www.instagram.com/source_account/'},
  EventTarget: EventTargetMock,
  Object,
  String,
  Number,
  Date,
  Error,
  console: {log(...args) { logs.push(args); }},
};
vm.runInNewContext(code, context, {filename:'unload_guard_close_v279d.js'});
const result = context.guard({operation:'diagnostic_only', target_url:'https://www.instagram.com/next_account/', reason:'legacy_guard_unit'});
assert.strictEqual(result.ok, true);
assert.ok(logs.some((row) => String(row[0]).includes('CALL_BEFORE_URL')));
assert.ok(logs.some((row) => String(row[0]).includes('INSTALL_OK')));
const target = new EventTargetMock();
assert.strictEqual(target.addEventListener('unload', () => {}, false), undefined);
assert.strictEqual(calls.length, 0);
assert.ok(logs.some((row) => String(row[0]).includes('BLOCKED_ADD')));
assert.strictEqual(target.addEventListener('click', () => {}, true), 'delegated');
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].type, 'click');
windowObject.onunload = () => {};
assert.strictEqual(windowObject.onunload, null);

console.log('OK test_ext_navigation_unload_guard_v279cc');
