Spaces:
Running
Running
File size: 12,086 Bytes
d7694d8 a743902 d7694d8 a743902 d7694d8 a743902 d7694d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | 'use strict';
/**
* Unit tests for tickerValidation.js and the TickerInput component behaviour.
* Run with: node --test tests/test_ticker_validation.js
* Requires: npm install (installs jsdom for DOM tests)
*/
const { test, describe } = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const { JSDOM } = require('jsdom');
// Load the validation module (UMD β sets module.exports in Node.js)
const { validateTickerFormat, validateTickerRemote } = require(
path.join(__dirname, '..', 'static', 'tickerValidation.js')
);
// ---------------------------------------------------------------------------
// Pure format-validation tests (no DOM, no network)
// ---------------------------------------------------------------------------
describe('validateTickerFormat', () => {
test('test_empty_input_shows_error β empty string returns error', () => {
const result = validateTickerFormat('');
assert.equal(result.valid, false);
assert.match(result.error, /Please enter/i);
});
test('test_empty_input_shows_error β whitespace-only returns error', () => {
const result = validateTickerFormat(' ');
assert.equal(result.valid, false);
assert.match(result.error, /Please enter/i);
});
test('test_numbers_rejected_instantly β digits in input', () => {
for (const bad of ['123', '1A', 'A1B', '9XYZ']) {
const result = validateTickerFormat(bad);
assert.equal(result.valid, false,
`Expected "${bad}" to be rejected`);
assert.match(result.error, /invalid ticker format/i);
}
});
test('test_too_long_ticker_rejected β 6+ letters rejected', () => {
const result = validateTickerFormat('ABCDEF');
assert.equal(result.valid, false);
assert.match(result.error, /invalid ticker format/i);
});
test('reserved words rejected', () => {
for (const word of ['TEST', 'NULL', 'NONE', 'HELP', 'NA']) {
const result = validateTickerFormat(word);
assert.equal(result.valid, false,
`Expected "${word}" to be rejected as reserved`);
assert.match(result.error, /not a stock ticker/i);
}
});
test('test_valid_format_triggers_remote_check β valid formats pass', () => {
for (const good of ['AAPL', 'msft', ' TSLA ', 'A', 'GOOGL']) {
const result = validateTickerFormat(good);
assert.equal(result.valid, true,
`Expected "${good}" to pass format check`);
assert.equal(result.cleaned, good.trim().toUpperCase());
}
});
test('index symbols pass format check', () => {
for (const sym of ['^GSPC', '^DJI', '^IXIC', '^RUT', '^VIX']) {
const result = validateTickerFormat(sym);
assert.equal(result.valid, true, `Expected "${sym}" to pass format check`);
}
});
test('futures symbols pass format check', () => {
for (const sym of ['CL=F', 'GC=F', 'SI=F', 'HG=F', 'NG=F']) {
const result = validateTickerFormat(sym);
assert.equal(result.valid, true, `Expected "${sym}" to pass format check`);
}
});
test('composite symbols pass format check', () => {
const result = validateTickerFormat('DX-Y.NYB');
assert.equal(result.valid, true, 'Expected "DX-Y.NYB" to pass format check');
});
test('invalid special-looking inputs still rejected', () => {
for (const bad of ['^', '=F', 'CL=X', '^^DJI', 'TOOLONGBASE=F']) {
const result = validateTickerFormat(bad);
assert.equal(result.valid, false, `Expected "${bad}" to be rejected`);
}
});
});
// ---------------------------------------------------------------------------
// DOM behaviour tests (requires jsdom)
// ---------------------------------------------------------------------------
/** Build a minimal DOM that mirrors the search section in index.html */
function buildDOM() {
const dom = new JSDOM(`<!DOCTYPE html>
<html><body>
<form id="analyze-form">
<div class="search-box">
<div class="input-wrapper" id="ticker-input-wrapper">
<input type="text" id="ticker-input" />
<button type="button" id="ticker-clear" class="ticker-clear hidden">Γ</button>
<span id="ticker-val-indicator" class="ticker-val-indicator hidden"></span>
</div>
<button type="submit" id="analyze-btn" disabled>
<span class="btn-text">Analyze Risk</span>
</button>
</div>
</form>
<div id="validation-hint" class="hidden">
<span id="validation-msg"></span>
<div id="suggestion-chips"></div>
</div>
<div id="error-message" class="error-msg hidden"></div>
</body></html>`, { runScripts: 'dangerously', pretendToBeVisual: true });
const { window } = dom;
// Expose TickerValidation in the fake window before loading app logic
window.TickerValidation = { validateTickerFormat, validateTickerRemote };
return { window, document: window.document };
}
/**
* Wire up a minimal version of the validation logic from app.js so we can
* test component behaviour without importing the full 1 000-line app.js
* (which has heavyweight dependencies like LightweightCharts).
*/
function wireValidation(window) {
const { document } = window;
const input = document.getElementById('ticker-input');
const analyzeBtn = document.getElementById('analyze-btn');
const clearBtn = document.getElementById('ticker-clear');
const validationHint = document.getElementById('validation-hint');
const validationMsgEl = document.getElementById('validation-msg');
const suggestionChips = document.getElementById('suggestion-chips');
let _validatedTicker = null;
let _debounceTimer = null;
let _abortController = null;
function getValidatedTicker() { return _validatedTicker; }
function _setInputState(state) {
input.className = state ? `ticker-input--${state}` : '';
}
function _showHint(text, type) {
validationMsgEl.textContent = text;
validationMsgEl.className = `validation-msg validation-msg--${type}`;
validationHint.classList.remove('hidden');
}
function _clearHint() {
validationHint.classList.add('hidden');
validationMsgEl.textContent = '';
suggestionChips.innerHTML = '';
}
function _renderSuggestions(suggestions) {
suggestionChips.innerHTML = '';
if (!Array.isArray(suggestions)) return;
suggestions.forEach((s) => {
const chip = document.createElement('button');
chip.type = 'button';
chip.className = 'suggestion-chip';
chip.textContent = s;
chip.addEventListener('click', () => {
input.value = s;
triggerValidation(s);
});
suggestionChips.appendChild(chip);
});
}
async function triggerValidation(rawValue) {
const val = String(rawValue || '').trim().toUpperCase();
if (_abortController) _abortController.abort();
_abortController = new window.AbortController();
const fmt = window.TickerValidation.validateTickerFormat(val);
if (!fmt.valid) {
_validatedTicker = null;
analyzeBtn.disabled = true;
_setInputState('error');
_showHint(fmt.error, 'error');
_renderSuggestions([]);
return;
}
_validatedTicker = null;
analyzeBtn.disabled = true;
_setInputState('validating');
_clearHint();
const { signal } = _abortController;
const result = await window.TickerValidation.validateTickerRemote(val, signal);
if (!result) return;
if (result.valid) {
_validatedTicker = val;
analyzeBtn.disabled = false;
_setInputState('valid');
_showHint(`β ${result.company_name || val}`, 'success');
_renderSuggestions([]);
} else {
_setInputState('error');
_showHint(result.error || 'Not found.', 'error');
_renderSuggestions(result.suggestions || []);
}
}
input.addEventListener('input', () => {
input.value = input.value.toUpperCase();
clearBtn.classList.toggle('hidden', !input.value);
const val = input.value.trim();
clearTimeout(_debounceTimer);
const fmt = window.TickerValidation.validateTickerFormat(val);
if (!fmt.valid) {
if (_abortController) _abortController.abort();
_validatedTicker = null;
analyzeBtn.disabled = true;
_setInputState(val ? 'error' : '');
if (val) _showHint(fmt.error, 'error');
else _clearHint();
_renderSuggestions([]);
return;
}
_validatedTicker = null;
analyzeBtn.disabled = true;
_setInputState('validating');
_clearHint();
_debounceTimer = setTimeout(() => triggerValidation(val), 500);
});
clearBtn.addEventListener('click', () => {
input.value = '';
_validatedTicker = null;
analyzeBtn.disabled = true;
clearTimeout(_debounceTimer);
if (_abortController) _abortController.abort();
_setInputState('');
_clearHint();
clearBtn.classList.add('hidden');
});
return { input, analyzeBtn, clearBtn, validationHint, validationMsgEl,
suggestionChips, triggerValidation, getValidatedTicker };
}
// Helper: fire input event
function fireInput(input, value) {
input.value = value;
input.dispatchEvent(new input.ownerDocument.defaultView.Event('input', { bubbles: true }));
}
describe('TickerInput DOM behaviour', () => {
test('test_submit_disabled_until_valid β button starts disabled', () => {
const { document } = buildDOM();
const btn = document.getElementById('analyze-btn');
assert.equal(btn.disabled, true);
});
test('test_suggestion_click_fills_input β clicking chip fills input', async () => {
const { window } = buildDOM();
// Mock validateTickerRemote to return suggestions for a bad ticker
window.TickerValidation.validateTickerRemote = async () => ({
valid: false,
error: 'Not found.',
suggestions: ['AAPL', 'APD'],
});
const { input, suggestionChips, triggerValidation } = wireValidation(window);
await triggerValidation('AAPL1'); // invalid format handled locally
// Use a format-valid but server-rejected ticker to get suggestions
// Bypass format check by directly calling with a valid-format ticker
// Override validateTickerFormat temporarily
const original = window.TickerValidation.validateTickerFormat;
window.TickerValidation.validateTickerFormat = () => ({ valid: true, cleaned: 'AAPX' });
await triggerValidation('AAPX');
window.TickerValidation.validateTickerFormat = original;
const chips = suggestionChips.querySelectorAll('.suggestion-chip');
assert.ok(chips.length > 0, 'Expected suggestion chips to be rendered');
// Click the first chip β it should fill the input
chips[0].click();
assert.equal(input.value, chips[0].textContent);
});
test('test_submit_disabled_until_valid β button enabled after valid remote result', async () => {
const { window } = buildDOM();
window.TickerValidation.validateTickerRemote = async () => ({
valid: true,
ticker: 'AAPL',
company_name: 'Apple Inc.',
});
const { analyzeBtn, triggerValidation } = wireValidation(window);
assert.equal(analyzeBtn.disabled, true, 'Should start disabled');
await triggerValidation('AAPL');
assert.equal(analyzeBtn.disabled, false, 'Should be enabled after valid result');
});
});
|