const { test } = require('node:test') const assert = require('node:assert/strict') const vm = require('node:vm') const cheerio = require('cheerio') const { inspectLoginState, classifyLoginState, authenticatedDestination, clearLoginInput, typeVisibleInput, submitInputForm } = require('../bloomberg-login') // Synthetic DOM fixtures only; no browser navigation, input capture or publisher HTML. function fixture (html) { const $ = cheerio.load(html); const clicked = []; const values = new Map() const wrap = node => ({ hidden: $(node).is('[hidden]'), disabled: $(node).is('[disabled]'), getAttribute: name => $(node).attr(name) ?? null, getClientRects: () => $(node).parents().addBack().is('[hidden], [aria-hidden="true"], [style="display:none"]') ? [] : [{}], get form () { const form = $(node).closest('form')[0];return form ? wrap(form) : null }, querySelectorAll: selector => $(node).find(selector).toArray().map(wrap), click () { clicked.push($(node).attr('id')) }, get value () { return values.has(node) ? values.get(node) : $(node).attr('value') }, set value (value) { values.set(node,value) }, dispatchEvent () {} }) const document = {querySelectorAll: selector => $(selector).toArray().map(wrap), querySelector: selector => {const n=$(selector)[0];return n ? wrap(n) : null}} const run = (fn,arg) => vm.runInNewContext(`(${fn.toString()})(${arg === undefined ? '' : JSON.stringify(arg)})`, {document, Event:class {}}) return {document,run,clicked} } test('only explicit visible session evidence without login form confirms authentication', () => { for (const [html,want] of [ ['
Public page
','authentication_unconfirmed'], ['Account control','authenticated'], ['','authentication_unconfirmed'], ['Account control','authentication_unconfirmed'], ['Account control','authenticated'], ['
','challenge'], ['','two_factor_required'], ['
','auth_rejected'] ]) {const f=fixture(html);assert.equal(classifyLoginState(f.run(inspectLoginState)),want)} assert.equal(authenticatedDestination('https://www.bloomberg.com/account/signin'),false) assert.equal(authenticatedDestination('https://other.invalid/'),false) assert.equal(authenticatedDestination('https://www.bloomberg.com/markets'),true) }) test('submission targets the entered field form rather than another page form', () => { const f=fixture('
') assert.equal(f.run(submitInputForm,'#entered'),true) assert.deepEqual(f.clicked,['correct']) assert.equal(f.run(submitInputForm,'#missing'),false) }) test('visible input is cleared before typing and hidden fields cannot receive credentials', async () => { const f=fixture('
') assert.equal(f.run(clearLoginInput,'#hidden'),false) const typed=[] const page={async waitForSelector(selector,options){assert.equal(options.visible,true)},async evaluate(fn,selector){return f.run(fn,selector)},async type(selector){assert.equal(f.document.querySelector(selector).value,'');typed.push(selector)}} assert.equal(await typeVisibleInput(page,['#hidden','#entered'],'synthetic-test-input'),'#entered') assert.deepEqual(typed,['#entered']) })