tga 2.16.0

Developer productivity analytics — git commit collection, classification, and reporting
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
//! Unit tests for [`super::BitbucketClient`].
//!
//! Why: split from `client.rs` to keep that file under the 500-line cap
//! while keeping the tests adjacent to the code they exercise.
//! What: covers JSON deserialisation, PR state mapping, pagination, auth
//! resolution via [`super::resolve_auth`] with an injected env lookup
//! (including env-expansion of `app_password` — issue #842 — and the
//! env-fallback path — issue #1653), and auth rejection when credentials
//! are absent. Auth tests inject an in-memory env map rather than mutating
//! `std::env`, so they cannot race under the parallel test harness.
//! Test: `cargo test -p tga collect::bitbucket::tests`.

use super::*;

/// Verify the paged envelope tolerates a fully-populated PR with author,
/// merge commit, and a `next` cursor.
#[test]
fn bb_paged_full_pr_deserializes() {
    let json = r#"{
        "values": [{
            "id": 42,
            "title": "Add foo widget",
            "state": "MERGED",
            "created_on": "2024-01-02T03:04:05+00:00",
            "updated_on": "2024-01-03T00:00:00+00:00",
            "author": {
                "display_name": "Ada Lovelace",
                "nickname": "ada",
                "uuid": "{abc}"
            },
            "merge_commit": {"hash": "deadbeefcafe"}
        }],
        "next": "https://api.bitbucket.org/2.0/repositories/w/r/pullrequests?page=2"
    }"#;
    let page: BbPaged<BbPullRequest> = serde_json::from_str(json).expect("parses");
    assert_eq!(page.values.len(), 1);
    assert!(page.next.is_some());

    let mapped = map_pr(page.values.into_iter().next().unwrap(), "w/r");
    assert_eq!(mapped.pr_number, 42);
    assert_eq!(mapped.repository, "w/r");
    assert_eq!(mapped.state, PrState::Merged);
    assert_eq!(mapped.author, "ada");
    assert!(mapped.commit_shas.contains("deadbeefcafe"));
    assert!(mapped.merged_at.is_some());
}

/// A `DECLINED` PR with no merge commit should map to `Closed` and have
/// an empty `commit_shas` array, mirroring how GitHub stores unmerged PRs.
#[test]
fn bb_declined_pr_maps_to_closed_with_empty_shas() {
    let json = r#"{
        "id": 7,
        "title": "abandoned",
        "state": "DECLINED",
        "created_on": "2024-05-01T12:00:00Z",
        "author": {"display_name": "Bob"}
    }"#;
    let pr: BbPullRequest = serde_json::from_str(json).expect("parses");
    let mapped = map_pr(pr, "w/r");
    assert_eq!(mapped.pr_number, 7);
    assert_eq!(mapped.state, PrState::Closed);
    assert!(mapped.merged_at.is_none());
    assert_eq!(mapped.commit_shas, "[]");
    assert_eq!(mapped.author, "Bob");
}

/// `SUPERSEDED` should also collapse to `Closed`.
#[test]
fn bb_superseded_pr_maps_to_closed() {
    let json = r#"{
        "id": 8,
        "title": "old version",
        "state": "SUPERSEDED",
        "created_on": "2024-05-01T12:00:00Z"
    }"#;
    let pr: BbPullRequest = serde_json::from_str(json).expect("parses");
    let mapped = map_pr(pr, "w/r");
    assert_eq!(mapped.state, PrState::Closed);
    // No author block — `author` should be empty rather than panicking.
    assert_eq!(mapped.author, "");
}

/// Author fallback: missing `nickname` → `display_name`; missing both →
/// `uuid`; missing all → empty string.
#[test]
fn bb_author_best_name_priority() {
    use crate::collect::bitbucket::types::BbAuthor;
    let a = BbAuthor {
        display_name: Some("Ada Lovelace".into()),
        nickname: Some("ada".into()),
        uuid: Some("{abc}".into()),
    };
    assert_eq!(a.best_name(), "ada");

    let a = BbAuthor {
        display_name: Some("Ada Lovelace".into()),
        nickname: None,
        uuid: Some("{abc}".into()),
    };
    assert_eq!(a.best_name(), "Ada Lovelace");

    let a = BbAuthor {
        display_name: None,
        nickname: Some("  ".into()),
        uuid: Some("{abc}".into()),
    };
    assert_eq!(a.best_name(), "{abc}");

    let a = BbAuthor {
        display_name: None,
        nickname: None,
        uuid: None,
    };
    assert_eq!(a.best_name(), "");
}

/// Construct against a mock server and verify the client follows the
/// `next` cursor across two pages, returning the union.
///
/// Why: cursor pagination is the single biggest semantic difference
/// from the GitHub client; if we ever fail to follow `next`, half the
/// PR history disappears silently.
#[tokio::test]
async fn fetch_pull_requests_follows_next_cursor() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    let base = server.uri();

    // Page 2 (terminal) — declare first because page 1 references its URL.
    let page2_url = format!("{base}/repositories/acme/widgets/pullrequests?page=2");
    let page1 = serde_json::json!({
        "values": [{
            "id": 1,
            "title": "first",
            "state": "OPEN",
            "created_on": "2024-01-01T00:00:00Z"
        }],
        "next": page2_url,
    });
    let page2 = serde_json::json!({
        "values": [{
            "id": 2,
            "title": "second",
            "state": "MERGED",
            "created_on": "2024-02-01T00:00:00Z",
            "updated_on": "2024-02-02T00:00:00Z",
            "merge_commit": {"hash": "abc123"}
        }]
    });

    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page1.clone()))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page2.clone()))
        .mount(&server)
        .await;

    let client = BitbucketClient::new(&BitbucketConfig {
        token: Some("dummy".into()),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        fetch_prs: true,
        api_base_url: Some(base),
        ..Default::default()
    })
    .expect("client builds");

    let prs = client.fetch_pull_requests().await.expect("fetch");
    assert_eq!(prs.len(), 2);
    assert_eq!(prs[0].pr_number, 1);
    assert_eq!(prs[0].state, PrState::Open);
    assert_eq!(prs[1].pr_number, 2);
    assert_eq!(prs[1].state, PrState::Merged);
    assert!(prs[1].commit_shas.contains("abc123"));
}

/// The per-PR commits endpoint follows its own `next` cursor across two
/// pages, returning the union of both pages' SHAs in order.
///
/// Why: this is the same cursor-pagination contract as the PR list endpoint
/// (issue #841's fix reuses it for `fetch_pr_commits`); a regression here
/// would silently truncate large PRs to their first page of commits.
#[tokio::test]
async fn fetch_pr_commits_follows_next_cursor() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    let base = server.uri();

    let page2_url = format!("{base}/repositories/acme/widgets/pullrequests/9/commits?page=2");
    let page1 = serde_json::json!({
        "values": [{"hash": "1111111111111111111111111111111111111a"}],
        "next": page2_url,
    });
    let page2 = serde_json::json!({
        "values": [{"hash": "2222222222222222222222222222222222222b"}],
    });

    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests/9/commits"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page1.clone()))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests/9/commits"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page2.clone()))
        .mount(&server)
        .await;

    let client = BitbucketClient::new(&BitbucketConfig {
        token: Some("dummy".into()),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        fetch_prs: true,
        api_base_url: Some(base),
        ..Default::default()
    })
    .expect("client builds");

    let shas = client.fetch_pr_commits(9).await.expect("fetch");
    assert_eq!(
        shas,
        vec![
            "1111111111111111111111111111111111111a".to_string(),
            "2222222222222222222222222222222222222b".to_string(),
        ]
    );
}

/// `fetch_pull_requests` persists the *full* per-PR commit list (from the
/// dedicated commits endpoint), not the abbreviated `merge_commit` hash —
/// the regression this issue (#841) was filed against.
///
/// Why: the PR list payload alone yields only a short, often-unjoinable
/// merge SHA; this proves the enrichment call overwrites it with the real,
/// fully-qualified, potentially multi-commit list.
#[tokio::test]
async fn fetch_pull_requests_persists_full_commit_list() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    let base = server.uri();

    let prs_page = serde_json::json!({
        "values": [{
            "id": 5,
            "title": "multi-commit PR",
            "state": "MERGED",
            "created_on": "2024-01-01T00:00:00Z",
            "updated_on": "2024-01-02T00:00:00Z",
            "merge_commit": {"hash": "36c721d47ff0"}
        }]
    });
    let commits_page = serde_json::json!({
        "values": [
            {"hash": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
            {"hash": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
        ]
    });

    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests"))
        .respond_with(ResponseTemplate::new(200).set_body_json(prs_page))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests/5/commits"))
        .respond_with(ResponseTemplate::new(200).set_body_json(commits_page))
        .mount(&server)
        .await;

    let client = BitbucketClient::new(&BitbucketConfig {
        token: Some("dummy".into()),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        fetch_prs: true,
        api_base_url: Some(base),
        ..Default::default()
    })
    .expect("client builds");

    let prs = client.fetch_pull_requests().await.expect("fetch");
    assert_eq!(prs.len(), 1);
    let shas: Vec<String> =
        serde_json::from_str(&prs[0].commit_shas).expect("commit_shas is a JSON array");
    assert_eq!(
        shas,
        vec![
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
            "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_string(),
        ]
    );
    // The abbreviated merge SHA must NOT be what gets persisted.
    assert!(!prs[0].commit_shas.contains("36c721d47ff0"));
}

/// When the per-PR commits endpoint errors (e.g. transient 5xx exhausting
/// retries), `fetch_pull_requests` degrades gracefully: the PR is still
/// returned, keeping the merge-commit fallback rather than failing the
/// whole batch or dropping the PR.
///
/// Why: one PR's enrichment call hiccuping must not take down collection
/// for every other PR in the repository (same partial-success philosophy
/// as the GitHub per-repo fetch, issue #87).
#[tokio::test]
async fn fetch_pull_requests_falls_back_on_commit_fetch_error() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    let base = server.uri();

    let prs_page = serde_json::json!({
        "values": [{
            "id": 6,
            "title": "commits endpoint unreachable",
            "state": "MERGED",
            "created_on": "2024-01-01T00:00:00Z",
            "updated_on": "2024-01-02T00:00:00Z",
            "merge_commit": {"hash": "deadbeefcafe"}
        }]
    });

    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests"))
        .respond_with(ResponseTemplate::new(200).set_body_json(prs_page))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/repositories/acme/widgets/pullrequests/6/commits"))
        .respond_with(ResponseTemplate::new(404))
        .mount(&server)
        .await;

    let client = BitbucketClient::new(&BitbucketConfig {
        token: Some("dummy".into()),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        fetch_prs: true,
        api_base_url: Some(base),
        ..Default::default()
    })
    .expect("client builds");

    let prs = client.fetch_pull_requests().await.expect("fetch");
    assert_eq!(prs.len(), 1);
    assert!(prs[0].commit_shas.contains("deadbeefcafe"));
}

/// Build a `BitbucketConfig` with workspace/repo set and the given auth
/// fields, so each auth test only states what it actually cares about.
fn auth_config(
    token: Option<&str>,
    username: Option<&str>,
    app_password: Option<&str>,
) -> BitbucketConfig {
    BitbucketConfig {
        token: token.map(Into::into),
        username: username.map(Into::into),
        app_password: app_password.map(Into::into),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        fetch_prs: true,
        ..Default::default()
    }
}

/// Build an injected env lookup from a fixed `(name, value)` table.
///
/// Why: `resolve_auth` takes the environment as a closure precisely so tests
/// never mutate process-global `std::env` — that ambient mutation is what
/// raced across parallel tests and flaked the env-fallback case (issue #1653).
/// What: returns a closure that resolves names against an owned map; any name
/// not in the table reads as unset, regardless of the real process env.
fn env_map(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
    let owned: Vec<(String, String)> = pairs
        .iter()
        .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
        .collect();
    move |name: &str| {
        owned
            .iter()
            .find(|(k, _)| k == name)
            .map(|(_, v)| v.clone())
    }
}

/// `resolve_auth` rejects a config with neither token nor username+password,
/// even when the (empty) environment offers nothing either.
///
/// Why: missing credentials must surface as a `Config` error, never a panic
/// or a silent default.
/// What: empty config auth fields + empty env → `CollectError::Config`.
/// Test: this test (pure, no env mutation).
#[test]
fn resolve_auth_rejects_missing_auth() {
    let cfg = auth_config(None, None, None);
    match resolve_auth(&cfg, env_map(&[])) {
        Ok(_) => panic!("expected auth failure, got Ok(_)"),
        Err(CollectError::Config(_)) => {}
        Err(other) => panic!("unexpected error: {other:?}"),
    }
}

/// A `username` set without any app_password (config or env) is rejected.
///
/// Why: half-specified Basic auth must error rather than send an empty
/// password.
/// What: username present, app_password absent, empty env → `Config` error.
/// Test: this test.
#[test]
fn resolve_auth_rejects_username_without_password() {
    let cfg = auth_config(None, Some("carol"), None);
    match resolve_auth(&cfg, env_map(&[])) {
        Err(CollectError::Config(_)) => {}
        other => panic!("expected Config error, got {other:?}"),
    }
}

/// A plain config `token` resolves to Bearer auth and wins over Basic fields.
///
/// Why: documents the precedence — token always supersedes username/password.
/// What: config token + username/app_password → `BbAuth::Bearer(token)`.
/// Test: this test.
#[test]
fn resolve_auth_config_token_yields_bearer() {
    let cfg = auth_config(Some("tok-123"), Some("u"), Some("p"));
    match resolve_auth(&cfg, env_map(&[])).expect("resolves") {
        BbAuth::Bearer(t) => assert_eq!(t, "tok-123"),
        BbAuth::Basic { .. } => panic!("expected Bearer, got Basic"),
    }
}

/// A `${VAR}` token placeholder is expanded from the injected env, and the
/// `BITBUCKET_TOKEN` env var is used as a fallback when config token is unset.
///
/// Why: keeps token env-expansion (#741) and the env fallback covered after
/// the refactor.
/// What: placeholder token → expanded value; absent token → env var value.
/// Test: this test.
#[test]
fn resolve_auth_token_expands_and_falls_back() {
    let cfg = auth_config(Some("${TGA_BB_TOKEN}"), None, None);
    match resolve_auth(&cfg, env_map(&[("TGA_BB_TOKEN", "expanded-tok")])).expect("resolves") {
        BbAuth::Bearer(t) => assert_eq!(t, "expanded-tok"),
        BbAuth::Basic { .. } => panic!("expected Bearer"),
    }

    let cfg = auth_config(None, None, None);
    match resolve_auth(&cfg, env_map(&[("BITBUCKET_TOKEN", "env-tok")])).expect("resolves") {
        BbAuth::Bearer(t) => assert_eq!(t, "env-tok"),
        BbAuth::Basic { .. } => panic!("expected Bearer from env fallback"),
    }
}

/// `app_password` config values written as `${VAR}` are expanded from the
/// environment before being used for Basic auth (closes #842).
///
/// Why: PR #743 fixed env-expansion for `token` but missed `app_password`,
/// so users who wrote `app_password = "${MY_SECRET}"` in their YAML config
/// got a literal `${MY_SECRET}` sent as the HTTP Basic password → silent 401.
/// What: injects an env value, configures `app_password` as a placeholder,
/// resolves auth, and asserts the resolved password equals the env value.
/// Test: this test (pure; injected env, no `std::env` mutation).
#[test]
fn resolve_auth_app_password_env_var_expanded() {
    let cfg = auth_config(None, Some("myuser"), Some("${TGA_BB_APP_PW}"));
    match resolve_auth(&cfg, env_map(&[("TGA_BB_APP_PW", "s3cr3t-expanded")])).expect("resolves") {
        BbAuth::Basic { username, password } => {
            assert_eq!(username, "myuser");
            assert_eq!(
                password, "s3cr3t-expanded",
                "app_password placeholder must be expanded from env; \
                 got literal placeholder instead"
            );
        }
        BbAuth::Bearer(_) => panic!("expected Basic auth, got Bearer"),
    }
}

/// When `app_password` in config is a plain value (no `${…}` placeholder),
/// it is used as-is; the `BITBUCKET_APP_PASSWORD` env fallback is NOT used.
///
/// Why: verifies the precedence chain — config value wins over env fallback —
/// so adding env-expansion does not accidentally break callers who store the
/// literal password in YAML.
/// What: injects a different `BITBUCKET_APP_PASSWORD`, configures a plain
/// `app_password`, and asserts the config value wins.
/// Test: this test.
#[test]
fn resolve_auth_app_password_config_takes_precedence_over_env_fallback() {
    let cfg = auth_config(None, Some("bob"), Some("config-literal-pw"));
    match resolve_auth(
        &cfg,
        env_map(&[("BITBUCKET_APP_PASSWORD", "env-fallback-value")]),
    )
    .expect("resolves")
    {
        BbAuth::Basic { password, .. } => {
            assert_eq!(
                password, "config-literal-pw",
                "config app_password must win over BITBUCKET_APP_PASSWORD env fallback"
            );
        }
        BbAuth::Bearer(_) => panic!("expected Basic auth, got Bearer"),
    }
}

/// When `app_password` is absent from config, the `BITBUCKET_APP_PASSWORD`
/// env var is used as the fallback credential.
///
/// Why: validates the fallback branch is still reachable after the
/// env-expansion fix — a regression here would silently break users who rely
/// on the env var. This is the case that flaked in CI (issue #1653); injecting
/// the env removes the parallel-test race entirely.
/// What: config `app_password` absent, env fallback present → env value used.
/// Test: this test (pure; injected env, no `std::env` mutation).
#[test]
fn resolve_auth_app_password_falls_back_to_env_when_config_absent() {
    let cfg = auth_config(None, Some("carol"), None);
    match resolve_auth(&cfg, env_map(&[("BITBUCKET_APP_PASSWORD", "env-only-pw")]))
        .expect("resolves")
    {
        BbAuth::Basic { password, .. } => {
            assert_eq!(password, "env-only-pw");
        }
        BbAuth::Bearer(_) => panic!("expected Basic auth, got Bearer"),
    }
}

/// Build a minimal, fully-populated [`PullRequest`] for upsert-guard tests.
fn sample_pr(repository: &str, pr_number: u64, state: PrState, fetched_at: &str) -> PullRequest {
    PullRequest {
        id: 0,
        pr_number,
        repository: repository.to_string(),
        title: "T".to_string(),
        author: "ada".to_string(),
        state,
        created_at: Utc::now(),
        merged_at: None,
        commit_shas: "[]".to_string(),
        fetched_at: fetched_at.to_string(),
    }
}

fn store_test_client() -> BitbucketClient {
    BitbucketClient::new(&BitbucketConfig {
        token: Some("dummy".into()),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        ..Default::default()
    })
    .expect("client builds")
}

/// Why: regression guard for issue #821. A background job re-ingesting an
/// OLDER snapshot must not be able to downgrade a PR already recorded as
/// `merged` back to `open` — the `fetched_at` guard on `store_pull_requests`
/// must reject a conflicting write whose `fetched_at` is not strictly newer.
/// What: upsert a `merged` PR with `fetched_at = "2026-01-02T00:00:00Z"`,
/// then upsert the same `(provider, repository, pr_number)` with `state =
/// "open"` and an OLDER `fetched_at = "2026-01-01T00:00:00Z"`. Asserts the
/// row still reads `state = "merged"` afterwards.
/// Test: this test.
#[test]
fn store_pull_requests_stale_write_guard_rejects_older_fetched_at() {
    let db = Database::open_in_memory().expect("open db");
    let client = store_test_client();

    let fresh = sample_pr("acme/widgets", 42, PrState::Merged, "2026-01-02T00:00:00Z");
    client
        .store_pull_requests(&db, &[fresh])
        .expect("initial upsert");

    let stale = sample_pr("acme/widgets", 42, PrState::Open, "2026-01-01T00:00:00Z");
    client
        .store_pull_requests(&db, &[stale])
        .expect("stale upsert must not error, just be rejected by the WHERE guard");

    let state: String = db
        .connection()
        .query_row(
            "SELECT state FROM pull_requests \
             WHERE provider = 'bitbucket' AND repository = ?1 AND pr_number = 42",
            params!["acme/widgets"],
            |r| r.get(0),
        )
        .expect("read back state");
    assert_eq!(
        state, "merged",
        "stale write with an older fetched_at must be rejected"
    );
}

/// Why: the flip side of the guard — issue #821 requires both branches of
/// the `WHERE excluded.fetched_at > pull_requests.fetched_at` predicate to
/// be exercised, not just the rejection path.
/// What: upsert a `merged` PR, then upsert the same triple with `state =
/// "open"` and a genuinely NEWER `fetched_at`. Asserts the update DOES
/// apply this time.
/// Test: this test.
#[test]
fn store_pull_requests_applies_genuinely_newer_fetched_at() {
    let db = Database::open_in_memory().expect("open db");
    let client = store_test_client();

    let first = sample_pr("acme/widgets", 77, PrState::Merged, "2026-01-01T00:00:00Z");
    client
        .store_pull_requests(&db, &[first])
        .expect("initial upsert");

    let newer = sample_pr("acme/widgets", 77, PrState::Open, "2026-01-02T00:00:00Z");
    client
        .store_pull_requests(&db, &[newer])
        .expect("newer upsert");

    let state: String = db
        .connection()
        .query_row(
            "SELECT state FROM pull_requests \
             WHERE provider = 'bitbucket' AND repository = ?1 AND pr_number = 77",
            params!["acme/widgets"],
            |r| r.get(0),
        )
        .expect("read back state");
    assert_eq!(
        state, "open",
        "a genuinely newer fetched_at must overwrite the previous row"
    );
}