-
Notifications
You must be signed in to change notification settings - Fork 31
Fire information detection events in youtube detector #2439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+455
−19
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0df8b19
yt events
jdorweiler e5551b5
update test
jdorweiler 6d472e1
remove extra hostname check
jdorweiler 93bf411
Merge branch 'main' into jd/yt-events
jdorweiler 807bbf4
fix test
jdorweiler dc2e53b
fix: apply prettier formatting to youtube-ad-detection unit tests
jdorweiler 292f33e
fix: handle promise rejection in fireEvent callback
jdorweiler 9c7b8b3
fix: use async/await instead of .catch() in fireEvent callback
jdorweiler b296019
add hostname gating unit tests for runYoutubeAdDetection
jdorweiler 216f94b
add integration tests and debug logging for YouTube detection events
jdorweiler d91b562
Merge branch 'main' into jd/yt-events
jdorweiler 1aa0925
fix: update callback on existing detector singleton
jdorweiler a45e53b
Handle async rejection in onEvent callback to prevent unhandled promi…
cursoragent dbf6b3c
Merge branch 'main' into jd/yt-events
jdorweiler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { YouTubeAdDetector } from '../src/detectors/detections/youtube-ad-detection.js'; | ||
|
|
||
| const minimalConfig = { | ||
| playerSelectors: ['#movie_player'], | ||
| adClasses: ['ad-showing'], | ||
| adTextPatterns: [], | ||
| sweepIntervalMs: 2000, | ||
| slowLoadThresholdMs: 5000, | ||
| staticAdSelectors: { background: '', thumbnail: '', image: '' }, | ||
| playabilityErrorSelectors: [], | ||
| playabilityErrorPatterns: [], | ||
| adBlockerDetectionSelectors: [], | ||
| adBlockerDetectionPatterns: [], | ||
| loginStateSelectors: { signInButton: '', avatarButton: '', premiumLogo: '' }, | ||
| }; | ||
|
|
||
| const configWithAllEvents = { | ||
| ...minimalConfig, | ||
| fireDetectionEvents: { | ||
| adBlocker: true, | ||
| playabilityError: true, | ||
| videoAd: true, | ||
| staticAd: true, | ||
| }, | ||
| }; | ||
|
|
||
| describe('YouTubeAdDetector', () => { | ||
| describe('onEvent callback', () => { | ||
| it('calls onEvent with youtube_ prefix when a new detection occurs', () => { | ||
| const events = []; | ||
| const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('adBlocker'); | ||
|
|
||
| expect(events).toEqual(['youtube_adBlocker']); | ||
| }); | ||
|
|
||
| it('fires for each detection type', () => { | ||
| const events = []; | ||
| const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('videoAd'); | ||
| detector.reportDetection('playabilityError', { message: 'error' }); | ||
| detector.reportDetection('adBlocker'); | ||
| detector.reportDetection('staticAd'); | ||
|
|
||
| expect(events).toEqual(['youtube_videoAd', 'youtube_playabilityError', 'youtube_adBlocker', 'youtube_staticAd']); | ||
| }); | ||
|
|
||
| it('does not fire for duplicate detections', () => { | ||
| const events = []; | ||
| const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('adBlocker'); | ||
| detector.reportDetection('adBlocker'); | ||
| detector.reportDetection('adBlocker'); | ||
|
|
||
| expect(events).toEqual(['youtube_adBlocker']); | ||
| }); | ||
|
|
||
| it('fires again after detection is cleared and re-detected', () => { | ||
| const events = []; | ||
| const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('adBlocker'); | ||
| detector.clearDetection('adBlocker'); | ||
| detector.reportDetection('adBlocker'); | ||
|
|
||
| expect(events).toEqual(['youtube_adBlocker', 'youtube_adBlocker']); | ||
| }); | ||
|
|
||
| it('fires for playabilityError with a different message', () => { | ||
| const events = []; | ||
| const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('playabilityError', { message: 'error A' }); | ||
| detector.reportDetection('playabilityError', { message: 'error B' }); | ||
| detector.reportDetection('playabilityError', { message: 'error B' }); | ||
|
|
||
| expect(events).toEqual(['youtube_playabilityError', 'youtube_playabilityError']); | ||
| }); | ||
|
|
||
| it('does not break detection when callback throws', () => { | ||
| const detector = new YouTubeAdDetector(configWithAllEvents, undefined, () => { | ||
| throw new Error('callback failure'); | ||
| }); | ||
|
|
||
| const result = detector.reportDetection('adBlocker'); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(detector.state.detections.adBlocker.count).toBe(1); | ||
| expect(detector.state.detections.adBlocker.showing).toBe(true); | ||
| }); | ||
|
|
||
| it('defaults to no-op when onEvent is not provided', () => { | ||
| const detector = new YouTubeAdDetector(minimalConfig); | ||
|
|
||
| const result = detector.reportDetection('videoAd'); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(detector.state.detections.videoAd.count).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fireDetectionEvents gating', () => { | ||
| it('does not fire events when fireDetectionEvents is absent', () => { | ||
| const events = []; | ||
| const detector = new YouTubeAdDetector(minimalConfig, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('adBlocker'); | ||
| detector.reportDetection('playabilityError', { message: 'error' }); | ||
|
|
||
| expect(events).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not fire events for types set to false', () => { | ||
| const events = []; | ||
| const config = { | ||
| ...minimalConfig, | ||
| fireDetectionEvents: { adBlocker: false, playabilityError: false }, | ||
| }; | ||
| const detector = new YouTubeAdDetector(config, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('adBlocker'); | ||
| detector.reportDetection('playabilityError', { message: 'error' }); | ||
|
|
||
| expect(events).toEqual([]); | ||
| }); | ||
|
|
||
| it('fires only for types set to true', () => { | ||
| const events = []; | ||
| const config = { | ||
| ...minimalConfig, | ||
| fireDetectionEvents: { adBlocker: true, playabilityError: false }, | ||
| }; | ||
| const detector = new YouTubeAdDetector(config, undefined, (type) => events.push(type)); | ||
|
|
||
| detector.reportDetection('adBlocker'); | ||
| detector.reportDetection('playabilityError', { message: 'error' }); | ||
|
|
||
| expect(events).toEqual(['youtube_adBlocker']); | ||
| }); | ||
|
|
||
| it('still tracks detection state even when events are gated off', () => { | ||
| const detector = new YouTubeAdDetector(minimalConfig); | ||
|
|
||
| const result = detector.reportDetection('adBlocker'); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(detector.state.detections.adBlocker.count).toBe(1); | ||
| expect(detector.state.detections.adBlocker.showing).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.