mb-userscripts icon indicating copy to clipboard operation
mb-userscripts copied to clipboard

Testing URL match patterns

Open ROpdebee opened this issue 3 years ago • 6 comments

On a sidenote: Something I would really like to have tests for are the match patterns in the userscript's metadata block, since the very unspecific * wildcard is rather unsatisfying in most cases and Tampermonkey interprets these patterns differntly from Violentmonkey as we have already noticed. What do you think about adding test cases for URLs that should or should not be matched to each userscript? As time passes I tend to forget why I designed these patterns in a certain way and it's too easy to miss something during a refactoring or when the scope of the script is extended. A possible solution could be realized by keeping two lists with (representative) "positive" and "negative" URLs inside the meta.ts module of each script. Would it be feasible to create unit tests which would do some basic checks for the match patterns against these lists or is this rather something you would combine with E2E tests because they are userscript engine dependent?

Originally posted by @kellnerd in https://github.com/ROpdebee/mb-userscripts/pull/68#discussion_r720848981

ROpdebee avatar Oct 03 '21 17:10 ROpdebee

Unit testing would theoretically make the most sense, but would be practically infeasible. We'd have to simulate the way each userscript engine (on each browser, if that makes a difference) matches and excludes URLs. If one of them ever changes that, we'd have to evolve our implementation too.

What we could instead do, as you mentioned, is put two arrays in the meta.ts file (ex: matchedUrls and excludedUrls) and run an E2E on each browser/engine combination with a fake userscript which uses the same @include patterns as the real one, but just does an alert or console.log when loaded. The E2E test would then check whether whatever the fake script does, happens on pages in matchedUrls, but not on excludedUrls.

I'll admit that some of my own @include patterns are much too broad (just look at blind votes or supercharged CAA edits, which for some reason run on every page 😅). Proper test cases would make it much easier to restrict those to run only on the pages that are relevant.

ROpdebee avatar Oct 03 '21 17:10 ROpdebee

I'll admit that some of my own @include patterns are much too broad (just look at blind votes or supercharged CAA edits, which for some reason run on every page 😅).

By the way, just got this meaningful error message which is caused by the supercharged CAA edits script running on an /artist/<gid>/credit/<id>/edit page while I was testing my own script 😂:

Script terminated by timeout at:
fix@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:4567:11
handler@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:4780:60
r@https://staticbrainz.org/MB/common-chunks-3952990.js:2:480837
EventListener.handleEvent*TVzBjg//ae</e.prototype._wrapEventTarget/</<@https://staticbrainz.org/MB/common-chunks-3952990.js:2:482445
ce/</</</<@https://staticbrainz.org/MB/common-chunks-3952990.js:2:485490
setup@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:4789:10
add@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:4165:42
on/<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:4861:17
each@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:374:23
each@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:139:17
on@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:4860:15
_on/<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:9956
each@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:382:23
_on@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:9650
_trackFocus@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9222:6459
window.VMin05tm0po9/</</t.widget/</r[e]</<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:4499
_create@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9221:31441
window.VMin05tm0po9/</</t.widget/</r[e]</<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:4499
t@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:4326
_create@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:10669:10
window.VMin05tm0po9/</</t.widget/</r[e]</<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:4499
_createWidget@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:6645
window.VMin05tm0po9/</</t.widget/t[h][e]@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:4063
window.VMin05tm0po9/</</t.widget.bridge/t.fn[e]/<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:5877
each@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:374:23
each@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:139:17
window.VMin05tm0po9/</</t.widget.bridge/t.fn[e]@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:9217:5787
window.VMin05tm0po9/</</openComparisonDialog<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:10979:10
window.VMin05tm0po9/</<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:11006:3
window.VMin05tm0po9/<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:11518:3
window.VMin05tm0po9@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:11518:7
a@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:20074
v@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:20181
set@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:19954
@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/%20MB%EF%BC%9A%20Supercharged%20Cover%20Art%20Edits.user.js#52:1:1
c/<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:13961
ScriptData@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:20555
onHandle@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:3498
c/<@moz-extension://6ef284f8-ca1b-4084-b765-066f6c2cb72f/sandbox/injected-web.js:1:13824
edit line 1 > injectedScript:4567:11

kellnerd avatar Oct 05 '21 20:10 kellnerd

Yeah it definitely shouldn't be running on those pages. Is it hanging your browser or something? Apparently that timeout termination error is something built into Firefox to prevent pages getting locked up when something is looping indefinitely. I hope that's caused by the script running on a page it shouldn't be running on... I can't seem to reproduce it here though, so I'm not sure what's hogging resources specifically.

I know the image comparison will hog resources, but that should under no circumstances be running on those pages. The script basically just queries some HTML elements which are cover art edits, and processes each of them individually. If there are none of those elements (which should be the case for those pages), the script just does nothing. So I'm not sure what could be going on... Very strange.

ROpdebee avatar Oct 05 '21 20:10 ROpdebee

Ah wait, did you happen to press the left or right on the arrow keys on your keyboard? Since the traceback says EventListener.handleEvent and the supercharged script apparently is listening to those key presses all the time to attempt to browse through the images in the comparison dialog. Do you happen to have the "auto-compute image differences" option set? (It is if localStorage.ROpdebee_autoComputeDiff exists and is truthy).

I suspect that if that option is turned on, and you hit left or right on the keyboard, it might start freaking out a bit if it's not attached to an edit. Further investigation would be needed.

Or it might just be a complete fluke. If you see it happen again, please open a new issue. If supercharged CAA edits is causing browser hangs, that needs to be fixed ASAP.

ROpdebee avatar Oct 05 '21 20:10 ROpdebee

No, it is not causing notable issues. I just got surprised by the huge yellow stack trace because I was expecting to see nothing go wrong while I was testing the new version of my guess punctuation script. It happened immediately after the page had been loaded, so I think I did not press any key (and the autoComputeDiff option is on).

kellnerd avatar Oct 05 '21 20:10 kellnerd

Very strange. It might be having spurious issues loading a dependency. As long as it doesn't cause immediate problems and it works properly on pages where it matters, I won't lose sleep over it. We'll fix those @includes in the future.

ROpdebee avatar Oct 05 '21 20:10 ROpdebee