This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
CardGeneratorTests.swift
284 lines (269 loc) · 10.6 KB
/
CardGeneratorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import Foundation
import XCTest
@testable import BraveNews
extension FeedCardGenerator {
/// Convenience method for tests to retrieve all cards in the sequence
fileprivate var allCards: [FeedCard] {
get async throws {
var result: [FeedCard] = []
for try await cards in self {
result.append(contentsOf: cards)
}
return result
}
}
}
final class CardGeneratorTests: XCTestCase {
private static let mockSources: [FeedItem.Source] = [
.init(id: "1", isDefault: false, category: "Top News", name: "Top News", destinationDomains: [], localeDetails: [.init(channels: ["Top Sources"], locale: "en_US")]),
.init(id: "2", isDefault: false, category: "Top News", name: "Top News 2", destinationDomains: [], localeDetails: [.init(channels: ["Top Sources"], locale: "en_US")]),
.init(id: "3", isDefault: false, category: "Top News", name: "Top News 3", destinationDomains: [], localeDetails: [.init(channels: ["Top Sources"], locale: "en_US")]),
.init(id: "4", isDefault: false, category: "Technology", name: "Technology", destinationDomains: [], localeDetails: [.init(channels: ["Technology"], locale: "ja_JP")]),
.init(id: "5", isDefault: false, category: "Technology", name: "Technology 2", destinationDomains: [], localeDetails: [.init(channels: ["Technology"], locale: "ja_JP")]),
.init(id: "6", isDefault: false, category: "Technology", name: "Technology 3", destinationDomains: [], localeDetails: [.init(channels: ["Technology"], locale: "ja_JP")]),
]
private static let scoredItems: [FeedItem] = Range<Int>(1...6)
.compactMap { id -> FeedItem? in
guard let source = mockSources.first(where: { $0.id == String(id) }) else { return nil }
return .init(
score: Double(id),
content: .init(
publishTime: Date(),
imageURL: URL(string: "image")!,
title: "",
description: "",
contentType: .article,
publisherID: String(id),
urlHash: "1",
baseScore: Double(id)
),
source: source
)
}
/// Tests that no cards will generate when the user follows no sources or channels within the list of items
func testEmptyFollowList() async throws {
let sequence: [FeedSequenceElement] = [
.headline(paired: false)
]
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: sequence,
followedSources: [],
hiddenSources: [],
followedChannels: [:],
ads: nil
)
let cards = try await generator.allCards
XCTAssertTrue(cards.isEmpty)
}
/// Tests that following a channel will render cards from sources that the user doesn't follow explicilty
func testFollowedChannel() async throws {
let sequence: [FeedSequenceElement] = [
.repeating([
.headline(paired: false),
]),
]
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: sequence,
followedSources: [],
hiddenSources: [],
followedChannels: ["en_US": ["Top Sources"]],
ads: nil
)
let expectedItems = Self.scoredItems.filter({
$0.source.localeDetails?.contains(where: {
$0.locale == "en_US" && !$0.channels.intersection(["Top Sources"]).isEmpty
}) ?? false
})
let cards = try await generator.allCards
XCTAssertFalse(cards.isEmpty)
XCTAssertEqual(cards.map(\.items).flatMap { $0 }, expectedItems)
}
/// Tests that hidden sources dont generate cards even if they exist in a followed channel
func testHiddenSources() async throws {
let sequence: [FeedSequenceElement] = [
.repeating([
.headline(paired: false),
]),
]
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: sequence,
followedSources: [],
hiddenSources: ["1"],
followedChannels: ["en_US": ["Top Sources"]],
ads: nil
)
let expectedItems = Self.scoredItems.filter({
if $0.source.id == "1" { return false }
return $0.source.localeDetails?.contains(where: {
$0.locale == "en_US" && !$0.channels.intersection(["Top Sources"]).isEmpty
}) ?? false
})
let cards = try await generator.allCards
XCTAssertFalse(cards.isEmpty)
XCTAssertEqual(cards.map(\.items).flatMap { $0 }, expectedItems)
}
/// Tests that an empty sequence results in no cards
func testEmptySequence() async throws {
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: [],
followedSources: [],
hiddenSources: [],
followedChannels: [:],
ads: nil
)
let cards = try await generator.allCards
XCTAssertTrue(cards.isEmpty)
}
/// Tests a finite sequence that only generates cards until rules run out
func testFiniteSequence() async throws {
let sequence: [FeedSequenceElement] = [
.headline(paired: false),
]
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: sequence,
followedSources: Set(Self.mockSources.map(\.id)),
hiddenSources: [],
followedChannels: [:],
ads: nil
)
let expectedItem = Self.scoredItems.first(where: { $0.content.contentType == .article })
let cards = try await generator.allCards
XCTAssertEqual(cards.count, 1)
if case .headline(let item) = cards.first {
XCTAssertEqual(item, expectedItem)
} else {
XCTFail()
}
}
/// Tests generating cards using a repeating sequence that will fill cards until there are none left to fill
func testRepeatingSequence() async throws {
let sequence: [FeedSequenceElement] = [
.repeating([
.headline(paired: false),
]),
]
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: sequence,
followedSources: Set(Self.mockSources.map(\.id)),
hiddenSources: [],
followedChannels: [:],
ads: nil
)
let expectedItems = Self.scoredItems.filter({
$0.content.contentType == .article && $0.content.imageURL != nil
})
let cards = try await generator.allCards
XCTAssertEqual(cards.count, expectedItems.count)
XCTAssertTrue(cards.allSatisfy({
if case .headline = $0 {
return true
}
return false
}))
XCTAssertEqual(cards.map(\.items).flatMap { $0 }, expectedItems)
}
/// Tests generating a category group card, which by default will always be a Top News category initially
func testInitialCategoryGroup() async throws {
let sequence: [FeedSequenceElement] = [
.categoryGroup
]
let generator = FeedCardGenerator(
scoredItems: Self.scoredItems,
sequence: sequence,
followedSources: Set(Self.mockSources.map(\.id)),
hiddenSources: [],
followedChannels: [:],
ads: nil
)
let expectedItems = Self.scoredItems.filter({
$0.content.contentType == .article && $0.source.category == "Top News"
})
let cards = try await generator.allCards
XCTAssertEqual(cards.count, 1)
XCTAssertTrue(cards.allSatisfy({
if case .group = $0 {
return true
}
return false
}))
XCTAssertEqual(cards.map(\.items).flatMap { $0 }, expectedItems)
}
/// Tests generating a deals card with feed items that have the `deals` content type and an offers category
func testDealsCard() async throws {
let sequence: [FeedSequenceElement] = [
.deals
]
let dealsSource = FeedItem.Source(id: "1", isDefault: false, category: "Deals", name: "Deals", destinationDomains: [], backgroundColor: nil, localeDetails: [.init(channels: ["Brave"], locale: "en_US")])
let dealsItems: [FeedItem] = (1...3).map {
.init(score: Double($0), content: .init(publishTime: Date(), title: "", description: "", contentType: .deals, publisherID: dealsSource.id, urlHash: "1", baseScore: Double($0), offersCategory: "Deal"), source: dealsSource)
}
let generator = FeedCardGenerator(
scoredItems: dealsItems,
sequence: sequence,
followedSources: [dealsSource.id],
hiddenSources: [],
followedChannels: [:],
ads: nil
)
let cards = try await generator.allCards
XCTAssertEqual(cards.count, 1)
if case .deals(let items, _) = cards.first {
XCTAssertEqual(items, dealsItems)
} else {
XCTFail()
}
}
/// Tests that feed items are scored properly when following a source that is also included in channel
func testScoringFromSourcesAndChannelFollow() async throws {
let sequence: [FeedSequenceElement] = [
.repeating([
.headline(paired: false),
]),
]
let source = FeedItem.Source(id: "1", isDefault: false, category: "Top News", name: "Source 1", destinationDomains: [], localeDetails: [.init(channels: ["Top Sources"], locale: "en_US")])
let source2 = FeedItem.Source(id: "2", isDefault: false, category: "Top News", name: "Source 2", destinationDomains: [], localeDetails: [.init(channels: ["Top Sources"], locale: "en_US")])
let items = [
FeedItem(score: 10, content: .testArticle(sourceID: source.id, contentID: "a", score: 10), source: source),
FeedItem(score: 4, content: .testArticle(sourceID: source.id, contentID: "b", score: 4), source: source),
FeedItem(score: 2, content: .testArticle(sourceID: source.id, contentID: "c", score: 2), source: source),
FeedItem(score: 44, content: .testArticle(sourceID: source.id, contentID: "d", score: 44), source: source),
FeedItem(score: 1, content: .testArticle(sourceID: source2.id, contentID: "e", score: 1), source: source2),
]
let expectedItems = items.sorted(by: { $0.score < $1.score })
let generator = FeedCardGenerator(
scoredItems: items,
sequence: sequence,
followedSources: ["1"], // Following source 1 directly
hiddenSources: [],
followedChannels: ["en_US": ["Top Sources"]], // Following source 2 indirectly
ads: nil
)
let cards = try await generator.allCards
XCTAssertEqual(cards.count, items.count)
XCTAssertEqual(cards, expectedItems.map({ .headline($0) }))
}
}
extension FeedItem.Content {
fileprivate static func testArticle(sourceID: String, contentID: String, score: Double, containsImage: Bool = true) -> Self {
.init(
publishTime: Date(),
imageURL: containsImage ? URL(string: "image") : nil,
title: "",
description: "",
contentType: .article,
publisherID: sourceID,
urlHash: contentID,
baseScore: score
)
}
}