dragoman 0.3.1

DOI redirection and content negotiation server
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
654
655
656
657
658
659
660
661
662
663
664
665
use std::path::{Path, PathBuf};

use commonmeta::{Citation, Data};
use rusqlite::{Connection, OpenFlags, params};

use crate::error::AppError;

const CACHE_DDL: &str = "
    CREATE TABLE IF NOT EXISTS pid_records (
        pid               TEXT PRIMARY KEY NOT NULL,
        source_id         INTEGER NOT NULL,
        resource_url      TEXT NOT NULL DEFAULT '',
        last_modified     TEXT,
        last_fetched      TEXT NOT NULL DEFAULT (datetime('now')),
        raw_metadata      TEXT NOT NULL,
        raw_metadata_type TEXT NOT NULL
    );
    CREATE INDEX IF NOT EXISTS idx_pid_records_fetched  ON pid_records(last_fetched);
    CREATE INDEX IF NOT EXISTS idx_pid_records_modified ON pid_records(last_modified);
";

fn source_to_id(source: &str) -> Result<i64, AppError> {
    match source {
        "crossref"  => Ok(1),
        "datacite"  => Ok(2),
        "openalex"  => Ok(3),
        other => Err(AppError::Internal(format!("unknown source: {other}"))),
    }
}

#[cfg(test)]
pub(crate) const TEST_DDL: &str = r#"
CREATE TABLE works (
    "id"             TEXT PRIMARY KEY NOT NULL,
    "type"           TEXT NOT NULL DEFAULT '',
    "url"            TEXT NOT NULL DEFAULT '',
    "title"          TEXT NOT NULL DEFAULT '',
    "subjects"       TEXT NOT NULL DEFAULT '[]',
    "language"       TEXT NOT NULL DEFAULT '',
    "date_published" TEXT NOT NULL DEFAULT '',
    "date_updated"   TEXT NOT NULL DEFAULT '',
    "provider"       TEXT NOT NULL DEFAULT '',
    "pmid"           TEXT NOT NULL DEFAULT '',
    "pmcid"          TEXT NOT NULL DEFAULT '',
    "openalex"       TEXT NOT NULL DEFAULT '',
    "valid"          INTEGER NOT NULL DEFAULT 0,
    "metadata"       BLOB NOT NULL DEFAULT x''
);
CREATE INDEX works_pmid ON works("pmid") WHERE "pmid" != '';
CREATE INDEX works_pmcid ON works("pmcid") WHERE "pmcid" != '';
CREATE INDEX works_openalex ON works("openalex") WHERE "openalex" != ''
"#;

fn connect(path: &PathBuf) -> Result<Connection, AppError> {
    Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)
        .map_err(|e| AppError::Internal(format!("sqlite open '{}': {e}", path.display())))
}

fn connect_rw(path: &PathBuf) -> Result<Connection, AppError> {
    Connection::open(path)
        .map_err(|e| AppError::Internal(format!("sqlite open rw '{}': {e}", path.display())))
}

/// Validate that `path` can be opened as a SQLite database with a `works` table.
///
/// Returns `Ok(None)` when the file does not exist or contains no `works` table
/// (e.g. a commonmeta database used only for organisations or settings).
/// Returns `Err` only when the file exists but cannot be opened at all.
pub fn open(path: &std::path::Path) -> Result<Option<PathBuf>, AppError> {
    if !path.exists() {
        tracing::warn!(path = %path.display(), "sqlite file not found, running without local database");
        return Ok(None);
    }
    let path = path.to_path_buf();
    let conn = connect(&path)?;
    let has_works: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='works'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_works {
        tracing::info!(path = %path.display(), "sqlite database has no works table, running without local database");
        return Ok(None);
    }
    Ok(Some(path))
}

/// Return a random `id` chosen uniformly across the works, organizations, and people
/// tables (only tables that exist and are non-empty are included).
pub fn random_id(path: &PathBuf) -> Result<Option<String>, AppError> {
    let conn = connect(path)?;

    // Build the list of non-empty tables available.
    let mut tables: Vec<&str> = Vec::new();

    let works_ok: bool = conn
        .query_row("SELECT 1 FROM works LIMIT 1", [], |_| Ok(true))
        .unwrap_or(false);
    if works_ok {
        tables.push("works");
    }

    for tbl in &["organizations", "people"] {
        let exists: bool = conn
            .query_row(
                "SELECT 1 FROM sqlite_master WHERE type='table' AND name=?1",
                params![tbl],
                |_| Ok(true),
            )
            .unwrap_or(false);
        if exists {
            let has_rows: bool = conn
                .query_row(&format!("SELECT 1 FROM {tbl} LIMIT 1"), [], |_| Ok(true))
                .unwrap_or(false);
            if has_rows {
                tables.push(tbl);
            }
        }
    }

    if tables.is_empty() {
        return Ok(None);
    }

    // Pick a random table, then a random row from it.
    // O(log n) per query: max(rowid) + WHERE rowid >= avoids ORDER BY RANDOM() O(n log n).
    let n = tables.len() as i64;
    let idx = conn
        .query_row("SELECT abs(random()) % ?1", params![n], |r| r.get::<_, i64>(0))
        .unwrap_or(0) as usize;
    let table = tables[idx];

    let query = format!(
        "SELECT id FROM {table} \
         WHERE rowid >= abs(random()) % (SELECT max(rowid) FROM {table}) + 1 \
         LIMIT 1"
    );
    match conn.query_row(&query, [], |row| row.get(0)) {
        Ok(id) => Ok(Some(id)),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(AppError::Internal(format!("sqlite random: {e}"))),
    }
}

/// Look up a single DOI in a commonmeta SQLite database.
pub fn lookup(path: &PathBuf, doi: &str) -> Result<Option<Data>, AppError> {
    let id = commonmeta::doi_utils::normalize_doi(doi);
    if id.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    fetch_metadata(&conn, "SELECT metadata FROM works WHERE id = ?1", &id)
}

/// Return citing works from the `works_references` reverse index.
/// Works in the production DB that list `doi` in their references.
pub fn lookup_citing_dois(path: &PathBuf, doi: &str) -> Result<Vec<Citation>, AppError> {
    let id = commonmeta::doi_utils::normalize_doi(doi);
    if id.is_empty() {
        return Ok(vec![]);
    }
    let conn = connect(path)?;
    let mut stmt = conn
        .prepare("SELECT work_id FROM works_references WHERE ref_id = ?1 LIMIT 50")
        .map_err(|e| AppError::Internal(format!("prepare citing: {e}")))?;
    let citing = stmt
        .query_map(params![id], |row| row.get::<_, String>(0))
        .map_err(|e| AppError::Internal(format!("query citing: {e}")))?
        .filter_map(|r| r.ok())
        .map(|work_id| Citation { id: work_id, ..Default::default() })
        .collect();
    Ok(citing)
}

/// Look up a single record by PMID in a commonmeta SQLite database.
pub fn lookup_by_pmid(path: &PathBuf, pmid: &str) -> Result<Option<Data>, AppError> {
    if pmid.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    fetch_metadata(&conn, "SELECT metadata FROM works WHERE pmid = ?1", pmid)
}

/// Look up a single record by arXiv ID in a commonmeta SQLite database.
pub fn lookup_by_arxiv(path: &PathBuf, id: &str) -> Result<Option<Data>, AppError> {
    if id.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    fetch_metadata(&conn, "SELECT metadata FROM works WHERE arxiv = ?1", id)
}

/// Look up a single record by OpenAlex ID in a commonmeta SQLite database.
/// The column always stores the URL form (`https://openalex.org/W…`);
/// normalise any input (bare W ID, or URL with optional /works/ segment) before querying.
pub fn lookup_by_openalex(path: &PathBuf, id: &str) -> Result<Option<Data>, AppError> {
    if id.is_empty() {
        return Ok(None);
    }
    // Strip any URL prefix and /works/ segment to get the bare W-prefixed ID.
    let bare = id
        .trim_start_matches("https://openalex.org/works/")
        .trim_start_matches("https://openalex.org/")
        .trim_start_matches("http://openalex.org/works/")
        .trim_start_matches("http://openalex.org/");
    let canonical = format!("https://openalex.org/{bare}");
    let conn = connect(path)?;
    fetch_metadata(&conn, "SELECT metadata FROM works WHERE openalex = ?1", &canonical)
}

/// Look up a single record by PMCID in a commonmeta SQLite database.
pub fn lookup_by_pmcid(path: &PathBuf, pmcid: &str) -> Result<Option<Data>, AppError> {
    if pmcid.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    fetch_metadata(&conn, "SELECT metadata FROM works WHERE pmcid = ?1", pmcid)
}

/// Return the ROR `types` array (e.g. `["education","funder"]`) from the `organizations` table.
/// Returns an empty vec when the table doesn't exist or the record isn't found.
pub fn lookup_ror_types(path: &PathBuf, ror_url: &str) -> Result<Vec<String>, AppError> {
    if ror_url.is_empty() {
        return Ok(Vec::new());
    }
    let conn = connect(path)?;
    let has_orgs: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='organizations'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_orgs {
        return Ok(Vec::new());
    }
    match conn.query_row(
        "SELECT types FROM organizations WHERE id = ?1",
        params![ror_url],
        |row| row.get::<_, String>(0),
    ) {
        Ok(json) => Ok(serde_json::from_str::<Vec<String>>(&json).unwrap_or_default()),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(Vec::new()),
        Err(e) => Err(AppError::Internal(format!("sqlite ror types: {e}"))),
    }
}

/// Look up a ROR organization by its full URL from the `organizations` table.
/// The blob stores zstd-compressed ROR JSON (parsed with `commonmeta::read("ror", ...)`).
/// Returns `Ok(None)` if the table doesn't exist or the ID isn't found.
pub fn lookup_by_ror(path: &PathBuf, ror_url: &str) -> Result<Option<Data>, AppError> {
    if ror_url.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    let has_orgs: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='organizations'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_orgs {
        return Ok(None);
    }
    let result = conn.query_row(
        "SELECT metadata FROM organizations WHERE id = ?1",
        params![ror_url],
        |row| row.get::<_, Vec<u8>>(0),
    );
    let blob = match result {
        Ok(b) => b,
        Err(rusqlite::Error::QueryReturnedNoRows) => return Ok(None),
        Err(e) => return Err(AppError::Internal(format!("sqlite ror query: {e}"))),
    };
    let json = zstd::decode_all(blob.as_slice())
        .map_err(|e| AppError::Internal(format!("zstd decompress: {e}")))?;
    let json_str = String::from_utf8(json)
        .map_err(|e| AppError::Internal(format!("metadata utf8: {e}")))?;
    commonmeta::read("ror", &json_str)
        .map(Some)
        .map_err(|e| AppError::Internal(format!("ror parse: {e}")))
}

fn fetch_metadata(conn: &Connection, sql: &str, param: &str) -> Result<Option<Data>, AppError> {
    let result = conn.query_row(sql, params![param], |row| row.get::<_, Vec<u8>>(0));
    let blob = match result {
        Ok(b) => b,
        Err(rusqlite::Error::QueryReturnedNoRows) => return Ok(None),
        Err(e) => return Err(AppError::Internal(format!("sqlite query: {e}"))),
    };
    let json = zstd::decode_all(blob.as_slice())
        .map_err(|e| AppError::Internal(format!("zstd decompress: {e}")))?;
    let json_str = String::from_utf8(json)
        .map_err(|e| AppError::Internal(format!("metadata utf8: {e}")))?;
    commonmeta::read("commonmeta", &json_str)
        .map(Some)
        .map_err(|e| AppError::Internal(format!("metadata parse: {e}")))
}

/// Open (or create) the dragoman-managed cache database with WAL mode and the
/// `pid_records` schema (same structure as vraix).
pub fn cache_open(path: &Path) -> Result<PathBuf, AppError> {
    let path = path.to_path_buf();
    let conn = connect_rw(&path)?;
    let _: String = conn
        .query_row("PRAGMA journal_mode=WAL", [], |row| row.get(0))
        .map_err(|e| AppError::Internal(format!("cache wal: {e}")))?;
    conn.execute_batch(CACHE_DDL)
        .map_err(|e| AppError::Internal(format!("cache init: {e}")))?;
    Ok(path)
}

/// Look up a DOI in the cache, converting its stored source-format JSON on the fly.
pub fn cache_lookup(path: &PathBuf, doi: &str) -> Result<Option<Data>, AppError> {
    let id = commonmeta::doi_utils::normalize_doi(doi);
    if id.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    let result = conn.query_row(
        "SELECT raw_metadata, raw_metadata_type FROM pid_records WHERE pid = ?1",
        params![id],
        |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
    );
    match result {
        Ok((raw, raw_type)) => commonmeta::read(&raw_type, &raw)
            .map(Some)
            .map_err(|e| AppError::Internal(format!("cache parse: {e}"))),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(AppError::Internal(format!("cache lookup: {e}"))),
    }
}

/// Return only the `resource_url` for a DOI from the cache (no JSON parsing).
#[allow(dead_code)]
pub fn cache_lookup_url(path: &PathBuf, doi: &str) -> Result<Option<String>, AppError> {
    let id = commonmeta::doi_utils::normalize_doi(doi);
    if id.is_empty() {
        return Ok(None);
    }
    let conn = connect(path)?;
    let result = conn.query_row(
        "SELECT resource_url FROM pid_records WHERE pid = ?1",
        params![id],
        |row| row.get::<_, String>(0),
    );
    match result {
        Ok(url) if !url.is_empty() => Ok(Some(url)),
        Ok(_) => Ok(None),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(AppError::Internal(format!("cache url lookup: {e}"))),
    }
}

/// Insert or replace a record in the cache. The metadata is stored in the
/// original source format (crossref/datacite) so it can be re-converted using
/// the latest commonmeta-rs logic on each read.
pub fn cache_insert(path: &PathBuf, data: &Data, source: &str) -> Result<(), AppError> {
    let raw_bytes = commonmeta::write(source, data)
        .map_err(|e| AppError::Internal(format!("serialize to {source}: {e}")))?;
    let raw_metadata = String::from_utf8(raw_bytes)
        .map_err(|e| AppError::Internal(format!("utf8: {e}")))?;

    let sid = source_to_id(source)?;
    let conn = connect_rw(path)?;
    conn.execute(
        "INSERT OR REPLACE INTO pid_records
         (pid, source_id, resource_url, raw_metadata, raw_metadata_type)
         VALUES (?1, ?2, ?3, ?4, ?5)",
        params![data.id, sid, data.url, raw_metadata, source],
    )
    .map_err(|e| AppError::Internal(format!("cache insert: {e}")))?;
    Ok(())
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use std::path::Path;

    pub(crate) fn make_test_cache_db(path: &Path) -> PathBuf {
        let db_path = cache_open(path).expect("cache_open");
        let data = commonmeta::Data {
            id: "https://doi.org/10.5678/cached".to_string(),
            type_: "JournalArticle".to_string(),
            url: "https://example.com/cached-article".to_string(),
            title: "Cached Article".to_string(),
            date_published: "2024-06-01".to_string(),
            provider: "Crossref".to_string(),
            ..commonmeta::Data::default()
        };
        cache_insert(&db_path, &data, "crossref").expect("cache_insert");
        db_path
    }

    pub(crate) fn make_test_db(path: &Path) -> PathBuf {
        let conn = Connection::open(path).expect("open test db");
        conn.execute_batch(TEST_DDL).expect("create schema");

        // Record without PMID/PMCID (DOI-only lookup)
        let data = commonmeta::Data {
            id: "https://doi.org/10.1234/test".to_string(),
            type_: "JournalArticle".to_string(),
            url: "https://example.com/test-article".to_string(),
            title: "Test Article on Content Negotiation".to_string(),
            date_published: "2024-01-15".to_string(),
            provider: "Crossref".to_string(),
            ..commonmeta::Data::default()
        };
        let compressed = compress_data(&data);
        conn.execute(
            r#"INSERT INTO works ("id","type","url","title","date_published","provider","metadata")
               VALUES (?1,?2,?3,?4,?5,?6,?7)"#,
            params![&data.id, &data.type_, &data.url, &data.title,
                    &data.date_published, &data.provider, compressed],
        ).expect("insert doi-only record");

        // Record with PMID and PMCID
        let data_pmid = commonmeta::Data {
            id: "https://doi.org/10.1234/pmid-test".to_string(),
            type_: "JournalArticle".to_string(),
            url: "https://example.com/pmid-article".to_string(),
            title: "Article with PubMed IDs".to_string(),
            date_published: "2024-02-01".to_string(),
            provider: "PubMed".to_string(),
            identifiers: vec![
                commonmeta::data::Identifier {
                    identifier: "12345678".to_string(),
                    identifier_type: "PMID".to_string(),
                    ..Default::default()
                },
                commonmeta::data::Identifier {
                    identifier: "PMC9876543".to_string(),
                    identifier_type: "PMCID".to_string(),
                    ..Default::default()
                },
            ],
            ..commonmeta::Data::default()
        };
        let compressed_pmid = compress_data(&data_pmid);
        conn.execute(
            r#"INSERT INTO works ("id","type","url","title","date_published","provider",
                                  "pmid","pmcid","metadata")
               VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)"#,
            params![&data_pmid.id, &data_pmid.type_, &data_pmid.url, &data_pmid.title,
                    &data_pmid.date_published, &data_pmid.provider,
                    "12345678", "PMC9876543", compressed_pmid],
        ).expect("insert pmid record");

        path.to_path_buf()
    }

    fn compress_data(data: &commonmeta::Data) -> Vec<u8> {
        let json_bytes = commonmeta::write("commonmeta", data).unwrap();
        zstd::encode_all(json_bytes.as_slice(), 0).unwrap()
    }

    #[test]
    fn open_returns_none_for_missing_file() {
        let result = open(Path::new("/nonexistent/path/db.sqlite3"));
        assert!(matches!(result, Ok(None)), "expected Ok(None), got {result:?}");
    }

    #[test]
    fn open_returns_none_when_no_works_table() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("no_works.sqlite3");
        let conn = Connection::open(&path).unwrap();
        conn.execute_batch("CREATE TABLE organizations (id TEXT PRIMARY KEY, name TEXT);")
            .unwrap();
        drop(conn);
        let result = open(&path);
        assert!(matches!(result, Ok(None)), "expected Ok(None), got {result:?}");
    }

    #[test]
    fn lookup_returns_none_for_unknown_doi() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        let result = lookup(&path, "10.9999/does-not-exist").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn lookup_finds_existing_doi() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        let data = lookup(&path, "10.1234/test").unwrap().expect("should find DOI");
        assert_eq!(data.id, "https://doi.org/10.1234/test");
        assert_eq!(data.title, "Test Article on Content Negotiation");
        assert_eq!(data.url, "https://example.com/test-article");
        assert_eq!(data.type_, "JournalArticle");
    }

    #[test]
    fn lookup_normalises_doi_prefix_form() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        for doi in &[
            "10.1234/test",
            "https://doi.org/10.1234/test",
            "http://dx.doi.org/10.1234/test",
        ] {
            assert!(
                lookup(&path, doi).unwrap().is_some(),
                "should find DOI in form '{doi}'"
            );
        }
    }

    #[test]
    fn lookup_empty_string_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        assert!(lookup(&path, "").unwrap().is_none());
    }

    #[test]
    fn lookup_by_pmid_finds_record() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        let data = lookup_by_pmid(&path, "12345678").unwrap().expect("should find by PMID");
        assert_eq!(data.id, "https://doi.org/10.1234/pmid-test");
    }

    #[test]
    fn lookup_by_pmid_returns_none_for_unknown() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        assert!(lookup_by_pmid(&path, "99999999").unwrap().is_none());
    }

    #[test]
    fn lookup_by_pmid_empty_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        assert!(lookup_by_pmid(&path, "").unwrap().is_none());
    }

    #[test]
    fn lookup_by_pmcid_finds_record() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        let data = lookup_by_pmcid(&path, "PMC9876543").unwrap().expect("should find by PMCID");
        assert_eq!(data.id, "https://doi.org/10.1234/pmid-test");
    }

    #[test]
    fn lookup_by_pmcid_returns_none_for_unknown() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_db(&dir.path().join("test.sqlite3"));
        assert!(lookup_by_pmcid(&path, "PMC0000000").unwrap().is_none());
    }

    // ── cache ─────────────────────────────────────────────────────────────────

    #[test]
    fn cache_open_creates_pid_records_table() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.sqlite3");
        cache_open(&path).unwrap();

        let conn = Connection::open(&path).unwrap();
        let cols: Vec<String> = conn
            .prepare("PRAGMA table_info(pid_records)").unwrap()
            .query_map([], |row| row.get::<_, String>(1)).unwrap()
            .filter_map(|r| r.ok())
            .collect();

        for col in &["pid", "source_id", "resource_url", "last_fetched", "raw_metadata", "raw_metadata_type"] {
            assert!(cols.iter().any(|c| c == col), "missing column: {col}");
        }
    }

    #[test]
    fn cache_schema_compatible_with_vraix_transport_table() {
        // pid + source_id + raw_metadata are required by find_transport_table
        // in commonmeta-rs so that stream_pidbox_to_sqlite can consume cache.sqlite3.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.sqlite3");
        cache_open(&path).unwrap();

        let conn = Connection::open(&path).unwrap();
        let cols: Vec<String> = conn
            .prepare("PRAGMA table_info(pid_records)").unwrap()
            .query_map([], |row| row.get::<_, String>(1)).unwrap()
            .filter_map(|r| r.ok())
            .collect();

        for required in &["pid", "source_id", "raw_metadata"] {
            assert!(cols.iter().any(|c| c == required), "missing vraix-required column: {required}");
        }
    }

    #[test]
    fn cache_lookup_returns_none_for_unknown_doi() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_cache_db(&dir.path().join("cache.sqlite3"));
        assert!(cache_lookup(&path, "10.9999/nope").unwrap().is_none());
    }

    #[test]
    fn cache_lookup_finds_inserted_record() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_cache_db(&dir.path().join("cache.sqlite3"));
        let data = cache_lookup(&path, "10.5678/cached").unwrap().expect("should find cached DOI");
        assert_eq!(data.id, "https://doi.org/10.5678/cached");
        assert_eq!(data.title, "Cached Article");
        assert_eq!(data.url, "https://example.com/cached-article");
        assert_eq!(data.type_, "JournalArticle");
    }

    #[test]
    fn cache_lookup_empty_string_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_cache_db(&dir.path().join("cache.sqlite3"));
        assert!(cache_lookup(&path, "").unwrap().is_none());
    }

    #[test]
    fn cache_lookup_url_returns_url_without_json_parsing() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_cache_db(&dir.path().join("cache.sqlite3"));
        let url = cache_lookup_url(&path, "10.5678/cached").unwrap();
        assert_eq!(url.as_deref(), Some("https://example.com/cached-article"));
    }

    #[test]
    fn cache_lookup_url_returns_none_for_unknown_doi() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_cache_db(&dir.path().join("cache.sqlite3"));
        assert!(cache_lookup_url(&path, "10.9999/nope").unwrap().is_none());
    }

    #[test]
    fn cache_insert_unknown_source_returns_error() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.sqlite3");
        cache_open(&path).unwrap();
        let data = commonmeta::Data {
            id: "https://doi.org/10.1/x".to_string(),
            ..commonmeta::Data::default()
        };
        assert!(cache_insert(&path, &data, "openalex").is_err());
    }

    #[test]
    fn cache_insert_replaces_existing_record() {
        let dir = tempfile::tempdir().unwrap();
        let path = make_test_cache_db(&dir.path().join("cache.sqlite3"));
        let updated = commonmeta::Data {
            id: "https://doi.org/10.5678/cached".to_string(),
            type_: "JournalArticle".to_string(),
            url: "https://example.com/cached-article".to_string(),
            title: "Updated Title".to_string(),
            ..commonmeta::Data::default()
        };
        cache_insert(&path, &updated, "crossref").unwrap();
        let data = cache_lookup(&path, "10.5678/cached").unwrap().expect("should still exist");
        assert_eq!(data.title, "Updated Title");
    }
}