stemedb/applications/aphoria/tests/scale_adaptive_test.rs
jordan 02ecac9a07 fix: merge upstream 10 commits, fix DashMap deadlock, deterministic sim ingestion
Merged 10 upstream commits (MemTable, read-your-writes tests, feed endpoint,
security hardening, signed assertions, source registry, dashboard enhancements)
and fixed all test failures across the full workspace (2656/2656 passing).

Key fixes:
- fix(cluster): DashMap deadlock in swim.rs suspect_node/fail_node/alive_node
  - DashMap::get_mut RefMut + iter() on same map = non-reentrant write lock deadlock
  - Fix: extract clone in scoped block to drop RefMut before calling update_node_gauges()
  - 6 previously-hanging SWIM tests now pass in <2s
- fix(sim): replace background-task+polling ingestion with synchronous process_pending()
  - smoke_high_volume_simulation was CPU-starved under 2656 parallel tests
  - Removed ingestor.start() + wait_until_ingested() pattern throughout sim
  - All arena functions now call ingestor.process_pending() directly (deterministic)
- fix(test): v2 signature helper used wrong hash (rkyv vs canonical compute_content_hash_v2)
- fix(test): quota test signed "test" but v1 requires "subject:predicate" format
- fix(test): http_validation now accepts 400 for valid-format-but-invalid-crypto hex
- fix(test): scale_adaptive micro tier assertions updated (auto_promote upstream change)
- config: add nextest.toml with slow-timeout for background-task-tests group

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 20:27:32 -07:00

132 lines
4.8 KiB
Rust

//! Integration tests for scale-adaptive promotion thresholds.
//!
//! Verifies that promotion criteria automatically adjust based on organization size,
//! enabling small teams to see value immediately while maintaining quality gates
//! for larger organizations.
use aphoria::corpus::thresholds::{PromotionDecision, ScaleAdaptiveThresholds, ScaleTier};
use stemedb_core::types::SourceClass;
#[test]
fn test_micro_team_sees_patterns() {
let thresholds = ScaleAdaptiveThresholds::default();
// Micro team with 3 projects, pattern appears in 2
let decision = thresholds.evaluate(
2, // project_count
3, // total_projects
false, // no authority
None,
);
// With adaptive thresholds:
// - Scale tier: Micro (1-5 projects)
// - Emerging min_projects: max(2, 0.50*3) = max(2, 1.5) = 2
// - Adoption rate: 2/3 = 67% >= 50%
// Micro emerging auto-promotes for immediate visibility
assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Community));
}
#[test]
fn test_micro_team_regulatory_disabled() {
let thresholds = ScaleAdaptiveThresholds::default();
// Micro team with 5 projects, pattern appears in all 5 with RFC match
let decision = thresholds.evaluate(
5, // project_count
5, // total_projects
true, // has authority
Some("rfc://1234"), // RFC scheme
);
// Regulatory tier is disabled for micro teams
// Falls through to emerging tier, which auto-promotes for immediate visibility
assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Community));
}
#[test]
fn test_small_team_enables_all_tiers() {
let thresholds = ScaleAdaptiveThresholds::default();
// Small team with 10 projects, pattern in 9 with RFC match
let decision = thresholds.evaluate(
9, // project_count
10, // total_projects
true, // has authority
Some("rfc://5246"), // RFC scheme
);
// Small tier regulatory: max(5, 0.90*10) = max(5, 9) = 9
// Adoption rate: 9/10 = 90% >= 90%
// Should auto-promote to regulatory
assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Regulatory));
}
#[test]
fn test_enterprise_maintains_strict_thresholds() {
let thresholds = ScaleAdaptiveThresholds::default();
// Enterprise with 1000 projects, pattern in 950 with RFC match
let decision = thresholds.evaluate(
950, // project_count
1000, // total_projects
true, // has authority
Some("rfc://9110"), // RFC scheme
);
// Enterprise tier: max(100, 0.95*1000) = max(100, 950) = 950
// Adoption rate: 950/1000 = 95% >= 95%
// Should auto-promote to regulatory (backward compatible behavior)
assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Regulatory));
}
#[test]
fn test_scale_tier_progression() {
// Verify scale tier boundaries
assert_eq!(ScaleTier::from_total_projects(1), ScaleTier::Micro);
assert_eq!(ScaleTier::from_total_projects(5), ScaleTier::Micro);
assert_eq!(ScaleTier::from_total_projects(6), ScaleTier::Small);
assert_eq!(ScaleTier::from_total_projects(25), ScaleTier::Small);
assert_eq!(ScaleTier::from_total_projects(26), ScaleTier::Medium);
assert_eq!(ScaleTier::from_total_projects(100), ScaleTier::Medium);
assert_eq!(ScaleTier::from_total_projects(101), ScaleTier::Large);
assert_eq!(ScaleTier::from_total_projects(500), ScaleTier::Large);
assert_eq!(ScaleTier::from_total_projects(501), ScaleTier::Enterprise);
}
#[test]
fn test_adaptive_floor_prevents_noise() {
let thresholds = ScaleAdaptiveThresholds::default();
// Micro team with 3 projects, pattern appears in only 1
let decision = thresholds.evaluate(
1, // project_count
3, // total_projects
false, // no authority
None,
);
// Even though 1/3 = 33% meets percentage (50% of 3 = 1.5),
// the floor of 2 prevents single-project noise
// Adoption rate: 1/3 = 33% < 50%
assert_eq!(decision, PromotionDecision::Skip);
}
#[test]
fn test_medium_team_clinical_tier() {
let thresholds = ScaleAdaptiveThresholds::default();
// Medium team with 50 projects, pattern in 38 with OWASP match
let decision = thresholds.evaluate(
38, // project_count
50, // total_projects
true, // has authority
Some("owasp://top-10/a01"), // OWASP scheme
);
// Medium tier clinical: max(10, 0.75*50) = max(10, 37.5) = 38
// Adoption rate: 38/50 = 76% >= 75%
// Should auto-promote to clinical
assert_eq!(decision, PromotionDecision::AutoPromote(SourceClass::Clinical));
}