fix "Replying to..." issues and improve related tests

This commit is contained in:
Bryan Montz
2023-03-14 12:08:18 -05:00
committed by William Casarin
parent dd511c3061
commit cabe584938
3 changed files with 46 additions and 49 deletions

View File

@@ -144,8 +144,13 @@ struct Profile: Codable {
if pubkey == "anon" { if pubkey == "anon" {
return "Anonymous" return "Anonymous"
} }
let pk = bech32_nopre_pubkey(pubkey) ?? pubkey
return profile?.name ?? abbrev_pubkey(pk) if let name = profile?.name, !name.isEmpty {
return name
} else {
let pk = bech32_nopre_pubkey(pubkey) ?? pubkey
return abbrev_pubkey(pk)
}
} }
} }

View File

@@ -42,16 +42,18 @@ func reply_desc(profiles: Profiles, event: NostrEvent, locale: Locale = Locale.c
return Profile.displayName(profile: prof, pubkey: $0) return Profile.displayName(profile: prof, pubkey: $0)
} }
if names.count > 1 { let uniqueNames = NSOrderedSet(array: names).array as! [String]
if uniqueNames.count > 1 {
let othersCount = n - pubkeys.count let othersCount = n - pubkeys.count
if othersCount == 0 { if othersCount == 0 {
return String(format: NSLocalizedString("Replying to %@ & %@", bundle: bundle, comment: "Label to indicate that the user is replying to 2 users."), locale: locale, names[0], names[1]) return String(format: NSLocalizedString("Replying to %@ & %@", bundle: bundle, comment: "Label to indicate that the user is replying to 2 users."), locale: locale, uniqueNames[0], uniqueNames[1])
} else { } else {
return String(format: bundle.localizedString(forKey: "replying_to_two_and_others", value: nil, table: nil), locale: locale, othersCount, names[0], names[1]) return String(format: bundle.localizedString(forKey: "replying_to_two_and_others", value: nil, table: nil), locale: locale, othersCount, uniqueNames[0], uniqueNames[1])
} }
} }
return String(format: NSLocalizedString("Replying to %@", bundle: bundle, comment: "Label to indicate that the user is replying to 1 user."), locale: locale, names[0]) return String(format: NSLocalizedString("Replying to %@", bundle: bundle, comment: "Label to indicate that the user is replying to 1 user."), locale: locale, uniqueNames[0])
} }

View File

@@ -11,57 +11,47 @@ import XCTest
final class ReplyDescriptionTests: XCTestCase { final class ReplyDescriptionTests: XCTestCase {
let enUsLocale = Locale(identifier: "en-US") let enUsLocale = Locale(identifier: "en-US")
let profiles = test_damus_state().profiles
override func setUpWithError() throws { private func descriptionForEvent(withTags tags: [[String]]) -> String {
// Put setup code here. This method is called before the invocation of each test method in the class. var allTags = [["e", "123"]]
} allTags.append(contentsOf: tags)
let replyingToOne = NostrEvent(
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool",
pubkey: "pk",
tags: allTags,
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
override func tearDownWithError() throws { Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
// Put teardown code here. This method is called after the invocation of each test method in the class. XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToOne, locale: $0))
}
return reply_desc(profiles: profiles, event: replyingToOne, locale: enUsLocale)
} }
// Test that English strings work properly with argument substitution and pluralization, and that other locales don't crash. // Test that English strings work properly with argument substitution and pluralization, and that other locales don't crash.
func testReplyDesc() throws { func testReplyDesc() throws {
let profiles = test_damus_state().profiles
let replyingToSelfEvent = test_event let replyingToSelfEvent = test_event
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToSelfEvent, locale: enUsLocale), "Replying to self") XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToSelfEvent, locale: enUsLocale), "Replying to self")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach { Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToSelfEvent, locale: $0)) XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToSelfEvent, locale: $0))
} }
let replyingToOne = NostrEvent( // replying to one
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool", XCTAssertEqual(descriptionForEvent(withTags: [["p", "123"]]),
pubkey: "pk", "Replying to \(Profile.displayName(profile: nil, pubkey: "123"))")
tags: [["e", "123"], ["p", "123"]],
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToOne, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "123"))")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToOne, locale: $0))
}
let replyingToTwo = NostrEvent( // replying to two
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool", XCTAssertEqual(descriptionForEvent(withTags: [["p", "123"], ["p", "456"]]),
pubkey: "pk", "Replying to \(Profile.displayName(profile: nil, pubkey: "456")) & \(Profile.displayName(profile: nil, pubkey: "123"))")
tags: [["e", "123"], ["p", "123"], ["p", "456"]],
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToTwo, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "456")) & \(Profile.displayName(profile: nil, pubkey: "123"))")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToTwo, locale: $0))
}
let replyingToTwoAndOneOther = NostrEvent( // replying to two that are the same
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool", XCTAssertEqual(descriptionForEvent(withTags: [["p", "123"], ["p", "123"]]),
pubkey: "pk", "Replying to \(Profile.displayName(profile: nil, pubkey: "123"))")
tags: [["e", "123"], ["p", "123"], ["p", "456"], ["p", "789"]],
createdAt: Int64(Date().timeIntervalSince1970 - 100) // replying to two and one other
) XCTAssertEqual(descriptionForEvent(withTags: [["p", "123"], ["p", "456"], ["p", "789"]]),
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToTwoAndOneOther, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "789")), \(Profile.displayName(profile: nil, pubkey: "456")) & 1 other") "Replying to \(Profile.displayName(profile: nil, pubkey: "789")), \(Profile.displayName(profile: nil, pubkey: "456")) & 1 other")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToTwoAndOneOther, locale: $0))
}
for othersCount in 2...10 { for othersCount in 2...10 {
var tags: [[String]] = [["e", "123"]] var tags: [[String]] = [["e", "123"]]