stygian-graph 0.9.1

High-performance graph-based web scraping engine with AI extraction, multi-modal support, and anti-bot capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Integration tests — full pipeline execution with in-memory adapters.
//!
//! These tests wire together the domain, ports, and adapters without any real I/O.
//! They use `NoopService` and in-memory cache adapters to execute realistic
//! pipeline shapes end-to-end without external dependencies.

#![allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::indexing_slicing,
    clippy::redundant_closure_for_method_calls
)]

use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Duration;

use serde_json::json;
use stygian_graph::adapters::cache::{BoundedLruCache, DashMapCache};
use stygian_graph::adapters::noop::NoopService;
use stygian_graph::adapters::resilience::{
    CircuitBreakerImpl, NoopCircuitBreaker, NoopRateLimiter, RetryPolicy,
};
use stygian_graph::application::health::{HealthReporter, HealthStatus};
use stygian_graph::application::metrics::{MetricEvent, MetricsRegistry};
use stygian_graph::application::registry::ServiceRegistry;
use stygian_graph::domain::executor::WorkerPool;
use stygian_graph::domain::graph::{DagExecutor, Edge, Node, Pipeline};
use stygian_graph::ports::{
    CachePort, CircuitBreaker, CircuitState, RateLimiter, ScrapingService, ServiceInput,
};

// ─── Helper types ─────────────────────────────────────────────────────────────

type Services = HashMap<String, Arc<dyn ScrapingService>>;

fn noop_services() -> Services {
    let mut m = Services::new();
    m.insert(
        "noop".to_string(),
        Arc::new(NoopService) as Arc<dyn ScrapingService>,
    );
    m
}

fn noop_node(id: &str) -> Node {
    Node::new(id, "noop", json!({"url": "https://example.com"}))
}

fn linear_pipeline(count: usize) -> Pipeline {
    let mut p = Pipeline::new("linear");
    for i in 0..count {
        let node_id = format!("n{i}");
        p.add_node(Node::new(node_id, "noop", json!({})));
    }
    for i in 1..count {
        let from = format!("n{}", i - 1);
        let to = format!("n{i}");
        p.add_edge(Edge::new(from, to));
    }
    p
}

// ─── Pipeline shape tests ──────────────────────────────────────────────────────

#[tokio::test]
async fn single_node_pipeline_executes() {
    let mut p = Pipeline::new("single");
    p.add_node(noop_node("fetch"));

    let executor = DagExecutor::from_pipeline(&p).expect("build executor");
    let results = executor.execute(&noop_services()).await.expect("execute");

    assert_eq!(results.len(), 1);
    assert!(results.iter().any(|r| r.node_id == "fetch"));
}

#[tokio::test]
async fn linear_two_node_pipeline_runs_sequential() {
    let mut p = Pipeline::new("linear");
    p.add_node(noop_node("a"));
    p.add_node(noop_node("b"));
    p.add_edge(Edge::new("a", "b"));

    let executor = DagExecutor::from_pipeline(&p).expect("build");
    let results = executor.execute(&noop_services()).await.expect("execute");
    assert_eq!(results.len(), 2);
}

#[tokio::test]
async fn diamond_graph_executes_all_four_nodes() {
    // top → left, top → right, left → bottom, right → bottom
    let mut p = Pipeline::new("diamond");
    for id in &["top", "left", "right", "bottom"] {
        p.add_node(noop_node(id));
    }
    p.add_edge(Edge::new("top", "left"));
    p.add_edge(Edge::new("top", "right"));
    p.add_edge(Edge::new("left", "bottom"));
    p.add_edge(Edge::new("right", "bottom"));

    let executor = DagExecutor::from_pipeline(&p).expect("build");
    let results = executor.execute(&noop_services()).await.expect("execute");
    assert_eq!(results.len(), 4);
}

#[tokio::test]
async fn pipeline_with_cycle_fails_at_construction() {
    let mut p = Pipeline::new("cyclic");
    p.add_node(noop_node("a"));
    p.add_node(noop_node("b"));
    p.add_edge(Edge::new("a", "b"));
    p.add_edge(Edge::new("b", "a")); // cycle

    assert!(DagExecutor::from_pipeline(&p).is_err());
}

#[tokio::test]
async fn missing_service_returns_error() {
    let mut p = Pipeline::new("missing");
    p.add_node(Node::new("n1", "does_not_exist", json!({})));

    let executor = DagExecutor::from_pipeline(&p).expect("build");
    let result = executor.execute(&HashMap::new()).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn ten_node_linear_pipeline_executes() {
    let p = linear_pipeline(10);
    let executor = DagExecutor::from_pipeline(&p).expect("build");
    let results = executor.execute(&noop_services()).await.expect("execute");
    assert_eq!(results.len(), 10);
}

// ─── ETL fixture ─────────────────────────────────────────────────────────────

fn etl_pipeline() -> Pipeline {
    let mut p = Pipeline::new("etl");
    p.add_node(Node::new("extract", "noop", json!({"stage": "extract"})));
    p.add_node(Node::new(
        "transform",
        "noop",
        json!({"stage": "transform"}),
    ));
    p.add_node(Node::new("load", "noop", json!({"stage": "load"})));
    p.add_edge(Edge::new("extract", "transform"));
    p.add_edge(Edge::new("transform", "load"));
    p
}

#[tokio::test]
async fn etl_pipeline_executes_all_three_stages() {
    let pipeline = etl_pipeline();
    let executor = DagExecutor::from_pipeline(&pipeline).expect("build");
    let results = executor
        .execute(&noop_services())
        .await
        .expect("execute etl");
    assert_eq!(results.len(), 3);
    for stage in &["extract", "transform", "load"] {
        assert!(
            results.iter().any(|r| r.node_id == *stage),
            "missing stage: {stage}"
        );
    }
}

// ─── ServiceRegistry tests ────────────────────────────────────────────────────

#[test]
fn registry_register_and_get_returns_service() {
    let registry = ServiceRegistry::new();
    registry.register("noop".into(), Arc::new(NoopService));
    assert!(registry.get("noop").is_some());
}

#[test]
fn registry_get_unknown_service_returns_none() {
    let registry = ServiceRegistry::new();
    assert!(registry.get("ghost").is_none());
}

#[test]
fn registry_names_returns_all_registered() {
    let registry = ServiceRegistry::new();
    registry.register("alpha".into(), Arc::new(NoopService));
    registry.register("beta".into(), Arc::new(NoopService));
    let mut names = registry.names();
    names.sort();
    assert_eq!(names, vec!["alpha", "beta"]);
}

#[test]
fn registry_deregister_removes_service() {
    let registry = ServiceRegistry::new();
    registry.register("temp".into(), Arc::new(NoopService));
    assert!(registry.get("temp").is_some());
    registry.deregister("temp");
    assert!(registry.get("temp").is_none());
}

// ─── Cache lifecycle tests ────────────────────────────────────────────────────

#[tokio::test]
async fn dashmap_cache_set_get_delete_lifecycle() {
    let cache = DashMapCache::new(Duration::from_secs(300));

    cache.set("key", "value".into(), None).await.expect("set");
    let val = cache.get("key").await.expect("get");
    assert_eq!(val, Some("value".into()));

    cache.invalidate("key").await.expect("invalidate");
    let gone = cache.get("key").await.expect("get after delete");
    assert!(gone.is_none());
}

#[tokio::test]
async fn dashmap_cache_exists_returns_correct_bool() {
    let cache = DashMapCache::new(Duration::from_secs(300));

    assert!(!cache.exists("missing").await.expect("exists"));
    cache.set("present", "v".into(), None).await.expect("set");
    assert!(cache.exists("present").await.expect("exists"));
}

#[tokio::test]
async fn lru_cache_evicts_beyond_capacity() {
    let cache = BoundedLruCache::new(NonZeroUsize::new(2).unwrap());

    cache.set("a", "1".into(), None).await.expect("set a");
    cache.set("b", "2".into(), None).await.expect("set b");
    cache.set("c", "3".into(), None).await.expect("set c"); // evicts "a"

    let a = cache.get("a").await.expect("get a");
    assert!(a.is_none(), "LRU should have evicted a");
    let c = cache.get("c").await.expect("get c");
    assert_eq!(c, Some("3".into()));
}

// ─── Circuit breaker tests ────────────────────────────────────────────────────

#[test]
fn circuit_breaker_opens_after_threshold_failures() {
    let cb = CircuitBreakerImpl::new(3, Duration::from_secs(60));
    assert_eq!(cb.state(), CircuitState::Closed);

    cb.record_failure();
    cb.record_failure();
    cb.record_failure();

    assert_ne!(
        cb.state(),
        CircuitState::Closed,
        "should be open after 3 failures"
    );
}

#[test]
fn circuit_breaker_closes_after_success() {
    let cb = CircuitBreakerImpl::new(1, Duration::from_secs(0));
    cb.record_failure();
    assert_ne!(cb.state(), CircuitState::Closed);

    cb.record_success();
    assert_eq!(
        cb.state(),
        CircuitState::Closed,
        "success resets the breaker"
    );
}

#[test]
fn noop_circuit_breaker_always_closed() {
    let cb = NoopCircuitBreaker;
    cb.record_failure();
    cb.record_failure();
    assert_eq!(cb.state(), CircuitState::Closed);
}

#[tokio::test]
async fn noop_rate_limiter_always_allows() {
    let rl = NoopRateLimiter;
    for _ in 0..10 {
        assert!(rl.check_rate_limit("any").await.expect("rate limit check"));
    }
}

// ─── RetryPolicy tests ────────────────────────────────────────────────────────

#[test]
fn retry_policy_delays_increase_then_cap() {
    let policy = RetryPolicy::new(5, Duration::from_millis(100), Duration::from_millis(500))
        .with_jitter_ms(0);

    let d0 = policy.delay_for(0);
    let d1 = policy.delay_for(1);
    let d_big = policy.delay_for(20);

    assert!(d1 >= d0, "delay should grow: d0={d0:?} d1={d1:?}");
    assert!(
        d_big <= Duration::from_millis(500),
        "delay capped at max: d_big={d_big:?}"
    );
}

// ─── Metrics + Health composition ────────────────────────────────────────────

#[test]
fn metrics_records_are_visible_in_snapshot() {
    let m = MetricsRegistry::new();
    m.record(MetricEvent::RequestStarted {
        service: "http".into(),
    });
    m.record(MetricEvent::RequestCompleted {
        service: "http".into(),
        duration_ms: 80,
        success: true,
    });
    m.record(MetricEvent::CacheAccess { hit: true });

    let snap = m.snapshot();
    assert_eq!(snap.requests_total, 1);
    assert_eq!(snap.errors_total, 0);
    assert_eq!(snap.cache_hits_total, 1);
}

#[test]
fn health_reporter_composition() {
    let health = HealthReporter::new();
    health.register("http_pool", HealthStatus::Healthy);
    health.register("cache", HealthStatus::Healthy);

    let report = health.report();
    assert!(report.is_ready());
    assert!(report.is_live());
}

#[test]
fn prometheus_text_contains_expected_metric_families() {
    let m = MetricsRegistry::new();
    m.record(MetricEvent::RequestStarted {
        service: "svc".into(),
    });
    m.record(MetricEvent::PipelineExecuted {
        pipeline_id: "p".into(),
        duration_ms: 300,
        success: true,
    });

    let text = m.render_prometheus();
    for expected in &[
        "stygian_requests_total",
        "stygian_errors_total",
        "stygian_pipelines_total",
        "stygian_active_workers",
        "stygian_queue_depth",
    ] {
        assert!(
            text.contains(expected),
            "prometheus text missing: {expected}"
        );
    }
}

// ─── Node / Edge domain validation ───────────────────────────────────────────

#[test]
fn node_with_empty_id_fails_validation() {
    assert!(Node::new("", "noop", json!({})).validate().is_err());
}

#[test]
fn node_with_empty_service_fails_validation() {
    assert!(Node::new("id", "", json!({})).validate().is_err());
}

#[test]
fn edge_with_empty_from_fails_validation() {
    assert!(Edge::new("", "b").validate().is_err());
}

#[test]
fn valid_pipeline_with_ten_nodes_passes_validation() {
    assert!(linear_pipeline(10).validate().is_ok());
}

// ─── WorkerPool scraping-focused tests ───────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn worker_pool_submits_noop_service_successfully() {
    let pool = WorkerPool::new(4, 32);
    let svc = Arc::new(NoopService) as Arc<dyn ScrapingService>;
    let input = ServiceInput {
        url: "https://example.com".into(),
        params: json!({}),
    };

    let output = pool.submit(svc, input).await.expect("submit");
    assert!(output.metadata["success"].as_bool().unwrap_or(false));

    pool.shutdown().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn worker_pool_handles_concurrent_submissions() {
    use futures::future::join_all;

    let pool = Arc::new(WorkerPool::new(4, 64));
    let mut handles = Vec::new();

    for _ in 0..20 {
        let p = Arc::clone(&pool);
        let svc = Arc::new(NoopService) as Arc<dyn ScrapingService>;
        let input = ServiceInput {
            url: "https://example.com".into(),
            params: json!({}),
        };
        handles.push(tokio::spawn(async move { p.submit(svc, input).await }));
    }

    let results = join_all(handles).await;
    let successes = results
        .iter()
        .filter(|r| r.as_ref().map(Result::is_ok).unwrap_or(false))
        .count();
    assert_eq!(successes, 20, "all 20 submissions must succeed");
}