ptox 0.6.0

A language-neutral SDK for building file-search clients across many networks.
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
use async_trait::async_trait;
use futures::StreamExt;
use ptox::{
    Address, Capabilities, Engine, FileQuery, FileResult, ProviderError, ResultStream,
    SearchProvider,
};
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

struct Mock {
    result: FileResult,
}

impl SearchProvider for Mock {
    fn name(&self) -> &str {
        &self.result.provider
    }

    fn search(&self, _query: &FileQuery) -> ResultStream<'_> {
        let result = self.result.clone();
        Box::pin(async_stream::stream! { yield Ok(result); })
    }
}

struct Failing;

impl SearchProvider for Failing {
    fn name(&self) -> &str {
        "failing"
    }

    fn search(&self, _query: &FileQuery) -> ResultStream<'_> {
        Box::pin(async_stream::stream! { yield Err(ProviderError::new("refused", "nope")); })
    }
}

/// Accepts only hash queries (like an infohash-only network).
struct HashOnly;

#[async_trait]
impl SearchProvider for HashOnly {
    fn name(&self) -> &str {
        "hash-only"
    }

    async fn capabilities(&self) -> Capabilities {
        Capabilities {
            query: vec!["hashes".to_owned()],
            requires: vec!["hashes".to_owned()],
            schemes: vec!["magnet".to_owned()],
        }
    }

    fn search(&self, _query: &FileQuery) -> ResultStream<'_> {
        Box::pin(async_stream::stream! {
            yield Ok(FileResult::new(
                "hash-only",
                vec![Address::new("magnet", "magnet:?xt=urn:btih:abc")],
            ));
        })
    }
}

/// Pages its fixed list using the query's `offset`/`limit`, as a real backend would.
struct Paged {
    results: Vec<FileResult>,
}

impl SearchProvider for Paged {
    fn name(&self) -> &str {
        "paged"
    }

    fn search(&self, query: &FileQuery) -> ResultStream<'_> {
        let offset = query.offset.unwrap_or(0) as usize;
        let limit = query
            .limit
            .map(|limit| limit as usize)
            .unwrap_or(usize::MAX);
        let page: Vec<FileResult> = self
            .results
            .iter()
            .skip(offset)
            .take(limit)
            .cloned()
            .collect();
        Box::pin(async_stream::stream! {
            for result in page {
                yield Ok(result);
            }
        })
    }
}

/// Counts how often its capabilities are computed and invalidated.
struct Counting {
    calls: Arc<AtomicUsize>,
    invalidations: Arc<AtomicUsize>,
}

impl Counting {
    fn new() -> (Self, Arc<AtomicUsize>, Arc<AtomicUsize>) {
        let calls = Arc::new(AtomicUsize::new(0));
        let invalidations = Arc::new(AtomicUsize::new(0));
        (
            Self {
                calls: calls.clone(),
                invalidations: invalidations.clone(),
            },
            calls,
            invalidations,
        )
    }
}

#[async_trait]
impl SearchProvider for Counting {
    fn name(&self) -> &str {
        "counting"
    }

    async fn capabilities(&self) -> Capabilities {
        self.calls.fetch_add(1, Ordering::SeqCst);
        Capabilities {
            query: vec!["text".to_owned()],
            ..Capabilities::default()
        }
    }

    fn invalidate_capabilities(&self) {
        self.invalidations.fetch_add(1, Ordering::SeqCst);
    }

    fn search(&self, _query: &FileQuery) -> ResultStream<'_> {
        Box::pin(async_stream::stream! {
            yield Ok(FileResult::new("counting", vec![Address::new("https", "https://x/f")]));
        })
    }
}

fn result(provider: &str, sha: &str) -> FileResult {
    let mut result = FileResult::new(
        provider,
        vec![Address::new("https", format!("https://{provider}/f"))],
    );
    result.title = Some("f.bin".to_owned());
    result.size = Some(10);
    result.hashes = Some(HashMap::from([("sha256".to_owned(), sha.to_owned())]));
    result
}

#[tokio::test]
async fn capability_cache_is_opt_in_and_avoids_reprobing() {
    // Without the cache, capabilities are computed once per search.
    let (provider, calls, _) = Counting::new();
    let engine = Engine::new(vec![Arc::new(provider) as Arc<dyn SearchProvider>]);
    engine.search(FileQuery::new("x")).await;
    engine.search(FileQuery::new("x")).await;
    assert_eq!(calls.load(Ordering::SeqCst), 2);

    // With the cache, the second search reuses the first answer.
    let (provider, calls, _) = Counting::new();
    let engine =
        Engine::new(vec![Arc::new(provider) as Arc<dyn SearchProvider>]).with_capability_cache();
    engine.search(FileQuery::new("x")).await;
    engine.search(FileQuery::new("x")).await;
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn search_stream_also_uses_the_capability_cache() {
    let (provider, calls, _) = Counting::new();
    let engine =
        Engine::new(vec![Arc::new(provider) as Arc<dyn SearchProvider>]).with_capability_cache();

    let mut stream = Box::pin(engine.search_stream(FileQuery::new("x")).await);
    while stream.next().await.is_some() {}
    let mut stream = Box::pin(engine.search_stream(FileQuery::new("x")).await);
    while stream.next().await.is_some() {}

    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn invalidate_capabilities_reprobes_and_delegates_to_providers() {
    let (provider, calls, invalidations) = Counting::new();
    let engine =
        Engine::new(vec![Arc::new(provider) as Arc<dyn SearchProvider>]).with_capability_cache();

    engine.search(FileQuery::new("x")).await;
    assert_eq!(calls.load(Ordering::SeqCst), 1);

    engine.invalidate_capabilities();
    assert_eq!(invalidations.load(Ordering::SeqCst), 1);

    engine.search(FileQuery::new("x")).await;
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn probe_uses_the_capability_cache() {
    let (provider, calls, _) = Counting::new();
    let engine =
        Engine::new(vec![Arc::new(provider) as Arc<dyn SearchProvider>]).with_capability_cache();

    engine.probe(FileQuery::new("x")).await;
    engine.probe(FileQuery::new("x")).await;
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn engine_searches_and_merges_across_providers() {
    let providers: Vec<Arc<dyn SearchProvider>> = vec![
        Arc::new(Mock {
            result: result("a", "deadbeef"),
        }),
        Arc::new(Mock {
            result: result("b", "deadbeef"),
        }),
    ];
    let engine = Engine::new(providers);
    let report = engine.search(FileQuery::new("x")).await;

    assert_eq!(report.results.len(), 1);
    assert_eq!(report.results[0].providers, vec!["a", "b"]);
    assert!(report.failures.is_empty());
}

#[tokio::test]
async fn engine_passes_paging_selectors_to_providers() {
    let results = (0..5)
        .map(|i| {
            let mut result = FileResult::new(
                "paged",
                vec![Address::new("https", format!("https://paged/file-{i}"))],
            );
            result.hashes = Some(HashMap::from([("sha256".to_owned(), format!("{i:064x}"))]));
            result
        })
        .collect();
    let engine = Engine::new(vec![Arc::new(Paged { results }) as Arc<dyn SearchProvider>]);

    // Page one: the provider skips 0 and returns 2.
    let first = engine
        .search(FileQuery::new("x").with_offset(0).with_limit(2))
        .await;
    let mut first_uris: Vec<&str> = first
        .results
        .iter()
        .map(|result| result.addresses[0].uri.as_str())
        .collect();
    first_uris.sort();
    assert_eq!(
        first_uris,
        vec!["https://paged/file-0", "https://paged/file-1"]
    );

    // Page two: the same query with an offset returns the next slice, without
    // the previous page having to be fetched and discarded client-side.
    let second = engine
        .search(FileQuery::new("x").with_offset(2).with_limit(2))
        .await;
    let mut second_uris: Vec<&str> = second
        .results
        .iter()
        .map(|result| result.addresses[0].uri.as_str())
        .collect();
    second_uris.sort();
    assert_eq!(
        second_uris,
        vec!["https://paged/file-2", "https://paged/file-3"]
    );
}

#[tokio::test]
async fn engine_lists_provider_capabilities_and_reuses_the_cache() {
    let (provider, calls, _) = Counting::new();
    let engine = Engine::new(vec![
        Arc::new(provider) as Arc<dyn SearchProvider>,
        Arc::new(HashOnly),
    ])
    .with_capability_cache();

    let first = engine.provider_capabilities().await;
    let second = engine.provider_capabilities().await;

    assert_eq!(first.len(), 2);
    assert_eq!(first[0].provider, "counting");
    assert!(first[0].answered);
    assert_eq!(first[0].capabilities.query, vec!["text"]);
    assert_eq!(first[1].provider, "hash-only");
    assert_eq!(first[1].capabilities.schemes, vec!["magnet"]);

    // The opt-in memo is reused by the second listing, so the provider's
    // capabilities are computed once.
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    assert_eq!(second[0].capabilities, first[0].capabilities);
}

#[tokio::test]
async fn engine_reports_provider_failures() {
    let providers: Vec<Arc<dyn SearchProvider>> = vec![
        Arc::new(Mock {
            result: result("a", "1111"),
        }),
        Arc::new(Failing),
    ];
    let engine = Engine::new(providers);
    let report = engine.search(FileQuery::new("x")).await;

    assert_eq!(report.results.len(), 1);
    assert_eq!(report.failures.len(), 1);
    assert_eq!(report.failures[0].provider, "failing");
}

#[tokio::test]
async fn engine_skips_incapable_providers() {
    let providers: Vec<Arc<dyn SearchProvider>> = vec![
        Arc::new(Mock {
            result: result("a", "1111"),
        }),
        Arc::new(HashOnly),
    ];
    let engine = Engine::new(providers);

    let keyword = engine.search(FileQuery::new("annual report")).await;
    assert_eq!(keyword.skipped, vec!["hash-only"]);
    assert_eq!(keyword.results.len(), 1);

    let mut hash_query = FileQuery::new("");
    hash_query.hashes = Some(HashMap::from([("infohash".to_owned(), "abc".to_owned())]));
    let hash = engine.search(hash_query).await;
    assert!(hash.skipped.is_empty());
}

#[tokio::test]
async fn engine_reports_per_provider_metrics() {
    let providers: Vec<Arc<dyn SearchProvider>> = vec![
        Arc::new(Mock {
            result: result("a", "1111"),
        }),
        Arc::new(Mock {
            result: result("b", "2222"),
        }),
        Arc::new(Failing),
    ];
    let engine = Engine::new(providers);
    let report = engine.search(FileQuery::new("x")).await;

    let metrics = report.metrics;
    assert_eq!(metrics.len(), 3);
    // Sorted by provider name: a, b, failing.
    assert_eq!(metrics[0].provider, "a");
    assert_eq!(metrics[0].results, 1);
    assert_eq!(metrics[0].failures, 0);
    assert_eq!(metrics[1].provider, "b");
    assert_eq!(metrics[1].results, 1);
    assert_eq!(metrics[2].provider, "failing");
    assert_eq!(metrics[2].results, 0);
    assert_eq!(metrics[2].failures, 1);
}

#[tokio::test]
async fn engine_streams_outcomes_and_reports_skipped() {
    let providers: Vec<Arc<dyn SearchProvider>> = vec![
        Arc::new(Mock {
            result: result("a", "1111"),
        }),
        Arc::new(HashOnly),
    ];
    let engine = Engine::new(providers);

    let stream = engine.search_stream(FileQuery::new("x")).await;
    assert_eq!(stream.skipped, vec!["hash-only"]);

    let mut stream = Box::pin(stream);
    let mut results = 0;
    while let Some(outcome) = stream.next().await {
        if outcome.as_result().is_some() {
            results += 1;
        }
    }
    assert_eq!(results, 1);
}

#[tokio::test]
async fn engine_reports_invalid_provider_results_as_failures() {
    struct Raw;

    impl SearchProvider for Raw {
        fn name(&self) -> &str {
            "raw"
        }

        fn search(&self, _query: &FileQuery) -> ResultStream<'_> {
            Box::pin(async_stream::stream! {
                yield Ok(FileResult::new("raw", Vec::new()));
                yield Ok(FileResult::new(
                    "raw",
                    vec![Address::new("https", "https://example.org/ok")],
                ));
            })
        }
    }

    let report = Engine::new(vec![Arc::new(Raw) as Arc<dyn SearchProvider>])
        .search(FileQuery::new("x"))
        .await;

    // The address-less result is rejected, the valid one is merged.
    assert_eq!(report.results.len(), 1);
    assert_eq!(report.failures.len(), 1);
    assert_eq!(report.failures[0].provider, "raw");
    assert_eq!(report.failures[0].error.code, ptox::ErrorCode::malformed());
}