|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +// @flow |
| 6 | + |
| 7 | +import { getSourceViewCode } from '../../selectors/code'; |
| 8 | +import { storeWithProfile } from '../fixtures/stores'; |
| 9 | +import { updateBottomBoxContentsAndMaybeOpen } from '../../actions/profile-view'; |
| 10 | + |
| 11 | +import type { State, SourceCodeStatus } from 'firefox-profiler/types'; |
| 12 | + |
| 13 | +describe('source code cache with sourceId', function () { |
| 14 | + it('should handle source code with sourceId correctly', function () { |
| 15 | + const store = storeWithProfile(); |
| 16 | + |
| 17 | + // Test that the selector works with sourceId |
| 18 | + store.dispatch( |
| 19 | + updateBottomBoxContentsAndMaybeOpen('calltree', { |
| 20 | + libIndex: 0, |
| 21 | + globalJSSourceId: { pid: '123', sourceId: 42 }, |
| 22 | + sourceFile: 'test-file.js', |
| 23 | + nativeSymbols: [], |
| 24 | + }) |
| 25 | + ); |
| 26 | + |
| 27 | + const sourceViewCode = getSourceViewCode(store.getState()); |
| 28 | + |
| 29 | + // The selector should return undefined since there's no cached source code |
| 30 | + expect(sourceViewCode).toBe(undefined); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should handle source code without sourceId', function () { |
| 34 | + const store = storeWithProfile(); |
| 35 | + |
| 36 | + // Test that the selector works without sourceId |
| 37 | + store.dispatch( |
| 38 | + updateBottomBoxContentsAndMaybeOpen('calltree', { |
| 39 | + libIndex: 0, |
| 40 | + globalJSSourceId: null, |
| 41 | + sourceFile: 'test-file.js', |
| 42 | + nativeSymbols: [], |
| 43 | + }) |
| 44 | + ); |
| 45 | + |
| 46 | + const sourceViewCode = getSourceViewCode(store.getState()); |
| 47 | + |
| 48 | + // The selector should return undefined since there's no cached source code |
| 49 | + expect(sourceViewCode).toBe(undefined); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should create different cache lookups for different sourceIds', function () { |
| 53 | + const store = storeWithProfile(); |
| 54 | + |
| 55 | + // Mock the source code cache to have entries |
| 56 | + const mockCache = new Map(); |
| 57 | + mockCache.set('test-file.js', { |
| 58 | + type: 'AVAILABLE', |
| 59 | + code: 'source without sourceId', |
| 60 | + }); |
| 61 | + mockCache.set('test-file.js-111-42', { |
| 62 | + type: 'AVAILABLE', |
| 63 | + code: 'source with sourceId 42', |
| 64 | + }); |
| 65 | + mockCache.set('test-file.js-111-123', { |
| 66 | + type: 'AVAILABLE', |
| 67 | + code: 'source with sourceId 123', |
| 68 | + }); |
| 69 | + |
| 70 | + // First, let's open the bottom box with sourceId |
| 71 | + store.dispatch( |
| 72 | + updateBottomBoxContentsAndMaybeOpen('calltree', { |
| 73 | + libIndex: 0, |
| 74 | + globalJSSourceId: { pid: '111', sourceId: 42 }, |
| 75 | + sourceFile: 'test-file.js', |
| 76 | + nativeSymbols: [], |
| 77 | + }) |
| 78 | + ); |
| 79 | + |
| 80 | + // Manually set the cache in the state for testing |
| 81 | + const state = { |
| 82 | + ...store.getState(), |
| 83 | + code: { |
| 84 | + ...store.getState().code, |
| 85 | + sourceCodeCache: mockCache, |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + const sourceViewCode = getSourceViewCode(state); |
| 90 | + |
| 91 | + expect(sourceViewCode).toEqual({ |
| 92 | + type: 'AVAILABLE', |
| 93 | + code: 'source with sourceId 42', |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should fallback to cache without sourceId when sourceId is null', function () { |
| 98 | + const store = storeWithProfile(); |
| 99 | + |
| 100 | + // Mock the source code cache to have entries |
| 101 | + const mockCache: Map<string, SourceCodeStatus> = new Map(); |
| 102 | + mockCache.set('test-file.js', { |
| 103 | + type: 'AVAILABLE', |
| 104 | + code: 'source without sourceId', |
| 105 | + }); |
| 106 | + mockCache.set('test-file.js-42', { |
| 107 | + type: 'AVAILABLE', |
| 108 | + code: 'source with sourceId 42', |
| 109 | + }); |
| 110 | + |
| 111 | + // First, let's open the bottom box with null sourceId |
| 112 | + store.dispatch( |
| 113 | + updateBottomBoxContentsAndMaybeOpen('calltree', { |
| 114 | + libIndex: 0, |
| 115 | + globalJSSourceId: null, |
| 116 | + sourceFile: 'test-file.js', |
| 117 | + nativeSymbols: [], |
| 118 | + }) |
| 119 | + ); |
| 120 | + |
| 121 | + // Manually set the cache in the state for testing |
| 122 | + const state: State = { |
| 123 | + ...store.getState(), |
| 124 | + code: { |
| 125 | + ...store.getState().code, |
| 126 | + sourceCodeCache: mockCache, |
| 127 | + }, |
| 128 | + }; |
| 129 | + |
| 130 | + const sourceViewCode = getSourceViewCode(state); |
| 131 | + |
| 132 | + expect(sourceViewCode).toEqual({ |
| 133 | + type: 'AVAILABLE', |
| 134 | + code: 'source without sourceId', |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments