-
Notifications
You must be signed in to change notification settings - Fork 9
test: add metadata NIP-65 relay discovery regression #698
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
Open
nogringo
wants to merge
4
commits into
master
Choose a base branch
from
test/metadata-nip65-relay-discovery
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
95f7a49
test: add metadata NIP-65 relay discovery regression
nogringo 5063baf
test: run metadata NIP-65 relay discovery against both engines
nogringo 19b82be
fix: match all filter kinds in mock relay requests
nogringo f509816
fix: follow the author NIP-65 write relays in loadMetadata
nogringo 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
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
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
160 changes: 160 additions & 0 deletions
160
packages/ndk/test/mocks/mock_relay_multi_kind_filter_test.dart
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,160 @@ | ||
| import 'package:ndk/domain_layer/entities/nip_65.dart'; | ||
| import 'package:ndk/ndk.dart'; | ||
| import 'package:ndk/shared/nips/nip01/bip340.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'mock_event_verifier.dart'; | ||
| import 'mock_relay.dart'; | ||
|
|
||
| void main() { | ||
| group('MockRelay multi-kind filter matching', () { | ||
| late MockRelay mockRelay; | ||
|
|
||
| setUp(() async { | ||
| mockRelay = MockRelay(name: 'multi-kind-filter-test-relay'); | ||
| await mockRelay.startServer(); | ||
| }); | ||
|
|
||
| tearDown(() async { | ||
| await mockRelay.stopServer(); | ||
| }); | ||
|
|
||
| test( | ||
| 'a filter with kinds [10002, 3, 0, 30382] should return stored events of all kinds', | ||
| () async { | ||
| final keyPair = Bip340.generatePrivateKey(); | ||
|
|
||
| final ndkWriter = Ndk( | ||
| NdkConfig( | ||
| cache: MemCacheManager(), | ||
| eventVerifier: MockEventVerifier(), | ||
| bootstrapRelays: [mockRelay.url], | ||
| ), | ||
| ); | ||
|
|
||
| final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; | ||
|
|
||
| final relayListEvent = Nip01Event( | ||
| pubKey: keyPair.publicKey, | ||
| kind: Nip65.kKind, | ||
| tags: [ | ||
| ['r', 'wss://write.example.com'], | ||
| ], | ||
| content: '', | ||
| createdAt: now - 1, | ||
| ); | ||
| final signedRelayList = Nip01Utils.signWithPrivateKey( | ||
| event: relayListEvent, | ||
| privateKey: keyPair.privateKey!, | ||
| ); | ||
| await ndkWriter.broadcast | ||
| .broadcast( | ||
| nostrEvent: signedRelayList, | ||
| specificRelays: [mockRelay.url], | ||
| ) | ||
| .broadcastDoneFuture; | ||
|
|
||
| final contactListEvent = Nip01Event( | ||
| pubKey: keyPair.publicKey, | ||
| kind: ContactList.kKind, | ||
| tags: [ | ||
| ['p', keyPair.publicKey], | ||
| ], | ||
| content: '', | ||
| createdAt: now, | ||
| ); | ||
| final signedContactList = Nip01Utils.signWithPrivateKey( | ||
| event: contactListEvent, | ||
| privateKey: keyPair.privateKey!, | ||
| ); | ||
| await ndkWriter.broadcast | ||
| .broadcast( | ||
| nostrEvent: signedContactList, | ||
| specificRelays: [mockRelay.url], | ||
| ) | ||
| .broadcastDoneFuture; | ||
|
|
||
| final metadataEvent = Metadata( | ||
| pubKey: keyPair.publicKey, | ||
| name: 'multi-kind-profile', | ||
| ).toEvent(); | ||
| final signedMetadata = Nip01Utils.signWithPrivateKey( | ||
| event: metadataEvent, | ||
| privateKey: keyPair.privateKey!, | ||
| ); | ||
| await ndkWriter.broadcast | ||
| .broadcast( | ||
| nostrEvent: signedMetadata, | ||
| specificRelays: [mockRelay.url], | ||
| ) | ||
| .broadcastDoneFuture; | ||
|
|
||
| final assertionEvent = Nip01Event( | ||
| pubKey: keyPair.publicKey, | ||
| kind: 30382, | ||
| tags: [ | ||
| ['d', keyPair.publicKey], | ||
| ], | ||
| content: '', | ||
| createdAt: now, | ||
| ); | ||
| final signedAssertion = Nip01Utils.signWithPrivateKey( | ||
| event: assertionEvent, | ||
| privateKey: keyPair.privateKey!, | ||
| ); | ||
| await ndkWriter.broadcast | ||
| .broadcast( | ||
| nostrEvent: signedAssertion, | ||
| specificRelays: [mockRelay.url], | ||
| ) | ||
| .broadcastDoneFuture; | ||
|
|
||
| // Fresh client to bypass any local cache | ||
| final ndkReader = Ndk( | ||
| NdkConfig( | ||
| cache: MemCacheManager(), | ||
| eventVerifier: MockEventVerifier(), | ||
| bootstrapRelays: [mockRelay.url], | ||
| ), | ||
| ); | ||
|
|
||
| final received = await ndkReader.requests | ||
| .query( | ||
| filter: Filter( | ||
| kinds: [Nip65.kKind, ContactList.kKind, Metadata.kKind, 30382], | ||
| authors: [keyPair.publicKey], | ||
| ), | ||
| ) | ||
| .future; | ||
|
|
||
| expect( | ||
| received.map((event) => event.id), | ||
| contains(signedContactList.id), | ||
| reason: 'The contact list should match the combined filter.', | ||
| ); | ||
| expect( | ||
| received.map((event) => event.id), | ||
| contains(signedMetadata.id), | ||
| reason: 'The metadata should match the combined filter.', | ||
| ); | ||
| expect( | ||
| received.map((event) => event.id), | ||
| contains(signedAssertion.id), | ||
| reason: | ||
| 'The stored kind 30382 event should match the combined filter.', | ||
| ); | ||
| expect( | ||
| received.map((event) => event.id), | ||
| contains(signedRelayList.id), | ||
| reason: | ||
| 'The stored kind 10002 event should match the combined filter; ' | ||
| 'the mock skips general event matching when a specialized ' | ||
| 'branch handled the filter.', | ||
| ); | ||
|
|
||
| await ndkWriter.destroy(); | ||
| await ndkReader.destroy(); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
packages/ndk/test/usecases/metadatas/metadata_nip65_test.dart
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,70 @@ | ||
| import 'package:test/test.dart'; | ||
| import 'package:ndk/domain_layer/entities/nip_65.dart'; | ||
| import 'package:ndk/domain_layer/entities/read_write_marker.dart'; | ||
| import 'package:ndk/ndk.dart'; | ||
| import 'package:ndk/shared/nips/nip01/bip340.dart'; | ||
|
|
||
| import '../../mocks/mock_relay.dart'; | ||
|
|
||
| void main() { | ||
| Future<void> loadMetadataFollowsWriteRelay(NdkEngine engine) async { | ||
| final key = Bip340.generatePrivateKey(); | ||
| final metadata = Metadata(pubKey: key.publicKey, name: 'nip65-profile'); | ||
|
|
||
| final profileRelay = MockRelay(name: 'profile relay'); | ||
| final signedMetadataEvent = Nip01Utils.signWithPrivateKey( | ||
| event: metadata.toEvent(), | ||
| privateKey: key.privateKey!, | ||
| ); | ||
| await profileRelay.startServer( | ||
| metadatas: {key.publicKey: signedMetadataEvent}, | ||
| ); | ||
|
|
||
| final relayList = Nip65.fromMap(key.publicKey, { | ||
| profileRelay.url: ReadWriteMarker.readWrite, | ||
| }); | ||
| final signedRelayListEvent = Nip01Utils.signWithPrivateKey( | ||
| event: relayList.toEvent(), | ||
| privateKey: key.privateKey!, | ||
| ); | ||
| final relayListBootstrap = MockRelay(name: 'relay list bootstrap'); | ||
| await relayListBootstrap.startServer(); | ||
|
|
||
| final ndk = Ndk( | ||
| NdkConfig( | ||
| eventVerifier: Bip340EventVerifier(), | ||
| cache: MemCacheManager(), | ||
| bootstrapRelays: [relayListBootstrap.url], | ||
| engine: engine, | ||
| ), | ||
| ); | ||
|
|
||
| await ndk.relays.seedRelaysConnected; | ||
|
|
||
| addTearDown(() async { | ||
| await ndk.destroy(); | ||
| await relayListBootstrap.stopServer(); | ||
| await profileRelay.stopServer(); | ||
| }); | ||
|
|
||
| await ndk.broadcast | ||
| .broadcast( | ||
| nostrEvent: signedRelayListEvent, | ||
| specificRelays: [relayListBootstrap.url], | ||
| saveToCache: false, | ||
| ) | ||
| .broadcastDoneFuture; | ||
|
|
||
| final loaded = await ndk.metadata.loadMetadata(key.publicKey); | ||
|
|
||
| expect(loaded?.name, equals(metadata.name)); | ||
| } | ||
|
|
||
| test('loadMetadata follows the author write relay (RELAY_SETS)', () async { | ||
| await loadMetadataFollowsWriteRelay(NdkEngine.RELAY_SETS); | ||
| }); | ||
|
|
||
| test('loadMetadata follows the author write relay (JIT)', () async { | ||
| await loadMetadataFollowsWriteRelay(NdkEngine.JIT); | ||
| }); | ||
| } |
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Destroy each NDK instance through teardown.
If an assertion fails before Lines 155-156, neither
destroy()call runs. The test can leave relay connections and retry work active.packages/ndk/test/mocks/mock_relay_multi_kind_filter_test.dart#L27-L33: RegisteraddTearDown(() => ndkWriter.destroy())immediately after construction.packages/ndk/test/mocks/mock_relay_multi_kind_filter_test.dart#L112-L119: RegisteraddTearDown(() => ndkReader.destroy())immediately after construction.📍 Affects 1 file
packages/ndk/test/mocks/mock_relay_multi_kind_filter_test.dart#L27-L33(this comment)packages/ndk/test/mocks/mock_relay_multi_kind_filter_test.dart#L112-L119🤖 Prompt for AI Agents