|
| 1 | +/** |
| 2 | + * Tests for dom-snapshot.ts: DOM snapshot engine. |
| 3 | + * |
| 4 | + * Since the engine generates JavaScript strings for in-page evaluation, |
| 5 | + * these tests validate: |
| 6 | + * 1. The generated code is syntactically valid JS |
| 7 | + * 2. Options are correctly embedded |
| 8 | + * 3. The output structure matches expected format |
| 9 | + * 4. All features are present (Shadow DOM, iframe, table, diff, etc.) |
| 10 | + */ |
| 11 | + |
| 12 | +import { describe, it, expect } from 'vitest'; |
| 13 | +import { generateSnapshotJs, scrollToRefJs, getFormStateJs } from './dom-snapshot.js'; |
| 14 | + |
| 15 | +describe('generateSnapshotJs', () => { |
| 16 | + it('returns a non-empty string', () => { |
| 17 | + const js = generateSnapshotJs(); |
| 18 | + expect(typeof js).toBe('string'); |
| 19 | + expect(js.length).toBeGreaterThan(100); |
| 20 | + }); |
| 21 | + |
| 22 | + it('generates syntactically valid JS (can be parsed)', () => { |
| 23 | + const js = generateSnapshotJs(); |
| 24 | + expect(() => new Function(js)).not.toThrow(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('embeds default options correctly', () => { |
| 28 | + const js = generateSnapshotJs(); |
| 29 | + expect(js).toContain('VIEWPORT_EXPAND = 800'); |
| 30 | + expect(js).toContain('MAX_DEPTH = 50'); |
| 31 | + expect(js).toContain('INTERACTIVE_ONLY = false'); |
| 32 | + expect(js).toContain('MAX_TEXT_LEN = 120'); |
| 33 | + expect(js).toContain('INCLUDE_SCROLL_INFO = true'); |
| 34 | + expect(js).toContain('BBOX_DEDUP = true'); |
| 35 | + expect(js).toContain('INCLUDE_SHADOW_DOM = true'); |
| 36 | + expect(js).toContain('INCLUDE_IFRAMES = true'); |
| 37 | + expect(js).toContain('PAINT_ORDER_CHECK = true'); |
| 38 | + expect(js).toContain('ANNOTATE_REFS = true'); |
| 39 | + expect(js).toContain('REPORT_HIDDEN = true'); |
| 40 | + expect(js).toContain('FILTER_ADS = true'); |
| 41 | + expect(js).toContain('MARKDOWN_TABLES = true'); |
| 42 | + expect(js).toContain('PREV_HASHES = null'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('embeds custom options correctly', () => { |
| 46 | + const js = generateSnapshotJs({ |
| 47 | + viewportExpand: 2000, |
| 48 | + maxDepth: 30, |
| 49 | + interactiveOnly: true, |
| 50 | + maxTextLength: 200, |
| 51 | + includeScrollInfo: false, |
| 52 | + bboxDedup: false, |
| 53 | + includeShadowDom: false, |
| 54 | + includeIframes: false, |
| 55 | + maxIframes: 3, |
| 56 | + paintOrderCheck: false, |
| 57 | + annotateRefs: false, |
| 58 | + reportHidden: false, |
| 59 | + filterAds: false, |
| 60 | + markdownTables: false, |
| 61 | + }); |
| 62 | + expect(js).toContain('VIEWPORT_EXPAND = 2000'); |
| 63 | + expect(js).toContain('MAX_DEPTH = 30'); |
| 64 | + expect(js).toContain('INTERACTIVE_ONLY = true'); |
| 65 | + expect(js).toContain('MAX_TEXT_LEN = 200'); |
| 66 | + expect(js).toContain('INCLUDE_SCROLL_INFO = false'); |
| 67 | + expect(js).toContain('BBOX_DEDUP = false'); |
| 68 | + expect(js).toContain('INCLUDE_SHADOW_DOM = false'); |
| 69 | + expect(js).toContain('INCLUDE_IFRAMES = false'); |
| 70 | + expect(js).toContain('MAX_IFRAMES = 3'); |
| 71 | + expect(js).toContain('PAINT_ORDER_CHECK = false'); |
| 72 | + expect(js).toContain('ANNOTATE_REFS = false'); |
| 73 | + expect(js).toContain('REPORT_HIDDEN = false'); |
| 74 | + expect(js).toContain('FILTER_ADS = false'); |
| 75 | + expect(js).toContain('MARKDOWN_TABLES = false'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('clamps maxDepth between 1 and 200', () => { |
| 79 | + expect(generateSnapshotJs({ maxDepth: -5 })).toContain('MAX_DEPTH = 1'); |
| 80 | + expect(generateSnapshotJs({ maxDepth: 999 })).toContain('MAX_DEPTH = 200'); |
| 81 | + expect(generateSnapshotJs({ maxDepth: 75 })).toContain('MAX_DEPTH = 75'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('wraps output as an IIFE', () => { |
| 85 | + const js = generateSnapshotJs(); |
| 86 | + expect(js.startsWith('(() =>')).toBe(true); |
| 87 | + expect(js.trimEnd().endsWith(')()')).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it('embeds previousHashes for incremental diff', () => { |
| 91 | + const hashes = JSON.stringify(['12345', '67890']); |
| 92 | + const js = generateSnapshotJs({ previousHashes: hashes }); |
| 93 | + expect(js).toContain('new Set(["12345","67890"])'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('includes all core features in generated code', () => { |
| 97 | + const js = generateSnapshotJs(); |
| 98 | + |
| 99 | + // Tag filtering |
| 100 | + expect(js).toContain('SKIP_TAGS'); |
| 101 | + expect(js).toContain("'script'"); |
| 102 | + expect(js).toContain("'style'"); |
| 103 | + |
| 104 | + // SVG collapsing |
| 105 | + expect(js).toContain('SVG_CHILDREN'); |
| 106 | + |
| 107 | + // Interactive detection |
| 108 | + expect(js).toContain('INTERACTIVE_TAGS'); |
| 109 | + expect(js).toContain('INTERACTIVE_ROLES'); |
| 110 | + expect(js).toContain('isInteractive'); |
| 111 | + |
| 112 | + // Visibility |
| 113 | + expect(js).toContain('isVisibleByCSS'); |
| 114 | + expect(js).toContain('isInExpandedViewport'); |
| 115 | + |
| 116 | + // BBox dedup |
| 117 | + expect(js).toContain('isContainedBy'); |
| 118 | + expect(js).toContain('PROPAGATING_TAGS'); |
| 119 | + |
| 120 | + // Shadow DOM |
| 121 | + expect(js).toContain('shadowRoot'); |
| 122 | + expect(js).toContain('|shadow|'); |
| 123 | + |
| 124 | + // iframe |
| 125 | + expect(js).toContain('walkIframe'); |
| 126 | + expect(js).toContain('|iframe|'); |
| 127 | + |
| 128 | + // Paint order |
| 129 | + expect(js).toContain('isOccludedByOverlay'); |
| 130 | + expect(js).toContain('elementFromPoint'); |
| 131 | + |
| 132 | + // Ad filtering |
| 133 | + expect(js).toContain('isAdElement'); |
| 134 | + expect(js).toContain('AD_PATTERNS'); |
| 135 | + |
| 136 | + // data-ref annotation |
| 137 | + expect(js).toContain('data-opencli-ref'); |
| 138 | + |
| 139 | + // Hidden elements report |
| 140 | + expect(js).toContain('hiddenInteractives'); |
| 141 | + expect(js).toContain('hidden_interactive'); |
| 142 | + |
| 143 | + // Incremental diff |
| 144 | + expect(js).toContain('hashElement'); |
| 145 | + expect(js).toContain('currentHashes'); |
| 146 | + expect(js).toContain('__opencli_prev_hashes'); |
| 147 | + |
| 148 | + // Table serialization |
| 149 | + expect(js).toContain('serializeTable'); |
| 150 | + expect(js).toContain('|table|'); |
| 151 | + |
| 152 | + // Synthetic attributes |
| 153 | + expect(js).toContain("'YYYY-MM-DD'"); |
| 154 | + expect(js).toContain('value=••••'); |
| 155 | + |
| 156 | + // Page metadata |
| 157 | + expect(js).toContain('location.href'); |
| 158 | + expect(js).toContain('document.title'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('contains proper attribute whitelist', () => { |
| 162 | + const js = generateSnapshotJs(); |
| 163 | + const expectedAttrs = [ |
| 164 | + 'aria-label', 'aria-expanded', 'aria-checked', 'aria-selected', |
| 165 | + 'placeholder', 'href', 'role', 'data-testid', 'autocomplete', |
| 166 | + ]; |
| 167 | + for (const attr of expectedAttrs) { |
| 168 | + expect(js).toContain(`'${attr}'`); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + it('includes scroll info formatting', () => { |
| 173 | + const js = generateSnapshotJs(); |
| 174 | + expect(js).toContain('scrollHeight'); |
| 175 | + expect(js).toContain('scrollTop'); |
| 176 | + expect(js).toContain('|scroll|'); |
| 177 | + expect(js).toContain('page_scroll'); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +describe('scrollToRefJs', () => { |
| 182 | + it('generates valid JS', () => { |
| 183 | + const js = scrollToRefJs('42'); |
| 184 | + expect(() => new Function(js)).not.toThrow(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('targets data-opencli-ref', () => { |
| 188 | + const js = scrollToRefJs('7'); |
| 189 | + expect(js).toContain('data-opencli-ref'); |
| 190 | + expect(js).toContain('scrollIntoView'); |
| 191 | + expect(js).toContain('"7"'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('falls back to data-ref', () => { |
| 195 | + const js = scrollToRefJs('3'); |
| 196 | + expect(js).toContain('data-ref'); |
| 197 | + }); |
| 198 | + |
| 199 | + it('returns scrolled info', () => { |
| 200 | + const js = scrollToRefJs('1'); |
| 201 | + expect(js).toContain('scrolled: true'); |
| 202 | + expect(js).toContain('tag:'); |
| 203 | + }); |
| 204 | +}); |
| 205 | + |
| 206 | +describe('getFormStateJs', () => { |
| 207 | + it('generates valid JS', () => { |
| 208 | + const js = getFormStateJs(); |
| 209 | + expect(() => new Function(js)).not.toThrow(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('collects form elements', () => { |
| 213 | + const js = getFormStateJs(); |
| 214 | + expect(js).toContain('document.forms'); |
| 215 | + expect(js).toContain('form.elements'); |
| 216 | + }); |
| 217 | + |
| 218 | + it('collects orphan fields', () => { |
| 219 | + const js = getFormStateJs(); |
| 220 | + expect(js).toContain('orphanFields'); |
| 221 | + expect(js).toContain('el.form'); |
| 222 | + }); |
| 223 | + |
| 224 | + it('handles different input types', () => { |
| 225 | + const js = getFormStateJs(); |
| 226 | + expect(js).toContain('checkbox'); |
| 227 | + expect(js).toContain('radio'); |
| 228 | + expect(js).toContain('password'); |
| 229 | + expect(js).toContain('contenteditable'); |
| 230 | + }); |
| 231 | + |
| 232 | + it('extracts labels', () => { |
| 233 | + const js = getFormStateJs(); |
| 234 | + expect(js).toContain('aria-label'); |
| 235 | + expect(js).toContain('label[for='); |
| 236 | + expect(js).toContain('closest'); |
| 237 | + expect(js).toContain('placeholder'); |
| 238 | + }); |
| 239 | + |
| 240 | + it('masks passwords', () => { |
| 241 | + const js = getFormStateJs(); |
| 242 | + expect(js).toContain('••••'); |
| 243 | + }); |
| 244 | + |
| 245 | + it('includes data-opencli-ref in output', () => { |
| 246 | + const js = getFormStateJs(); |
| 247 | + expect(js).toContain('data-opencli-ref'); |
| 248 | + }); |
| 249 | +}); |
0 commit comments