|
| 1 | +// Copyright 2026 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import type * as SDK from '../../core/sdk/sdk.js'; |
| 6 | +import * as Protocol from '../../generated/protocol.js'; |
| 7 | +import {describeWithEnvironment} from '../../testing/EnvironmentHelpers.js'; |
| 8 | +import {setupLocaleHooks} from '../../testing/LocaleHelpers.js'; |
| 9 | +import {MockIssuesModel} from '../../testing/MockIssuesModel.js'; |
| 10 | + |
| 11 | +import * as IssuesManager from './issues_manager.js'; |
| 12 | + |
| 13 | +describeWithEnvironment('SelectivePermissionsInterventionIssue', () => { |
| 14 | + setupLocaleHooks(); |
| 15 | + |
| 16 | + const mockModel = new MockIssuesModel([]) as unknown as SDK.IssuesModel.IssuesModel; |
| 17 | + const mockTarget = { |
| 18 | + id: () => 'fake-id', |
| 19 | + isDisposed: () => false, |
| 20 | + model: () => null, |
| 21 | + }; |
| 22 | + mockModel.target = () => mockTarget as unknown as SDK.Target.Target; |
| 23 | + |
| 24 | + function createProtocolIssueWithDetails( |
| 25 | + selectivePermissionsInterventionIssueDetails: Protocol.Audits.SelectivePermissionsInterventionIssueDetails): |
| 26 | + Protocol.Audits.InspectorIssue { |
| 27 | + return { |
| 28 | + code: Protocol.Audits.InspectorIssueCode.SelectivePermissionsInterventionIssue, |
| 29 | + details: {selectivePermissionsInterventionIssueDetails}, |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + const issueDetails = { |
| 34 | + apiName: 'geolocation', |
| 35 | + stackTrace: { |
| 36 | + callFrames: [ |
| 37 | + { |
| 38 | + functionName: 'foo', |
| 39 | + scriptId: '1' as Protocol.Runtime.ScriptId, |
| 40 | + url: 'https://example.com/foo.js', |
| 41 | + lineNumber: 10, |
| 42 | + columnNumber: 5, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + adAncestry: { |
| 47 | + adAncestryChain: [ |
| 48 | + { |
| 49 | + scriptId: '2' as Protocol.Runtime.ScriptId, |
| 50 | + debuggerId: '123' as Protocol.Runtime.UniqueDebuggerId, |
| 51 | + name: 'https://ads.com/ad.js', |
| 52 | + }, |
| 53 | + ], |
| 54 | + rootScriptFilterlistRule: '||ads.com^', |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + it('correctly creates an issue with valid details', () => { |
| 59 | + const issue = createProtocolIssueWithDetails(issueDetails); |
| 60 | + |
| 61 | + const interventionIssues = |
| 62 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 63 | + mockModel, issue); |
| 64 | + assert.lengthOf(interventionIssues, 1); |
| 65 | + const interventionIssue = interventionIssues[0]; |
| 66 | + |
| 67 | + assert.strictEqual( |
| 68 | + interventionIssue.getCategory(), IssuesManager.Issue.IssueCategory.SELECTIVE_PERMISSIONS_INTERVENTION); |
| 69 | + assert.strictEqual(interventionIssue.getKind(), IssuesManager.Issue.IssueKind.PAGE_ERROR); |
| 70 | + const description = interventionIssue.getDescription(); |
| 71 | + assert.strictEqual(description?.file, 'selectivePermissionsIntervention.md'); |
| 72 | + assert.lengthOf(description?.links || [], 1); |
| 73 | + assert.strictEqual(description?.links[0].link, 'https://crbug.com/435223477'); |
| 74 | + assert.deepEqual(Array.from([interventionIssue.details()]), [issueDetails]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('generates a stable primary key', () => { |
| 78 | + const issue = createProtocolIssueWithDetails(issueDetails); |
| 79 | + const interventionIssue1 = |
| 80 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 81 | + mockModel, issue)[0]; |
| 82 | + const interventionIssue2 = |
| 83 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 84 | + mockModel, issue)[0]; |
| 85 | + |
| 86 | + assert.strictEqual(interventionIssue1.primaryKey(), interventionIssue2.primaryKey()); |
| 87 | + }); |
| 88 | + |
| 89 | + it('generates different primary keys for different details', () => { |
| 90 | + const issue1 = createProtocolIssueWithDetails(issueDetails); |
| 91 | + const issue2 = createProtocolIssueWithDetails({...issueDetails, apiName: 'microphone'}); |
| 92 | + const interventionIssue1 = |
| 93 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 94 | + mockModel, issue1)[0]; |
| 95 | + const interventionIssue2 = |
| 96 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 97 | + mockModel, issue2)[0]; |
| 98 | + |
| 99 | + assert.notStrictEqual(interventionIssue1.primaryKey(), interventionIssue2.primaryKey()); |
| 100 | + }); |
| 101 | + |
| 102 | + it('handles missing optional fields', () => { |
| 103 | + const issueDetailsMinimal = { |
| 104 | + apiName: 'geolocation', |
| 105 | + adAncestry: { |
| 106 | + adAncestryChain: [], |
| 107 | + }, |
| 108 | + } as Protocol.Audits.SelectivePermissionsInterventionIssueDetails; |
| 109 | + const issue = createProtocolIssueWithDetails(issueDetailsMinimal); |
| 110 | + |
| 111 | + const interventionIssues = |
| 112 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 113 | + mockModel, issue); |
| 114 | + assert.lengthOf(interventionIssues, 1); |
| 115 | + const interventionIssue = interventionIssues[0]; |
| 116 | + |
| 117 | + assert.deepEqual(Array.from([interventionIssue.details()]), [issueDetailsMinimal]); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns an empty array when details are missing', () => { |
| 121 | + const issue = { |
| 122 | + code: Protocol.Audits.InspectorIssueCode.SelectivePermissionsInterventionIssue, |
| 123 | + details: {}, |
| 124 | + }; |
| 125 | + const interventionIssues = |
| 126 | + IssuesManager.SelectivePermissionsInterventionIssue.SelectivePermissionsInterventionIssue.fromInspectorIssue( |
| 127 | + mockModel, issue); |
| 128 | + assert.lengthOf(interventionIssues, 0); |
| 129 | + }); |
| 130 | +}); |
0 commit comments