|
3 | 3 | * Licensed under Apache-2.0 with Commons Clause and Attribution/Naming Clause |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { expect } from 'vitest'; |
| 6 | +import { afterEach, expect } from 'vitest'; |
7 | 7 | import { mockFredy } from './utils.js'; |
8 | 8 | import * as mockStore from './mocks/mockStore.js'; |
| 9 | +import { get as getLastNotification } from './mocks/mockNotification.js'; |
9 | 10 |
|
10 | 11 | describe('Issue reproduction: listings filtered by similarity or area should be marked as manually deleted', () => { |
11 | 12 | it('should call deleteListingsById when listings are filtered by similarity', async () => { |
@@ -113,3 +114,223 @@ describe('Issue reproduction: listings filtered by similarity or area should be |
113 | 114 | expect(mockStore.deletedIds).toContain('2'); |
114 | 115 | }); |
115 | 116 | }); |
| 117 | + |
| 118 | +describe('Blacklist is re-applied after detail enrichment', () => { |
| 119 | + afterEach(() => { |
| 120 | + mockStore.setUserSettings(null); |
| 121 | + }); |
| 122 | + |
| 123 | + it('filters out a listing whose blacklisted term only appears in the enriched description', async () => { |
| 124 | + const Fredy = await mockFredy(); |
| 125 | + const providerId = 'test-provider'; |
| 126 | + |
| 127 | + mockStore.setUserSettings({ |
| 128 | + provider_details: [providerId], |
| 129 | + blacklist_filter_on_provider_details: true, |
| 130 | + }); |
| 131 | + |
| 132 | + const mockSimilarityCache = { |
| 133 | + checkAndAddEntry: () => false, |
| 134 | + }; |
| 135 | + |
| 136 | + const blacklist = ['allkauf']; |
| 137 | + |
| 138 | + // The search results page returns a clean snippet (no blacklisted term). |
| 139 | + // fetchDetails simulates loading the full detail page and discovers the |
| 140 | + // blacklisted term hidden deep in the description. |
| 141 | + const providerConfig = { |
| 142 | + url: 'http://example.com', |
| 143 | + getListings: () => |
| 144 | + Promise.resolve([ |
| 145 | + { |
| 146 | + id: 'kept', |
| 147 | + title: 'Nice house', |
| 148 | + address: 'Some street', |
| 149 | + price: '500000', |
| 150 | + link: 'http://example.com/kept', |
| 151 | + description: 'Cozy home with garden', |
| 152 | + }, |
| 153 | + { |
| 154 | + id: 'blacklisted', |
| 155 | + title: 'Eleganz trifft Raumkomfort', |
| 156 | + address: 'Other street', |
| 157 | + price: '600000', |
| 158 | + link: 'http://example.com/blacklisted', |
| 159 | + description: 'Eleganz trifft Raumkomfort', |
| 160 | + }, |
| 161 | + ]), |
| 162 | + normalize: (l) => l, |
| 163 | + filter: (l) => { |
| 164 | + const text = `${l.title ?? ''} ${l.description ?? ''}`.toLowerCase(); |
| 165 | + return !blacklist.some((term) => text.includes(term)); |
| 166 | + }, |
| 167 | + fetchDetails: (listing) => { |
| 168 | + if (listing.id === 'blacklisted') { |
| 169 | + return Promise.resolve({ |
| 170 | + ...listing, |
| 171 | + description: 'Mit allkauf Haus wird dein Traum vom Eigenheim wahr.', |
| 172 | + }); |
| 173 | + } |
| 174 | + return Promise.resolve(listing); |
| 175 | + }, |
| 176 | + crawlFields: { |
| 177 | + id: 'id', |
| 178 | + title: 'title', |
| 179 | + address: 'address', |
| 180 | + price: 'price', |
| 181 | + link: 'link', |
| 182 | + description: 'description', |
| 183 | + }, |
| 184 | + requiredFieldNames: ['id', 'title', 'address', 'price', 'link', 'description'], |
| 185 | + }; |
| 186 | + |
| 187 | + const mockedJob = { |
| 188 | + id: 'blacklist-test-job', |
| 189 | + notificationAdapter: null, |
| 190 | + specFilter: null, |
| 191 | + spatialFilter: null, |
| 192 | + }; |
| 193 | + |
| 194 | + const fredy = new Fredy(providerConfig, mockedJob, providerId, mockSimilarityCache, undefined); |
| 195 | + |
| 196 | + const result = await fredy.execute(); |
| 197 | + |
| 198 | + expect(result).toBeInstanceOf(Array); |
| 199 | + const ids = result.map((l) => l.id); |
| 200 | + expect(ids).toContain('kept'); |
| 201 | + expect(ids).not.toContain('blacklisted'); |
| 202 | + |
| 203 | + const notification = getLastNotification(); |
| 204 | + const notifiedIds = (notification?.payload ?? []).map((p) => p.id); |
| 205 | + expect(notifiedIds).not.toContain('blacklisted'); |
| 206 | + }); |
| 207 | + |
| 208 | + it('short-circuits the pipeline when all listings get blacklisted after enrichment', async () => { |
| 209 | + const Fredy = await mockFredy(); |
| 210 | + const providerId = 'all-blacklisted-provider'; |
| 211 | + |
| 212 | + mockStore.setUserSettings({ |
| 213 | + provider_details: [providerId], |
| 214 | + blacklist_filter_on_provider_details: true, |
| 215 | + }); |
| 216 | + |
| 217 | + const mockSimilarityCache = { |
| 218 | + checkAndAddEntry: () => false, |
| 219 | + }; |
| 220 | + |
| 221 | + const blacklist = ['allkauf']; |
| 222 | + |
| 223 | + const providerConfig = { |
| 224 | + url: 'http://example.com', |
| 225 | + getListings: () => |
| 226 | + Promise.resolve([ |
| 227 | + { |
| 228 | + id: 'only', |
| 229 | + title: 'Eleganz trifft Raumkomfort', |
| 230 | + address: 'Some street', |
| 231 | + price: '700000', |
| 232 | + link: 'http://example.com/only', |
| 233 | + description: 'Eleganz trifft Raumkomfort', |
| 234 | + }, |
| 235 | + ]), |
| 236 | + normalize: (l) => l, |
| 237 | + filter: (l) => { |
| 238 | + const text = `${l.title ?? ''} ${l.description ?? ''}`.toLowerCase(); |
| 239 | + return !blacklist.some((term) => text.includes(term)); |
| 240 | + }, |
| 241 | + fetchDetails: (listing) => |
| 242 | + Promise.resolve({ |
| 243 | + ...listing, |
| 244 | + description: 'Mit allkauf Haus wird dein Traum vom Eigenheim wahr.', |
| 245 | + }), |
| 246 | + crawlFields: { |
| 247 | + id: 'id', |
| 248 | + title: 'title', |
| 249 | + address: 'address', |
| 250 | + price: 'price', |
| 251 | + link: 'link', |
| 252 | + description: 'description', |
| 253 | + }, |
| 254 | + requiredFieldNames: ['id', 'title', 'address', 'price', 'link', 'description'], |
| 255 | + }; |
| 256 | + |
| 257 | + const mockedJob = { |
| 258 | + id: 'all-blacklisted-job', |
| 259 | + notificationAdapter: null, |
| 260 | + specFilter: null, |
| 261 | + spatialFilter: null, |
| 262 | + }; |
| 263 | + |
| 264 | + const fredy = new Fredy(providerConfig, mockedJob, providerId, mockSimilarityCache, undefined); |
| 265 | + |
| 266 | + // Should resolve to undefined (NoNewListingsWarning is caught in _handleError). |
| 267 | + const result = await fredy.execute(); |
| 268 | + expect(result).toBeUndefined(); |
| 269 | + }); |
| 270 | + |
| 271 | + it('does NOT re-filter when blacklist_filter_on_provider_details is disabled', async () => { |
| 272 | + const Fredy = await mockFredy(); |
| 273 | + const providerId = 'opt-out-provider'; |
| 274 | + |
| 275 | + // provider_details enabled (so fetchDetails runs) but blacklist re-filter NOT enabled. |
| 276 | + mockStore.setUserSettings({ |
| 277 | + provider_details: [providerId], |
| 278 | + blacklist_filter_on_provider_details: false, |
| 279 | + }); |
| 280 | + |
| 281 | + const mockSimilarityCache = { |
| 282 | + checkAndAddEntry: () => false, |
| 283 | + }; |
| 284 | + |
| 285 | + const blacklist = ['allkauf']; |
| 286 | + |
| 287 | + const providerConfig = { |
| 288 | + url: 'http://example.com', |
| 289 | + getListings: () => |
| 290 | + Promise.resolve([ |
| 291 | + { |
| 292 | + id: 'leaks-through', |
| 293 | + title: 'Eleganz trifft Raumkomfort', |
| 294 | + address: 'Other street', |
| 295 | + price: '600000', |
| 296 | + link: 'http://example.com/leaks-through', |
| 297 | + description: 'Eleganz trifft Raumkomfort', |
| 298 | + }, |
| 299 | + ]), |
| 300 | + normalize: (l) => l, |
| 301 | + filter: (l) => { |
| 302 | + const text = `${l.title ?? ''} ${l.description ?? ''}`.toLowerCase(); |
| 303 | + return !blacklist.some((term) => text.includes(term)); |
| 304 | + }, |
| 305 | + fetchDetails: (listing) => |
| 306 | + Promise.resolve({ |
| 307 | + ...listing, |
| 308 | + description: 'Mit allkauf Haus wird dein Traum vom Eigenheim wahr.', |
| 309 | + }), |
| 310 | + crawlFields: { |
| 311 | + id: 'id', |
| 312 | + title: 'title', |
| 313 | + address: 'address', |
| 314 | + price: 'price', |
| 315 | + link: 'link', |
| 316 | + description: 'description', |
| 317 | + }, |
| 318 | + requiredFieldNames: ['id', 'title', 'address', 'price', 'link', 'description'], |
| 319 | + }; |
| 320 | + |
| 321 | + const mockedJob = { |
| 322 | + id: 'opt-out-job', |
| 323 | + notificationAdapter: null, |
| 324 | + specFilter: null, |
| 325 | + spatialFilter: null, |
| 326 | + }; |
| 327 | + |
| 328 | + const fredy = new Fredy(providerConfig, mockedJob, providerId, mockSimilarityCache, undefined); |
| 329 | + |
| 330 | + const result = await fredy.execute(); |
| 331 | + |
| 332 | + // Listing leaks through because user has not opted in to the stricter check. |
| 333 | + expect(result).toBeInstanceOf(Array); |
| 334 | + expect(result.map((l) => l.id)).toContain('leaks-through'); |
| 335 | + }); |
| 336 | +}); |
0 commit comments