damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 69c7acea76c3b931d404148ff2779278587a198c
parent 22d635d85046ac8f2ed50f2c562ef456e0b9b25f
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 10 Sep 2023 15:50:27 -0700

tests: add ndb support to tests

stops it from crashing

Diffstat:
Mdamus/Components/ImageCarousel.swift | 2+-
Mdamus/Components/InvoiceView.swift | 2+-
Mdamus/Components/InvoicesView.swift | 2+-
Mdamus/Components/NIP05Badge.swift | 2+-
Mdamus/Components/Reposted.swift | 2+-
Mdamus/Components/Search/SearchHeaderView.swift | 4++--
Mdamus/Components/Status/UserStatusSheet.swift | 2+-
Mdamus/Components/TranslateView.swift | 2+-
Mdamus/Components/UserView.swift | 2+-
Mdamus/Components/ZapButton.swift | 2+-
Mdamus/TestData.swift | 54++++++++++++++++++++++++++++++++++++++++++++++++------
Mdamus/Views/ActionBar/EventActionBar.swift | 2+-
Mdamus/Views/ActionBar/EventDetailBar.swift | 2+-
Mdamus/Views/ActionBar/RepostAction.swift | 2+-
Mdamus/Views/AddRelayView.swift | 2+-
Mdamus/Views/ConfigView.swift | 2+-
Mdamus/Views/DMChatView.swift | 2+-
Mdamus/Views/DMView.swift | 2+-
Mdamus/Views/DirectMessagesView.swift | 2+-
Mdamus/Views/EventDetailView.swift | 2+-
Mdamus/Views/EventView.swift | 4++--
Mdamus/Views/Events/BuilderEventView.swift | 2+-
Mdamus/Views/Events/Components/EventTop.swift | 2+-
Mdamus/Views/Events/Components/ReplyDescription.swift | 2+-
Mdamus/Views/Events/Components/ReplyPart.swift | 2+-
Mdamus/Views/Events/EventBody.swift | 2+-
Mdamus/Views/Events/EventProfile.swift | 2+-
Mdamus/Views/Events/EventShell.swift | 4++--
Mdamus/Views/Events/Longform/LongformPreview.swift | 4++--
Mdamus/Views/Events/Longform/LongformView.swift | 2+-
Mdamus/Views/Events/MutedEventView.swift | 2+-
Mdamus/Views/Events/SelectedEventView.swift | 2+-
Mdamus/Views/Events/TextEvent.swift | 4++--
Mdamus/Views/Events/ZapEvent.swift | 4++--
Mdamus/Views/Images/ImageContainerView.swift | 2+-
Mdamus/Views/Images/ImageView.swift | 2+-
Mdamus/Views/Muting/MutelistView.swift | 2+-
Mdamus/Views/NoteContentView.swift | 4++--
Mdamus/Views/Notifications/EventGroupView.swift | 9+++++----
Mdamus/Views/Notifications/NotificationItemView.swift | 2+-
Mdamus/Views/Notifications/NotificationsView.swift | 2+-
Mdamus/Views/Notifications/ProfilePicturesView.swift | 2+-
Mdamus/Views/Onboarding/SuggestedUserView.swift | 2+-
Mdamus/Views/Onboarding/SuggestedUsersView.swift | 2+-
Mdamus/Views/PostView.swift | 2+-
Mdamus/Views/Posting/UserSearch.swift | 2+-
Mdamus/Views/Profile/CondensedProfilePicturesView.swift | 2+-
Mdamus/Views/Profile/EditMetadataView.swift | 2+-
Mdamus/Views/Profile/EventProfileName.swift | 2+-
Mdamus/Views/Profile/MaybeAnonPfpView.swift | 2+-
Mdamus/Views/Profile/ProfileEditButton.swift | 2+-
Mdamus/Views/Profile/ProfileName.swift | 2+-
Mdamus/Views/Profile/ProfileNameView.swift | 4++--
Mdamus/Views/Profile/ProfilePicView.swift | 2+-
Mdamus/Views/Profile/ProfileView.swift | 2+-
Mdamus/Views/QRCodeView.swift | 2+-
Mdamus/Views/Reactions/ReactionView.swift | 2+-
Mdamus/Views/ReactionsView.swift | 2+-
Mdamus/Views/RelayFilterView.swift | 2+-
Mdamus/Views/Relays/RecommendedRelayView.swift | 2+-
Mdamus/Views/Relays/RelayConfigView.swift | 2+-
Mdamus/Views/Relays/RelayDetailView.swift | 2+-
Mdamus/Views/Relays/RelayStatusView.swift | 2+-
Mdamus/Views/Relays/RelayToggle.swift | 2+-
Mdamus/Views/Relays/RelayView.swift | 2+-
Mdamus/Views/Relays/SignalView.swift | 2+-
Mdamus/Views/ReplyView.swift | 4++--
Mdamus/Views/ReportView.swift | 2+-
Mdamus/Views/Reposts/RepostView.swift | 2+-
Mdamus/Views/Reposts/RepostedEvent.swift | 2+-
Mdamus/Views/RepostsView.swift | 2+-
Mdamus/Views/Search/SearchingEventView.swift | 2+-
Mdamus/Views/SearchHomeView.swift | 2+-
Mdamus/Views/SearchView.swift | 2+-
Mdamus/Views/Settings/AppearanceSettingsView.swift | 2+-
Mdamus/Views/SideMenuView.swift | 2+-
Mdamus/Views/ThreadView.swift | 2+-
Mdamus/Views/Timeline/InnerTimelineView.swift | 2+-
Mdamus/Views/UserRelaysView.swift | 2+-
Mdamus/Views/Wallet/WalletView.swift | 4++--
Mdamus/Views/Zaps/CustomizeZapView.swift | 2+-
Mdamus/Views/Zaps/ZapTypePicker.swift | 2+-
Mdamus/Views/Zaps/ZapUserView.swift | 2+-
Mdamus/Views/Zaps/ZapsView.swift | 2+-
MdamusTests/EventGroupViewTests.swift | 6+++---
MdamusTests/LongPostTests.swift | 2+-
MdamusTests/NoteContentViewTests.swift | 2+-
MdamusTests/ProfileViewTests.swift | 17+++++++++--------
MdamusTests/UserSearchCacheTests.swift | 7+------
MdamusTests/ZapTests.swift | 2+-
Mnostrdb/Ndb.swift | 14+++++++++++---
Mnostrdb/Test/NdbTests.swift | 31+++++++++++++++++++++++--------
92 files changed, 195 insertions(+), 133 deletions(-)

diff --git a/damus/Components/ImageCarousel.swift b/damus/Components/ImageCarousel.swift @@ -285,7 +285,7 @@ public struct ImageFill { struct ImageCarousel_Previews: PreviewProvider { static var previews: some View { let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!) - ImageCarousel(state: test_damus_state(), evid: test_note.id, urls: [url, url]) + ImageCarousel(state: test_damus_state, evid: test_note.id, urls: [url, url]) } } diff --git a/damus/Components/InvoiceView.swift b/damus/Components/InvoiceView.swift @@ -108,7 +108,7 @@ let test_invoice = Invoice(description: .description("this is a description"), a struct InvoiceView_Previews: PreviewProvider { static var previews: some View { - InvoiceView(our_pubkey: .empty, invoice: test_invoice, settings: test_damus_state().settings) + InvoiceView(our_pubkey: .empty, invoice: test_invoice, settings: test_damus_state.settings) .frame(width: 300, height: 200) } } diff --git a/damus/Components/InvoicesView.swift b/damus/Components/InvoicesView.swift @@ -29,7 +29,7 @@ struct InvoicesView: View { struct InvoicesView_Previews: PreviewProvider { static var previews: some View { - InvoicesView(our_pubkey: test_note.pubkey, invoices: [Invoice.init(description: .description("description"), amount: .specific(10000), string: "invstr", expiry: 100000, payment_hash: Data(), created_at: 1000000)], settings: test_damus_state().settings) + InvoicesView(our_pubkey: test_note.pubkey, invoices: [Invoice.init(description: .description("description"), amount: .specific(10000), string: "invstr", expiry: 100000, payment_hash: Data(), created_at: 1000000)], settings: test_damus_state.settings) .frame(width: 300) } } diff --git a/damus/Components/NIP05Badge.swift b/damus/Components/NIP05Badge.swift @@ -96,7 +96,7 @@ func use_nip05_color(pubkey: Pubkey, contacts: Contacts) -> Bool { struct NIP05Badge_Previews: PreviewProvider { static var previews: some View { - let test_state = test_damus_state() + let test_state = test_damus_state VStack { NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: test_state.contacts, show_domain: true, profiles: test_state.profiles) diff --git a/damus/Components/Reposted.swift b/damus/Components/Reposted.swift @@ -25,7 +25,7 @@ struct Reposted: View { struct Reposted_Previews: PreviewProvider { static var previews: some View { - let test_state = test_damus_state() + let test_state = test_damus_state Reposted(damus: test_state, pubkey: test_state.pubkey) } } diff --git a/damus/Components/Search/SearchHeaderView.swift b/damus/Components/Search/SearchHeaderView.swift @@ -168,9 +168,9 @@ func is_following_hashtag(contacts: NostrEvent?, hashtag: String) -> Bool { struct SearchHeaderView_Previews: PreviewProvider { static var previews: some View { VStack(alignment: .leading) { - SearchHeaderView(state: test_damus_state(), described: .hashtag("damus")) + SearchHeaderView(state: test_damus_state, described: .hashtag("damus")) - SearchHeaderView(state: test_damus_state(), described: .unknown) + SearchHeaderView(state: test_damus_state, described: .unknown) } } } diff --git a/damus/Components/Status/UserStatusSheet.swift b/damus/Components/Status/UserStatusSheet.swift @@ -136,6 +136,6 @@ struct UserStatusSheet: View { struct UserStatusSheet_Previews: PreviewProvider { static var previews: some View { - UserStatusSheet(postbox: PostBox(pool: RelayPool(ndb: .empty)), keypair: Keypair(pubkey: .empty, privkey: nil), status: .init()) + UserStatusSheet(postbox: test_damus_state.postbox, keypair: test_keypair, status: .init()) } } diff --git a/damus/Components/TranslateView.swift b/damus/Components/TranslateView.swift @@ -120,7 +120,7 @@ extension View { struct TranslateView_Previews: PreviewProvider { static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state TranslateView(damus_state: ds, event: test_note, size: .normal) } } diff --git a/damus/Components/UserView.swift b/damus/Components/UserView.swift @@ -55,6 +55,6 @@ struct UserView: View { struct UserView_Previews: PreviewProvider { static var previews: some View { - UserView(damus_state: test_damus_state(), pubkey: test_note.pubkey) + UserView(damus_state: test_damus_state, pubkey: test_note.pubkey) } } diff --git a/damus/Components/ZapButton.swift b/damus/Components/ZapButton.swift @@ -144,7 +144,7 @@ struct ZapButton_Previews: PreviewProvider { let pending_zap = PendingZap(amount_msat: 1000, target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), request: .normal(test_zap_request), type: .pub, state: .external(.init(state: .fetching_invoice))) let zaps = ZapsDataModel([.pending(pending_zap)]) - ZapButton(damus_state: test_damus_state(), target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), lnurl: "lnurl", zaps: zaps) + ZapButton(damus_state: test_damus_state, target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), lnurl: "lnurl", zaps: zaps) } } diff --git a/damus/TestData.swift b/damus/TestData.swift @@ -48,11 +48,53 @@ let test_private_zap = Zap(event: test_note, invoice: test_zap_invoice, zapper: let test_pending_zap = PendingZap(amount_msat: 10000, target: .note(id: test_note.id, author: test_note.pubkey), request: .normal(test_zap_request), type: .pub, state: .external(.init(state: .fetching_invoice))) -let test_following_model = FollowingModel(damus_state: test_damus_state(), contacts: [test_pubkey, test_pubkey_2], hashtags: [Hashtag(hashtag: "grownostr"), Hashtag(hashtag: "zapathon")]) - - -func test_damus_state() -> DamusState { - let damus = DamusState.empty +let test_following_model = FollowingModel(damus_state: test_damus_state, contacts: [test_pubkey, test_pubkey_2], hashtags: [Hashtag(hashtag: "grownostr"), Hashtag(hashtag: "zapathon")]) + + +var test_damus_state: DamusState = ({ + // Create a unique temporary directory + var tempDir: String! + do { + let fileManager = FileManager.default + let temp = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try fileManager.createDirectory(at: temp, withIntermediateDirectories: true, attributes: nil) + tempDir = temp.absoluteString + } catch { + tempDir = "." + } + + print("opening \(tempDir!)") + let ndb = Ndb(path: tempDir)! + let our_pubkey = test_pubkey + let user_search_cache = UserSearchCache() + let pool = RelayPool(ndb: ndb) + let settings = UserSettingsStore() + let damus = DamusState(pool: pool, + keypair: test_keypair, + likes: .init(our_pubkey: our_pubkey), + boosts: .init(our_pubkey: our_pubkey), + contacts: .init(our_pubkey: our_pubkey), + profiles: .init(user_search_cache: user_search_cache, ndb: ndb), + dms: .init(our_pubkey: our_pubkey), + previews: .init(), + zaps: .init(our_pubkey: our_pubkey), + lnurls: .init(), + settings: settings, + relay_filters: .init(our_pubkey: our_pubkey), + relay_model_cache: .init(), + drafts: .init(), + events: .init(ndb: ndb), + bookmarks: .init(pubkey: our_pubkey), + postbox: .init(pool: pool), + bootstrap_relays: .init(), + replies: .init(our_pubkey: our_pubkey), + muted_threads: .init(keypair: test_keypair), + wallet: .init(settings: settings), + nav: .init(), + user_search_cache: user_search_cache, + music: .init(onChange: {_ in }), + video: .init(), + ndb: ndb) /* let prof = Profile(name: "damus", display_name: "damus", about: "iOS app!", picture: "https://damus.io/img/logo.png", banner: "", website: "https://damus.io", lud06: nil, lud16: "jb55@sendsats.lol", nip05: "damus.io", damus_donation: nil) @@ -60,7 +102,7 @@ func test_damus_state() -> DamusState { damus.profiles.add(id: test_pubkey, profile: tsprof) */ return damus -} +})() let test_wire_events = """ ["EVENT","s",{"id":"d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349","pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","created_at":1650049978,"kind":0,"tags":[],"content":"{\\"name\\\":\\"jb55\\",\\"picture\\":\\\"http://cdn.jb55.com/img/red-me.jpg\\",\\"about\\":\\"bitcoin, lightning and nostr dev\\",\\"nip05\\":\\"jb55.com\\"}","sig":"1315045da793c4825de1517149172bf35a6da39d91b7787afb3263721e07bc816cb898996ed8d69af05d6efcd1c926a089bd66cad870bcc361405c11ba302c51"}] diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift @@ -374,7 +374,7 @@ struct LikeButton: View { struct EventActionBar_Previews: PreviewProvider { static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state let ev = NostrEvent(content: "hi", keypair: test_keypair)! let bar = ActionBarModel.empty() diff --git a/damus/Views/ActionBar/EventDetailBar.swift b/damus/Views/ActionBar/EventDetailBar.swift @@ -56,6 +56,6 @@ struct EventDetailBar: View { struct EventDetailBar_Previews: PreviewProvider { static var previews: some View { - EventDetailBar(state: test_damus_state(), target: .empty, target_pk: .empty) + EventDetailBar(state: test_damus_state, target: .empty, target_pk: .empty) } } diff --git a/damus/Views/ActionBar/RepostAction.swift b/damus/Views/ActionBar/RepostAction.swift @@ -61,6 +61,6 @@ struct RepostAction: View { struct RepostAction_Previews: PreviewProvider { static var previews: some View { - RepostAction(damus_state: test_damus_state(), event: test_note) + RepostAction(damus_state: test_damus_state, event: test_note) } } diff --git a/damus/Views/AddRelayView.swift b/damus/Views/AddRelayView.swift @@ -158,6 +158,6 @@ struct AddRelayView_Previews: PreviewProvider { @State static var relay: String = "" static var previews: some View { - AddRelayView(state: test_damus_state()) + AddRelayView(state: test_damus_state) } } diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift @@ -153,7 +153,7 @@ struct ConfigView: View { struct ConfigView_Previews: PreviewProvider { static var previews: some View { NavigationView { - ConfigView(state: test_damus_state()) + ConfigView(state: test_damus_state) } } } diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift @@ -182,7 +182,7 @@ struct DMChatView_Previews: PreviewProvider { let model = DirectMessageModel(events: [ev], our_pubkey: test_pubkey, pubkey: test_pubkey) - DMChatView(damus_state: test_damus_state(), dms: model) + DMChatView(damus_state: test_damus_state, dms: model) } } diff --git a/damus/Views/DMView.swift b/damus/Views/DMView.swift @@ -76,6 +76,6 @@ struct DMView: View { struct DMView_Previews: PreviewProvider { static var previews: some View { let ev = NostrEvent(content: "Hey there *buddy*, want to grab some drinks later? 🍻", keypair: test_keypair, kind: 1, tags: [])! - DMView(event: ev, damus_state: test_damus_state()) + DMView(event: ev, damus_state: test_damus_state) } } diff --git a/damus/Views/DirectMessagesView.swift b/damus/Views/DirectMessagesView.swift @@ -107,7 +107,7 @@ func would_filter_non_friends_from_dms(contacts: Contacts, dms: [DirectMessageMo struct DirectMessagesView_Previews: PreviewProvider { static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state DirectMessagesView(damus_state: ds, model: ds.dms, settings: ds.settings) } } diff --git a/damus/Views/EventDetailView.swift b/damus/Views/EventDetailView.swift @@ -16,7 +16,7 @@ struct EventDetailView: View { struct EventDetailView_Previews: PreviewProvider { static var previews: some View { - let _ = test_damus_state() + let _ = test_damus_state EventDetailView() } } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift @@ -131,9 +131,9 @@ struct EventView_Previews: PreviewProvider { */ - EventView( damus: test_damus_state(), event: test_note ) + EventView( damus: test_damus_state, event: test_note ) - EventView( damus: test_damus_state(), event: test_longform_event.event, options: [.wide] ) + EventView( damus: test_damus_state, event: test_longform_event.event, options: [.wide] ) } .padding() } diff --git a/damus/Views/Events/BuilderEventView.swift b/damus/Views/Events/BuilderEventView.swift @@ -93,7 +93,7 @@ struct BuilderEventView: View { struct BuilderEventView_Previews: PreviewProvider { static var previews: some View { - BuilderEventView(damus: test_damus_state(), event_id: test_note.id) + BuilderEventView(damus: test_damus_state, event_id: test_note.id) } } diff --git a/damus/Views/Events/Components/EventTop.swift b/damus/Views/Events/Components/EventTop.swift @@ -40,6 +40,6 @@ struct EventTop: View { struct EventTop_Previews: PreviewProvider { static var previews: some View { - EventTop(state: test_damus_state(), event: test_note, pubkey: test_note.pubkey, is_anon: false) + EventTop(state: test_damus_state, event: test_note, pubkey: test_note.pubkey, is_anon: false) } } diff --git a/damus/Views/Events/Components/ReplyDescription.swift b/damus/Views/Events/Components/ReplyDescription.swift @@ -23,7 +23,7 @@ struct ReplyDescription: View { struct ReplyDescription_Previews: PreviewProvider { static var previews: some View { - ReplyDescription(event: test_note, replying_to: test_note, ndb: test_damus_state().ndb) + ReplyDescription(event: test_note, replying_to: test_note, ndb: test_damus_state.ndb) } } diff --git a/damus/Views/Events/Components/ReplyPart.swift b/damus/Views/Events/Components/ReplyPart.swift @@ -34,6 +34,6 @@ struct ReplyPart: View { struct ReplyPart_Previews: PreviewProvider { static var previews: some View { - ReplyPart(events: test_damus_state().events, event: test_note, keypair: Keypair(pubkey: .empty, privkey: nil), ndb: test_damus_state().ndb) + ReplyPart(events: test_damus_state.events, event: test_note, keypair: Keypair(pubkey: .empty, privkey: nil), ndb: test_damus_state.ndb) } } diff --git a/damus/Views/Events/EventBody.swift b/damus/Views/Events/EventBody.swift @@ -43,6 +43,6 @@ struct EventBody: View { struct EventBody_Previews: PreviewProvider { static var previews: some View { - EventBody(damus_state: test_damus_state(), event: test_note, size: .normal, options: []) + EventBody(damus_state: test_damus_state, event: test_note, size: .normal, options: []) } } diff --git a/damus/Views/Events/EventProfile.swift b/damus/Views/Events/EventProfile.swift @@ -53,6 +53,6 @@ struct EventProfile: View { struct EventProfile_Previews: PreviewProvider { static var previews: some View { - EventProfile(damus_state: test_damus_state(), pubkey: test_note.pubkey, size: .normal) + EventProfile(damus_state: test_damus_state, pubkey: test_note.pubkey, size: .normal) } } diff --git a/damus/Views/Events/EventShell.swift b/damus/Views/Events/EventShell.swift @@ -137,11 +137,11 @@ struct EventShell_Previews: PreviewProvider { static var previews: some View { VStack { - EventShell(state: test_damus_state(), event: test_note, options: [.no_action_bar]) { + EventShell(state: test_damus_state, event: test_note, options: [.no_action_bar]) { Text(verbatim: "Hello") } - EventShell(state: test_damus_state(), event: test_note, options: [.no_action_bar, .wide]) { + EventShell(state: test_damus_state, event: test_note, options: [.no_action_bar, .wide]) { Text(verbatim: "Hello") } } diff --git a/damus/Views/Events/Longform/LongformPreview.swift b/damus/Views/Events/Longform/LongformPreview.swift @@ -87,9 +87,9 @@ struct LongformPreview: View { struct LongformPreview_Previews: PreviewProvider { static var previews: some View { VStack { - LongformPreview(state: test_damus_state(), ev: test_longform_event.event, options: []) + LongformPreview(state: test_damus_state, ev: test_longform_event.event, options: []) - LongformPreview(state: test_damus_state(), ev: test_longform_event.event, options: [.wide]) + LongformPreview(state: test_damus_state, ev: test_longform_event.event, options: [.wide]) } .frame(height: 400) } diff --git a/damus/Views/Events/Longform/LongformView.swift b/damus/Views/Events/Longform/LongformView.swift @@ -75,7 +75,7 @@ let test_longform_event = LongformEvent.parse(from: NostrEvent( struct LongformView_Previews: PreviewProvider { static var previews: some View { - let st = test_damus_state() + let st = test_damus_state let artifacts = render_note_content(ev: test_longform_event.event, profiles: st.profiles, keypair: Keypair(pubkey: .empty, privkey: nil)) let model = NoteArtifactsModel(state: .loaded(artifacts)) diff --git a/damus/Views/Events/MutedEventView.swift b/damus/Views/Events/MutedEventView.swift @@ -77,7 +77,7 @@ struct MutedEventView_Previews: PreviewProvider { static var previews: some View { - MutedEventView(damus_state: test_damus_state(), event: test_note, selected: false) + MutedEventView(damus_state: test_damus_state, event: test_note, selected: false) .frame(width: .infinity, height: 50) } } diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift @@ -92,6 +92,6 @@ struct SelectedEventView: View { struct SelectedEventView_Previews: PreviewProvider { static var previews: some View { - SelectedEventView(damus: test_damus_state(), event: test_note, size: .selected) + SelectedEventView(damus: test_damus_state, event: test_note, size: .selected) } } diff --git a/damus/Views/Events/TextEvent.swift b/damus/Views/Events/TextEvent.swift @@ -75,10 +75,10 @@ func event_is_anonymous(ev: NostrEvent) -> Bool { struct TextEvent_Previews: PreviewProvider { static var previews: some View { VStack(spacing: 20) { - TextEvent(damus: test_damus_state(), event: test_note, pubkey: test_pubkey, options: []) + TextEvent(damus: test_damus_state, event: test_note, pubkey: test_pubkey, options: []) .frame(height: 400) - TextEvent(damus: test_damus_state(), event: test_note, pubkey: test_pubkey, options: [.wide]) + TextEvent(damus: test_damus_state, event: test_note, pubkey: test_pubkey, options: [.wide]) .frame(height: 400) } } diff --git a/damus/Views/Events/ZapEvent.swift b/damus/Views/Events/ZapEvent.swift @@ -51,9 +51,9 @@ struct ZapEvent: View { struct ZapEvent_Previews: PreviewProvider { static var previews: some View { VStack { - ZapEvent(damus: test_damus_state(), zap: .zap(test_zap), is_top_zap: true) + ZapEvent(damus: test_damus_state, zap: .zap(test_zap), is_top_zap: true) - ZapEvent(damus: test_damus_state(), zap: .zap(test_private_zap), is_top_zap: false) + ZapEvent(damus: test_damus_state, zap: .zap(test_private_zap), is_top_zap: false) } } } diff --git a/damus/Views/Images/ImageContainerView.swift b/damus/Views/Images/ImageContainerView.swift @@ -57,6 +57,6 @@ let test_image_url = URL(string: "https://jb55.com/red-me.jpg")! struct ImageContainerView_Previews: PreviewProvider { static var previews: some View { - ImageContainerView(video_controller: test_damus_state().video, url: .image(test_image_url), disable_animation: false) + ImageContainerView(video_controller: test_damus_state.video, url: .image(test_image_url), disable_animation: false) } } diff --git a/damus/Views/Images/ImageView.swift b/damus/Views/Images/ImageView.swift @@ -85,6 +85,6 @@ struct ImageView: View { struct ImageView_Previews: PreviewProvider { static var previews: some View { let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!) - ImageView(video_controller: test_damus_state().video, urls: [url], disable_animation: false) + ImageView(video_controller: test_damus_state.video, urls: [url], disable_animation: false) } } diff --git a/damus/Views/Muting/MutelistView.swift b/damus/Views/Muting/MutelistView.swift @@ -58,6 +58,6 @@ func get_mutelist_users(_ mutelist: NostrEvent?) -> Array<Pubkey> { struct MutelistView_Previews: PreviewProvider { static var previews: some View { - MutelistView(damus_state: test_damus_state(), users: [test_note.pubkey, test_note.pubkey]) + MutelistView(damus_state: test_damus_state, users: [test_note.pubkey, test_note.pubkey]) } } diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift @@ -613,8 +613,8 @@ func trim_prefix(_ str: String) -> String { struct NoteContentView_Previews: PreviewProvider { static var previews: some View { - let state = test_damus_state() - let state2 = test_damus_state() + let state = test_damus_state + let state2 = test_damus_state Group { VStack { diff --git a/damus/Views/Notifications/EventGroupView.swift b/damus/Views/Notifications/EventGroupView.swift @@ -69,8 +69,9 @@ func determine_reacting_to(our_pubkey: Pubkey, ev: NostrEvent?) -> ReactingTo { } func event_author_name(profiles: Profiles, pubkey: Pubkey) -> String { - let alice_prof_txn = profiles.lookup(id: pubkey).unsafeUnownedValue - return Profile.displayName(profile: alice_prof_txn, pubkey: pubkey).username.truncate(maxLength: 50) + return profiles.lookup(id: pubkey).map({ profile in + Profile.displayName(profile: profile, pubkey: pubkey).username.truncate(maxLength: 50) + }).value } func event_group_unique_pubkeys(profiles: Profiles, group: EventGroupType) -> [Pubkey] { @@ -261,11 +262,11 @@ struct EventGroupView: View { struct EventGroupView_Previews: PreviewProvider { static var previews: some View { VStack { - EventGroupView(state: test_damus_state(), event: test_note, group: .repost(test_event_group)) + EventGroupView(state: test_damus_state, event: test_note, group: .repost(test_event_group)) .frame(height: 200) .padding() - EventGroupView(state: test_damus_state(), event: test_note, group: .reaction(test_event_group)) + EventGroupView(state: test_damus_state, event: test_note, group: .reaction(test_event_group)) .frame(height: 200) .padding() } diff --git a/damus/Views/Notifications/NotificationItemView.swift b/damus/Views/Notifications/NotificationItemView.swift @@ -88,6 +88,6 @@ let test_notification_item: NotificationItem = .repost(test_note.id, test_event_ struct NotificationItemView_Previews: PreviewProvider { static var previews: some View { - NotificationItemView(state: test_damus_state(), item: test_notification_item) + NotificationItemView(state: test_damus_state, item: test_notification_item) } } diff --git a/damus/Views/Notifications/NotificationsView.swift b/damus/Views/Notifications/NotificationsView.swift @@ -195,7 +195,7 @@ struct NotificationsView: View { struct NotificationsView_Previews: PreviewProvider { static var previews: some View { - NotificationsView(state: test_damus_state(), notifications: NotificationsModel(), filter: NotificationFilter()) + NotificationsView(state: test_damus_state, notifications: NotificationsModel(), filter: NotificationFilter()) } } diff --git a/damus/Views/Notifications/ProfilePicturesView.swift b/damus/Views/Notifications/ProfilePicturesView.swift @@ -26,6 +26,6 @@ struct ProfilePicturesView: View { struct ProfilePicturesView_Previews: PreviewProvider { static var previews: some View { let pubkey = test_note.pubkey - ProfilePicturesView(state: test_damus_state(), pubkeys: [pubkey, pubkey]) + ProfilePicturesView(state: test_damus_state, pubkeys: [pubkey, pubkey]) } } diff --git a/damus/Views/Onboarding/SuggestedUserView.swift b/damus/Views/Onboarding/SuggestedUserView.swift @@ -61,7 +61,7 @@ struct SuggestedUserView_Previews: PreviewProvider { let user = SuggestedUser(name: "klabo", about: "name", picture: "about", pubkey: test_pubkey)! List { - SuggestedUserView(user: user, damus_state: test_damus_state()) + SuggestedUserView(user: user, damus_state: test_damus_state) } } } diff --git a/damus/Views/Onboarding/SuggestedUsersView.swift b/damus/Views/Onboarding/SuggestedUsersView.swift @@ -72,6 +72,6 @@ struct SuggestedUsersSectionHeader: View { struct SuggestedUsersView_Previews: PreviewProvider { static var previews: some View { - SuggestedUsersView(model: SuggestedUsersViewModel(damus_state: test_damus_state())) + SuggestedUsersView(model: SuggestedUsersViewModel(damus_state: test_damus_state)) } } diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -511,7 +511,7 @@ func get_searching_string(_ word: String?) -> String? { struct PostView_Previews: PreviewProvider { static var previews: some View { - PostView(action: .posting(.none), damus_state: test_damus_state()) + PostView(action: .posting(.none), damus_state: test_damus_state) } } diff --git a/damus/Views/Posting/UserSearch.swift b/damus/Views/Posting/UserSearch.swift @@ -72,7 +72,7 @@ struct UserSearch_Previews: PreviewProvider { @State static var newCursorIndex: Int? static var previews: some View { - UserSearch(damus_state: test_damus_state(), search: search, focusWordAttributes: $word, newCursorIndex: $newCursorIndex, post: $post) + UserSearch(damus_state: test_damus_state, search: search, focusWordAttributes: $word, newCursorIndex: $newCursorIndex, post: $post) } } diff --git a/damus/Views/Profile/CondensedProfilePicturesView.swift b/damus/Views/Profile/CondensedProfilePicturesView.swift @@ -33,6 +33,6 @@ struct CondensedProfilePicturesView: View { struct CondensedProfilePicturesView_Previews: PreviewProvider { static var previews: some View { - CondensedProfilePicturesView(state: test_damus_state(), pubkeys: [test_pubkey, test_pubkey, test_pubkey, test_pubkey], maxPictures: 3) + CondensedProfilePicturesView(state: test_damus_state, pubkeys: [test_pubkey, test_pubkey, test_pubkey, test_pubkey], maxPictures: 3) } } diff --git a/damus/Views/Profile/EditMetadataView.swift b/damus/Views/Profile/EditMetadataView.swift @@ -206,7 +206,7 @@ struct EditMetadataView: View { struct EditMetadataView_Previews: PreviewProvider { static var previews: some View { - EditMetadataView(damus_state: test_damus_state()) + EditMetadataView(damus_state: test_damus_state) } } diff --git a/damus/Views/Profile/EventProfileName.swift b/damus/Views/Profile/EventProfileName.swift @@ -123,6 +123,6 @@ struct EventProfileName: View { struct EventProfileName_Previews: PreviewProvider { static var previews: some View { - EventProfileName(pubkey: test_note.pubkey, damus: test_damus_state()) + EventProfileName(pubkey: test_note.pubkey, damus: test_damus_state) } } diff --git a/damus/Views/Profile/MaybeAnonPfpView.swift b/damus/Views/Profile/MaybeAnonPfpView.swift @@ -39,6 +39,6 @@ struct MaybeAnonPfpView: View { struct MaybeAnonPfpView_Previews: PreviewProvider { static var previews: some View { - MaybeAnonPfpView(state: test_damus_state(), is_anon: true, pubkey: ANON_PUBKEY, size: PFP_SIZE) + MaybeAnonPfpView(state: test_damus_state, is_anon: true, pubkey: ANON_PUBKEY, size: PFP_SIZE) } } diff --git a/damus/Views/Profile/ProfileEditButton.swift b/damus/Views/Profile/ProfileEditButton.swift @@ -42,7 +42,7 @@ struct ProfileEditButton: View { struct ProfileEditButton_Previews: PreviewProvider { static var previews: some View { Group { - ProfileEditButton(damus_state: test_damus_state()) + ProfileEditButton(damus_state: test_damus_state) } } } diff --git a/damus/Views/Profile/ProfileName.swift b/damus/Views/Profile/ProfileName.swift @@ -140,6 +140,6 @@ struct ProfileName: View { struct ProfileName_Previews: PreviewProvider { static var previews: some View { - ProfileName(pubkey: test_damus_state().pubkey, damus: test_damus_state()) + ProfileName(pubkey: test_damus_state.pubkey, damus: test_damus_state) } } diff --git a/damus/Views/Profile/ProfileNameView.swift b/damus/Views/Profile/ProfileNameView.swift @@ -126,9 +126,9 @@ struct ProfileNameView: View { struct ProfileNameView_Previews: PreviewProvider { static var previews: some View { VStack { - ProfileNameView(pubkey: test_note.pubkey, damus: test_damus_state()) + ProfileNameView(pubkey: test_note.pubkey, damus: test_damus_state) - ProfileNameView(pubkey: test_note.pubkey, damus: test_damus_state()) + ProfileNameView(pubkey: test_note.pubkey, damus: test_damus_state) } } } diff --git a/damus/Views/Profile/ProfilePicView.swift b/damus/Views/Profile/ProfilePicView.swift @@ -114,7 +114,7 @@ func get_profile_url(picture: String?, pubkey: Pubkey, profiles: Profiles) -> UR func make_preview_profiles(_ pubkey: Pubkey) -> Profiles { let user_search_cache = UserSearchCache() - let profiles = Profiles(user_search_cache: user_search_cache, ndb: .empty) + let profiles = Profiles(user_search_cache: user_search_cache, ndb: test_damus_state.ndb) let picture = "http://cdn.jb55.com/img/red-me.jpg" let profile = Profile(name: "jb55", display_name: "William Casarin", about: "It's me", picture: picture, banner: "", website: "https://jb55.com", lud06: nil, lud16: nil, nip05: "jb55.com", damus_donation: nil) //let ts_profile = TimestampedProfile(profile: profile, timestamp: 0, event: test_note) diff --git a/damus/Views/Profile/ProfileView.swift b/damus/Views/Profile/ProfileView.swift @@ -505,7 +505,7 @@ struct ProfileView: View { struct ProfileView_Previews: PreviewProvider { static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state ProfileView(damus_state: ds, pubkey: ds.pubkey) } } diff --git a/damus/Views/QRCodeView.swift b/damus/Views/QRCodeView.swift @@ -295,7 +295,7 @@ struct QRCodeView: View { struct QRCodeView_Previews: PreviewProvider { static var previews: some View { - QRCodeView(damus_state: test_damus_state(), pubkey: test_note.pubkey) + QRCodeView(damus_state: test_damus_state, pubkey: test_note.pubkey) } } diff --git a/damus/Views/Reactions/ReactionView.swift b/damus/Views/Reactions/ReactionView.swift @@ -28,6 +28,6 @@ struct ReactionView: View { struct ReactionView_Previews: PreviewProvider { static var previews: some View { - ReactionView(damus_state: test_damus_state(), reaction: NostrEvent(content: "🤙🏼", keypair: test_keypair)!) + ReactionView(damus_state: test_damus_state, reaction: NostrEvent(content: "🤙🏼", keypair: test_keypair)!) } } diff --git a/damus/Views/ReactionsView.swift b/damus/Views/ReactionsView.swift @@ -37,7 +37,7 @@ struct ReactionsView: View { struct ReactionsView_Previews: PreviewProvider { static var previews: some View { - let state = test_damus_state() + let state = test_damus_state ReactionsView(damus_state: state, model: ReactionsModel(state: state, target: test_note.id)) } } diff --git a/damus/Views/RelayFilterView.swift b/damus/Views/RelayFilterView.swift @@ -36,6 +36,6 @@ struct RelayFilterView: View { struct RelayFilterView_Previews: PreviewProvider { static var previews: some View { - RelayFilterView(state: test_damus_state(), timeline: .search) + RelayFilterView(state: test_damus_state, timeline: .search) } } diff --git a/damus/Views/Relays/RecommendedRelayView.swift b/damus/Views/Relays/RecommendedRelayView.swift @@ -128,6 +128,6 @@ struct RecommendedRelayView: View { struct RecommendedRelayView_Previews: PreviewProvider { static var previews: some View { - RecommendedRelayView(damus: test_damus_state(), relay: "wss://relay.damus.io", user_recommended: true) + RecommendedRelayView(damus: test_damus_state, relay: "wss://relay.damus.io", user_recommended: true) } } diff --git a/damus/Views/Relays/RelayConfigView.swift b/damus/Views/Relays/RelayConfigView.swift @@ -125,6 +125,6 @@ struct RelayConfigView: View { struct RelayConfigView_Previews: PreviewProvider { static var previews: some View { - RelayConfigView(state: test_damus_state()) + RelayConfigView(state: test_damus_state) } } diff --git a/damus/Views/Relays/RelayDetailView.swift b/damus/Views/Relays/RelayDetailView.swift @@ -174,6 +174,6 @@ struct RelayDetailView: View { struct RelayDetailView_Previews: PreviewProvider { static var previews: some View { let metadata = RelayMetadata(name: "name", description: "desc", pubkey: test_pubkey, contact: "contact", supported_nips: [1,2,3], software: "software", version: "version", limitation: Limitations.empty, payments_url: "https://jb55.com", icon: "") - RelayDetailView(state: test_damus_state(), relay: "relay", nip11: metadata) + RelayDetailView(state: test_damus_state, relay: "relay", nip11: metadata) } } diff --git a/damus/Views/Relays/RelayStatusView.swift b/damus/Views/Relays/RelayStatusView.swift @@ -57,7 +57,7 @@ struct RelayStatusView: View { struct RelayStatusView_Previews: PreviewProvider { static var previews: some View { - let connection = test_damus_state().pool.get_relay("wss://relay.damus.io")!.connection + let connection = test_damus_state.pool.get_relay("wss://relay.damus.io")!.connection RelayStatusView(connection: connection) } } diff --git a/damus/Views/Relays/RelayToggle.swift b/damus/Views/Relays/RelayToggle.swift @@ -42,7 +42,7 @@ struct RelayToggle: View { struct RelayToggle_Previews: PreviewProvider { static var previews: some View { - RelayToggle(state: test_damus_state(), timeline: .search, relay_id: "wss://jb55.com") + RelayToggle(state: test_damus_state, timeline: .search, relay_id: "wss://jb55.com") .padding() } } diff --git a/damus/Views/Relays/RelayView.swift b/damus/Views/Relays/RelayView.swift @@ -117,6 +117,6 @@ struct RelayView: View { struct RelayView_Previews: PreviewProvider { static var previews: some View { - RelayView(state: test_damus_state(), relay: "wss://relay.damus.io", showActionButtons: .constant(false)) + RelayView(state: test_damus_state, relay: "wss://relay.damus.io", showActionButtons: .constant(false)) } } diff --git a/damus/Views/Relays/SignalView.swift b/damus/Views/Relays/SignalView.swift @@ -29,6 +29,6 @@ struct SignalView: View { struct SignalView_Previews: PreviewProvider { static var previews: some View { - SignalView(state: test_damus_state(), signal: SignalModel(signal: 5, max_signal: 10)) + SignalView(state: test_damus_state, signal: SignalModel(signal: 5, max_signal: 10)) } } diff --git a/damus/Views/ReplyView.swift b/damus/Views/ReplyView.swift @@ -93,13 +93,13 @@ struct ReplyView_Previews: PreviewProvider { static var previews: some View { VStack { ReplyView(replying_to: test_note, - damus: test_damus_state(), + damus: test_damus_state, original_pubkeys: [], filtered_pubkeys: .constant([])) .frame(height: 300) ReplyView(replying_to: test_longform_event.event, - damus: test_damus_state(), + damus: test_damus_state, original_pubkeys: [], filtered_pubkeys: .constant([])) .frame(height: 300) diff --git a/damus/Views/ReportView.swift b/damus/Views/ReportView.swift @@ -129,7 +129,7 @@ struct ReportView: View { struct ReportView_Previews: PreviewProvider { static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state VStack { ReportView(postbox: ds.postbox, target: ReportTarget.user(test_pubkey), keypair: test_keypair.to_full()!) diff --git a/damus/Views/Reposts/RepostView.swift b/damus/Views/Reposts/RepostView.swift @@ -18,7 +18,7 @@ struct RepostView: View { struct RepostView_Previews: PreviewProvider { static var previews: some View { - RepostView(damus_state: test_damus_state(), repost: NostrEvent(content: "", keypair: test_keypair)!) + RepostView(damus_state: test_damus_state, repost: NostrEvent(content: "", keypair: test_keypair)!) } } diff --git a/damus/Views/Reposts/RepostedEvent.swift b/damus/Views/Reposts/RepostedEvent.swift @@ -29,6 +29,6 @@ struct RepostedEvent: View { struct RepostedEvent_Previews: PreviewProvider { static var previews: some View { - RepostedEvent(damus: test_damus_state(), event: test_note, inner_ev: test_note, options: []) + RepostedEvent(damus: test_damus_state, event: test_note, inner_ev: test_note, options: []) } } diff --git a/damus/Views/RepostsView.swift b/damus/Views/RepostsView.swift @@ -32,7 +32,7 @@ struct RepostsView: View { struct RepostsView_Previews: PreviewProvider { static var previews: some View { - let state = test_damus_state() + let state = test_damus_state RepostsView(damus_state: state, model: RepostsModel(state: state, target: test_note.id)) } } diff --git a/damus/Views/Search/SearchingEventView.swift b/damus/Views/Search/SearchingEventView.swift @@ -126,7 +126,7 @@ struct SearchingEventView: View { struct SearchingEventView_Previews: PreviewProvider { static var previews: some View { - let state = test_damus_state() + let state = test_damus_state SearchingEventView(state: state, search_type: .event(test_note.id)) } } diff --git a/damus/Views/SearchHomeView.swift b/damus/Views/SearchHomeView.swift @@ -134,7 +134,7 @@ struct SearchHomeView: View { struct SearchHomeView_Previews: PreviewProvider { static var previews: some View { - let state = test_damus_state() + let state = test_damus_state SearchHomeView(damus_state: state, model: SearchHomeModel(damus_state: state)) } } diff --git a/damus/Views/SearchView.swift b/damus/Views/SearchView.swift @@ -79,7 +79,7 @@ func describe_search(_ filter: NostrFilter) -> DescribedSearch { struct SearchView_Previews: PreviewProvider { static var previews: some View { - let test_state = test_damus_state() + let test_state = test_damus_state let filter = NostrFilter(hashtag: ["bitcoin"]) let model = SearchModel(state: test_state, search: filter) diff --git a/damus/Views/Settings/AppearanceSettingsView.swift b/damus/Views/Settings/AppearanceSettingsView.swift @@ -107,6 +107,6 @@ struct AppearanceSettingsView: View { struct TextFormattingSettings_Previews: PreviewProvider { static var previews: some View { - AppearanceSettingsView(damus_state: test_damus_state(), settings: UserSettingsStore()) + AppearanceSettingsView(damus_state: test_damus_state, settings: UserSettingsStore()) } } diff --git a/damus/Views/SideMenuView.swift b/damus/Views/SideMenuView.swift @@ -233,7 +233,7 @@ struct SideMenuView: View { struct Previews_SideMenuView_Previews: PreviewProvider { static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state SideMenuView(damus_state: ds, isSidebarVisible: .constant(true)) } } diff --git a/damus/Views/ThreadView.swift b/damus/Views/ThreadView.swift @@ -103,7 +103,7 @@ struct ThreadView: View { struct ThreadView_Previews: PreviewProvider { static var previews: some View { - let state = test_damus_state() + let state = test_damus_state let thread = ThreadModel(event: test_note, damus_state: state) ThreadView(state: state, thread: thread) } diff --git a/damus/Views/Timeline/InnerTimelineView.swift b/damus/Views/Timeline/InnerTimelineView.swift @@ -69,7 +69,7 @@ struct InnerTimelineView: View { struct InnerTimelineView_Previews: PreviewProvider { static var previews: some View { - InnerTimelineView(events: test_event_holder, damus: test_damus_state(), filter: { _ in true }) + InnerTimelineView(events: test_event_holder, damus: test_damus_state, filter: { _ in true }) .frame(width: 300, height: 500) .border(Color.red) } diff --git a/damus/Views/UserRelaysView.swift b/damus/Views/UserRelaysView.swift @@ -40,6 +40,6 @@ struct UserRelaysView: View { struct UserRelaysView_Previews: PreviewProvider { static var previews: some View { - UserRelaysView(state: test_damus_state(), relays: []) + UserRelaysView(state: test_damus_state, relays: []) } } diff --git a/damus/Views/Wallet/WalletView.swift b/damus/Views/Wallet/WalletView.swift @@ -194,10 +194,10 @@ struct WalletView: View { } } -let test_wallet_connect_url = WalletConnectURL(pubkey: test_pubkey, relay: .init("wss://relay.damus.io")!, keypair: test_damus_state().keypair.to_full()!, lud16: "jb55@sendsats.com") +let test_wallet_connect_url = WalletConnectURL(pubkey: test_pubkey, relay: .init("wss://relay.damus.io")!, keypair: test_damus_state.keypair.to_full()!, lud16: "jb55@sendsats.com") struct WalletView_Previews: PreviewProvider { - static let tds = test_damus_state() + static let tds = test_damus_state static var previews: some View { WalletView(damus_state: tds, model: WalletModel(state: .existing(test_wallet_connect_url), settings: tds.settings)) } diff --git a/damus/Views/Zaps/CustomizeZapView.swift b/damus/Views/Zaps/CustomizeZapView.swift @@ -302,7 +302,7 @@ extension View { struct CustomizeZapView_Previews: PreviewProvider { static var previews: some View { - CustomizeZapView(state: test_damus_state(), target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), lnurl: "") + CustomizeZapView(state: test_damus_state, target: ZapTarget.note(id: test_note.id, author: test_note.pubkey), lnurl: "") .frame(width: 400, height: 600) } } diff --git a/damus/Views/Zaps/ZapTypePicker.swift b/damus/Views/Zaps/ZapTypePicker.swift @@ -105,7 +105,7 @@ struct ZapTypePicker: View { struct ZapTypePicker_Previews: PreviewProvider { @State static var zap_type: ZapType = .pub static var previews: some View { - let ds = test_damus_state() + let ds = test_damus_state ZapTypePicker(zap_type: $zap_type, settings: ds.settings, profiles: ds.profiles, pubkey: test_pubkey) } } diff --git a/damus/Views/Zaps/ZapUserView.swift b/damus/Views/Zaps/ZapUserView.swift @@ -23,6 +23,6 @@ struct ZapUserView: View { struct ZapUserView_Previews: PreviewProvider { static var previews: some View { - ZapUserView(state: test_damus_state(), pubkey: ANON_PUBKEY) + ZapUserView(state: test_damus_state, pubkey: ANON_PUBKEY) } } diff --git a/damus/Views/Zaps/ZapsView.swift b/damus/Views/Zaps/ZapsView.swift @@ -40,6 +40,6 @@ struct ZapsView: View { struct ZapsView_Previews: PreviewProvider { static var previews: some View { - ZapsView(state: test_damus_state(), target: .profile(test_pubkey)) + ZapsView(state: test_damus_state, target: .profile(test_pubkey)) } } diff --git a/damusTests/EventGroupViewTests.swift b/damusTests/EventGroupViewTests.swift @@ -19,14 +19,14 @@ final class EventGroupViewTests: XCTestCase { } func testEventAuthorName() { - let damusState = test_damus_state() + let damusState = test_damus_state XCTAssertEqual(event_author_name(profiles: damusState.profiles, pubkey: test_pubkey), "damus") XCTAssertEqual(event_author_name(profiles: damusState.profiles, pubkey: test_pubkey_2), "1rppft3m:4qxhsgnj") XCTAssertEqual(event_author_name(profiles: damusState.profiles, pubkey: ANON_PUBKEY), "Anonymous") } func testEventGroupUniquePubkeys() { - let damusState = test_damus_state() + let damusState = test_damus_state let encodedPost = "{\"id\": \"8ba545ab96959fe0ce7db31bc10f3ac3aa5353bc4428dbf1e56a7be7062516db\",\"pubkey\": \"7e27509ccf1e297e1df164912a43406218f8bd80129424c3ef798ca3ef5c8444\",\"created_at\": 1677013417,\"kind\": 1,\"tags\": [],\"content\": \"hello\",\"sig\": \"93684f15eddf11f42afbdd81828ee9fc35350344d8650c78909099d776e9ad8d959cd5c4bff7045be3b0b255144add43d0feef97940794a1bc9c309791bebe4a\"}" @@ -49,7 +49,7 @@ final class EventGroupViewTests: XCTestCase { func testReactingToText() throws { let enUsLocale = Locale(identifier: "en-US") - let damusState = test_damus_state() + let damusState = test_damus_state let encodedPost = "{\"id\": \"8ba545ab96959fe0ce7db31bc10f3ac3aa5353bc4428dbf1e56a7be7062516db\",\"pubkey\": \"7e27509ccf1e297e1df164912a43406218f8bd80129424c3ef798ca3ef5c8444\",\"created_at\": 1677013417,\"kind\": 1,\"tags\": [],\"content\": \"hello\",\"sig\": \"93684f15eddf11f42afbdd81828ee9fc35350344d8650c78909099d776e9ad8d959cd5c4bff7045be3b0b255144add43d0feef97940794a1bc9c309791bebe4a\"}" diff --git a/damusTests/LongPostTests.swift b/damusTests/LongPostTests.swift @@ -34,7 +34,7 @@ final class LongPostTests: XCTestCase { XCTAssertEqual(subid, "subid") XCTAssertTrue(ev.should_show_event) XCTAssertTrue(!ev.too_big) - XCTAssertTrue(should_show_event(keypair: test_keypair, hellthreads: test_damus_state().muted_threads, contacts: contacts, ev: ev)) + XCTAssertTrue(should_show_event(keypair: test_keypair, hellthreads: test_damus_state.muted_threads, contacts: contacts, ev: ev)) XCTAssertTrue(validate_event(ev: ev) == .ok ) } diff --git a/damusTests/NoteContentViewTests.swift b/damusTests/NoteContentViewTests.swift @@ -14,7 +14,7 @@ class NoteContentViewTests: XCTestCase { let note = NostrEvent(content: content, keypair: test_keypair, tags: [["t", "かっこいい"]])! let parsed: Blocks = parse_note_content(content: .init(note: note, keypair: test_keypair)) - let testState = test_damus_state() + let testState = test_damus_state let text: NoteArtifactsSeparated = render_blocks(blocks: parsed, profiles: testState.profiles) let attributedText: AttributedString = text.content.attributed diff --git a/damusTests/ProfileViewTests.swift b/damusTests/ProfileViewTests.swift @@ -21,24 +21,25 @@ final class ProfileViewTests: XCTestCase { } func testFollowedByString() throws { - let profiles = test_damus_state().profiles - let pk1 = test_pubkey let pk2 = test_pubkey_2 let pk3 = Pubkey(hex: "b42e44b555013239a0d5dcdb09ebde0857cd8a5a57efbba5a2b6ac78833cb9f0")! let pk4 = Pubkey(hex: "cc590e46363d0fa66bb27081368d01f169b8ffc7c614629d4e9eef6c88b38670")! let pk5 = Pubkey(hex: "f2aa579bb998627e04a8f553842a09446360c9d708c6141dd119c479f6ab9d29")! - XCTAssertEqual(followedByString([pk1], profiles: profiles, locale: enUsLocale), "Followed by damus") - XCTAssertEqual(followedByString([pk1, pk2], profiles: profiles, locale: enUsLocale), "Followed by damus & 1rppft3m:4qxhsgnj") - XCTAssertEqual(followedByString([pk1, pk2, pk3], profiles: profiles, locale: enUsLocale), "Followed by damus, 1rppft3m:4qxhsgnj & 1kshyfd2:cq04aze0") - XCTAssertEqual(followedByString([pk1, pk2, pk3, pk4,], profiles: profiles, locale: enUsLocale), "Followed by damus, 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 1 other") - XCTAssertEqual(followedByString([pk1, pk2, pk3, pk4, pk5], profiles: profiles, locale: enUsLocale), "Followed by damus, 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 2 others") + let ndb = Ndb(path: Ndb.db_path)! + let txn = NdbTxn(ndb: ndb) + + XCTAssertEqual(followedByString(txn: txn, [pk1], ndb: ndb, locale: enUsLocale), "Followed by damus") + XCTAssertEqual(followedByString(txn: txn, [pk1, pk2], ndb: ndb, locale: enUsLocale), "Followed by damus & 1rppft3m:4qxhsgnj") + XCTAssertEqual(followedByString(txn: txn, [pk1, pk2, pk3], ndb: ndb, locale: enUsLocale), "Followed by damus, 1rppft3m:4qxhsgnj & 1kshyfd2:cq04aze0") + XCTAssertEqual(followedByString(txn: txn, [pk1, pk2, pk3, pk4,], ndb: ndb, locale: enUsLocale), "Followed by damus, 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 1 other") + XCTAssertEqual(followedByString(txn: txn, [pk1, pk2, pk3, pk4, pk5], ndb: ndb, locale: enUsLocale), "Followed by damus, 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 2 others") let pubkeys = [pk1, pk2, pk3, pk4, pk5, pk1, pk2, pk3, pk4, pk5] Bundle.main.localizations.map { Locale(identifier: $0) }.forEach { for count in 1...10 { - XCTAssertNoThrow(followedByString(pubkeys.prefix(count).map { $0 }, profiles: profiles, locale: $0)) + XCTAssertNoThrow(followedByString(txn: txn, pubkeys.prefix(count).map { $0 }, ndb: ndb, locale: $0)) } } } diff --git a/damusTests/UserSearchCacheTests.swift b/damusTests/UserSearchCacheTests.swift @@ -11,7 +11,7 @@ import XCTest final class UserSearchCacheTests: XCTestCase { var keypair: FullKeypair? = nil - let damusState = DamusState.empty + let damusState = test_damus_state let nip05 = "_@somedomain.com" @MainActor @@ -55,11 +55,6 @@ final class UserSearchCacheTests: XCTestCase { damusState.profiles.set_validated(keypair.pubkey, nip05: NIP05.parse(newNip05)) - let newProfile = Profile(name: "whoami", display_name: "T-DAWG", about: nil, picture: nil, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: newNip05, damus_donation: nil) - - // Lookup to synchronize access on profiles dictionary to avoid race conditions. - let _ = damusState.profiles.lookup(id: keypair.pubkey) - // Old profile attributes are removed from cache. XCTAssertEqual(damusState.user_search_cache.search(key: "tyiu"), []) XCTAssertEqual(damusState.user_search_cache.search(key: "ty"), []) diff --git a/damusTests/ZapTests.swift b/damusTests/ZapTests.swift @@ -69,7 +69,7 @@ final class ZapTests: XCTestCase { XCTAssertEqual(zap.target, ZapTarget.profile(profile)) XCTAssertEqual(zap_notification_title(zap), "Zap") - XCTAssertEqual(zap_notification_body(profiles: Profiles(user_search_cache: UserSearchCache(), ndb: .empty), zap: zap), "You received 1k sats from 107jk7ht:2quqncxg") + XCTAssertEqual(zap_notification_body(profiles: Profiles(user_search_cache: UserSearchCache(), ndb: test_damus_state.ndb), zap: zap), "You received 1k sats from 107jk7ht:2quqncxg") } } diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift @@ -11,14 +11,15 @@ class Ndb { let ndb: ndb_t static var db_path: String { - (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: ""))! + let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString + return remove_file_prefix(path!) } static var empty: Ndb { Ndb(ndb: ndb_t(ndb: nil)) } - init?() { + init?(path: String? = nil) { //try? FileManager.default.removeItem(atPath: Ndb.db_path + "/lock.mdb") //try? FileManager.default.removeItem(atPath: Ndb.db_path + "/data.mdb") @@ -27,7 +28,9 @@ class Ndb { let ingest_threads: Int32 = 4 var mapsize: Int = 1024 * 1024 * 1024 * 32 - let ok = Ndb.db_path.withCString { testdir in + let path = path.map(remove_file_prefix) ?? Ndb.db_path + + let ok = path.withCString { testdir in var ok = false while !ok && mapsize > 1024 * 1024 * 700 { ok = ndb_init(&ndb_p, testdir, mapsize, ingest_threads) != 0 @@ -199,3 +202,8 @@ func getDebugCheckedRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer) thro return getRoot(byteBuffer: &byteBuffer) } #endif + +func remove_file_prefix(_ str: String) -> String { + return str.replacingOccurrences(of: "file://", with: "") +} + diff --git a/nostrdb/Test/NdbTests.swift b/nostrdb/Test/NdbTests.swift @@ -8,12 +8,26 @@ import XCTest @testable import damus +func test_ndb_dir() -> String? { + do { + let fileManager = FileManager.default + let tempDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true, attributes: nil) + return remove_file_prefix(tempDir.absoluteString) + } catch { + return nil + } +} + final class NdbTests: XCTestCase { + var db_dir: String = "" override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - try? FileManager.default.removeItem(atPath: Ndb.db_path + "/lock.mdb") - try? FileManager.default.removeItem(atPath: Ndb.db_path + "/data.mdb") + guard let db = test_ndb_dir() else { + XCTFail("Could not create temp directory") + return + } + db_dir = db } override func tearDownWithError() throws { @@ -41,26 +55,27 @@ final class NdbTests: XCTestCase { func test_ndb_init() { do { - let ndb = Ndb()! + let ndb = Ndb(path: db_dir)! let ok = ndb.process_events(test_wire_events) XCTAssertTrue(ok) } do { - let ndb = Ndb()! + let ndb = Ndb(path: db_dir)! let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")! - let note = ndb.lookup_note(id) + let txn = NdbTxn(ndb: ndb) + let note = ndb.lookup_note_with_txn(id: id, txn: txn) XCTAssertNotNil(note) guard let note else { return } let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")! XCTAssertEqual(note.pubkey, pk) - let profile = ndb.lookup_profile(pk) + let profile = ndb.lookup_profile_with_txn(pk, txn: txn) XCTAssertNotNil(profile) guard let profile else { return } XCTAssertEqual(profile.profile?.name, "jb55") - XCTAssertEqual(profile.lnurl, "fixme") + XCTAssertEqual(profile.lnurl, nil) }