|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const themeModulePath = '../../attack-theme/static/scripts/theme.js'; |
| 5 | + |
| 6 | +describe('site theme', () => { |
| 7 | + let theme; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + jest.resetModules(); |
| 11 | + theme = require(themeModulePath); |
| 12 | + }); |
| 13 | + |
| 14 | + test('uses System when no saved override exists', () => { |
| 15 | + const storage = createStorage(); |
| 16 | + const root = createRoot(); |
| 17 | + |
| 18 | + expect(theme.readStoredPreference(storage)).toBe('system'); |
| 19 | + |
| 20 | + theme.applyPreference(root, 'system'); |
| 21 | + |
| 22 | + expect(root.removeAttribute).toHaveBeenCalledWith('data-theme'); |
| 23 | + expect(root.style.setProperty).not.toHaveBeenCalled(); |
| 24 | + }); |
| 25 | + |
| 26 | + test.each(['light', 'dark'])('applies a saved %s override before controls initialize', preference => { |
| 27 | + const storage = createStorage(preference); |
| 28 | + const root = createRoot(); |
| 29 | + |
| 30 | + const storedPreference = theme.readStoredPreference(storage); |
| 31 | + theme.applyPreference(root, storedPreference); |
| 32 | + |
| 33 | + expect(storedPreference).toBe(preference); |
| 34 | + expect(root.setAttribute).toHaveBeenCalledWith('data-theme', preference); |
| 35 | + // CSS owns color-scheme so the print stylesheet can force light controls. |
| 36 | + expect(root.style.setProperty).not.toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + test('discards an invalid saved preference', () => { |
| 40 | + const storage = createStorage('sepia'); |
| 41 | + |
| 42 | + expect(theme.readStoredPreference(storage)).toBe('system'); |
| 43 | + expect(storage.removeItem).toHaveBeenCalledWith(theme.STORAGE_KEY); |
| 44 | + }); |
| 45 | + |
| 46 | + test('adds a working archive switch to the existing banner without replacing its content', () => { |
| 47 | + const fixture = createControllerFixture({ storedPreference: 'dark' }); |
| 48 | + const banner = { appendChild: jest.fn(), textContent: 'Currently viewing ATT&CK v3.0' }; |
| 49 | + fixture.document.querySelector = jest.fn(selector => ( |
| 50 | + selector === '.version-banner' ? banner : null |
| 51 | + )); |
| 52 | + fixture.document.createElement = jest.fn(() => fixture.toggle); |
| 53 | + fixture.document.readyState = 'complete'; |
| 54 | + |
| 55 | + theme.bootstrap({ ...fixture, archived: true }); |
| 56 | + |
| 57 | + expect(fixture.document.documentElement.setAttribute).toHaveBeenCalledWith('data-archive-theme', ''); |
| 58 | + expect(banner.appendChild).toHaveBeenCalledWith(fixture.toggle); |
| 59 | + expect(banner.textContent).toBe('Currently viewing ATT&CK v3.0'); |
| 60 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-label', 'Dark mode'); |
| 61 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-checked', 'true'); |
| 62 | + fixture.toggle.click(); |
| 63 | + expect(fixture.storage.setItem).toHaveBeenCalledWith(theme.STORAGE_KEY, 'light'); |
| 64 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-checked', 'false'); |
| 65 | + }); |
| 66 | + |
| 67 | + test('toggles from the system light theme to a saved dark override', () => { |
| 68 | + const fixture = createControllerFixture({ systemDark: false }); |
| 69 | + const controller = theme.createThemeController(fixture); |
| 70 | + |
| 71 | + controller.init(); |
| 72 | + fixture.toggle.click(); |
| 73 | + |
| 74 | + expect(fixture.document.documentElement.setAttribute).toHaveBeenLastCalledWith('data-theme', 'dark'); |
| 75 | + expect(fixture.storage.setItem).toHaveBeenCalledWith(theme.STORAGE_KEY, 'dark'); |
| 76 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-label', 'Switch to light mode'); |
| 77 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-checked', 'true'); |
| 78 | + }); |
| 79 | + |
| 80 | + test('toggles from the system dark theme to a saved light override', () => { |
| 81 | + const fixture = createControllerFixture({ systemDark: true }); |
| 82 | + const controller = theme.createThemeController(fixture); |
| 83 | + |
| 84 | + controller.init(); |
| 85 | + fixture.toggle.click(); |
| 86 | + |
| 87 | + expect(fixture.document.documentElement.setAttribute).toHaveBeenLastCalledWith('data-theme', 'light'); |
| 88 | + expect(fixture.storage.setItem).toHaveBeenCalledWith(theme.STORAGE_KEY, 'light'); |
| 89 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-label', 'Switch to dark mode'); |
| 90 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-checked', 'false'); |
| 91 | + }); |
| 92 | + |
| 93 | + test.each([ |
| 94 | + ['light', 'dark'], |
| 95 | + ['dark', 'light'], |
| 96 | + ])('toggles a saved %s override to %s', (storedPreference, expectedPreference) => { |
| 97 | + const fixture = createControllerFixture({ storedPreference }); |
| 98 | + const controller = theme.createThemeController(fixture); |
| 99 | + |
| 100 | + controller.init(); |
| 101 | + fixture.toggle.click(); |
| 102 | + |
| 103 | + expect(fixture.document.documentElement.setAttribute).toHaveBeenLastCalledWith( |
| 104 | + 'data-theme', |
| 105 | + expectedPreference, |
| 106 | + ); |
| 107 | + expect(fixture.storage.setItem).toHaveBeenCalledWith(theme.STORAGE_KEY, expectedPreference); |
| 108 | + }); |
| 109 | + |
| 110 | + test('continues applying a choice when storage access fails', () => { |
| 111 | + const fixture = createControllerFixture(); |
| 112 | + fixture.storage.setItem.mockImplementation(() => { |
| 113 | + throw new Error('Storage disabled'); |
| 114 | + }); |
| 115 | + const controller = theme.createThemeController(fixture); |
| 116 | + |
| 117 | + expect(() => { |
| 118 | + controller.init(); |
| 119 | + fixture.toggle.click(); |
| 120 | + }).not.toThrow(); |
| 121 | + expect(fixture.document.documentElement.setAttribute).toHaveBeenLastCalledWith('data-theme', 'dark'); |
| 122 | + }); |
| 123 | + |
| 124 | + test('updates the toggle when the system preference changes before an override', () => { |
| 125 | + const fixture = createControllerFixture({ systemDark: false }); |
| 126 | + const controller = theme.createThemeController(fixture); |
| 127 | + |
| 128 | + controller.init(); |
| 129 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-label', 'Switch to dark mode'); |
| 130 | + |
| 131 | + fixture.mediaQuery.matches = true; |
| 132 | + fixture.mediaQuery.dispatchChange(); |
| 133 | + |
| 134 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-label', 'Switch to light mode'); |
| 135 | + }); |
| 136 | + |
| 137 | + test('ignores OS preference changes while an explicit override is active', () => { |
| 138 | + const fixture = createControllerFixture({ storedPreference: 'light', systemDark: false }); |
| 139 | + const controller = theme.createThemeController(fixture); |
| 140 | + |
| 141 | + controller.init(); |
| 142 | + fixture.mediaQuery.matches = true; |
| 143 | + fixture.mediaQuery.dispatchChange(); |
| 144 | + |
| 145 | + expect(fixture.toggle.setAttribute).toHaveBeenCalledWith('aria-label', 'Switch to dark mode'); |
| 146 | + }); |
| 147 | + |
| 148 | + test('loads the early theme script before styles and renders one toggle before search', () => { |
| 149 | + const template = fs.readFileSync( |
| 150 | + path.join(__dirname, '../../attack-theme/templates/general/base-template.html'), |
| 151 | + 'utf8', |
| 152 | + ); |
| 153 | + const navigation = fs.readFileSync( |
| 154 | + path.join(__dirname, '../../attack-theme/templates/macros/navigation_menu.html'), |
| 155 | + 'utf8', |
| 156 | + ); |
| 157 | + |
| 158 | + expect(template).toContain('<meta name="color-scheme" content="light dark">'); |
| 159 | + expect(template.indexOf('/theme/scripts/theme.js')).toBeLessThan(template.indexOf('bootstrap.min.css')); |
| 160 | + expect(navigation.indexOf('data-theme-toggle')).toBeLessThan(navigation.indexOf('id="search-button"')); |
| 161 | + expect(navigation).toContain('role="switch"'); |
| 162 | + expect(navigation).toContain('class="theme-toggle-track"'); |
| 163 | + expect(navigation).toContain('class="theme-toggle-thumb"'); |
| 164 | + expect(navigation).toContain('aria-checked="false"'); |
| 165 | + expect(navigation).not.toContain('aria-pressed'); |
| 166 | + expect(navigation).not.toContain('data-theme-option'); |
| 167 | + expect(navigation).not.toContain('theme-menu'); |
| 168 | + expect(navigation).not.toContain('dropdown-toggle" type="button" data-theme-toggle'); |
| 169 | + }); |
| 170 | + |
| 171 | + test('uses theme-aware surfaces for the affected resource pages', () => { |
| 172 | + const council = fs.readFileSync( |
| 173 | + path.join(__dirname, '../../modules/resources/templates/attack-advisory-council-members.html'), |
| 174 | + 'utf8', |
| 175 | + ); |
| 176 | + const dataTools = fs.readFileSync( |
| 177 | + path.join(__dirname, '../../modules/resources/templates/attack-data-and-tools.html'), |
| 178 | + 'utf8', |
| 179 | + ); |
| 180 | + const attackcon = fs.readFileSync( |
| 181 | + path.join(__dirname, '../../modules/resources/templates/attackcon-overview.html'), |
| 182 | + 'utf8', |
| 183 | + ); |
| 184 | + |
| 185 | + expect(council).toContain('background: var(--attack-color-body-alternate);'); |
| 186 | + expect(council).toContain('color: var(--attack-on-color-body);'); |
| 187 | + expect(dataTools).toContain('class="tab-content card card-body p-3 attack-excel-files"'); |
| 188 | + expect(dataTools).not.toContain('style="background: #f8f9fa;"'); |
| 189 | + expect(attackcon).toContain('"ATT&CKcon 4.0", "ATT&CKcon 5.0", "ATT&CKcon 6.0", "ATT&CKcon 7.0"'); |
| 190 | + expect(attackcon).toContain('attackcon-banner-image{% if con.title in light_banner_titles %} on-light{% endif %}'); |
| 191 | + }); |
| 192 | + |
| 193 | + test('uses theme-aware home controls and announcement banner colors', () => { |
| 194 | + const home = fs.readFileSync( |
| 195 | + path.join(__dirname, '../../attack-theme/templates/general/attack-index.html'), |
| 196 | + 'utf8', |
| 197 | + ); |
| 198 | + const colors = fs.readFileSync( |
| 199 | + path.join(__dirname, '../../attack-style/themes/_palette.scss'), |
| 200 | + 'utf8', |
| 201 | + ); |
| 202 | + |
| 203 | + expect(home).toContain('fa-up-right-from-square external-link-icon'); |
| 204 | + expect(home).toContain('dropdown-toggle-split random-page-toggle'); |
| 205 | + expect(home).not.toContain('external-site-dark.jpeg'); |
| 206 | + expect(home).not.toContain('style="color: #4f7cac; background-color: white;'); |
| 207 | + expect(colors).toContain('--attack-color-banner: #e7f0f6;'); |
| 208 | + expect(colors).toContain('--attack-color-banner: #263a49;'); |
| 209 | + }); |
| 210 | + |
| 211 | + test('uses a warm metadata label color only in dark mode', () => { |
| 212 | + const colors = fs.readFileSync( |
| 213 | + path.join(__dirname, '../../attack-style/themes/_palette.scss'), |
| 214 | + 'utf8', |
| 215 | + ); |
| 216 | + const layout = fs.readFileSync( |
| 217 | + path.join(__dirname, '../../attack-style/layout/_layout.scss'), |
| 218 | + 'utf8', |
| 219 | + ); |
| 220 | + |
| 221 | + expect(colors).toContain('--attack-color-property-label: #1d2226;'); |
| 222 | + expect(colors).toContain('--attack-color-property-label: #f2d2a4;'); |
| 223 | + expect(layout).toMatch(/\.card-data \.card-title\s*\{\s*color: color-functions\.color\(property-label\);/); |
| 224 | + }); |
| 225 | +}); |
| 226 | + |
| 227 | +function createStorage(value = null) { |
| 228 | + return { |
| 229 | + getItem: jest.fn(() => value), |
| 230 | + removeItem: jest.fn(), |
| 231 | + setItem: jest.fn(), |
| 232 | + }; |
| 233 | +} |
| 234 | + |
| 235 | +function createRoot() { |
| 236 | + return { |
| 237 | + removeAttribute: jest.fn(), |
| 238 | + setAttribute: jest.fn(), |
| 239 | + style: { |
| 240 | + removeProperty: jest.fn(), |
| 241 | + setProperty: jest.fn(), |
| 242 | + }, |
| 243 | + }; |
| 244 | +} |
| 245 | + |
| 246 | +function createElement() { |
| 247 | + const listeners = {}; |
| 248 | + return { |
| 249 | + classList: { toggle: jest.fn() }, |
| 250 | + setAttribute: jest.fn(), |
| 251 | + addEventListener: jest.fn((eventName, listener) => { |
| 252 | + listeners[eventName] = listener; |
| 253 | + }), |
| 254 | + click: () => listeners.click({ preventDefault: jest.fn() }), |
| 255 | + }; |
| 256 | +} |
| 257 | + |
| 258 | +function createControllerFixture({ storedPreference = null, systemDark = false } = {}) { |
| 259 | + const root = createRoot(); |
| 260 | + const toggle = createElement(); |
| 261 | + let changeListener; |
| 262 | + const mediaQuery = { |
| 263 | + matches: systemDark, |
| 264 | + addEventListener: jest.fn((eventName, listener) => { |
| 265 | + if (eventName === 'change') changeListener = listener; |
| 266 | + }), |
| 267 | + dispatchChange: () => changeListener({ matches: mediaQuery.matches }), |
| 268 | + }; |
| 269 | + const document = { |
| 270 | + documentElement: root, |
| 271 | + querySelector: jest.fn(selector => (selector === '[data-theme-toggle]' ? toggle : null)), |
| 272 | + }; |
| 273 | + |
| 274 | + return { |
| 275 | + document, |
| 276 | + mediaQuery, |
| 277 | + storage: createStorage(storedPreference), |
| 278 | + toggle, |
| 279 | + }; |
| 280 | +} |
0 commit comments