-
Notifications
You must be signed in to change notification settings - Fork 4
Fix: disguised-ad-flag replaces entire Reddit feed as one placeholder (#228) #230
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
+307
−6
Merged
Changes from all commits
Commits
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
151 changes: 151 additions & 0 deletions
151
extension/src/rules/__tests__/disguised-ad-flag.property.test.ts
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,151 @@ | ||
| // Copyright (c) 2026 PixieBrix, Inc. | ||
| // Licensed under PolyForm Shield 1.0.0 — see LICENSE. | ||
|
|
||
| // Property-based tests for disguised-ad-flag. fast-check explores the | ||
| // boundary case the feed-wrapper guard is supposed to enforce: | ||
| // - a wrapper containing N >= 2 labeled card-shaped descendants must | ||
| // never itself be replaced as one placeholder. Each card gets its | ||
| // own placeholder; the wrapper survives. | ||
| // This is the invariant violated by #228 — entire reddit feed replaced | ||
| // as a single placeholder. | ||
|
|
||
| import fc from "fast-check"; | ||
|
|
||
| import { PLACEHOLDER_CLASS } from "../../lib/placeholder"; | ||
| import { disguisedAdFlagRule } from "../disguised-ad-flag"; | ||
|
|
||
| const LABEL_PHRASES = [ | ||
| "Sponsored", | ||
| "Promoted", | ||
| "Advertorial", | ||
| "Paid Post", | ||
| "Branded Content", | ||
| "[Ad]", | ||
| "(promoted)", | ||
| ] as const; | ||
|
|
||
| afterEach(() => { | ||
| disguisedAdFlagRule.teardown(); | ||
| document.body.innerHTML = ""; | ||
| }); | ||
|
|
||
| function buildFeed( | ||
| cardCount: number, | ||
| labelText: string, | ||
| headlessCount: number, | ||
| ): HTMLElement { | ||
| const wrapper = document.createElement("div"); | ||
| wrapper.dataset.fixture = "feed"; | ||
| for (let i = 0; i < cardCount; i++) { | ||
| const card = document.createElement("article"); | ||
| card.dataset.fixture = `card-${i}`; | ||
| const heading = | ||
| i < headlessCount ? "" : `<h2><a href="/${i}">Title ${i}</a></h2>`; | ||
| card.innerHTML = ` | ||
| <span class="label">${labelText}</span> | ||
| ${heading} | ||
| <img alt="" src="/${i}.jpg" /> | ||
| <a href="/${i}">Visit</a> | ||
| <p>Body copy for card ${i} that exceeds the eighty-character prose minimum the disguised-ad-flag rule applies to article-shape candidates.</p> | ||
| `; | ||
| wrapper.append(card); | ||
| } | ||
| return wrapper; | ||
| } | ||
|
|
||
| describe("disguisedAdFlagRule feed-wrapper invariants (property)", () => { | ||
| it("a wrapper with multiple labeled card-shaped children is never replaced", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| fc.integer({ min: 2, max: 6 }), | ||
| fc.constantFrom(...LABEL_PHRASES), | ||
| (cardCount, labelText) => { | ||
| document.body.innerHTML = ""; | ||
| const wrapper = buildFeed(cardCount, labelText, 0); | ||
| document.body.append(wrapper); | ||
|
|
||
| disguisedAdFlagRule.apply(document.body); | ||
|
|
||
| // Wrapper survives — only individual cards are replaced. | ||
| expect( | ||
| document.querySelector('[data-fixture="feed"]'), | ||
| ).not.toBeNull(); | ||
| // Every card got its own placeholder. | ||
| expect( | ||
| document.querySelectorAll(`.${PLACEHOLDER_CLASS}`), | ||
| ).toHaveLength(cardCount); | ||
|
|
||
| disguisedAdFlagRule.teardown(); | ||
| }, | ||
| ), | ||
| { numRuns: 30 }, | ||
| ); | ||
| }); | ||
|
|
||
| it("a wrapper with a mix of headed and headless labeled cards is never replaced", () => { | ||
| // Mirrors the #228 trigger: not every ad post carries its own | ||
| // headline. The headless cards walk past their own boundary; the | ||
| // feed wrapper must still reject them. | ||
| fc.assert( | ||
| fc.property( | ||
| fc.integer({ min: 2, max: 6 }), | ||
| fc.integer({ min: 1, max: 5 }), | ||
| (totalCards, headlessRaw) => { | ||
| const headlessCount = Math.min(headlessRaw, totalCards - 1); | ||
| document.body.innerHTML = ""; | ||
| const wrapper = buildFeed(totalCards, "Promoted", headlessCount); | ||
| document.body.append(wrapper); | ||
|
|
||
| disguisedAdFlagRule.apply(document.body); | ||
|
|
||
| expect( | ||
| document.querySelector('[data-fixture="feed"]'), | ||
| ).not.toBeNull(); | ||
| // Headed cards are hidden; headless ones are left alone. | ||
| const headedCount = totalCards - headlessCount; | ||
| expect( | ||
| document.querySelectorAll(`.${PLACEHOLDER_CLASS}`), | ||
| ).toHaveLength(headedCount); | ||
| for (let i = 0; i < headlessCount; i++) { | ||
| expect( | ||
| document.querySelector(`[data-fixture="card-${i}"]`), | ||
| ).not.toBeNull(); | ||
| } | ||
|
|
||
| disguisedAdFlagRule.teardown(); | ||
| }, | ||
| ), | ||
| { numRuns: 40 }, | ||
| ); | ||
| }); | ||
|
|
||
| it("a role='feed' wrapper is never crossed by the walk-up", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| fc.integer({ min: 1, max: 5 }), | ||
| fc.constantFrom(...LABEL_PHRASES), | ||
| (cardCount, labelText) => { | ||
| document.body.innerHTML = ""; | ||
| const wrapper = buildFeed(cardCount, labelText, cardCount); | ||
| wrapper.setAttribute("role", "feed"); | ||
| document.body.append(wrapper); | ||
|
|
||
| disguisedAdFlagRule.apply(document.body); | ||
|
|
||
| // No card has its own heading, and the feed boundary stops | ||
| // the walk — nothing gets hidden, but critically the feed | ||
| // itself is preserved. | ||
| expect( | ||
| document.querySelector('[data-fixture="feed"]'), | ||
| ).not.toBeNull(); | ||
| expect( | ||
| document.querySelectorAll(`.${PLACEHOLDER_CLASS}`), | ||
| ).toHaveLength(0); | ||
|
|
||
| disguisedAdFlagRule.teardown(); | ||
| }, | ||
| ), | ||
| { numRuns: 30 }, | ||
| ); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasOtherCardSubtreereturnstrueas soon as it finds any descendant outside the label's ancestor chain that contains both an<img>/<picture>and an<a href>. This correctly rejects a feed with multiple cards, but it also rejects a single ad card whenever a wrapper<div>sits between the label and the card's content.Example — common in the wild but absent from the test suite:
Trace:
div.card-bodyis not the label, is not contained by the label, and does not contain the label.div.card-body.querySelector("img, picture")→<img>(not null).div.card-body.querySelector("a[href]")→<a>(not null). → returnstrue→isArticleShapedreturnsfalse→ the legitimate ad card escapes detection.The comment on line R218-R219 says "a single advertorial card has at most one such subtree (its own)", but that assumption breaks whenever a card's label is a sibling to (rather than inside) a content wrapper.
Consider requiring two non-overlapping matches before rejecting, e.g. count the number of qualifying subtrees and return
count >= 2. That way a single card's content wrapper counts as one (tolerated), but a feed with multiple cards still trips the guard.