quantoxide 0.6.2

Rust framework for developing, backtesting, and deploying Bitcoin futures trading strategies.
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
use std::{collections::HashSet, sync::Arc};

use async_trait::async_trait;
use chrono::{DateTime, Duration, Timelike, Utc};
use lnm_sdk::rest::v3::models::OhlcCandle;
use sqlx::{QueryBuilder, Sqlite, SqlitePool, Transaction};

use crate::{
    db::{
        CANDLE_STABLE_AGE,
        error::{DbError, Result},
        models::OhlcCandleRow,
        repositories::{OhlcCandlesRepository, OhlcCandlesRepositoryRead},
    },
    shared::OhlcResolution,
    util::{DateTimeExt, OhlcBucketAccumulator},
};

pub(crate) struct SqliteOhlcCandlesRepo {
    pool: Arc<SqlitePool>,
}

impl SqliteOhlcCandlesRepo {
    pub(crate) fn new(pool: Arc<SqlitePool>) -> Self {
        Self { pool }
    }

    fn pool(&self) -> &SqlitePool {
        self.pool.as_ref()
    }

    async fn start_transaction(&self) -> Result<Transaction<'static, Sqlite>> {
        self.pool.begin().await.map_err(DbError::TransactionBegin)
    }
}

#[async_trait]
impl OhlcCandlesRepositoryRead for SqliteOhlcCandlesRepo {
    async fn get_candles(
        &self,
        from: DateTime<Utc>,
        to: DateTime<Utc>,
    ) -> Result<Vec<OhlcCandleRow>> {
        let rows = sqlx::query_as!(
            OhlcCandleRow,
            r#"
                SELECT
                    time as "time!: DateTime<Utc>",
                    open as "open!: f64",
                    high as "high!: f64",
                    low as "low!: f64",
                    close as "close!: f64",
                    volume as "volume!: i64",
                    created_at as "created_at!: DateTime<Utc>",
                    updated_at as "updated_at!: DateTime<Utc>",
                    stable as "stable!: bool"
                FROM ohlc_candles
                WHERE time >= ?1 AND time <= ?2
                ORDER BY time ASC
            "#,
            from,
            to,
        )
        .fetch_all(self.pool())
        .await
        .map_err(DbError::Query)?;

        Ok(rows)
    }

    async fn get_candles_consolidated(
        &self,
        from: DateTime<Utc>,
        to: DateTime<Utc>,
        resolution: OhlcResolution,
    ) -> Result<Vec<OhlcCandleRow>> {
        let candles = self.get_candles(from, to).await?;

        if matches!(resolution, OhlcResolution::OneMinute) {
            return Ok(candles);
        }

        let mut consolidated = Vec::new();
        let mut current: Option<OhlcBucketAccumulator> = None;

        let accumulator_from_candle = |bucket_time: DateTime<Utc>, candle: &OhlcCandleRow| {
            let mut accumulator = OhlcBucketAccumulator::new(bucket_time);
            accumulator.add_candle(candle);
            accumulator
        };

        let finish_consolidated = |acc: &OhlcBucketAccumulator| {
            let bucket_complete =
                acc.bucket_time() + Duration::seconds(resolution.as_seconds() as i64) <= to;
            acc.to_candle_row(bucket_complete)
        };

        for candle in candles {
            let bucket_time = candle.time.floor_to_resolution(resolution);

            match current.as_mut() {
                Some(acc) if acc.bucket_time() == bucket_time => acc.add_candle(&candle),
                Some(acc) => {
                    consolidated.push(finish_consolidated(acc));
                    current = Some(accumulator_from_candle(bucket_time, &candle));
                }
                None => current = Some(accumulator_from_candle(bucket_time, &candle)),
            }
        }

        if let Some(acc) = current.as_ref() {
            consolidated.push(finish_consolidated(acc));
        }

        Ok(consolidated)
    }

    async fn get_earliest_candle_time(&self) -> Result<Option<DateTime<Utc>>> {
        struct TimeRow {
            pub time: DateTime<Utc>,
        }

        let row = sqlx::query_as!(
            TimeRow,
            r#"
                SELECT time as "time!: DateTime<Utc>"
                FROM ohlc_candles
                ORDER BY time ASC
                LIMIT 1
            "#
        )
        .fetch_optional(self.pool())
        .await
        .map_err(DbError::Query)?;

        Ok(row.map(|r| r.time))
    }

    async fn get_latest_candle_time(&self) -> Result<Option<DateTime<Utc>>> {
        struct TimeRow {
            pub time: DateTime<Utc>,
        }

        let row = sqlx::query_as!(
            TimeRow,
            r#"
                SELECT time as "time!: DateTime<Utc>"
                FROM ohlc_candles
                ORDER BY time DESC
                LIMIT 1
            "#
        )
        .fetch_optional(self.pool())
        .await
        .map_err(DbError::Query)?;

        Ok(row.map(|r| r.time))
    }

    async fn get_gaps(&self) -> Result<Vec<(DateTime<Utc>, DateTime<Utc>)>> {
        let gaps = sqlx::query!(
            r#"
                SELECT
                    (
                        SELECT time FROM ohlc_candles
                        WHERE time < gap_candle.time AND stable = 1
                        ORDER BY time DESC
                        LIMIT 1
                    ) as "from_time!: DateTime<Utc>",
                    gap_candle.time as "gap_time!: DateTime<Utc>"
                FROM ohlc_candles gap_candle
                WHERE gap_candle.gap = 1
                AND gap_candle.stable = 1
                AND EXISTS (
                    SELECT 1 FROM ohlc_candles
                    WHERE time < gap_candle.time AND stable = 1
                )
                ORDER BY gap_candle.time ASC
            "#
        )
        .fetch_all(self.pool())
        .await
        .map_err(DbError::Query)?
        .into_iter()
        .map(|row| (row.from_time, row.gap_time))
        .collect();

        Ok(gaps)
    }
}

#[async_trait]
impl OhlcCandlesRepository for SqliteOhlcCandlesRepo {
    async fn add_candles(
        &self,
        before_candle_time: Option<DateTime<Utc>>,
        new_candles: &[OhlcCandle],
    ) -> Result<()> {
        if new_candles.is_empty() {
            return Ok(());
        }

        for window in new_candles.windows(2) {
            let [current, next] = window else {
                unreachable!()
            };

            if current.time().second() != 0 || current.time().nanosecond() != 0 {
                return Err(DbError::NewDbCandlesTimesNotRoundedToMinute);
            }

            if next.time() >= current.time() {
                return Err(DbError::NewDbCandlesNotOrderedByTimeDesc {
                    inconsistency_at: next.time(),
                });
            }
        }

        let period_start = new_candles.last().expect("not empty").time();

        if period_start.second() != 0 || period_start.nanosecond() != 0 {
            return Err(DbError::NewDbCandlesTimesNotRoundedToMinute);
        }

        let mut tx = self.start_transaction().await?;

        if let Some(before_candle_time) = before_candle_time {
            sqlx::query!(
                "UPDATE ohlc_candles SET gap = 0 WHERE time = ?1",
                before_candle_time
            )
            .execute(&mut *tx)
            .await
            .map_err(DbError::Query)?;
        }

        let before_period_time = period_start - Duration::minutes(1);
        let before_period_candle_exists = sqlx::query_scalar!(
            "SELECT EXISTS(SELECT 1 FROM ohlc_candles WHERE time = ?1 AND stable = 1)",
            before_period_time
        )
        .fetch_one(&mut *tx)
        .await
        .map_err(DbError::Query)?
            != 0;

        let mut gaps: Vec<bool> = vec![false; new_candles.len()];
        gaps[new_candles.len() - 1] = !before_period_candle_exists;

        let stable_cutoff = Utc::now() - CANDLE_STABLE_AGE;
        let stables: Vec<bool> = new_candles
            .iter()
            .map(|candle| candle.time() <= stable_cutoff)
            .collect();

        let mut query_builder = QueryBuilder::<Sqlite>::new(
            "INSERT INTO ohlc_candles (time, open, high, low, close, volume, gap, stable) ",
        );

        query_builder.push_values(
            new_candles.iter().zip(gaps.iter()).zip(stables.iter()),
            |mut row, ((candle, gap), stable)| {
                row.push_bind(candle.time())
                    .push_bind(candle.open().as_f64())
                    .push_bind(candle.high().as_f64())
                    .push_bind(candle.low().as_f64())
                    .push_bind(candle.close().as_f64())
                    .push_bind(candle.volume() as i64)
                    .push_bind(*gap)
                    .push_bind(*stable);
            },
        );

        query_builder.push(
            r#"
                ON CONFLICT (time) DO UPDATE
                SET open = excluded.open,
                    high = excluded.high,
                    low = excluded.low,
                    close = excluded.close,
                    volume = excluded.volume,
                    gap = excluded.gap,
                    stable = excluded.stable
                WHERE ohlc_candles.open IS NOT excluded.open
                   OR ohlc_candles.high IS NOT excluded.high
                   OR ohlc_candles.low IS NOT excluded.low
                   OR ohlc_candles.close IS NOT excluded.close
                   OR ohlc_candles.volume IS NOT excluded.volume
                   OR ohlc_candles.gap IS NOT excluded.gap
                   OR ohlc_candles.stable IS NOT excluded.stable
            "#,
        );

        query_builder
            .build()
            .execute(&mut *tx)
            .await
            .map_err(DbError::Query)?;

        tx.commit().await.map_err(DbError::TransactionCommit)?;

        Ok(())
    }

    async fn remove_gap_flag(&self, time: DateTime<Utc>) -> Result<()> {
        sqlx::query!("UPDATE ohlc_candles SET gap = 0 WHERE time = ?1", time)
            .execute(self.pool())
            .await
            .map_err(DbError::Query)?;

        Ok(())
    }

    async fn flag_missing_candles(&self, range: Duration) -> Result<()> {
        let mut tx = self.start_transaction().await?;
        let cutoff_time = Utc::now() - range;

        struct TimeRow {
            pub time: DateTime<Utc>,
        }

        let gap_after_times = sqlx::query_as!(
            TimeRow,
            r#"
                WITH ordered AS (
                    SELECT
                        time,
                        stable,
                        LEAD(time) OVER (ORDER BY time ASC) AS next_time,
                        LEAD(stable) OVER (ORDER BY time ASC) AS next_stable,
                        LEAD(gap) OVER (ORDER BY time ASC) AS next_gap
                    FROM ohlc_candles
                    WHERE time >= ?1
                )
                SELECT time as "time!: DateTime<Utc>"
                FROM ordered
                WHERE stable = 1
                AND next_time IS NOT NULL
                AND unixepoch(next_time) > unixepoch(time) + 60
                AND next_stable = 1
                AND next_gap = 0
                ORDER BY time ASC
            "#,
            cutoff_time,
        )
        .fetch_all(&mut *tx)
        .await
        .map_err(DbError::Query)?;

        let mut unstable_times = HashSet::new();
        let mut gap_times = HashSet::new();

        for gap_after_time in gap_after_times.into_iter().map(|row| row.time) {
            let before_gap = sqlx::query_as!(
                TimeRow,
                r#"
                    SELECT time as "time!: DateTime<Utc>"
                    FROM ohlc_candles
                    WHERE time >= ?1 AND time <= ?2
                    ORDER BY time DESC
                    LIMIT 5
                "#,
                cutoff_time,
                gap_after_time,
            )
            .fetch_all(&mut *tx)
            .await
            .map_err(DbError::Query)?;

            unstable_times.extend(before_gap.into_iter().map(|row| row.time));

            let after_gap = sqlx::query_as!(
                TimeRow,
                r#"
                    SELECT time as "time!: DateTime<Utc>"
                    FROM ohlc_candles
                    WHERE time >= ?1 AND time > ?2
                    ORDER BY time ASC
                    LIMIT 6
                "#,
                cutoff_time,
                gap_after_time,
            )
            .fetch_all(&mut *tx)
            .await
            .map_err(DbError::Query)?;

            for (index, row) in after_gap.into_iter().enumerate() {
                if index < 5 {
                    unstable_times.insert(row.time);
                } else {
                    gap_times.insert(row.time);
                }
            }
        }

        for time in unstable_times {
            sqlx::query!("UPDATE ohlc_candles SET stable = 0 WHERE time = ?1", time)
                .execute(&mut *tx)
                .await
                .map_err(DbError::Query)?;
        }

        for time in gap_times {
            sqlx::query!("UPDATE ohlc_candles SET gap = 1 WHERE time = ?1", time)
                .execute(&mut *tx)
                .await
                .map_err(DbError::Query)?;
        }

        sqlx::query!(
            r#"
                UPDATE ohlc_candles
                SET gap = 1
                WHERE time IN (
                    SELECT c_stable.time
                    FROM ohlc_candles c_stable
                    WHERE c_stable.time >= ?1
                    AND c_stable.stable = 1
                    AND c_stable.gap = 0
                    AND (
                        SELECT stable
                        FROM ohlc_candles
                        WHERE time < c_stable.time
                        ORDER BY time DESC
                        LIMIT 1
                    ) = 0
                )
            "#,
            cutoff_time,
        )
        .execute(&mut *tx)
        .await
        .map_err(DbError::Query)?;

        tx.commit().await.map_err(DbError::TransactionCommit)?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::{sync::Arc, time::Duration as StdDuration};

    use chrono::{TimeZone, Utc};
    use serde_json::json;
    use sqlx::sqlite::SqlitePoolOptions;

    use super::*;

    async fn repo() -> SqliteOhlcCandlesRepo {
        let pool = SqlitePoolOptions::new()
            .max_connections(1)
            .connect("sqlite::memory:")
            .await
            .unwrap();

        sqlx::migrate!("./migrations/sqlite")
            .run(&pool)
            .await
            .unwrap();

        SqliteOhlcCandlesRepo::new(Arc::new(pool))
    }

    fn candle(minute: u32, open: f64, high: f64, low: f64, close: f64, volume: u64) -> OhlcCandle {
        serde_json::from_value(json!({
            "time": Utc.with_ymd_and_hms(2025, 1, 1, 0, minute, 0).unwrap(),
            "open": open,
            "high": high,
            "low": low,
            "close": close,
            "volume": volume,
        }))
        .unwrap()
    }

    async fn insert_stable_candle(repo: &SqliteOhlcCandlesRepo, minute: u32) {
        sqlx::query(
            r#"
                INSERT INTO ohlc_candles (time, open, high, low, close, volume, stable)
                VALUES (?1, ?2, ?3, ?4, ?5, ?6, 1)
            "#,
        )
        .bind(Utc.with_ymd_and_hms(2025, 1, 1, 0, minute, 0).unwrap())
        .bind(100.0 + minute as f64)
        .bind(101.0 + minute as f64)
        .bind(99.0 + minute as f64)
        .bind(100.5 + minute as f64)
        .bind(minute as i64)
        .execute(repo.pool())
        .await
        .unwrap();
    }

    async fn candle_flags(repo: &SqliteOhlcCandlesRepo, minute: u32) -> (bool, bool) {
        sqlx::query_as::<_, (bool, bool)>(
            r#"
                SELECT gap, stable
                FROM ohlc_candles
                WHERE time = ?1
            "#,
        )
        .bind(Utc.with_ymd_and_hms(2025, 1, 1, 0, minute, 0).unwrap())
        .fetch_one(repo.pool())
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn add_candles_upserts_and_reads_ranges_without_touching_unchanged_rows() {
        let repo = repo().await;
        let candles = [
            candle(2, 102.0, 112.0, 92.0, 107.0, 12),
            candle(1, 101.0, 111.0, 91.0, 106.0, 11),
        ];

        repo.add_candles(None, &candles).await.unwrap();

        let from = Utc.with_ymd_and_hms(2025, 1, 1, 0, 1, 0).unwrap();
        let to = Utc.with_ymd_and_hms(2025, 1, 1, 0, 2, 0).unwrap();
        let first_read = repo.get_candles(from, to).await.unwrap();
        assert_eq!(first_read.len(), 2);
        assert_eq!(first_read[0].time, candles[1].time());
        assert_eq!(first_read[0].open, 101.0);
        assert!(first_read[0].stable);

        repo.add_candles(None, &candles).await.unwrap();
        let unchanged_read = repo.get_candles(from, to).await.unwrap();
        assert_eq!(unchanged_read[0].updated_at, first_read[0].updated_at);

        tokio::time::sleep(StdDuration::from_millis(10)).await;
        let changed = [
            candle(2, 102.0, 112.0, 92.0, 107.0, 12),
            candle(1, 101.0, 111.0, 91.0, 108.0, 11),
        ];
        repo.add_candles(None, &changed).await.unwrap();
        let changed_read = repo.get_candles(from, to).await.unwrap();
        assert_eq!(changed_read[0].close, 108.0);
        assert!(changed_read[0].updated_at > first_read[0].updated_at);
    }

    #[tokio::test]
    async fn add_candles_rejects_invalid_input_ordering_and_rounding() {
        let repo = repo().await;
        let ascending = [
            candle(1, 101.0, 111.0, 91.0, 106.0, 11),
            candle(2, 102.0, 112.0, 92.0, 107.0, 12),
        ];
        assert!(matches!(
            repo.add_candles(None, &ascending).await,
            Err(DbError::NewDbCandlesNotOrderedByTimeDesc { .. })
        ));

        let unrounded = serde_json::from_value::<OhlcCandle>(json!({
            "time": Utc.with_ymd_and_hms(2025, 1, 1, 0, 1, 1).unwrap(),
            "open": 101.0,
            "high": 111.0,
            "low": 91.0,
            "close": 106.0,
            "volume": 11,
        }))
        .unwrap();
        assert!(matches!(
            repo.add_candles(None, &[unrounded]).await,
            Err(DbError::NewDbCandlesTimesNotRoundedToMinute)
        ));
    }

    #[tokio::test]
    async fn consolidated_reads_aggregate_candles_by_resolution() {
        let repo = repo().await;
        let candles = [
            candle(2, 102.0, 112.0, 92.0, 107.0, 12),
            candle(1, 101.0, 111.0, 91.0, 106.0, 11),
            candle(0, 100.0, 110.0, 90.0, 105.0, 10),
        ];
        repo.add_candles(None, &candles).await.unwrap();

        let rows = repo
            .get_candles_consolidated(
                Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap(),
                Utc.with_ymd_and_hms(2025, 1, 1, 0, 3, 0).unwrap(),
                OhlcResolution::ThreeMinutes,
            )
            .await
            .unwrap();

        assert_eq!(rows.len(), 1);
        assert_eq!(
            rows[0].time,
            Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap()
        );
        assert_eq!(rows[0].open, 100.0);
        assert_eq!(rows[0].high, 112.0);
        assert_eq!(rows[0].low, 90.0);
        assert_eq!(rows[0].close, 107.0);
        assert_eq!(rows[0].volume, 33);
        assert!(rows[0].stable);
    }

    #[tokio::test]
    async fn earliest_and_latest_candle_time_return_bounds() {
        let repo = repo().await;
        assert_eq!(repo.get_earliest_candle_time().await.unwrap(), None);
        assert_eq!(repo.get_latest_candle_time().await.unwrap(), None);

        repo.add_candles(
            None,
            &[
                candle(2, 102.0, 112.0, 92.0, 107.0, 12),
                candle(1, 101.0, 111.0, 91.0, 106.0, 11),
            ],
        )
        .await
        .unwrap();

        assert_eq!(
            repo.get_earliest_candle_time().await.unwrap(),
            Some(Utc.with_ymd_and_hms(2025, 1, 1, 0, 1, 0).unwrap())
        );
        assert_eq!(
            repo.get_latest_candle_time().await.unwrap(),
            Some(Utc.with_ymd_and_hms(2025, 1, 1, 0, 2, 0).unwrap())
        );
    }

    #[tokio::test]
    async fn gap_flags_are_placed_cleared_and_read() {
        let repo = repo().await;
        insert_stable_candle(&repo, 0).await;

        repo.add_candles(None, &[candle(3, 103.0, 113.0, 93.0, 108.0, 13)])
            .await
            .unwrap();
        assert_eq!(
            repo.get_gaps().await.unwrap(),
            vec![(
                Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap(),
                Utc.with_ymd_and_hms(2025, 1, 1, 0, 3, 0).unwrap(),
            )]
        );

        repo.add_candles(
            Some(Utc.with_ymd_and_hms(2025, 1, 1, 0, 3, 0).unwrap()),
            &[
                candle(2, 102.0, 112.0, 92.0, 107.0, 12),
                candle(1, 101.0, 111.0, 91.0, 106.0, 11),
            ],
        )
        .await
        .unwrap();
        assert!(repo.get_gaps().await.unwrap().is_empty());

        repo.remove_gap_flag(Utc.with_ymd_and_hms(2025, 1, 1, 0, 3, 0).unwrap())
            .await
            .unwrap();
        assert!(repo.get_gaps().await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn flag_missing_candles_marks_unstable_neighbors_and_gap_marker() {
        let repo = repo().await;
        for minute in 0..=6 {
            insert_stable_candle(&repo, minute).await;
        }
        for minute in 10..=16 {
            insert_stable_candle(&repo, minute).await;
        }

        repo.flag_missing_candles(Duration::days(1000))
            .await
            .unwrap();

        assert_eq!(candle_flags(&repo, 1).await, (false, true));
        assert_eq!(candle_flags(&repo, 2).await, (false, false));
        assert_eq!(candle_flags(&repo, 10).await, (false, false));
        assert_eq!(candle_flags(&repo, 14).await, (false, false));
        assert_eq!(candle_flags(&repo, 15).await, (true, true));

        assert_eq!(
            repo.get_gaps().await.unwrap(),
            vec![(
                Utc.with_ymd_and_hms(2025, 1, 1, 0, 1, 0).unwrap(),
                Utc.with_ymd_and_hms(2025, 1, 1, 0, 15, 0).unwrap(),
            )]
        );
    }
}