diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-28 04:27:07 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-28 04:27:07 +0000 |
| commit | eee0a521afe0492f04bee58c9a236683fb23601b (patch) | |
| tree | b0414e164de2946e39613a45f5a2724eadfe1821 /grasp-audit/src/client.rs | |
| parent | b2a43ffbd965a2b561d7b4e4c582dfeadd099ed3 (diff) | |
audit: fix shared test context to minimise events sent to production relays
Diffstat (limited to 'grasp-audit/src/client.rs')
| -rw-r--r-- | grasp-audit/src/client.rs | 26 |
1 files changed, 26 insertions, 0 deletions
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 @@ | |||
| 1 | //! Audit client for testing GRASP implementations | 1 | //! Audit client for testing GRASP implementations |
| 2 | 2 | ||
| 3 | use crate::audit::{AuditConfig, AuditEventBuilder, AuditMode}; | 3 | use crate::audit::{AuditConfig, AuditEventBuilder, AuditMode}; |
| 4 | use crate::fixtures::FixtureKind; | ||
| 4 | use anyhow::{anyhow, Result}; | 5 | use anyhow::{anyhow, Result}; |
| 5 | use nostr_sdk::prelude::*; | 6 | use nostr_sdk::prelude::*; |
| 7 | use std::collections::HashMap; | ||
| 8 | use std::sync::{Arc, Mutex}; | ||
| 6 | use std::time::Duration; | 9 | use std::time::Duration; |
| 7 | 10 | ||
| 11 | /// Type alias for the fixture cache - shared across TestContext instances | ||
| 12 | pub type FixtureCache = Arc<Mutex<HashMap<FixtureKind, Event>>>; | ||
| 13 | |||
| 8 | /// Client for auditing GRASP implementations | 14 | /// Client for auditing GRASP implementations |
| 15 | /// | ||
| 16 | /// The AuditClient owns a fixture cache that is shared across all TestContext | ||
| 17 | /// instances created from this client. This provides natural cache sharing: | ||
| 18 | /// - CLI creates one AuditClient → fixtures shared across all tests | ||
| 19 | /// - cargo test creates one AuditClient per test → fixtures isolated per test | ||
| 9 | pub struct AuditClient { | 20 | pub struct AuditClient { |
| 10 | client: Client, | 21 | client: Client, |
| 11 | pub config: AuditConfig, | 22 | pub config: AuditConfig, |
| @@ -14,6 +25,8 @@ pub struct AuditClient { | |||
| 14 | maintainer_keys: Keys, | 25 | maintainer_keys: Keys, |
| 15 | /// Recursive maintainer keys for testing recursive authorization scenarios | 26 | /// Recursive maintainer keys for testing recursive authorization scenarios |
| 16 | recursive_maintainer_keys: Keys, | 27 | recursive_maintainer_keys: Keys, |
| 28 | /// Fixture cache for TestContext instances - shared across all contexts using this client | ||
| 29 | fixture_cache: FixtureCache, | ||
| 17 | } | 30 | } |
| 18 | 31 | ||
| 19 | impl AuditClient { | 32 | impl AuditClient { |
| @@ -30,6 +43,7 @@ impl AuditClient { | |||
| 30 | keys, | 43 | keys, |
| 31 | maintainer_keys, | 44 | maintainer_keys, |
| 32 | recursive_maintainer_keys, | 45 | recursive_maintainer_keys, |
| 46 | fixture_cache: Arc::new(Mutex::new(HashMap::new())), | ||
| 33 | } | 47 | } |
| 34 | } | 48 | } |
| 35 | 49 | ||
| @@ -88,9 +102,19 @@ impl AuditClient { | |||
| 88 | keys, | 102 | keys, |
| 89 | maintainer_keys, | 103 | maintainer_keys, |
| 90 | recursive_maintainer_keys, | 104 | recursive_maintainer_keys, |
| 105 | fixture_cache: Arc::new(Mutex::new(HashMap::new())), | ||
| 91 | }) | 106 | }) |
| 92 | } | 107 | } |
| 93 | 108 | ||
| 109 | /// Get the fixture cache for TestContext usage | ||
| 110 | /// | ||
| 111 | /// This cache is shared across all TestContext instances created from this client. | ||
| 112 | /// In CLI mode (one client for all tests), fixtures are reused. | ||
| 113 | /// In test mode (one client per test), fixtures are isolated. | ||
| 114 | pub fn fixture_cache(&self) -> &FixtureCache { | ||
| 115 | &self.fixture_cache | ||
| 116 | } | ||
| 117 | |||
| 94 | /// Get the public key for this audit client | 118 | /// Get the public key for this audit client |
| 95 | pub fn public_key(&self) -> PublicKey { | 119 | pub fn public_key(&self) -> PublicKey { |
| 96 | self.keys.public_key() | 120 | self.keys.public_key() |
| @@ -488,6 +512,7 @@ mod tests { | |||
| 488 | keys: keys.clone(), | 512 | keys: keys.clone(), |
| 489 | maintainer_keys, | 513 | maintainer_keys, |
| 490 | recursive_maintainer_keys, | 514 | recursive_maintainer_keys, |
| 515 | fixture_cache: Arc::new(Mutex::new(HashMap::new())), | ||
| 491 | }; | 516 | }; |
| 492 | 517 | ||
| 493 | let _builder = client.event_builder(Kind::TextNote, "test content"); | 518 | let _builder = client.event_builder(Kind::TextNote, "test content"); |
| @@ -508,6 +533,7 @@ mod tests { | |||
| 508 | keys: keys.clone(), | 533 | keys: keys.clone(), |
| 509 | maintainer_keys, | 534 | maintainer_keys, |
| 510 | recursive_maintainer_keys, | 535 | recursive_maintainer_keys, |
| 536 | fixture_cache: Arc::new(Mutex::new(HashMap::new())), | ||
| 511 | }; | 537 | }; |
| 512 | 538 | ||
| 513 | // Create an event with a custom tag | 539 | // Create an event with a custom tag |