doiget-cli 0.8.13

doiget CLI binary
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
//! End-to-end tests for `batch --mode json` (#205 + #210, ERRORS.md §3
//! CI persona).
//!
//! Validates the per-ref JSON-Lines wire shape:
//!
//! - Success: `{"ok":true,"ref":"...","result":{"safekey":"...","store_path":"...","canonical_digest":"..."}}`
//!   (#210 structured outcome plumbing).
//! - Failure: `{"ok":false,"ref":"...","error":{"code":"...","message":"..."[,"denial_context":{...}]}}`
//!   with `denial_context` per ADR-0023 when the underlying
//!   [`doiget_core::source::FetchError`] carries one.
//!
//! The exit code is the failure count (capped at 255, ERRORS.md §4).

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

use std::io::Write;

use assert_cmd::Command;
use serde_json::Value;
use tempfile::TempDir;

fn doiget(dir: &TempDir) -> Command {
    let mut cmd = Command::cargo_bin("doiget").expect("locate doiget binary");
    let p = dir.path().to_str().expect("tempdir path is UTF-8");
    cmd.env("HOME", p)
        .env("USERPROFILE", p)
        .env("APPDATA", p)
        .env("XDG_CONFIG_HOME", p)
        .env("DOIGET_LOG_PATH", dir.path().join("access.jsonl"))
        .env("DOIGET_STORE_ROOT", dir.path().join("store"))
        .env("DOIGET_CONTACT_EMAIL", "test@example.com");
    cmd
}

#[test]
fn batch_json_parse_failure_emits_invalid_ref_jsonl() {
    let dir = TempDir::new().expect("tempdir");
    let refs = dir.path().join("refs.txt");
    {
        let mut f = std::fs::File::create(&refs).expect("create refs file");
        // One malformed line — must NOT parse as a DOI / arXiv id. We
        // also include a comment + blank to confirm those are skipped
        // (they should not produce JSONL records).
        f.write_all(b"# comment\nnot-a-doi\n\n")
            .expect("write refs");
    }

    let output = doiget(&dir)
        .args(["--json", "batch", refs.to_str().unwrap()])
        .assert()
        .failure() // parse_errors > 0 → CliExit(1)
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).expect("stdout utf-8");

    // Filter to non-empty lines so a stray trailing newline doesn't
    // break the count assertion.
    let lines: Vec<&str> = stdout.lines().filter(|s| !s.trim().is_empty()).collect();
    assert_eq!(lines.len(), 1, "exactly one JSONL record, got: {stdout}");

    let v: Value = serde_json::from_str(lines[0]).expect("line parses as JSON");
    assert_eq!(v["ok"], Value::Bool(false));
    assert_eq!(v["ref"], "not-a-doi");
    assert_eq!(
        v["error"]["code"], "INVALID_REF",
        "ERRORS.md §3 INVALID_REF on parse failure"
    );
    assert!(
        v["error"]["message"].is_string() && !v["error"]["message"].as_str().unwrap().is_empty(),
        "error.message MUST be a non-empty string"
    );
}

#[test]
fn batch_json_fetch_failure_emits_fetch_error_jsonl() {
    // Point the arxiv resolver at a closed loopback port so a parseable
    // ref deterministically fails at the transport layer. This exercises
    // the JoinSet drain's `Err(e)` branch and `emit_jsonl_failure` with
    // FETCH_ERROR — the previously-uncovered emit path.
    let dir = TempDir::new().expect("tempdir");
    let refs = dir.path().join("refs.txt");
    std::fs::File::create(&refs)
        .expect("create refs file")
        .write_all(b"arxiv:2401.99999\n")
        .expect("write refs");

    let output = doiget(&dir)
        // Closed port → connect-refused → fetch_one returns Err →
        // FETCH_ERROR JSONL.
        .env("DOIGET_ARXIV_BASE", "http://127.0.0.1:1/")
        .args(["--json", "batch", refs.to_str().unwrap()])
        .assert()
        .failure()
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).expect("stdout utf-8");
    let lines: Vec<&str> = stdout.lines().filter(|s| !s.trim().is_empty()).collect();
    assert_eq!(lines.len(), 1, "exactly one JSONL record, got: {stdout}");
    let v: Value = serde_json::from_str(lines[0]).expect("line parses as JSON");
    assert_eq!(v["ok"], Value::Bool(false));
    // ADR-0030 + #210: the batch input now goes through
    // `refs::parse_input` → `Ref::as_input_str()`, which returns the
    // bare identifier per docs/PROVENANCE_LOG.md §3 (no `arxiv:` URI
    // scheme — that prefix is stripped at parse time). The
    // pre-ADR-0030 pipeline echoed the raw file line verbatim.
    assert_eq!(v["ref"], "2401.99999");
    // #210: the typed `FetchError → ErrorCode` mapping now surfaces
    // the closed-set wire code (`NETWORK_ERROR` for a transport-
    // layer connect-refused) instead of the previous generic
    // `FETCH_ERROR`.
    assert_eq!(
        v["error"]["code"], "NETWORK_ERROR",
        "connect-refused at the transport layer MUST surface as NETWORK_ERROR"
    );
    assert!(
        v["error"]["message"].is_string() && !v["error"]["message"].as_str().unwrap().is_empty(),
        "error.message MUST be a non-empty string"
    );
}

#[test]
fn batch_failure_digest_names_ref_and_code_on_stderr() {
    // Issue #222: the end-of-batch stderr digest names WHICH refs failed
    // and their primary error code, so a human / agent need not grep the
    // JSONL provenance log. Human mode (no `--json`) so the digest is the
    // visible failure surface.
    let dir = TempDir::new().expect("tempdir");
    let refs = dir.path().join("refs.txt");
    std::fs::File::create(&refs)
        .expect("create refs file")
        .write_all(b"arxiv:2401.99999\n")
        .expect("write refs");

    let stderr = doiget(&dir)
        // Closed port → connect-refused → NETWORK_ERROR.
        .env("DOIGET_ARXIV_BASE", "http://127.0.0.1:1/")
        .args(["batch", refs.to_str().unwrap()])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let stderr = String::from_utf8(stderr).expect("stderr utf-8");
    assert!(
        stderr.contains("batch failures"),
        "digest header missing: {stderr}"
    );
    assert!(
        stderr.contains("2401.99999 -> NETWORK_ERROR"),
        "digest must name the ref and its primary error code: {stderr}"
    );
}

/// #210: a successful single-arxiv batch produces a structured success
/// JSONL record carrying `result.{safekey, store_path, canonical_digest}`.
/// Wiremock-driven so no real network traffic; the subprocess inherits
/// `DOIGET_ARXIV_BASE` pointing at the in-process mock.
///
/// Acceptance for #210: a CI consumer pipelining `batch --json` can
/// pull `result.safekey` to construct a store-relative path and
/// `result.canonical_digest` to deduplicate against an audit DB, all
/// without a follow-up `info` round-trip per ref.
#[tokio::test]
async fn batch_json_success_emits_structured_result_record() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    let body = b"%PDF-1.7\n%fixture-bytes\n".to_vec();
    Mock::given(method("GET"))
        .and(path("/pdf/2401.12345.pdf"))
        .respond_with(ResponseTemplate::new(200).set_body_bytes(body.clone()))
        .mount(&server)
        .await;

    let dir = TempDir::new().expect("tempdir");
    let refs = dir.path().join("refs.txt");
    std::fs::File::create(&refs)
        .expect("create refs file")
        .write_all(b"arxiv:2401.12345\n")
        .expect("write refs");

    let output = doiget(&dir)
        .env("DOIGET_ARXIV_BASE", server.uri())
        .args(["--json", "batch", refs.to_str().unwrap()])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).expect("stdout utf-8");
    let lines: Vec<&str> = stdout.lines().filter(|s| !s.trim().is_empty()).collect();
    assert_eq!(lines.len(), 1, "exactly one JSONL record, got: {stdout}");

    let v: Value = serde_json::from_str(lines[0]).expect("line parses as JSON");
    assert_eq!(v["ok"], Value::Bool(true));
    // ADR-0030: ref is the canonical bare identifier
    // (`Ref::as_input_str`), not the raw input line.
    assert_eq!(v["ref"], "2401.12345");
    let result = v.get("result").expect("success record carries `result`");
    assert!(
        result["safekey"]
            .as_str()
            .map(|s| s.contains("2401.12345"))
            .unwrap_or(false),
        "result.safekey must echo the input id: {result}"
    );
    assert!(
        result["store_path"]
            .as_str()
            .map(|s| s.ends_with(".pdf"))
            .unwrap_or(false),
        "result.store_path must be the on-disk PDF path: {result}"
    );
    let digest = result["canonical_digest"]
        .as_str()
        .expect("canonical_digest is a string");
    assert_eq!(
        digest.len(),
        64,
        "canonical_digest MUST be 64-char lowercase hex (ADR-0021 §1): got {digest:?}"
    );
    assert!(
        digest
            .chars()
            .all(|c| c.is_ascii_hexdigit() && !c.is_uppercase()),
        "canonical_digest MUST be lowercase hex only: got {digest:?}"
    );
}

/// ADR-0030 slice 1: `doiget batch library.json` reads a CSL-JSON
/// export from a reference manager (Zotero / Mendeley) and walks the
/// resulting Refs through the same per-entry pipeline as plain refs.
///
/// This test seeds a 2-entry CSL-JSON file with one valid DOI and one
/// `archivePrefix=arXiv` entry, points the arxiv resolver at a closed
/// loopback (no actual fetch — the goal here is purely to verify the
/// adapter integration produces two JSONL lines, not to exercise the
/// orchestrator twice). Both entries surface as JSONL records with
/// the correct `ref` field, confirming the CSL-JSON parser landed
/// upstream of the fetch pipeline.
#[test]
fn batch_json_csl_input_yields_one_record_per_entry() {
    let dir = TempDir::new().expect("tempdir");
    let lib = dir.path().join("library.json");
    let body = r#"[
        {"id":"FooDOI","DOI":"10.1234/foo"},
        {"id":"BarArxiv","archivePrefix":"arXiv","eprint":"2401.12345"}
    ]"#;
    std::fs::File::create(&lib)
        .expect("create library.json")
        .write_all(body.as_bytes())
        .expect("write library");

    let output = doiget(&dir)
        // Closed port → every fetch fails fast; the test cares about
        // record count + per-record `ref` strings, not about success.
        .env("DOIGET_ARXIV_BASE", "http://127.0.0.1:1/")
        .env("DOIGET_CROSSREF_BASE", "http://127.0.0.1:1/")
        .env("DOIGET_UNPAYWALL_BASE", "http://127.0.0.1:1/")
        .args(["--json", "batch", lib.to_str().unwrap()])
        .assert()
        .failure() // every fetch fails → exit > 0
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).expect("stdout utf-8");
    let lines: Vec<&str> = stdout.lines().filter(|s| !s.trim().is_empty()).collect();
    assert_eq!(
        lines.len(),
        2,
        "expected one JSONL record per CSL-JSON entry, got: {stdout}"
    );
    let refs: Vec<String> = lines
        .iter()
        .map(|l| {
            let v: Value = serde_json::from_str(l).expect("line parses as JSON");
            v["ref"].as_str().expect("ref is string").to_string()
        })
        .collect();
    assert!(
        refs.iter().any(|r| r.contains("10.1234/foo")),
        "DOI entry must appear: {refs:?}"
    );
    assert!(
        refs.iter().any(|r| r.contains("2401.12345")),
        "arXiv entry must appear: {refs:?}"
    );
}

/// ADR-0030 slice 1: a malformed CSL-JSON document is surfaced as a
/// loud whole-input parse error before any fetch runs, not silently
/// treated as an empty batch.
#[test]
fn batch_malformed_csl_json_aborts_with_decode_error() {
    let dir = TempDir::new().expect("tempdir");
    let lib = dir.path().join("library.json");
    std::fs::File::create(&lib)
        .expect("create library.json")
        .write_all(b"{this is not JSON}")
        .expect("write library");

    let assert_result = doiget(&dir)
        .args(["batch", lib.to_str().unwrap()])
        .assert()
        .failure();
    let stderr =
        String::from_utf8(assert_result.get_output().stderr.clone()).expect("stderr utf-8");
    assert!(
        stderr.contains("csl-json") && stderr.to_lowercase().contains("deserialise"),
        "stderr must name the failed format + 'deserialise' verb: {stderr:?}"
    );
}

#[test]
fn batch_human_mode_remains_silent_on_stdout() {
    // ADR-0001 / pre-existing: batch in human mode emits its summary on
    // STDERR, not stdout. Regression-test that this is true even after
    // #205 wires the json branch onto stdout.
    let dir = TempDir::new().expect("tempdir");
    let refs = dir.path().join("refs.txt");
    std::fs::File::create(&refs)
        .expect("create refs file")
        .write_all(b"not-a-doi\n")
        .expect("write refs");

    let output = doiget(&dir)
        .env("DOIGET_MODE", "human")
        .args(["batch", refs.to_str().unwrap()])
        .assert()
        .failure()
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).expect("stdout utf-8");
    assert!(
        stdout.is_empty(),
        "human-mode batch stdout MUST be empty (summary is stderr): {stdout:?}"
    );
}

#[test]
fn batch_failure_digest_includes_parse_errors() {
    // Review #318: the stderr failure digest must list INVALID_REF
    // (parse-failure) entries, not only fetch errors.
    let dir = TempDir::new().expect("tempdir");
    let refs = dir.path().join("refs.txt");
    std::fs::File::create(&refs)
        .expect("create refs file")
        .write_all(b"not-a-doi\n")
        .expect("write refs");

    let stderr = doiget(&dir)
        .args(["batch", refs.to_str().unwrap()])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let stderr = String::from_utf8(stderr).expect("stderr utf-8");
    assert!(stderr.contains("batch failures"), "digest header: {stderr}");
    assert!(
        stderr.contains("not-a-doi -> INVALID_REF"),
        "digest must list the parse failure: {stderr}"
    );
}

/// #500 on the CLI `batch` surface.
///
/// A PubMed-exported `.bib` record carries `pmid = {9659853}` and no DOI. The
/// entry is fine; doiget cannot resolve that identifier class yet. Reporting
/// `INVALID_REF` sends the user to edit a bibliography that is correct, which
/// is the one claim #500 exists to stop doiget making.
///
/// It reported `INVALID_REF` anyway, on this surface only: the parser's
/// verdict was flattened into a placeholder string and handed back to
/// `Ref::parse`, whose only possible answer is `INVALID_REF`. The MCP tool and
/// `doiget verify` had said `NOT_IMPLEMENTED` all along.
#[test]
fn batch_json_pmid_only_entry_is_not_implemented_not_invalid_ref() {
    let dir = TempDir::new().expect("tempdir");
    let bib = dir.path().join("refs.bib");
    {
        let mut f = std::fs::File::create(&bib).expect("create bib file");
        f.write_all(
            b"@article{Smith2020,
  title = {A PubMed-only record},
  pmid = {9659853},
}
",
        )
        .expect("write bib");
    }

    let out = doiget(&dir)
        .args(["batch", bib.to_str().expect("utf-8"), "--mode", "json"])
        .output()
        .expect("run batch");

    let stdout = String::from_utf8(out.stdout).expect("utf-8 stdout");
    let line = stdout
        .lines()
        .find(|l| l.contains("\"ok\""))
        .unwrap_or_else(|| panic!("no JSONL record in: {stdout}"));
    let v: Value = serde_json::from_str(line).expect("JSONL record parses");

    assert_eq!(v["ok"], serde_json::json!(false), "record: {v}");
    assert_eq!(
        v["error"]["code"],
        serde_json::json!("NOT_IMPLEMENTED"),
        "a PMID entry is unsupported, not malformed: {v}"
    );
    // The message must describe the user's entry, not the synthetic
    // placeholder the old code round-tripped through `Ref::parse`.
    let message = v["error"]["message"].as_str().unwrap_or_default();
    assert!(
        message.contains("PMID") && message.contains("9659853"),
        "message names the identifier the entry actually carries: {message:?}"
    );
    assert!(
        !message.contains("<unsupported-"),
        "message must not describe the internal placeholder: {message:?}"
    );
}