|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC. |
| 3 | + * Copyright (c) Microsoft Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import {expect} from 'chai'; |
| 19 | + |
| 20 | +import {ContextConfig} from './ContextConfig.js'; |
| 21 | +import {ContextConfigStorage} from './ContextConfigStorage.js'; |
| 22 | + |
| 23 | +describe('ContextConfigStorage', () => { |
| 24 | + const USER_CONTEXT = 'some user context'; |
| 25 | + const BROWSER_CONTEXT = 'some browsing context'; |
| 26 | + |
| 27 | + let storage: ContextConfigStorage; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + storage = new ContextConfigStorage(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('getActiveConfig', () => { |
| 34 | + // Test cases for various configuration scenarios. |
| 35 | + [ |
| 36 | + // Global config only. |
| 37 | + { |
| 38 | + global: {acceptInsecureCerts: true, prerenderingDisabled: true}, |
| 39 | + expected: {acceptInsecureCerts: true, prerenderingDisabled: true}, |
| 40 | + name: 'should return the global config if no other configs are set', |
| 41 | + }, |
| 42 | + // User context overrides global. |
| 43 | + { |
| 44 | + global: {acceptInsecureCerts: true, prerenderingDisabled: true}, |
| 45 | + user: {acceptInsecureCerts: false}, |
| 46 | + expected: {acceptInsecureCerts: false, prerenderingDisabled: true}, |
| 47 | + name: 'should override global config with user context config', |
| 48 | + }, |
| 49 | + // Browsing context overrides global and user context. |
| 50 | + { |
| 51 | + global: {acceptInsecureCerts: false, prerenderingDisabled: true}, |
| 52 | + user: {acceptInsecureCerts: false, prerenderingDisabled: false}, |
| 53 | + browsing: {acceptInsecureCerts: true}, |
| 54 | + expected: {acceptInsecureCerts: true, prerenderingDisabled: false}, |
| 55 | + name: 'should override global and user context configs with browsing context config', |
| 56 | + }, |
| 57 | + // Undefined in user context does not override global. |
| 58 | + { |
| 59 | + global: {acceptInsecureCerts: true, prerenderingDisabled: true}, |
| 60 | + user: {acceptInsecureCerts: undefined, prerenderingDisabled: false}, |
| 61 | + expected: {acceptInsecureCerts: true, prerenderingDisabled: false}, |
| 62 | + name: 'should not override with undefined from user context config', |
| 63 | + }, |
| 64 | + // Undefined in browsing context does not override user context. |
| 65 | + { |
| 66 | + global: {acceptInsecureCerts: true, prerenderingDisabled: true}, |
| 67 | + user: {acceptInsecureCerts: false, prerenderingDisabled: false}, |
| 68 | + browsing: {acceptInsecureCerts: undefined, prerenderingDisabled: true}, |
| 69 | + expected: {acceptInsecureCerts: false, prerenderingDisabled: true}, |
| 70 | + name: 'should not override with undefined from browsing context config', |
| 71 | + }, |
| 72 | + // Undefined in both user and browsing context does not override global. |
| 73 | + { |
| 74 | + global: {acceptInsecureCerts: true, prerenderingDisabled: true}, |
| 75 | + user: {acceptInsecureCerts: undefined, prerenderingDisabled: false}, |
| 76 | + browsing: { |
| 77 | + acceptInsecureCerts: undefined, |
| 78 | + prerenderingDisabled: undefined, |
| 79 | + }, |
| 80 | + expected: {acceptInsecureCerts: true, prerenderingDisabled: false}, |
| 81 | + name: 'should not override with undefined from either user or browsing context config', |
| 82 | + }, |
| 83 | + // Sequential updates to global context. |
| 84 | + { |
| 85 | + global: [{acceptInsecureCerts: true}, {acceptInsecureCerts: undefined}], |
| 86 | + expected: {acceptInsecureCerts: true}, |
| 87 | + name: 'should ignore undefined when updating global config', |
| 88 | + }, |
| 89 | + // Sequential updates to user context. |
| 90 | + { |
| 91 | + global: {acceptInsecureCerts: true}, |
| 92 | + user: [{acceptInsecureCerts: false}, {acceptInsecureCerts: undefined}], |
| 93 | + expected: {acceptInsecureCerts: false}, |
| 94 | + name: 'should ignore undefined when updating user context config', |
| 95 | + }, |
| 96 | + // Sequential updates to browsing context. |
| 97 | + { |
| 98 | + global: {acceptInsecureCerts: true}, |
| 99 | + user: {acceptInsecureCerts: false}, |
| 100 | + browsing: [ |
| 101 | + {acceptInsecureCerts: true}, |
| 102 | + {acceptInsecureCerts: undefined}, |
| 103 | + ], |
| 104 | + expected: {acceptInsecureCerts: true}, |
| 105 | + name: 'should ignore undefined when updating browsing context config', |
| 106 | + }, |
| 107 | + // Null in user context overrides global. |
| 108 | + { |
| 109 | + global: {viewport: {width: 1, height: 1}}, |
| 110 | + user: {viewport: null}, |
| 111 | + expected: {viewport: null}, |
| 112 | + name: 'should override with null from user context config', |
| 113 | + }, |
| 114 | + // Null in browsing context overrides user context. |
| 115 | + { |
| 116 | + global: {viewport: {width: 1, height: 1}}, |
| 117 | + user: {viewport: {width: 2, height: 2}}, |
| 118 | + browsing: {viewport: null}, |
| 119 | + expected: {viewport: null}, |
| 120 | + name: 'should override with null from browsing context config', |
| 121 | + }, |
| 122 | + // Value in browsing context overrides null in user context. |
| 123 | + { |
| 124 | + global: {viewport: {width: 1, height: 1}}, |
| 125 | + user: {viewport: null}, |
| 126 | + browsing: {viewport: {width: 3, height: 3}}, |
| 127 | + expected: {viewport: {width: 3, height: 3}}, |
| 128 | + name: 'should override null with value from browsing context config', |
| 129 | + }, |
| 130 | + ].forEach(({name, global, user, browsing, expected}) => { |
| 131 | + it(name, () => { |
| 132 | + if (global) { |
| 133 | + for (const config of Array.isArray(global) ? global : [global]) { |
| 134 | + storage.updateGlobalConfig(config); |
| 135 | + } |
| 136 | + } |
| 137 | + if (user) { |
| 138 | + for (const config of Array.isArray(user) ? user : [user]) { |
| 139 | + storage.updateUserContextConfig(USER_CONTEXT, config); |
| 140 | + } |
| 141 | + } |
| 142 | + if (browsing) { |
| 143 | + for (const config of Array.isArray(browsing) |
| 144 | + ? browsing |
| 145 | + : [browsing]) { |
| 146 | + storage.updateBrowsingContextConfig(BROWSER_CONTEXT, config); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + const activeConfig = storage.getActiveConfig( |
| 151 | + BROWSER_CONTEXT, |
| 152 | + USER_CONTEXT, |
| 153 | + ); |
| 154 | + |
| 155 | + const expectedConfig = new ContextConfig(); |
| 156 | + Object.assign(expectedConfig, expected); |
| 157 | + |
| 158 | + expect(activeConfig).to.deep.equal(expectedConfig); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments