From eee0a521afe0492f04bee58c9a236683fb23601b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 28 Nov 2025 04:27:07 +0000 Subject: audit: fix shared test context to minimise events sent to production relays --- grasp-audit/src/client.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'grasp-audit/src/client.rs') diff --git a/grasp-audit/src/client.rs b/grasp-audit/src/client.rs index 8b96f4f..4f3401c 100644 --- a/grasp-audit/src/client.rs +++ b/grasp-audit/src/client.rs @@ -1,11 +1,22 @@ //! Audit client for testing GRASP implementations use crate::audit::{AuditConfig, AuditEventBuilder, AuditMode}; +use crate::fixtures::FixtureKind; use anyhow::{anyhow, Result}; use nostr_sdk::prelude::*; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; use std::time::Duration; +/// Type alias for the fixture cache - shared across TestContext instances +pub type FixtureCache = Arc>>; + /// Client for auditing GRASP implementations +/// +/// The AuditClient owns a fixture cache that is shared across all TestContext +/// instances created from this client. This provides natural cache sharing: +/// - CLI creates one AuditClient → fixtures shared across all tests +/// - cargo test creates one AuditClient per test → fixtures isolated per test pub struct AuditClient { client: Client, pub config: AuditConfig, @@ -14,6 +25,8 @@ pub struct AuditClient { maintainer_keys: Keys, /// Recursive maintainer keys for testing recursive authorization scenarios recursive_maintainer_keys: Keys, + /// Fixture cache for TestContext instances - shared across all contexts using this client + fixture_cache: FixtureCache, } impl AuditClient { @@ -30,6 +43,7 @@ impl AuditClient { keys, maintainer_keys, recursive_maintainer_keys, + fixture_cache: Arc::new(Mutex::new(HashMap::new())), } } @@ -88,9 +102,19 @@ impl AuditClient { keys, maintainer_keys, recursive_maintainer_keys, + fixture_cache: Arc::new(Mutex::new(HashMap::new())), }) } + /// Get the fixture cache for TestContext usage + /// + /// This cache is shared across all TestContext instances created from this client. + /// In CLI mode (one client for all tests), fixtures are reused. + /// In test mode (one client per test), fixtures are isolated. + pub fn fixture_cache(&self) -> &FixtureCache { + &self.fixture_cache + } + /// Get the public key for this audit client pub fn public_key(&self) -> PublicKey { self.keys.public_key() @@ -488,6 +512,7 @@ mod tests { keys: keys.clone(), maintainer_keys, recursive_maintainer_keys, + fixture_cache: Arc::new(Mutex::new(HashMap::new())), }; let _builder = client.event_builder(Kind::TextNote, "test content"); @@ -508,6 +533,7 @@ mod tests { keys: keys.clone(), maintainer_keys, recursive_maintainer_keys, + fixture_cache: Arc::new(Mutex::new(HashMap::new())), }; // Create an event with a custom tag -- cgit v1.2.3