|
| 1 | +/** |
| 2 | + * Tests for the Finviz fundamentals parser. |
| 3 | + * |
| 4 | + * Uses a trimmed fixture that mirrors the real quote-page markup |
| 5 | + * (table.snapshot-table2 with .snapshot-td-label / .snapshot-td-content cells |
| 6 | + * and .is-positive / .is-negative color spans, plus .quote_profile-bio). |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import { parseFinvizSnapshot, getFinvizFundamentals } from './finviz'; |
| 11 | + |
| 12 | +const FIXTURE = ` |
| 13 | +<html><body> |
| 14 | +<table class="js-snapshot-table snapshot-table2 screener_snapshot-table-body"> |
| 15 | + <tr> |
| 16 | + <td class="snapshot-td2"><div class="snapshot-td-label">P/E</div></td> |
| 17 | + <td class="snapshot-td2"><div class="snapshot-td-content"><b>24.50</b></div></td> |
| 18 | + <td class="snapshot-td2"><div class="snapshot-td-label">Perf Year</div></td> |
| 19 | + <td class="snapshot-td2"><div class="snapshot-td-content"><b><span class="color-text is-positive">24.00%</span></b></div></td> |
| 20 | + </tr> |
| 21 | + <tr> |
| 22 | + <td class="snapshot-td2"><div class="snapshot-td-label">Perf Week</div></td> |
| 23 | + <td class="snapshot-td2"><div class="snapshot-td-content"><b><span class="color-text is-negative">-1.25%</span></b></div></td> |
| 24 | + <td class="snapshot-td2"><div class="snapshot-td-label">Beta</div></td> |
| 25 | + <td class="snapshot-td2"><div class="snapshot-td-content"><b>1.01</b></div></td> |
| 26 | + </tr> |
| 27 | + <tr> |
| 28 | + <td class="snapshot-td2"><div class="snapshot-td-label">Short Float</div></td> |
| 29 | + <td class="snapshot-td2"><div class="snapshot-td-content"><b>-</b></div></td> |
| 30 | + </tr> |
| 31 | +</table> |
| 32 | +<div class="quote_profile-bio">The fund seeks to provide investment results |
| 33 | + that correspond to the S&P 500 Index.</div> |
| 34 | +</body></html> |
| 35 | +`; |
| 36 | + |
| 37 | +describe('parseFinvizSnapshot', () => { |
| 38 | + it('extracts ordered label/value metrics with tone and description', () => { |
| 39 | + const result = parseFinvizSnapshot(FIXTURE, 'spy'); |
| 40 | + expect(result).not.toBeNull(); |
| 41 | + const f = result!; |
| 42 | + |
| 43 | + expect(f.symbol).toBe('SPY'); |
| 44 | + expect(f.source).toBe('finviz'); |
| 45 | + expect(typeof f.asOf).toBe('number'); |
| 46 | + |
| 47 | + // Order is preserved; the "-" placeholder row is dropped. |
| 48 | + expect(f.metrics).toEqual([ |
| 49 | + { label: 'P/E', value: '24.50', tone: null }, |
| 50 | + { label: 'Perf Year', value: '24.00%', tone: 'positive' }, |
| 51 | + { label: 'Perf Week', value: '-1.25%', tone: 'negative' }, |
| 52 | + { label: 'Beta', value: '1.01', tone: null }, |
| 53 | + ]); |
| 54 | + |
| 55 | + expect(f.description).toContain('S&P 500 Index'); |
| 56 | + // Whitespace is collapsed. |
| 57 | + expect(f.description).not.toMatch(/\s{2,}/); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns null when the snapshot table is missing', () => { |
| 61 | + expect(parseFinvizSnapshot('<html><body>no table</body></html>', 'SPY')).toBeNull(); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('getFinvizFundamentals', () => { |
| 66 | + it('sends a browser UA and parses the response', async () => { |
| 67 | + const fetchFn = vi.fn().mockResolvedValue({ |
| 68 | + ok: true, |
| 69 | + status: 200, |
| 70 | + text: async () => FIXTURE, |
| 71 | + }); |
| 72 | + |
| 73 | + const result = await getFinvizFundamentals('spy', { fetchFn }); |
| 74 | + expect(result?.symbol).toBe('SPY'); |
| 75 | + expect(result?.metrics.length).toBe(4); |
| 76 | + |
| 77 | + const [url, init] = fetchFn.mock.calls[0]; |
| 78 | + expect(url).toContain('t=SPY'); |
| 79 | + expect(init.headers['User-Agent']).toMatch(/Mozilla/); |
| 80 | + }); |
| 81 | + |
| 82 | + it('throws on a non-ok response (caller renders defensively)', async () => { |
| 83 | + const fetchFn = vi.fn().mockResolvedValue({ ok: false, status: 403, text: async () => '' }); |
| 84 | + await expect(getFinvizFundamentals('SPY', { fetchFn })).rejects.toThrow(/403/); |
| 85 | + }); |
| 86 | +}); |
0 commit comments