Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/integration_tests/core/test_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ pub async fn publish_relay_lists(

Ok(())
}

pub async fn publish_follow_list(
client: &Client,
contacts: &[PublicKey],
) -> Result<(), WhitenoiseError> {
let tags: Vec<Tag> = contacts
.iter()
.map(|pk| Tag::custom(TagKind::p(), [pk.to_hex()]))
.collect();

client
.send_event_builder(EventBuilder::new(Kind::ContactList, "").tags(tags))
.await?;
Ok(())
}
16 changes: 15 additions & 1 deletion src/integration_tests/scenarios/subscription_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,25 @@ impl Scenario for SubscriptionProcessingScenario {

// Test 4: External user relay list update
let alice_relay_url = "wss://alice-relay.example.com".to_string();
PublishSubscriptionUpdateTestCase::for_external_user(alice_keys)
PublishSubscriptionUpdateTestCase::for_external_user(alice_keys.clone())
.with_relay_update(alice_relay_url)
.execute(&mut self.context)
.await?;

// Test 5: Publish a follow list (as a TestCase with assertion)
PublishSubscriptionUpdateTestCase::for_account("subscription_test_account")
.with_follow_list(vec![alice_keys.public_key()])
.execute(&mut self.context)
.await?;

// Test 6: Verify timestamp policy using a single builder-style test case
VerifyLastSyncedTimestampTestCase::for_account_follow_event()
.execute(&mut self.context)
.await?;
VerifyLastSyncedTimestampTestCase::for_global_metadata_event()
.execute(&mut self.context)
.await?;

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod publish_subscription_update;
pub mod verify_last_synced_timestamp;

pub use publish_subscription_update::*;
pub use verify_last_synced_timestamp::*;
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use crate::integration_tests::core::*;
use crate::{RelayType, WhitenoiseError};
use async_trait::async_trait;
use nostr_sdk::prelude::*;
use std::collections::HashSet;

/// Test case for subscription-driven updates using builder pattern
pub struct PublishSubscriptionUpdateTestCase {
keys: Keys,
account_name: Option<String>,
metadata: Option<Metadata>,
new_relay_url: Option<String>,
follow_pubkeys: Option<Vec<PublicKey>>,
}

impl PublishSubscriptionUpdateTestCase {
Expand All @@ -19,6 +21,7 @@ impl PublishSubscriptionUpdateTestCase {
account_name: Some(account_name.to_string()),
metadata: None,
new_relay_url: None,
follow_pubkeys: None,
}
}

Expand All @@ -29,6 +32,7 @@ impl PublishSubscriptionUpdateTestCase {
account_name: None,
metadata: None,
new_relay_url: None,
follow_pubkeys: None,
}
}

Expand All @@ -44,6 +48,12 @@ impl PublishSubscriptionUpdateTestCase {
self
}

/// Add follow list update to the test (account-based only)
pub fn with_follow_list(mut self, follows: Vec<PublicKey>) -> Self {
self.follow_pubkeys = Some(follows);
self
}
Comment on lines +51 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Enforce “account-only” for follow-list builder or during run

Builder says “account-based only,” but nothing prevents using it for external users. Guard it to fail fast.

 pub fn with_follow_list(mut self, follows: Vec<PublicKey>) -> Self {
+    // Guard at build time to avoid misusing this path
+    if self.account_name.is_none() {
+        panic!("with_follow_list is only supported for account-based tests");
+    }
     self.follow_pubkeys = Some(follows);
     self
 }

Alternatively, prefer a graceful error in run() (see next comment) if panics in tests are undesirable.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/// Add follow list update to the test (account-based only)
pub fn with_follow_list(mut self, follows: Vec<PublicKey>) -> Self {
self.follow_pubkeys = Some(follows);
self
}
/// Add follow list update to the test (account-based only)
pub fn with_follow_list(mut self, follows: Vec<PublicKey>) -> Self {
// Guard at build time to avoid misusing this path
if self.account_name.is_none() {
panic!("with_follow_list is only supported for account-based tests");
}
self.follow_pubkeys = Some(follows);
self
}
🤖 Prompt for AI Agents
In
src/integration_tests/test_cases/subscription_processing/publish_subscription_update.rs
around lines 51–55, the with_follow_list builder claims “account-based only” but
doesn't enforce it; add validation to fail fast by checking each PublicKey in
follows is an account-type key and return/raise an error immediately (or panic
in test code) if any are external, or instead keep the builder permissive but
perform the same validation in run() and return a Result/Err so tests can fail
gracefully; implement whichever approach the test harness prefers and ensure the
error message clearly states "follow list must contain account-based public keys
only" and includes the offending key(s).


/// Setup test client with relay lists published
async fn setup_test_client(
context: &ScenarioContext,
Expand Down Expand Up @@ -149,6 +159,18 @@ impl PublishSubscriptionUpdateTestCase {
Ok(())
}

/// Publish follow list update (account-based)
async fn publish_follow_list_update(
&self,
test_client: &Client,
follows: &[PublicKey],
) -> Result<(), WhitenoiseError> {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
publish_follow_list(test_client, follows).await?;
tracing::info!("✓ Follow list published via test client");
Ok(())
}

/// Verify metadata update
async fn verify_metadata(
&self,
Expand Down Expand Up @@ -238,24 +260,62 @@ impl PublishSubscriptionUpdateTestCase {
);
Ok(())
}

/// Verify follow list processed into follows (account-based)
async fn verify_follow_list(
&self,
context: &mut ScenarioContext,
expected_follows: &[PublicKey],
) -> Result<(), WhitenoiseError> {
let Some(account_name) = &self.account_name else {
return Err(WhitenoiseError::InvalidInput(
"Contact list verification only supported for accounts".to_string(),
));
};

let account = context.get_account(account_name)?;
let follows = context.whitenoise.follows(account).await?;

let expected: HashSet<PublicKey> = expected_follows.iter().copied().collect();
let actual: HashSet<PublicKey> = follows.iter().map(|u| u.pubkey).collect();

assert!(
actual == expected,
"Account follows do not match expected follows. Missing: {:?}, Extra: {:?}",
expected.difference(&actual).collect::<Vec<_>>(),
actual.difference(&expected).collect::<Vec<_>>()
);

tracing::info!("✓ Account follow list exactly matches expected set");
Ok(())
}
}

#[async_trait]
impl TestCase for PublishSubscriptionUpdateTestCase {
async fn run(&self, context: &mut ScenarioContext) -> Result<(), WhitenoiseError> {
let has_metadata = self.metadata.is_some();
let has_relay = self.new_relay_url.is_some();
let has_follows = self.follow_pubkeys.is_some();

let updates = match (has_metadata, has_relay) {
(true, true) => "metadata and relay list",
(true, false) => "metadata",
(false, true) => "relay list",
(false, false) => {
return Err(WhitenoiseError::InvalidInput(
"No updates specified".to_string(),
))
}
};
let mut updates_parts = Vec::new();
if has_metadata {
updates_parts.push("metadata");
}
if has_relay {
updates_parts.push("relay list");
}
if has_follows {
updates_parts.push("follows");
}

if updates_parts.is_empty() {
return Err(WhitenoiseError::InvalidInput(
"No updates specified".to_string(),
));
}

let updates = updates_parts.join(", ");

// Get appropriate keys first
let test_keys = self.get_keys(context).await?;
Expand Down Expand Up @@ -289,6 +349,11 @@ impl TestCase for PublishSubscriptionUpdateTestCase {
.await?;
}

if let Some(follows) = &self.follow_pubkeys {
self.publish_follow_list_update(&test_client, follows)
.await?;
}

// Wait for processing and disconnect
tokio::time::sleep(tokio::time::Duration::from_millis(600)).await;
test_client.disconnect().await;
Expand All @@ -303,6 +368,10 @@ impl TestCase for PublishSubscriptionUpdateTestCase {
.await?;
}

if let Some(follows) = &self.follow_pubkeys {
self.verify_follow_list(context, follows).await?;
}

Ok(())
}
}
Loading