|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-2026 Adguard Software Ltd. |
| 3 | + * |
| 4 | + * @file |
| 5 | + * This file is part of AdGuard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension). |
| 6 | + * |
| 7 | + * AdGuard Browser Extension is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * AdGuard Browser Extension is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | + * See the GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with AdGuard Browser Extension. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +import { type Storage } from 'webextension-polyfill'; |
| 22 | +import { |
| 23 | + afterEach, |
| 24 | + beforeEach, |
| 25 | + describe, |
| 26 | + expect, |
| 27 | + it, |
| 28 | + vi, |
| 29 | +} from 'vitest'; |
| 30 | + |
| 31 | +import { UserRulesApi } from '../../../../../Extension/src/background/api/filters/userrules'; |
| 32 | +import { FiltersStoragesAdapter } from '../../../../../Extension/src/background/storages/filters-adapter'; |
| 33 | +import { hybridStorage } from '../../../../../Extension/src/background/storages'; |
| 34 | +import { AntiBannerFiltersId } from '../../../../../Extension/src/common/constants'; |
| 35 | +import { mockLocalStorage } from '../../../../helpers'; |
| 36 | + |
| 37 | +describe('UserRulesApi', () => { |
| 38 | + let localStorage: Storage.StorageArea; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + localStorage = mockLocalStorage(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(async () => { |
| 45 | + await localStorage.clear(); |
| 46 | + await hybridStorage.clear(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('normalizes line endings when user rules are saved', async () => { |
| 50 | + await UserRulesApi.setUserRules('||a.com^\r\n||b.com^\r||c.com^'); |
| 51 | + |
| 52 | + await expect(UserRulesApi.getOriginalUserRules()) |
| 53 | + .resolves.toBe('||a.com^\n||b.com^\n||c.com^'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('normalizes line endings in existing user rules during initialization', async () => { |
| 57 | + await FiltersStoragesAdapter.set( |
| 58 | + AntiBannerFiltersId.UserFilterId, |
| 59 | + '||a.com^\r\n||b.com^', |
| 60 | + ); |
| 61 | + |
| 62 | + await expect(UserRulesApi.getOriginalUserRules()) |
| 63 | + .resolves.toBe('||a.com^\r\n||b.com^'); |
| 64 | + |
| 65 | + await UserRulesApi.init(false); |
| 66 | + |
| 67 | + await expect(UserRulesApi.getOriginalUserRules()) |
| 68 | + .resolves.toBe('||a.com^\n||b.com^'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('preserves existing rules when initialization cannot save normalized content', async () => { |
| 72 | + await FiltersStoragesAdapter.set( |
| 73 | + AntiBannerFiltersId.UserFilterId, |
| 74 | + '||a.com^\r\n||b.com^', |
| 75 | + ); |
| 76 | + const setSpy = vi.spyOn(FiltersStoragesAdapter, 'set') |
| 77 | + .mockRejectedValueOnce(new Error('Storage write failed')); |
| 78 | + |
| 79 | + try { |
| 80 | + await UserRulesApi.init(false); |
| 81 | + |
| 82 | + await expect(UserRulesApi.getOriginalUserRules()) |
| 83 | + .resolves.toBe('||a.com^\r\n||b.com^'); |
| 84 | + } finally { |
| 85 | + setSpy.mockRestore(); |
| 86 | + } |
| 87 | + }); |
| 88 | +}); |
0 commit comments