-
Notifications
You must be signed in to change notification settings - Fork 229
test(integration): add integration tests for NIP-02 contact lists #552
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
Merged
cameri
merged 5 commits into
cameri:main
from
vikashsiwach:test/nip02-contact-list-integration
Apr 21, 2026
+186
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35a466c
test(integration): add integration tests for NIP-02 contact lists
vikashsiwach 2644e0f
Merge branch 'main' into test/nip02-contact-list-integration
cameri 6c727f4
Merge branch 'main' into test/nip02-contact-list-integration
vikashsiwach 52720c6
Merge branch 'main' into test/nip02-contact-list-integration
vikashsiwach 8a5f991
Merge branch 'main' into test/nip02-contact-list-integration
cameri 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": patch | ||
| --- | ||
|
|
||
| Add integration tests for NIP-02 contact lists (Kind 3) |
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,26 @@ | ||
| Feature: NIP-02 Contact Lists | ||
| Scenario: Alice publishes a contact list | ||
| Given someone called Alice | ||
| When Alice sends a contact_list event with tags | ||
| And Alice subscribes to author Alice | ||
| Then Alice receives a contact_list event from Alice | ||
|
|
||
| Scenario: Alice publishes an updated contact list | ||
| Given someone called Alice | ||
| When Alice sends a contact_list event with tags | ||
| And Alice sends a second contact_list event with different tags | ||
| And Alice subscribes to author Alice | ||
| Then Alice receives 1 contact_list event from Alice with the latest tags and EOSE | ||
|
|
||
| Scenario: Tie-breaker on Identical Timestamps for contact list | ||
| Given someone called Alice | ||
| When Alice sends two identically-timestamped contact_list events where the second has a lower ID | ||
| And Alice subscribes to author Alice | ||
| Then Alice receives 1 contact_list event from Alice matching the lower ID event and EOSE | ||
|
|
||
| Scenario: Bob subscribes to Alice's contact list | ||
| Given someone called Alice | ||
| And someone called Bob | ||
| When Alice sends a contact_list event with tags | ||
| And Bob subscribes to author Alice | ||
| Then Bob receives a contact_list event from Alice |
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,155 @@ | ||
| import { Then, When } from '@cucumber/cucumber' | ||
| import { expect } from 'chai' | ||
| import WebSocket from 'ws' | ||
|
|
||
| import { createEvent, sendEvent, waitForEventCount, waitForNextEvent } from '../helpers' | ||
| import { Event } from '../../../../src/@types/event' | ||
| import { EventKinds, EventTags } from '../../../../src/constants/base' | ||
|
|
||
| When( | ||
| /^(\w+) sends a contact_list event with tags$/, | ||
| async function (name: string) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
| const { pubkey, privkey } = this.parameters.identities[name] | ||
|
|
||
| // Create a simple contact list with a few pubkeys | ||
| const contactPubkey1 = 'a'.repeat(64) | ||
| const contactPubkey2 = 'b'.repeat(64) | ||
|
|
||
| const event: Event = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: EventKinds.CONTACT_LIST, | ||
| tags: [ | ||
| [EventTags.Pubkey, contactPubkey1], | ||
| [EventTags.Pubkey, contactPubkey2], | ||
| ], | ||
| content: '', | ||
| }, | ||
| privkey, | ||
| ) | ||
|
|
||
| await sendEvent(ws, event) | ||
| this.parameters.events[name].push(event) | ||
| this.parameters.contactListEvent = event | ||
| }, | ||
| ) | ||
|
|
||
| When( | ||
| /^(\w+) sends a second contact_list event with different tags$/, | ||
| async function (name: string) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
| const { pubkey, privkey } = this.parameters.identities[name] | ||
|
|
||
| // Create an updated contact list with different pubkeys | ||
| const contactPubkey3 = 'c'.repeat(64) | ||
| const contactPubkey4 = 'd'.repeat(64) | ||
|
|
||
| const event: Event = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: EventKinds.CONTACT_LIST, | ||
| tags: [ | ||
| [EventTags.Pubkey, contactPubkey3], | ||
| [EventTags.Pubkey, contactPubkey4], | ||
| ], | ||
| content: '', | ||
| }, | ||
| privkey, | ||
| ) | ||
|
|
||
| await sendEvent(ws, event) | ||
| this.parameters.events[name].push(event) | ||
| this.parameters.updatedContactListEvent = event | ||
| }, | ||
| ) | ||
|
|
||
| Then( | ||
| /^(\w+) receives a contact_list event from (\w+)$/, | ||
| async function (name: string, author: string) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
| const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1] | ||
| const receivedEvent = await waitForNextEvent(ws, subscription.name) | ||
|
|
||
| expect(receivedEvent.kind).to.equal(EventKinds.CONTACT_LIST) | ||
| expect(receivedEvent.pubkey).to.equal(this.parameters.identities[author].pubkey) | ||
| }, | ||
| ) | ||
|
|
||
| Then( | ||
| /^(\w+) receives 1 contact_list event from (\w+) with the latest tags and EOSE$/, | ||
| async function (name: string, author: string) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
| const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1] | ||
| const events = await waitForEventCount(ws, subscription.name, 1, true) | ||
|
|
||
| expect(events.length).to.equal(1) | ||
| expect(events[0].kind).to.equal(EventKinds.CONTACT_LIST) | ||
| expect(events[0].pubkey).to.equal(this.parameters.identities[author].pubkey) | ||
|
|
||
| // Verify it's the updated event (has the different contact pubkeys) | ||
| expect(events[0].tags).to.deep.equal(this.parameters.updatedContactListEvent.tags) | ||
| }, | ||
| ) | ||
|
|
||
| When( | ||
| /^(\w+) sends two identically-timestamped contact_list events where the second has a lower ID$/, | ||
| async function (name: string) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
| const { pubkey, privkey } = this.parameters.identities[name] | ||
|
|
||
| const commonTimestamp = Math.floor(Date.now() / 1000) | ||
|
|
||
| const contactPubkey1 = 'e'.repeat(64) | ||
| const event1 = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: EventKinds.CONTACT_LIST, | ||
| tags: [[EventTags.Pubkey, contactPubkey1]], | ||
| content: 'first contact list', | ||
| created_at: commonTimestamp, | ||
| }, | ||
| privkey, | ||
| ) | ||
|
|
||
| let nonce = 0 | ||
| let event2: Event | ||
| const contactPubkey2 = 'f'.repeat(64) | ||
| for (;;) { | ||
| event2 = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: EventKinds.CONTACT_LIST, | ||
| tags: [[EventTags.Pubkey, contactPubkey2]], | ||
| content: `second contact list ${nonce++}`, | ||
| created_at: commonTimestamp, | ||
| }, | ||
| privkey, | ||
| ) | ||
|
|
||
| if (event2.id < event1.id) { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| await sendEvent(ws, event1) | ||
| await sendEvent(ws, event2) | ||
|
|
||
| this.parameters.events[name].push(event1, event2) | ||
| this.parameters.lowerIdContactListContent = event2.tags | ||
| }, | ||
| ) | ||
|
|
||
| Then( | ||
| /^(\w+) receives 1 contact_list event from (\w+) matching the lower ID event and EOSE$/, | ||
| async function (name: string, author: string) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
| const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1] | ||
| const events = await waitForEventCount(ws, subscription.name, 1, true) | ||
|
|
||
| expect(events.length).to.equal(1) | ||
| expect(events[0].kind).to.equal(EventKinds.CONTACT_LIST) | ||
| expect(events[0].pubkey).to.equal(this.parameters.identities[author].pubkey) | ||
| expect(events[0].tags).to.deep.equal(this.parameters.lowerIdContactListContent) | ||
| }, | ||
|
vikashsiwach marked this conversation as resolved.
|
||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.