tail-fin-common 0.7.7

Shared infrastructure for tail-fin: error types, page_fetch, cookies, CDP helpers
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Library-style helpers for endpoint discovery / probing scripts.
//!
//! Extracted from duplicated logic in:
//! - `crates/tail-fin-cli/examples/discover_nansen_api.rs`
//! - `crates/tail-fin-cli/examples/discover_sa_api.rs`
//! - PR #1's `crates/tail-fin-sa/tests/sa_explore_browser.rs` (PerimeterX trick)
//!
//! Each function is a pure utility — no enforced flow. Discoverer scripts
//! pick what they need and assemble themselves.
//!
//! Top-level helpers (`summarize_json`, `FixtureWriter`) have no extra
//! dependencies. Browser-mode helpers (`browser::delete_perimeterx_cookies`,
//! `browser::drain_captured_responses_as_map`) are gated on the existing
//! `browser` feature.

use serde_json::Value;
use std::collections::BTreeMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

/// One-line summary of a JSON response body for probe-loop logs.
///
/// Picks out the shape signals that matter when scanning probe output:
/// top-level keys, length of `data` array, presence/length of `included`
/// array, root type for non-object payloads.
///
/// Format examples:
/// - `keys=["data", "meta"] data:[5]` (JSON:API list response)
/// - `keys=["data", "included"] data:{obj} included:[3]` (JSON:API single)
/// - `array[12]` (raw array)
/// - `scalar` / `null`
pub fn summarize_json(body: &Value) -> String {
    match body {
        Value::Object(map) => {
            let keys: Vec<&str> = map.keys().take(8).map(String::as_str).collect();
            let data_shape = match map.get("data") {
                Some(Value::Array(a)) => format!(" data:[{}]", a.len()),
                Some(Value::Object(_)) => " data:{obj}".to_string(),
                _ => String::new(),
            };
            let included_shape = match map.get("included") {
                Some(Value::Array(a)) => format!(" included:[{}]", a.len()),
                _ => String::new(),
            };
            format!("keys={:?}{}{}", keys, data_shape, included_shape)
        }
        Value::Array(a) => format!("array[{}]", a.len()),
        Value::String(_) | Value::Number(_) | Value::Bool(_) => "scalar".to_string(),
        Value::Null => "null".to_string(),
    }
}

/// Writes captured response bodies to a directory with safe filenames
/// and a final `_index.json` manifest mapping each fixture back to its
/// source URL key, capturing page, row count, and size.
///
/// Use:
/// ```ignore
/// use tail_fin_common::explore::FixtureWriter;
///
/// let mut fw = FixtureWriter::new("crates/tail-fin-foo/tests/fixtures/discovered")?;
/// fw.write("/api/v3/symbols/AAPL/ratings", &body, "symbol_root")?;
/// fw.finalize()?; // writes _index.json
/// ```
pub struct FixtureWriter {
    dir: PathBuf,
    /// Keyed on slugified filename — last write wins (mirrors the
    /// filesystem reality, where `fs::write` overwrites). Avoids the
    /// "two index rows pointing at one file with different sizes"
    /// hazard that a `Vec<IndexEntry>` would have on dup keys.
    index: BTreeMap<String, IndexEntry>,
}

#[derive(Debug, serde::Serialize)]
struct IndexEntry {
    /// Original URL/path key as captured (preserves `?query=...` for traceability).
    key: String,
    /// Tag identifying which page/trigger first captured this response.
    source_page: String,
    /// Filename within the fixture directory.
    fixture_file: String,
    /// Length of `data` array if present, else 0.
    data_array_len: usize,
    /// Compact-serialized body size in bytes.
    body_size_bytes: usize,
}

impl FixtureWriter {
    /// Open (or create) the target directory.
    pub fn new(dir: impl AsRef<Path>) -> io::Result<Self> {
        let dir = dir.as_ref().to_path_buf();
        fs::create_dir_all(&dir)?;
        Ok(Self {
            dir,
            index: BTreeMap::new(),
        })
    }

    /// Write one fixture. Returns the absolute path written.
    ///
    /// The `key` is typically a URL path with optional `?query`; it gets
    /// slugified into a filesystem-safe filename. Different query strings
    /// on the same path produce different files (disambiguated via short
    /// hash of the query) so we don't overwrite e.g.
    /// `/estimates?period_type=quarterly` with `?period_type=annual`.
    ///
    /// **Duplicate keys:** if two `write` calls use the same key, the second
    /// overwrites the first on disk AND in the index. If distinct keys collide
    /// after slugification, the later key gets a deterministic hash suffix so
    /// both fixtures are preserved.
    pub fn write(&mut self, key: &str, body: &Value, source_page: &str) -> io::Result<PathBuf> {
        let (path_part, query_part) = key.split_once('?').unwrap_or((key, ""));
        let safe = slugify_path(path_part);
        let qhash = if query_part.is_empty() {
            String::new()
        } else {
            format!("__q{}", short_hash(query_part))
        };
        let base = format!("{safe}{qhash}");
        let mut fname = format!("{base}.json");
        if self.index.get(&fname).is_some_and(|entry| entry.key != key) {
            let key_hash = short_hash(key);
            fname = format!("{base}__k{key_hash}.json");
            let mut n = 2usize;
            while self.index.get(&fname).is_some_and(|entry| entry.key != key) {
                fname = format!("{base}__k{key_hash}_{n}.json");
                n += 1;
            }
        }
        let dst = self.dir.join(&fname);
        // serde_json::Value is always serializable — `to_string_pretty`
        // can only fail on numeric edge cases (NaN/Inf) that Value can't
        // even hold. Panic on the impossible rather than silently writing
        // an empty file.
        let pretty =
            serde_json::to_string_pretty(body).expect("serde_json::Value is always serializable");
        fs::write(&dst, &pretty)?;

        let data_array_len = body
            .get("data")
            .and_then(|d| d.as_array())
            .map(|a| a.len())
            .unwrap_or(0);
        let body_size_bytes = serde_json::to_string(body)
            .expect("serde_json::Value is always serializable")
            .len();

        // Last-write-wins on duplicate slugified filename — mirrors the
        // filesystem reality (we just overwrote whatever was on disk).
        self.index.insert(
            fname.clone(),
            IndexEntry {
                key: key.to_string(),
                source_page: source_page.to_string(),
                fixture_file: fname,
                data_array_len,
                body_size_bytes,
            },
        );
        Ok(dst)
    }

    /// Number of fixtures written so far (excluding `_index.json`).
    pub fn len(&self) -> usize {
        self.index.len()
    }

    pub fn is_empty(&self) -> bool {
        self.index.is_empty()
    }

    /// Write the `_index.json` manifest and return its path.
    /// Consumes the writer — no more writes after this.
    ///
    /// Manifest is a JSON array of entries sorted by fixture filename
    /// (deterministic — friendly to git diffs).
    pub fn finalize(self) -> io::Result<PathBuf> {
        let path = self.dir.join("_index.json");
        let entries: Vec<&IndexEntry> = self.index.values().collect();
        let json =
            serde_json::to_string_pretty(&entries).expect("IndexEntry is always serializable");
        fs::write(&path, json)?;
        Ok(path)
    }
}

/// Slugify a URL path into a filesystem-safe filename component.
///
/// Handles characters that are illegal on common filesystems
/// (`/`, `\\`, `:`, `[`, `]`, `<`, `>`, `"`, `|`, `?`, `*`) plus ASCII
/// control characters. Also avoids Windows reserved device basenames such as
/// `CON`, `PRN`, `COM1`, and `LPT1`.
fn slugify_path(path: &str) -> String {
    let mut slug: String = path
        .trim_matches(|c| c == '/' || c == '\\')
        .chars()
        .map(|c| match c {
            '/' | '\\' | ':' => '-',
            '[' | ']' | '<' | '>' | '"' | '|' | '?' | '*' => '_',
            c if c.is_ascii_control() => '_',
            other => other,
        })
        .collect();

    if slug.is_empty() {
        slug.push('_');
    }

    if is_windows_reserved_basename(&slug) {
        slug.insert(0, '_');
    }

    slug
}

fn is_windows_reserved_basename(name: &str) -> bool {
    let basename = name
        .split('.')
        .next()
        .unwrap_or(name)
        .trim_end_matches([' ', '.'])
        .to_ascii_uppercase();

    matches!(basename.as_str(), "CON" | "PRN" | "AUX" | "NUL")
        || basename
            .strip_prefix("COM")
            .and_then(|n| n.parse::<u8>().ok())
            .is_some_and(|n| (1..=9).contains(&n))
        || basename
            .strip_prefix("LPT")
            .and_then(|n| n.parse::<u8>().ok())
            .is_some_and(|n| (1..=9).contains(&n))
}

/// Stable short hash for fixture filename disambiguation.
///
/// Uses FNV-1a rather than Rust's `DefaultHasher` because these hashes become
/// part of checked-in fixture filenames; they must not change across Rust
/// toolchain upgrades.
fn short_hash(s: &str) -> String {
    const FNV_OFFSET: u64 = 0xcbf29ce484222325;
    const FNV_PRIME: u64 = 0x00000100000001b3;

    let mut hash = FNV_OFFSET;
    for byte in s.as_bytes() {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    format!("{:08x}", hash & 0xFFFFFFFF)
}

#[cfg(feature = "browser")]
pub mod browser {
    //! Browser-session helpers for endpoint discovery.

    use crate::TailFinError;
    use night_fury_core::BrowserSession;
    use serde_json::Value;
    use std::collections::BTreeMap;

    /// PerimeterX cookie names — bound to the TLS fingerprint that issued
    /// them, so replaying across browser installations causes captcha.
    pub const PX_COOKIE_NAMES: &[&str] = &["_px3", "_pxvid", "pxcts", "_pxde"];

    /// Delete PerimeterX cookies via CDP `Network.deleteCookies`
    /// (`BrowserSession::delete_cookie`). Returns the number of successful
    /// delete calls (4 names × 2 domain forms = up to 8 attempts; counts
    /// only the ones the CDP layer accepted).
    ///
    /// **When to call:** AFTER navigating to a page that successfully
    /// loaded with stale PX cookies present (the page boot let PX
    /// establish whatever session state it needs), BEFORE making in-page
    /// `fetch()` calls to data APIs. Calling on a fresh-launched browser
    /// with no PX history will not unblock anything — PX captchas the
    /// first request anyway.
    ///
    /// **Why this works:** discovered during PR #1 SA exploration
    /// (2026-04-28). PX is more lenient with "no PX cookies" than
    /// "stale fingerprint-mismatched PX cookies" once a successful
    /// page navigate has happened. Deleting the stale tokens lets
    /// subsequent XHRs go through.
    ///
    /// **Why CDP and not `document.cookie`:** PX sometimes sets `_px3`
    /// with `HttpOnly`, which is invisible to JS — `document.cookie`
    /// silently leaves those in the jar. CDP `Network.deleteCookies`
    /// reaches HttpOnly cookies and inspects each cookie's actual
    /// domain attribute (so we don't have to fan out across path
    /// combinations).
    pub async fn delete_perimeterx_cookies(
        session: &BrowserSession,
        site_domain: &str,
    ) -> Result<usize, TailFinError> {
        let domains = [format!(".{site_domain}"), site_domain.to_string()];
        let mut deleted = 0usize;
        for name in PX_COOKIE_NAMES {
            for d in &domains {
                if session.delete_cookie(name, d).await.is_ok() {
                    deleted += 1;
                }
            }
        }
        Ok(deleted)
    }

    /// Drain currently-buffered captured responses (registered via
    /// `BrowserSession::capture_responses(...)`) and bin them into a
    /// dedup'd map.
    ///
    /// `key_fn(url, body)` returns the dedup key (or `None` to skip the
    /// response — e.g. wrong URL pattern, irrelevant body shape). The
    /// `seen` map is updated with first-write-wins semantics: each key
    /// keeps the FIRST source/body it was captured under, so traces
    /// reflect "this page first triggered this XHR".
    ///
    /// Returns the number of newly-inserted entries (so the caller can
    /// log "+ 4 new" per page).
    ///
    /// Non-JSON bodies (binary, HTML 404, captcha pages) are silently
    /// skipped — `serde_json::from_str` failure → continue.
    pub async fn drain_captured_responses_as_map<K, F>(
        session: &BrowserSession,
        source_page: &str,
        seen: &mut BTreeMap<K, (String, Value)>,
        key_fn: F,
    ) -> Result<usize, TailFinError>
    where
        K: Ord,
        F: Fn(&str, &Value) -> Option<K>,
    {
        let captured = session
            .get_captured_responses()
            .await
            .map_err(|e| TailFinError::Api(format!("get_captured_responses: {e}")))?;
        let mut new_count = 0;
        for r in captured {
            let body: Value = match serde_json::from_str(&r.body) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let Some(key) = key_fn(&r.url, &body) else {
                continue;
            };
            if let std::collections::btree_map::Entry::Vacant(e) = seen.entry(key) {
                e.insert((source_page.to_string(), body));
                new_count += 1;
            }
        }
        Ok(new_count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // --- summarize_json ---

    #[test]
    fn summarize_json_object_with_data_array() {
        let s = summarize_json(&json!({"data": [1, 2, 3], "meta": {}}));
        assert!(s.contains("\"data\""), "got {s}");
        assert!(s.contains("\"meta\""), "got {s}");
        assert!(s.contains("data:[3]"), "got {s}");
    }

    #[test]
    fn summarize_json_object_with_included() {
        let s = summarize_json(&json!({
            "data": [{"id": 1}],
            "included": [{"a": 1}, {"b": 2}, {"c": 3}],
        }));
        assert!(s.contains("data:[1]"), "got {s}");
        assert!(s.contains("included:[3]"), "got {s}");
    }

    #[test]
    fn summarize_json_object_with_data_object_not_array() {
        let s = summarize_json(&json!({"data": {"id": "146"}, "meta": {}}));
        assert!(s.contains("data:{obj}"), "got {s}");
    }

    #[test]
    fn summarize_json_array_root() {
        let s = summarize_json(&json!([1, 2, 3, 4, 5]));
        assert_eq!(s, "array[5]");
    }

    #[test]
    fn summarize_json_scalars_and_null() {
        assert_eq!(summarize_json(&json!(42)), "scalar");
        assert_eq!(summarize_json(&json!("hi")), "scalar");
        assert_eq!(summarize_json(&json!(true)), "scalar");
        assert_eq!(summarize_json(&json!(null)), "null");
    }

    #[test]
    fn summarize_json_keys_capped_at_eight() {
        let body = json!({
            "a": 1, "b": 2, "c": 3, "d": 4,
            "e": 5, "f": 6, "g": 7, "h": 8,
            "i": 9, "j": 10,
        });
        let s = summarize_json(&body);
        // First 8 keys appear, j (the 10th alphabetically) does not.
        assert!(s.contains("\"a\""), "got {s}");
        assert!(!s.contains("\"j\""), "got {s}");
    }

    // --- FixtureWriter ---

    #[test]
    fn fixture_writer_writes_files_and_index() {
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();

        let p1 = fw
            .write(
                "/api/v3/symbols/AAPL/ratings",
                &json!({"data": [{"id": 1}]}),
                "symbol_root",
            )
            .unwrap();
        let p2 = fw
            .write(
                "/api/v3/symbols/AAPL/dividend_history",
                &json!({"data": [{"a": 1}, {"b": 2}]}),
                "symbol_root",
            )
            .unwrap();

        assert!(p1.exists());
        assert!(p2.exists());
        assert_eq!(fw.len(), 2);

        let idx_path = fw.finalize().unwrap();
        let idx_raw = std::fs::read_to_string(&idx_path).unwrap();
        let idx: serde_json::Value = serde_json::from_str(&idx_raw).unwrap();
        let arr = idx.as_array().unwrap();
        assert_eq!(arr.len(), 2);
        // Manifest is sorted by fixture filename → dividend_history < ratings.
        let by_key: std::collections::HashMap<&str, &serde_json::Value> = arr
            .iter()
            .map(|e| (e["key"].as_str().unwrap(), e))
            .collect();
        assert_eq!(by_key["/api/v3/symbols/AAPL/ratings"]["data_array_len"], 1);
        assert_eq!(
            by_key["/api/v3/symbols/AAPL/dividend_history"]["data_array_len"],
            2
        );
        assert_eq!(arr[0]["source_page"], "symbol_root");
    }

    #[test]
    fn fixture_writer_dedupes_index_on_duplicate_filename() {
        // Two writes that slugify to the same filename — second should
        // overwrite both on disk AND in the manifest. No two index rows
        // pointing at the same file with conflicting body_size_bytes.
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();
        fw.write(
            "/api/v3/symbols/AAPL/ratings",
            &json!({"first": true}),
            "src1",
        )
        .unwrap();
        fw.write(
            "/api/v3/symbols/AAPL/ratings",
            &json!({"second": true}),
            "src2",
        )
        .unwrap();

        let idx_path = fw.finalize().unwrap();
        let idx_raw = std::fs::read_to_string(&idx_path).unwrap();
        let idx: serde_json::Value = serde_json::from_str(&idx_raw).unwrap();
        assert_eq!(idx.as_array().unwrap().len(), 1, "dedup'd to single row");
        assert_eq!(
            idx[0]["source_page"], "src2",
            "last write wins (matches the body now on disk)"
        );
    }

    #[test]
    fn slugify_replaces_colons() {
        // Regression: bare keys with `:` (e.g. Nansen's `typesense:foo`
        // routing prefix) used to land as literal-`:` filenames, which
        // are not portable.
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();
        let p = fw.write("typesense:portfolio", &json!({}), "p").unwrap();
        let fname = p.file_name().unwrap().to_str().unwrap();
        assert!(!fname.contains(':'), "no colons in filename: {fname}");
    }

    #[test]
    fn fixture_writer_disambiguates_query_strings() {
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();
        let p1 = fw
            .write(
                "/api/v3/symbols/AAPL/estimates?period_type=quarterly",
                &json!({"x": 1}),
                "p",
            )
            .unwrap();
        let p2 = fw
            .write(
                "/api/v3/symbols/AAPL/estimates?period_type=annual",
                &json!({"x": 2}),
                "p",
            )
            .unwrap();
        assert_ne!(
            p1.file_name(),
            p2.file_name(),
            "different queries must yield different files"
        );
    }

    #[test]
    fn fixture_writer_disambiguates_distinct_keys_with_same_slug() {
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();
        let p1 = fw
            .write("/api/foo:bar", &json!({"colon": true}), "p1")
            .unwrap();
        let p2 = fw
            .write("/api/foo/bar", &json!({"slash": true}), "p2")
            .unwrap();

        assert_ne!(
            p1.file_name(),
            p2.file_name(),
            "distinct keys that slugify identically must not overwrite each other"
        );

        let idx_path = fw.finalize().unwrap();
        let idx_raw = std::fs::read_to_string(&idx_path).unwrap();
        let idx: serde_json::Value = serde_json::from_str(&idx_raw).unwrap();
        assert_eq!(idx.as_array().unwrap().len(), 2);
    }

    #[test]
    fn fixture_writer_disambiguates_fallback_filename_collision() {
        // Synthetic trigger: the second write uses a key shaped exactly like
        // the `__k<hash>` fallback that the third write would naturally pick,
        // forcing it into the `_2` counter path. Real-world FNV-1a 32-bit
        // truncation collisions are rare; this is the cheapest way to
        // exercise the loop body deterministically.
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();

        let colliding_key = "/api/foo/bar";
        let hash = short_hash(colliding_key);
        fw.write("/api/foo:bar", &json!({"base": true}), "p1")
            .unwrap();
        fw.write(
            &format!("/api/foo-bar__k{hash}"),
            &json!({"natural_suffix": true}),
            "p2",
        )
        .unwrap();
        let p3 = fw
            .write(colliding_key, &json!({"fallback": true}), "p3")
            .unwrap();

        assert_eq!(
            p3.file_name().unwrap().to_str().unwrap(),
            format!("api-foo-bar__k{hash}_2.json")
        );

        let idx_path = fw.finalize().unwrap();
        let idx_raw = std::fs::read_to_string(&idx_path).unwrap();
        let idx: serde_json::Value = serde_json::from_str(&idx_raw).unwrap();
        assert_eq!(idx.as_array().unwrap().len(), 3);
    }

    #[test]
    fn short_hash_is_stable() {
        assert_eq!(short_hash("period_type=quarterly"), "265d2857");
    }

    #[test]
    fn slugify_replaces_all_forbidden_filename_chars() {
        let slug = slugify_path("/a\\b:c[d]e<f>g\"h|i?j*k\u{0007}/");
        for forbidden in [
            '/', '\\', ':', '[', ']', '<', '>', '"', '|', '?', '*', '\u{0007}',
        ] {
            assert!(
                !slug.contains(forbidden),
                "slug contains forbidden char {forbidden:?}: {slug}"
            );
        }
    }

    #[test]
    fn fixture_writer_filename_is_filesystem_safe() {
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();
        let p = fw
            .write(
                "/api/v3/symbols/AAPL/sec:filings\\bad<name>|x?y*\u{0007}?include=form_type&page[size]=10",
                &json!({}),
                "p",
            )
            .unwrap();
        let fname = p.file_name().unwrap().to_str().unwrap();
        for forbidden in [
            '/', '\\', ':', '[', ']', '<', '>', '"', '|', '?', '*', '\u{0007}',
        ] {
            assert!(
                !fname.contains(forbidden),
                "filename contains forbidden char {forbidden:?}: {fname}"
            );
        }
    }

    #[test]
    fn fixture_writer_handles_root_and_windows_reserved_names() {
        let tmp = tempfile::tempdir().unwrap();
        let mut fw = FixtureWriter::new(tmp.path()).unwrap();

        let root = fw.write("/", &json!({}), "root").unwrap();
        assert_eq!(root.file_name().unwrap().to_str().unwrap(), "_.json");

        let con = fw.write("/CON", &json!({}), "reserved").unwrap();
        assert_eq!(con.file_name().unwrap().to_str().unwrap(), "_CON.json");

        let com1 = fw.write("/com1", &json!({}), "reserved").unwrap();
        assert_eq!(com1.file_name().unwrap().to_str().unwrap(), "_com1.json");

        // Cover the other matches!(...) arms + the LPT* parse branch.
        let prn = fw.write("/PRN", &json!({}), "reserved").unwrap();
        assert_eq!(prn.file_name().unwrap().to_str().unwrap(), "_PRN.json");

        let lpt3 = fw.write("/lpt3", &json!({}), "reserved").unwrap();
        assert_eq!(lpt3.file_name().unwrap().to_str().unwrap(), "_lpt3.json");
    }

    #[test]
    fn fixture_writer_create_dir_is_idempotent() {
        let tmp = tempfile::tempdir().unwrap();
        let nested = tmp.path().join("a/b/c/discovered");
        let mut fw = FixtureWriter::new(&nested).unwrap();
        fw.write("/api/x", &json!({"data": []}), "src").unwrap();
        // Reopen — should not error.
        let mut fw2 = FixtureWriter::new(&nested).unwrap();
        fw2.write("/api/y", &json!({"data": []}), "src").unwrap();
        fw2.finalize().unwrap();
    }
}