webspec-index 0.9.0

Query WHATWG/W3C/TC39 web specifications from the command line
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Write operations on the database
use crate::model::{ParsedIdlDefinition, ParsedReference, ParsedSection};
use anyhow::Result;
use rusqlite::{Connection, OptionalExtension};

/// Insert or get a spec, returning its ID
/// Uses INSERT OR IGNORE to avoid duplicates
pub fn insert_or_get_spec(
    conn: &Connection,
    name: &str,
    base_url: &str,
    provider: &str,
) -> Result<i64> {
    // Try to insert (will be ignored if already exists)
    conn.execute(
        "INSERT OR IGNORE INTO specs (name, base_url, provider) VALUES (?1, ?2, ?3)",
        (name, base_url, provider),
    )?;

    // Get the ID (whether we just inserted it or it already existed)
    let id: i64 = conn.query_row("SELECT id FROM specs WHERE name = ?1", [name], |row| {
        row.get(0)
    })?;

    Ok(id)
}

/// Upsert spec metadata from the authoritative spec list.
///
/// Updates base_url and provider if they changed, and clears any stale indexed
/// data so the spec gets re-fetched from the correct URL.
pub fn seed_spec(conn: &Connection, name: &str, base_url: &str, provider: &str) -> Result<()> {
    let existing: Option<(i64, String)> = conn
        .query_row(
            "SELECT id, base_url FROM specs WHERE name = ?1",
            [name],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .optional()?;

    match existing {
        None => {
            conn.execute(
                "INSERT INTO specs (name, base_url, provider) VALUES (?1, ?2, ?3)",
                (name, base_url, provider),
            )?;
        }
        Some((id, old_url)) if old_url != base_url => {
            conn.execute(
                "UPDATE specs SET base_url = ?1, provider = ?2 WHERE id = ?3",
                (base_url, provider, id),
            )?;
            delete_spec_data(conn, id)?;
        }
        _ => {}
    }
    Ok(())
}

/// Insert a snapshot, returning its ID
pub fn insert_snapshot(
    conn: &Connection,
    spec_id: i64,
    sha: &str,
    commit_date: &str,
) -> Result<i64> {
    // Get current timestamp for indexed_at
    let indexed_at = chrono::Utc::now().to_rfc3339();

    // Insert the snapshot
    conn.execute(
        "INSERT INTO snapshots (spec_id, sha, commit_date, indexed_at)
         VALUES (?1, ?2, ?3, ?4)",
        (spec_id, sha, commit_date, &indexed_at),
    )?;

    // Get the ID
    let id = conn.last_insert_rowid();

    Ok(id)
}

/// Bulk insert sections for a snapshot
pub fn insert_sections_bulk(
    conn: &Connection,
    snapshot_id: i64,
    sections: &[ParsedSection],
) -> Result<()> {
    let tx = conn.unchecked_transaction()?;

    {
        let mut stmt = tx.prepare(
            "INSERT OR IGNORE INTO sections
             (snapshot_id, anchor, title, content_text, section_type, parent_anchor, prev_anchor, next_anchor, depth)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
        )?;

        for section in sections {
            stmt.execute((
                snapshot_id,
                &section.anchor,
                &section.title,
                &section.content_text,
                section.section_type.as_str(),
                &section.parent_anchor,
                &section.prev_anchor,
                &section.next_anchor,
                section.depth,
            ))?;
        }
    }

    tx.commit()?;
    Ok(())
}

/// Bulk insert references for a snapshot
pub fn insert_refs_bulk(
    conn: &Connection,
    snapshot_id: i64,
    refs: &[ParsedReference],
) -> Result<()> {
    let tx = conn.unchecked_transaction()?;

    {
        let mut stmt = tx.prepare(
            "INSERT INTO refs (snapshot_id, from_anchor, to_spec, to_anchor)
             VALUES (?1, ?2, ?3, ?4)",
        )?;

        for reference in refs {
            stmt.execute((
                snapshot_id,
                &reference.from_anchor,
                &reference.to_spec,
                &reference.to_anchor,
            ))?;
        }
    }

    tx.commit()?;
    Ok(())
}

/// Bulk insert IDL definitions for a snapshot
pub fn insert_idl_defs_bulk(
    conn: &Connection,
    snapshot_id: i64,
    defs: &[ParsedIdlDefinition],
) -> Result<()> {
    let tx = conn.unchecked_transaction()?;

    {
        let mut stmt = tx.prepare(
            "INSERT INTO idl_defs (snapshot_id, anchor, name, owner, kind, canonical_name, idl_text)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        )?;

        for def in defs {
            stmt.execute((
                snapshot_id,
                &def.anchor,
                &def.name,
                &def.owner,
                &def.kind,
                &def.canonical_name,
                &def.idl_text,
            ))?;
        }
    }

    tx.commit()?;
    Ok(())
}

/// Insert a PR snapshot, returning its ID.
/// Sets pr_number, merge_base_sha, and pr_pages in addition to the standard snapshot fields.
pub fn insert_pr_snapshot(
    conn: &Connection,
    spec_id: i64,
    sha: &str,
    commit_date: &str,
    pr_number: i64,
    merge_base_sha: &str,
    pr_pages: &[String],
) -> Result<i64> {
    let indexed_at = chrono::Utc::now().to_rfc3339();
    let pages_str = pr_pages.join(",");
    conn.execute(
        "INSERT OR REPLACE INTO snapshots (spec_id, sha, commit_date, indexed_at, pr_number, merge_base_sha, pr_pages)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        (spec_id, sha, commit_date, &indexed_at, pr_number, merge_base_sha, &pages_str),
    )?;
    Ok(conn.last_insert_rowid())
}

/// Delete all indexed data for a specific PR number.
pub fn delete_pr_data(conn: &Connection, spec_id: i64, pr_number: i64) -> Result<()> {
    let tx = conn.unchecked_transaction()?;
    tx.execute(
        "DELETE FROM refs WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number = ?2)",
        (spec_id, pr_number),
    )?;
    tx.execute(
        "DELETE FROM idl_defs WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number = ?2)",
        (spec_id, pr_number),
    )?;
    tx.execute(
        "DELETE FROM sections WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number = ?2)",
        (spec_id, pr_number),
    )?;
    tx.execute(
        "DELETE FROM snapshots WHERE spec_id = ?1 AND pr_number = ?2",
        (spec_id, pr_number),
    )?;
    tx.commit()?;
    Ok(())
}

/// Delete all PR data for a spec (all PR snapshots + orphaned commit snapshots).
/// Returns the number of PR snapshots deleted.
pub fn delete_all_pr_data_for_spec(conn: &Connection, spec_id: i64) -> Result<usize> {
    let tx = conn.unchecked_transaction()?;
    let pr_count: i64 = tx.query_row(
        "SELECT COUNT(*) FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL",
        [spec_id],
        |row| row.get(0),
    )?;
    tx.execute(
        "DELETE FROM refs WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL)",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM idl_defs WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL)",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM sections WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL)",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL",
        [spec_id],
    )?;
    // Delete orphaned commit snapshots (merge bases no longer referenced)
    tx.execute(
        "DELETE FROM refs WHERE snapshot_id IN \
         (SELECT s.id FROM snapshots s WHERE s.spec_id = ?1 AND s.pr_number IS NULL \
          AND s.sha NOT LIKE 'hash:%' \
          AND s.sha NOT IN (SELECT merge_base_sha FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL))",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM idl_defs WHERE snapshot_id IN \
         (SELECT s.id FROM snapshots s WHERE s.spec_id = ?1 AND s.pr_number IS NULL \
          AND s.sha NOT LIKE 'hash:%' \
          AND s.sha NOT IN (SELECT merge_base_sha FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL))",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM sections WHERE snapshot_id IN \
         (SELECT s.id FROM snapshots s WHERE s.spec_id = ?1 AND s.pr_number IS NULL \
          AND s.sha NOT LIKE 'hash:%' \
          AND s.sha NOT IN (SELECT merge_base_sha FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL))",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM snapshots WHERE spec_id = ?1 AND pr_number IS NULL \
         AND sha NOT LIKE 'hash:%' \
         AND sha NOT IN (SELECT merge_base_sha FROM snapshots WHERE spec_id = ?1 AND pr_number IS NOT NULL)",
        [spec_id],
    )?;
    tx.commit()?;
    Ok(pr_count as usize)
}

/// Delete trunk indexed data for a spec (snapshot, sections, refs).
/// Only deletes snapshots with `sha LIKE 'hash:%'` and `pr_number IS NULL`,
/// preserving PR snapshots and commit snapshots (merge bases).
/// Used before re-indexing to avoid clobbering PR data.
pub fn delete_spec_data(conn: &Connection, spec_id: i64) -> Result<()> {
    let tx = conn.unchecked_transaction()?;

    tx.execute(
        "DELETE FROM refs WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number IS NULL AND sha LIKE 'hash:%')",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM idl_defs WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number IS NULL AND sha LIKE 'hash:%')",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM sections WHERE snapshot_id IN \
         (SELECT id FROM snapshots WHERE spec_id = ?1 AND pr_number IS NULL AND sha LIKE 'hash:%')",
        [spec_id],
    )?;
    tx.execute(
        "DELETE FROM snapshots WHERE spec_id = ?1 AND pr_number IS NULL AND sha LIKE 'hash:%'",
        [spec_id],
    )?;

    tx.commit()?;
    Ok(())
}

/// Record spec sync metadata for freshness/content-hash based updates.
pub fn record_update_check(
    conn: &Connection,
    spec_id: i64,
    last_checked: &str,
    last_indexed: Option<&str>,
    content_hash: Option<&str>,
) -> Result<()> {
    conn.execute(
        "INSERT OR REPLACE INTO update_checks (spec_id, last_checked, last_indexed, content_hash)
         VALUES (?1, ?2, ?3, ?4)",
        (spec_id, last_checked, last_indexed, content_hash),
    )?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db;
    use crate::model::SectionType;

    #[test]
    fn test_insert_or_get_spec() {
        let conn = db::open_test_db().unwrap();

        // First insert
        let id1 =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();
        assert!(id1 > 0);

        // Second insert should return same ID
        let id2 =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();
        assert_eq!(id1, id2);

        // Different spec should get different ID
        let id3 =
            insert_or_get_spec(&conn, "DOM", "https://dom.spec.whatwg.org", "whatwg").unwrap();
        assert_ne!(id1, id3);
    }

    #[test]
    fn test_insert_snapshot() {
        let conn = db::open_test_db().unwrap();

        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();

        let snapshot_id =
            insert_snapshot(&conn, spec_id, "abc123", "2026-01-01T00:00:00Z").unwrap();

        assert!(snapshot_id > 0);

        // Verify it was inserted
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM snapshots WHERE id = ?1",
                [snapshot_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 1);
    }

    #[test]
    fn test_insert_sections_bulk() {
        let conn = db::open_test_db().unwrap();

        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();
        let snapshot_id =
            insert_snapshot(&conn, spec_id, "abc123", "2026-01-01T00:00:00Z").unwrap();

        let sections = vec![
            ParsedSection {
                anchor: "intro".to_string(),
                title: Some("Introduction".to_string()),
                content_text: Some("This is the intro".to_string()),
                section_type: SectionType::Heading,
                parent_anchor: None,
                prev_anchor: None,
                next_anchor: None,
                depth: Some(2),
            },
            ParsedSection {
                anchor: "details".to_string(),
                title: Some("Details".to_string()),
                content_text: Some("More details here".to_string()),
                section_type: SectionType::Heading,
                parent_anchor: Some("intro".to_string()),
                prev_anchor: None,
                next_anchor: None,
                depth: Some(3),
            },
        ];

        insert_sections_bulk(&conn, snapshot_id, &sections).unwrap();

        // Verify sections were inserted
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sections WHERE snapshot_id = ?1",
                [snapshot_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 2);

        // Verify FTS index was updated
        let fts_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM sections_fts", [], |row| row.get(0))
            .unwrap();
        assert_eq!(fts_count, 2);
    }

    #[test]
    fn test_insert_refs_bulk() {
        let conn = db::open_test_db().unwrap();

        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();
        let snapshot_id =
            insert_snapshot(&conn, spec_id, "abc123", "2026-01-01T00:00:00Z").unwrap();

        let refs = vec![
            ParsedReference {
                from_anchor: "intro".to_string(),
                to_spec: "DOM".to_string(),
                to_anchor: "concept-tree".to_string(),
            },
            ParsedReference {
                from_anchor: "intro".to_string(),
                to_spec: "HTML".to_string(),
                to_anchor: "details".to_string(),
            },
        ];

        insert_refs_bulk(&conn, snapshot_id, &refs).unwrap();

        // Verify refs were inserted
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM refs WHERE snapshot_id = ?1",
                [snapshot_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 2);
    }

    #[test]
    fn test_delete_spec_data() {
        let conn = db::open_test_db().unwrap();

        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();
        // Use hash: prefix — delete_spec_data only removes trunk (hash:) snapshots
        let snapshot_id =
            insert_snapshot(&conn, spec_id, "hash:abc123", "2026-01-01T00:00:00Z").unwrap();

        let sections = vec![crate::model::ParsedSection {
            anchor: "intro".to_string(),
            title: Some("Introduction".to_string()),
            content_text: None,
            section_type: SectionType::Heading,
            parent_anchor: None,
            prev_anchor: None,
            next_anchor: None,
            depth: Some(2),
        }];
        insert_sections_bulk(&conn, snapshot_id, &sections).unwrap();

        // Verify data exists
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM sections", [], |row| row.get(0))
            .unwrap();
        assert_eq!(count, 1);

        // Delete spec data
        delete_spec_data(&conn, spec_id).unwrap();

        // Verify trunk snapshot is gone
        let snap_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM snapshots", [], |row| row.get(0))
            .unwrap();
        assert_eq!(snap_count, 0);

        let sec_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM sections", [], |row| row.get(0))
            .unwrap();
        assert_eq!(sec_count, 0);
    }

    #[test]
    fn test_insert_pr_snapshot() {
        let conn = db::open_test_db().unwrap();
        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();

        let snapshot_id = insert_pr_snapshot(
            &conn,
            spec_id,
            "hash:abc123",
            "2026-01-01T00:00:00Z",
            12345,
            "def456full",
            &[],
        )
        .unwrap();
        assert!(snapshot_id > 0);

        let (pr, base): (Option<i64>, Option<String>) = conn
            .query_row(
                "SELECT pr_number, merge_base_sha FROM snapshots WHERE id = ?1",
                [snapshot_id],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .unwrap();
        assert_eq!(pr, Some(12345));
        assert_eq!(base.as_deref(), Some("def456full"));
    }

    #[test]
    fn test_delete_pr_data() {
        let conn = db::open_test_db().unwrap();
        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();

        // Insert trunk snapshot
        let trunk_id = insert_snapshot(&conn, spec_id, "trunk1", "2026-01-01T00:00:00Z").unwrap();
        insert_sections_bulk(
            &conn,
            trunk_id,
            &[ParsedSection {
                anchor: "intro".into(),
                title: Some("Intro".into()),
                content_text: None,
                section_type: SectionType::Heading,
                parent_anchor: None,
                prev_anchor: None,
                next_anchor: None,
                depth: Some(2),
            }],
        )
        .unwrap();

        // Insert PR snapshot
        let pr_id = insert_pr_snapshot(
            &conn,
            spec_id,
            "pr1",
            "2026-01-01T00:00:00Z",
            123,
            "base1",
            &[],
        )
        .unwrap();
        insert_sections_bulk(
            &conn,
            pr_id,
            &[ParsedSection {
                anchor: "new-section".into(),
                title: Some("New".into()),
                content_text: None,
                section_type: SectionType::Heading,
                parent_anchor: None,
                prev_anchor: None,
                next_anchor: None,
                depth: Some(2),
            }],
        )
        .unwrap();

        // Delete only PR data
        delete_pr_data(&conn, spec_id, 123).unwrap();

        // Trunk still exists
        let trunk_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM snapshots WHERE spec_id = ?1 AND pr_number IS NULL",
                [spec_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(trunk_count, 1);

        // PR is gone
        let pr_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM snapshots WHERE spec_id = ?1 AND pr_number = 123",
                [spec_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(pr_count, 0);
    }

    #[test]
    fn test_delete_spec_data_preserves_pr_and_commit_snapshots() {
        let conn = db::open_test_db().unwrap();
        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();

        // Insert trunk (hash: prefix), commit snapshot (real SHA), and PR snapshot
        insert_snapshot(&conn, spec_id, "hash:abc123", "2026-01-01T00:00:00Z").unwrap();
        insert_snapshot(&conn, spec_id, "74cbe0af38fee8a0", "2026-01-01T00:00:00Z").unwrap();
        insert_pr_snapshot(
            &conn,
            spec_id,
            "pr:123:def",
            "2026-01-01T00:00:00Z",
            123,
            "74cbe0af38fee8a0",
            &[],
        )
        .unwrap();

        // delete_spec_data should only delete trunk (hash: prefix)
        delete_spec_data(&conn, spec_id).unwrap();

        let total: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM snapshots WHERE spec_id = ?1",
                [spec_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(total, 2); // PR snapshot + commit snapshot remain
    }

    #[test]
    fn test_insert_idl_defs_bulk() {
        let conn = db::open_test_db().unwrap();
        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();
        let snapshot_id =
            insert_snapshot(&conn, spec_id, "abc123", "2026-01-01T00:00:00Z").unwrap();

        let defs = vec![
            ParsedIdlDefinition {
                anchor: "dom-window".to_string(),
                name: "Window".to_string(),
                owner: None,
                kind: "interface".to_string(),
                canonical_name: "Window".to_string(),
                idl_text: Some("interface Window {};".to_string()),
            },
            ParsedIdlDefinition {
                anchor: "dom-window-navigation".to_string(),
                name: "navigation".to_string(),
                owner: Some("Window".to_string()),
                kind: "attribute".to_string(),
                canonical_name: "Window.navigation".to_string(),
                idl_text: Some(
                    "interface Window { attribute Navigation navigation; };".to_string(),
                ),
            },
        ];

        insert_idl_defs_bulk(&conn, snapshot_id, &defs).unwrap();

        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM idl_defs WHERE snapshot_id = ?1",
                [snapshot_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 2);
    }

    #[test]
    fn test_record_update_check() {
        let conn = db::open_test_db().unwrap();

        let spec_id =
            insert_or_get_spec(&conn, "HTML", "https://html.spec.whatwg.org", "whatwg").unwrap();

        record_update_check(
            &conn,
            spec_id,
            "2026-01-01T00:00:00Z",
            Some("2026-01-01T00:00:00Z"),
            Some("deadbeef"),
        )
        .unwrap();

        // Verify it was recorded
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM update_checks WHERE spec_id = ?1",
                [spec_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 1);

        // Record again (should update, not insert new row)
        record_update_check(
            &conn,
            spec_id,
            "2026-01-02T00:00:00Z",
            None,
            Some("beadfeed"),
        )
        .unwrap();

        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM update_checks WHERE spec_id = ?1",
                [spec_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 1);

        let (checked, indexed, hash): (String, Option<String>, Option<String>) = conn
            .query_row(
                "SELECT last_checked, last_indexed, content_hash FROM update_checks WHERE spec_id = ?1",
                [spec_id],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
            )
            .unwrap();
        assert_eq!(checked, "2026-01-02T00:00:00Z");
        assert_eq!(indexed, None);
        assert_eq!(hash.as_deref(), Some("beadfeed"));
    }
}