pagesift 1.4.0

A single-binary, Firecrawl-compatible web scraping engine that turns pages into clean Markdown.
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
//! CLI-level contracts added in 0.72.8:
//! - `--allow-cidr` on every command that performs outbound fetches
//! - honest exit codes for `crawl` / `map` when the seed is unreachable
//! - default config discovery, `PAGESIFT_CONFIG=`, and `doctor` reporting

use std::io::{Read, Write};
use std::net::{SocketAddr, TcpListener};
use std::path::Path;
use std::process::{Command, Output};

fn bin() -> &'static str {
    env!("CARGO_BIN_EXE_pagesift")
}

/// Keep tests independent from a developer's real `~/.config/pagesift`.
fn isolated(cmd: &mut Command, xdg: &Path) {
    cmd.env("XDG_CONFIG_HOME", xdg);
    cmd.env_remove("PAGESIFT_CONFIG");
}

fn run(mut cmd: Command) -> Output {
    cmd.output().unwrap()
}

/// Tiny blocking HTTP fixture: `/`, `/a.html`, `/b.html`, robots.
fn serve_pages() -> SocketAddr {
    serve_pages_with_robots("User-agent: *\nAllow: /\n")
}

fn serve_pages_with_robots(robots: &'static str) -> SocketAddr {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    std::thread::spawn(move || {
        for stream in listener.incoming() {
            let Ok(mut stream) = stream else { continue };
            let mut buf = [0u8; 4096];
            let _ = stream.read(&mut buf);
            let request = String::from_utf8_lossy(&buf);
            let path = request.split_whitespace().nth(1).unwrap_or("/");
            let (status, body) = match path {
                "/robots.txt" => ("200 OK", robots.to_string()),
                "/sitemap.xml" => ("404 Not Found", "no sitemap".to_string()),
                "/" => (
                    "200 OK",
                    "<!doctype html><html><body><h1>home</h1><p>fixture page with enough text for extraction to keep it.</p><a href=\"/a.html\">a</a><a href=\"/b.html\">b</a></body></html>".to_string(),
                ),
                "/a.html" | "/b.html" => (
                    "200 OK",
                    format!(
                        "<!doctype html><html><body><h1>{path}</h1><p>fixture page with enough text for extraction to keep it.</p></body></html>"
                    ),
                ),
                _ => ("404 Not Found", "not found".to_string()),
            };
            let _ = write!(
                stream,
                "HTTP/1.1 {status}\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            );
        }
    });
    addr
}

#[test]
fn every_fetch_command_exposes_allow_cidr() {
    for command in [
        "scrape",
        "extract",
        "crawl",
        "map",
        "download",
        "session-fetch",
        "collect",
        "serve",
    ] {
        let output = Command::new(bin())
            .args([command, "--help"])
            .output()
            .unwrap();
        assert!(output.status.success(), "{command} --help failed");
        let help = String::from_utf8_lossy(&output.stdout);
        assert!(
            help.contains("--allow-cidr"),
            "{command} --help does not list --allow-cidr:\n{help}"
        );
    }
}

#[test]
fn invalid_cidr_is_rejected_by_the_parser() {
    let output = Command::new(bin())
        .args([
            "scrape",
            "--allow-cidr",
            "not-a-cidr",
            "http://127.0.0.1:9/",
        ])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(2));
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("invalid CIDR"), "{stderr}");
}

#[test]
fn crawl_and_map_reach_a_private_origin_with_allow_cidr() {
    let addr = serve_pages();
    let root = format!("http://{addr}/");
    let xdg = tempfile::tempdir().unwrap();

    let mut crawl = Command::new(bin());
    isolated(&mut crawl, xdg.path());
    let crawl = run({
        crawl.args([
            "crawl",
            &root,
            "--engine",
            "http",
            "--sitemap",
            "skip",
            "--limit",
            "5",
            "--allow-cidr",
            "127.0.0.1/32",
        ]);
        crawl
    });
    assert!(
        crawl.status.success(),
        "crawl failed: {}",
        String::from_utf8_lossy(&crawl.stderr)
    );
    let lines = crawl
        .stdout
        .split(|byte| *byte == b'\n')
        .filter(|line| !line.is_empty())
        .count();
    assert_eq!(lines, 3, "crawl pages");

    let mut map = Command::new(bin());
    isolated(&mut map, xdg.path());
    let map = run({
        map.args([
            "map",
            &root,
            "--sitemap",
            "skip",
            "--limit",
            "10",
            "--allow-cidr",
            "127.0.0.1/32",
        ]);
        map
    });
    assert!(
        map.status.success(),
        "map failed: {}",
        String::from_utf8_lossy(&map.stderr)
    );
    let urls: Vec<_> = String::from_utf8_lossy(&map.stdout)
        .lines()
        .filter(|line| !line.is_empty())
        .map(str::to_owned)
        .collect();
    assert_eq!(urls.len(), 3, "map URLs: {urls:?}");
}

#[test]
fn crawl_with_a_blocked_seed_exits_1() {
    let xdg = tempfile::tempdir().unwrap();
    let mut cmd = Command::new(bin());
    isolated(&mut cmd, xdg.path());
    let output = cmd
        .args([
            "crawl",
            "http://127.0.0.1:9/",
            "--engine",
            "http",
            "--limit",
            "1",
        ])
        .output()
        .unwrap();
    assert_eq!(
        output.status.code(),
        Some(1),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("SSRF_BLOCKED"),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn map_with_a_blocked_seed_reports_an_error_instead_of_the_seed() {
    let xdg = tempfile::tempdir().unwrap();
    let mut cmd = Command::new(bin());
    isolated(&mut cmd, xdg.path());
    let output = cmd
        .args(["map", "http://127.0.0.1:9/", "--sitemap", "skip"])
        .output()
        .unwrap();
    assert_eq!(
        output.status.code(),
        Some(1),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("SSRF_BLOCKED"),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        output.stdout.is_empty(),
        "map printed a fake result: {}",
        String::from_utf8_lossy(&output.stdout)
    );
}

#[test]
fn default_config_file_is_loaded_and_doctor_reports_it() {
    let xdg = tempfile::tempdir().unwrap();
    let dir = xdg.path().join("pagesift");
    std::fs::create_dir_all(&dir).unwrap();
    let config = dir.join("pagesift.toml");
    std::fs::write(&config, "[server]\nport = 3999\n").unwrap();

    let mut cmd = Command::new(bin());
    isolated(&mut cmd, xdg.path());
    let output = cmd.arg("doctor").output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains(&config.display().to_string()),
        "doctor did not report the loaded config:\n{stdout}"
    );
}

#[test]
fn doctor_reports_an_explicit_config_path() {
    let dir = tempfile::tempdir().unwrap();
    let config = dir.path().join("custom.toml");
    std::fs::write(&config, "[server]\nport = 3999\n").unwrap();

    let output = Command::new(bin())
        .args(["--config", config.to_str().unwrap(), "doctor"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains(&config.display().to_string()),
        "doctor did not report the explicit config:\n{stdout}"
    );
}

#[test]
fn empty_pagesift_config_disables_default_files() {
    let xdg = tempfile::tempdir().unwrap();
    let dir = xdg.path().join("pagesift");
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("pagesift.toml"), "[server]\nport = 3999\n").unwrap();

    let output = Command::new(bin())
        .env("XDG_CONFIG_HOME", xdg.path())
        .env("PAGESIFT_CONFIG", "")
        .arg("doctor")
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("config files disabled"),
        "doctor did not report the disable switch:\n{stdout}"
    );
}

/// 共享服务的配置文件里开了审计日志时,`doctor` 的自检抓取不能写进去(每次都是一条误报)。
#[test]
fn doctor_does_not_write_the_audit_log() {
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("audit.jsonl");
    let config = dir.path().join("audit.toml");
    std::fs::write(
        &config,
        format!("[audit]\npath = {:?}\n", log.to_str().unwrap()),
    )
    .unwrap();

    let output = Command::new(bin())
        .args(["--config", config.to_str().unwrap(), "doctor"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("network"), "doctor did not run:\n{stdout}");
    let written = std::fs::read_to_string(&log).unwrap_or_default();
    assert!(written.is_empty(), "doctor wrote the audit log:\n{written}");
}

/// `--out` 目录里除了每页一行的 index.jsonl,还有 summary.json:停止原因和覆盖度不只打在 stderr。
/// 没有数据风险的页面,index 行里没有 warning。
#[test]
fn crawl_out_writes_a_summary_next_to_the_index() {
    let addr = serve_pages();
    let root = format!("http://{addr}/");
    let xdg = tempfile::tempdir().unwrap();
    let out = tempfile::tempdir().unwrap();

    let mut crawl = Command::new(bin());
    isolated(&mut crawl, xdg.path());
    let crawl = run({
        crawl.args([
            "crawl",
            &root,
            "--engine",
            "http",
            "--sitemap",
            "skip",
            "--limit",
            "5",
            "--query",
            "fixture page",
            "--adaptive",
            "--allow-cidr",
            "127.0.0.1/32",
            "--out",
        ]);
        crawl.arg(out.path());
        crawl
    });
    assert!(
        crawl.status.success(),
        "crawl failed: {}",
        String::from_utf8_lossy(&crawl.stderr)
    );
    let summary: serde_json::Value =
        serde_json::from_slice(&std::fs::read(out.path().join("summary.json")).unwrap()).unwrap();
    assert!(summary["stopReason"].is_string(), "{summary}");
    assert!(summary["coverage"].is_number(), "{summary}");
    assert_eq!(summary["pages"], 3, "{summary}");
    let index = std::fs::read_to_string(out.path().join("index.jsonl")).unwrap();
    let rows: Vec<serde_json::Value> = index
        .lines()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    assert_eq!(rows.len(), 3);
    assert!(rows.iter().all(|r| r.get("warning").is_none()), "{index}");
}

/// 取值固定的参数拼错了要报错,不能悄悄按默认值干活(`--engine potato` 以前照常按 auto 抓取)。
#[test]
fn misspelled_choices_are_rejected() {
    for args in [
        ["scrape", "http://127.0.0.1:9/", "--engine", "potato"],
        ["scrape", "http://127.0.0.1:9/", "--format", "yaml"],
        ["crawl", "http://127.0.0.1:9/", "--engine", "chrome"],
        ["crawl", "http://127.0.0.1:9/", "--sitemap", "never"],
        ["map", "http://127.0.0.1:9/", "--sitemap", "no"],
    ] {
        let output = Command::new(bin()).args(args).output().unwrap();
        assert!(!output.status.success(), "{args:?} was accepted");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("invalid value"), "{args:?}: {stderr}");
    }
}

/// crawl 和 map 也有 --ignore-robots:命令行用户自己就是管理员,参数本身就是许可,
/// 不用再改配置(0.80.5 以前命令行里没有办法,只能经 HTTP API 并在配置里放行)。
#[test]
fn crawl_and_map_can_ignore_robots_on_the_command_line() {
    let addr = serve_pages_with_robots("User-agent: *\nDisallow: /\n");
    let root = format!("http://{addr}/");
    let xdg = tempfile::tempdir().unwrap();
    let crawl = |extra: &[&str]| {
        let mut cmd = Command::new(bin());
        isolated(&mut cmd, xdg.path());
        cmd.args([
            "crawl",
            &root,
            "--engine",
            "http",
            "--sitemap",
            "skip",
            "--limit",
            "5",
            "--allow-cidr",
            "127.0.0.1/32",
        ])
        .args(extra);
        run(cmd)
    };
    let pages = |o: &Output| {
        o.stdout
            .split(|b| *b == b'\n')
            .filter(|l| !l.is_empty())
            .count()
    };
    // robots.txt 不许抓:默认照办
    let respected = crawl(&[]);
    assert_eq!(
        pages(&respected),
        0,
        "{}",
        String::from_utf8_lossy(&respected.stderr)
    );
    let ignored = crawl(&["--ignore-robots-txt"]);
    assert!(
        ignored.status.success(),
        "{}",
        String::from_utf8_lossy(&ignored.stderr)
    );
    assert_eq!(pages(&ignored), 3);

    let mut map = Command::new(bin());
    isolated(&mut map, xdg.path());
    map.args([
        "map",
        &root,
        "--sitemap",
        "skip",
        "--allow-cidr",
        "127.0.0.1/32",
        "--ignore-robots",
    ]);
    let map = run(map);
    assert!(
        map.status.success(),
        "{}",
        String::from_utf8_lossy(&map.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&map.stdout).lines().count(), 3);
}

/// doctor 真的启动内核:文件在、但一启动就崩的内核要报出来(0.80.5 只看文件在不在,报整条链 ok)。
#[test]
fn doctor_reports_an_engine_that_does_not_start() {
    if pagesift_cdp::running_as_root() {
        // root 下 doctor 直接报"不能以 root 运行浏览器",不做启动检查
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let stub = dir.path().join("lightpanda");
    std::fs::write(&stub, "#!/bin/sh\necho 'crashed at startup' >&2\nexit 1\n").unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(&stub, std::fs::Permissions::from_mode(0o755)).unwrap();
    let xdg = tempfile::tempdir().unwrap();
    let mut doctor = Command::new(bin());
    isolated(&mut doctor, xdg.path());
    let path = format!(
        "{}:{}",
        dir.path().display(),
        std::env::var("PATH").unwrap_or_default()
    );
    doctor.env("PATH", path).arg("doctor");
    let out = String::from_utf8_lossy(&run(doctor).stdout).into_owned();
    let line = out
        .lines()
        .find(|l| l.contains("engine lightpanda"))
        .unwrap_or_else(|| panic!("no launch check for the stub engine:\n{out}"));
    assert!(line.contains("does not start"), "{line}");
    assert!(
        line.starts_with("WARN") || line.starts_with("FAIL"),
        "{line}"
    );
}