crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! Credits domain model - pure domain entity without ORM annotations
//!
//! This module contains the pure domain model for Credits,
//! following Domain-Driven Design principles.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Credits domain model
///
/// Represents a team's credit balance for resource usage tracking.
/// This is a pure domain model without any ORM annotations.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Credits {
    /// Unique identifier
    pub id: crate::domain::id::Id,
    /// Team ID for multi-tenancy
    pub team_id: crate::domain::id::Id,
    /// Current credit balance (sensitive data)
    balance: i64,
    /// When the record was created
    pub created_at: DateTime<Utc>,
    /// When the record was last updated
    pub updated_at: DateTime<Utc>,
}

impl Credits {
    /// Create a new credits record
    pub fn new(
        id: crate::domain::id::Id,
        team_id: crate::domain::id::Id,
        initial_balance: i64,
    ) -> Self {
        let now = Utc::now();
        Self {
            id,
            team_id,
            balance: initial_balance,
            created_at: now,
            updated_at: now,
        }
    }

    /// Create a credits record with custom timestamps (for mappers)
    pub fn with_timestamps(
        id: crate::domain::id::Id,
        team_id: crate::domain::id::Id,
        balance: i64,
        created_at: DateTime<Utc>,
        updated_at: DateTime<Utc>,
    ) -> Self {
        Self {
            id,
            team_id,
            balance,
            created_at,
            updated_at,
        }
    }

    /// Get the current balance
    ///
    /// # Security Note
    ///
    /// This method returns the credit balance. Callers should handle
    /// this data carefully and avoid logging or exposing to users.
    pub fn balance(&self) -> i64 {
        self.balance
    }

    /// Check if the team has enough credits
    pub fn has_sufficient_balance(&self, amount: i64) -> bool {
        self.balance >= amount
    }

    /// Deduct credits from the balance
    ///
    /// Returns an error if insufficient balance
    pub fn deduct(&mut self, amount: i64) -> Result<(), CreditsError> {
        if self.balance < amount {
            return Err(CreditsError::InsufficientBalance {
                current: self.balance,
                requested: amount,
            });
        }
        self.balance -= amount;
        self.updated_at = Utc::now();
        Ok(())
    }

    /// Add credits to the balance
    pub fn add(&mut self, amount: i64) {
        self.balance += amount;
        self.updated_at = Utc::now();
    }

    /// Set balance directly (for administrative purposes)
    pub fn set_balance(&mut self, new_balance: i64) {
        self.balance = new_balance;
        self.updated_at = Utc::now();
    }
}

/// Credits transaction domain model
///
/// Represents a single credit transaction record.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CreditsTransaction {
    /// Unique identifier
    pub id: crate::domain::id::Id,
    /// Team ID for multi-tenancy
    pub team_id: crate::domain::id::Id,
    /// Transaction amount (positive for additions, negative for deductions)
    pub amount: i64,
    /// Type of transaction
    pub transaction_type: CreditsTransactionType,
    /// Human-readable description
    pub description: String,
    /// Reference to related entity (task, crawl, etc.)
    pub reference_id: Option<crate::domain::id::Id>,
    /// When the transaction occurred
    pub created_at: DateTime<Utc>,
}

impl CreditsTransaction {
    /// Create a new credits transaction
    pub fn new(
        id: crate::domain::id::Id,
        team_id: crate::domain::id::Id,
        amount: i64,
        transaction_type: CreditsTransactionType,
        description: String,
        reference_id: Option<crate::domain::id::Id>,
    ) -> Self {
        Self {
            id,
            team_id,
            amount,
            transaction_type,
            description,
            reference_id,
            created_at: Utc::now(),
        }
    }

    /// Create a transaction with custom timestamp (for mappers)
    pub fn with_timestamp(
        id: crate::domain::id::Id,
        team_id: crate::domain::id::Id,
        amount: i64,
        transaction_type: CreditsTransactionType,
        description: String,
        reference_id: Option<crate::domain::id::Id>,
        created_at: DateTime<Utc>,
    ) -> Self {
        Self {
            id,
            team_id,
            amount,
            transaction_type,
            description,
            reference_id,
            created_at,
        }
    }

    /// Check if this is a deduction transaction
    pub fn is_deduction(&self) -> bool {
        self.amount < 0
    }

    /// Check if this is an addition transaction
    pub fn is_addition(&self) -> bool {
        self.amount > 0
    }
}

/// Credits transaction type enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CreditsTransactionType {
    /// Credits used for search operation
    Search,
    /// Credits used for scrape operation
    Scrape,
    /// Credits used for extract operation
    Extract,
    /// Credits used for crawl operation
    Crawl,
    /// Manual adjustment by admin
    ManualAdjustment,
    /// Credits from subscription
    Subscription,
    /// Credits refunded
    Refund,
}

impl std::fmt::Display for CreditsTransactionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CreditsTransactionType::Search => write!(f, "search"),
            CreditsTransactionType::Scrape => write!(f, "scrape"),
            CreditsTransactionType::Extract => write!(f, "extract"),
            CreditsTransactionType::Crawl => write!(f, "crawl"),
            CreditsTransactionType::ManualAdjustment => write!(f, "manual_adjustment"),
            CreditsTransactionType::Subscription => write!(f, "subscription"),
            CreditsTransactionType::Refund => write!(f, "refund"),
        }
    }
}

impl std::str::FromStr for CreditsTransactionType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "search" => Ok(CreditsTransactionType::Search),
            "scrape" => Ok(CreditsTransactionType::Scrape),
            "extract" => Ok(CreditsTransactionType::Extract),
            "crawl" => Ok(CreditsTransactionType::Crawl),
            "manual_adjustment" => Ok(CreditsTransactionType::ManualAdjustment),
            "subscription" => Ok(CreditsTransactionType::Subscription),
            "refund" => Ok(CreditsTransactionType::Refund),
            _ => Err(format!("Invalid transaction type: {}", s)),
        }
    }
}

/// Credits domain errors
#[derive(Debug, thiserror::Error)]
pub enum CreditsError {
    /// Insufficient balance for operation
    #[error("Insufficient balance: have {current}, need {requested}")]
    InsufficientBalance { current: i64, requested: i64 },

    /// Invalid amount
    #[error("Invalid amount: {0}")]
    InvalidAmount(String),

    /// Database error
    #[error("Database error: {0}")]
    DatabaseError(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::str::FromStr;

    // Credits::new tests

    #[test]
    fn test_credits_new_sets_balance_and_timestamps() {
        let id = crate::domain::id::Id::generate();
        let team_id = crate::domain::id::Id::generate();

        let before = Utc::now();
        let credits = Credits::new(id, team_id, 1000);
        let after = Utc::now();

        assert_eq!(credits.id, id);
        assert_eq!(credits.team_id, team_id);
        assert_eq!(
            credits.balance(),
            1000,
            "balance should match initial_balance"
        );
        assert!(
            credits.created_at >= before && credits.created_at <= after,
            "created_at should be now"
        );
        assert_eq!(credits.created_at, credits.updated_at);
    }

    #[test]
    fn test_credits_new_zero_balance() {
        let credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            0,
        );
        assert_eq!(credits.balance(), 0);
    }

    #[test]
    fn test_credits_new_negative_balance() {
        let credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            -50,
        );
        assert_eq!(credits.balance(), -50, "negative initial balance allowed");
    }

    // Credits::with_timestamps tests

    #[test]
    fn test_credits_with_timestamps_preserves_values() {
        let id = crate::domain::id::Id::generate();
        let team_id = crate::domain::id::Id::generate();
        let created = Utc::now();
        let updated = created + chrono::Duration::seconds(60);

        let credits = Credits::with_timestamps(id, team_id, 500, created, updated);

        assert_eq!(credits.id, id);
        assert_eq!(credits.team_id, team_id);
        assert_eq!(credits.balance(), 500);
        assert_eq!(credits.created_at, created);
        assert_eq!(credits.updated_at, updated);
    }

    // has_sufficient_balance tests

    #[test]
    fn test_has_sufficient_balance_true_when_enough() {
        let credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            1000,
        );
        assert!(
            credits.has_sufficient_balance(1000),
            "exact amount should be sufficient"
        );
        assert!(
            credits.has_sufficient_balance(500),
            "less than balance is sufficient"
        );
    }

    #[test]
    fn test_has_sufficient_balance_false_when_insufficient() {
        let credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            100,
        );
        assert!(
            !credits.has_sufficient_balance(101),
            "more than balance is insufficient"
        );
    }

    #[test]
    fn test_has_sufficient_balance_zero_amount_always_true() {
        let credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            0,
        );
        assert!(
            credits.has_sufficient_balance(0),
            "zero amount against zero balance is sufficient"
        );
    }

    // deduct tests

    #[test]
    fn test_deduct_success_reduces_balance() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            1000,
        );
        let before = Utc::now();

        credits
            .deduct(300)
            .expect("deduct should succeed with sufficient balance");

        assert_eq!(
            credits.balance(),
            700,
            "balance should be reduced by deducted amount"
        );
        assert!(credits.updated_at >= before, "updated_at should advance");
    }

    #[test]
    fn test_deduct_exact_balance_succeeds() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            500,
        );
        credits
            .deduct(500)
            .expect("deducting exact balance should succeed");
        assert_eq!(
            credits.balance(),
            0,
            "balance should be 0 after full deduction"
        );
    }

    #[test]
    fn test_deduct_insufficient_balance_returns_error() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            100,
        );
        let err = credits
            .deduct(300)
            .expect_err("should error when insufficient balance");

        match err {
            CreditsError::InsufficientBalance { current, requested } => {
                assert_eq!(current, 100, "error should report current balance");
                assert_eq!(requested, 300, "error should report requested amount");
            }
            other => panic!("expected InsufficientBalance, got {:?}", other),
        }
        assert_eq!(
            credits.balance(),
            100,
            "balance should be unchanged on error"
        );
    }

    #[test]
    fn test_deduct_zero_amount_succeeds() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            100,
        );
        credits.deduct(0).expect("deducting 0 should succeed");
        assert_eq!(
            credits.balance(),
            100,
            "balance unchanged after deducting 0"
        );
    }

    // add tests

    #[test]
    fn test_add_increases_balance() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            500,
        );
        let before = Utc::now();

        credits.add(300);

        assert_eq!(
            credits.balance(),
            800,
            "balance should increase by added amount"
        );
        assert!(credits.updated_at >= before);
    }

    #[test]
    fn test_add_zero_unchanged_but_updates_timestamp() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            500,
        );
        let old_updated = credits.updated_at;
        // Sleep tiny bit to ensure timestamp differs
        std::thread::sleep(std::time::Duration::from_millis(2));
        credits.add(0);
        assert_eq!(credits.balance(), 500);
        assert!(
            credits.updated_at > old_updated,
            "updated_at should advance even for add(0)"
        );
    }

    // set_balance tests

    #[test]
    fn test_set_balance_overrides() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            1000,
        );
        credits.set_balance(42);
        assert_eq!(credits.balance(), 42, "set_balance should override");
    }

    #[test]
    fn test_set_balance_negative_allowed() {
        let mut credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            100,
        );
        credits.set_balance(-10);
        assert_eq!(
            credits.balance(),
            -10,
            "negative balance allowed via set_balance"
        );
    }

    // CreditsTransaction::new tests

    #[test]
    fn test_credits_transaction_new_sets_fields() {
        let id = crate::domain::id::Id::generate();
        let team_id = crate::domain::id::Id::generate();
        let ref_id = crate::domain::id::Id::generate();

        let before = Utc::now();
        let txn = CreditsTransaction::new(
            id,
            team_id,
            -50,
            CreditsTransactionType::Scrape,
            "scrape cost".to_string(),
            Some(ref_id),
        );
        let after = Utc::now();

        assert_eq!(txn.id, id);
        assert_eq!(txn.team_id, team_id);
        assert_eq!(txn.amount, -50);
        assert_eq!(txn.transaction_type, CreditsTransactionType::Scrape);
        assert_eq!(txn.description, "scrape cost");
        assert_eq!(txn.reference_id, Some(ref_id));
        assert!(
            txn.created_at >= before && txn.created_at <= after,
            "created_at should be now"
        );
    }

    #[test]
    fn test_credits_transaction_new_without_reference() {
        let txn = CreditsTransaction::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            100,
            CreditsTransactionType::Subscription,
            "monthly".to_string(),
            None,
        );
        assert!(txn.reference_id.is_none());
    }

    // CreditsTransaction::with_timestamp tests

    #[test]
    fn test_credits_transaction_with_timestamp_preserves_ts() {
        let id = crate::domain::id::Id::generate();
        let team_id = crate::domain::id::Id::generate();
        let ts = Utc::now();

        let txn = CreditsTransaction::with_timestamp(
            id,
            team_id,
            25,
            CreditsTransactionType::Refund,
            "refund".to_string(),
            None,
            ts,
        );

        assert_eq!(txn.id, id);
        assert_eq!(txn.team_id, team_id);
        assert_eq!(txn.amount, 25);
        assert_eq!(txn.transaction_type, CreditsTransactionType::Refund);
        assert_eq!(
            txn.created_at, ts,
            "with_timestamp should preserve custom ts"
        );
    }

    // is_deduction / is_addition tests

    #[test]
    fn test_is_deduction_true_for_negative_amount() {
        let txn = CreditsTransaction::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            -10,
            CreditsTransactionType::Scrape,
            "deduct".to_string(),
            None,
        );
        assert!(txn.is_deduction(), "negative amount is a deduction");
        assert!(!txn.is_addition(), "negative amount is not an addition");
    }

    #[test]
    fn test_is_addition_true_for_positive_amount() {
        let txn = CreditsTransaction::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            50,
            CreditsTransactionType::Subscription,
            "add".to_string(),
            None,
        );
        assert!(txn.is_addition(), "positive amount is an addition");
        assert!(!txn.is_deduction(), "positive amount is not a deduction");
    }

    #[test]
    fn test_is_deduction_and_is_addition_false_for_zero() {
        let txn = CreditsTransaction::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            0,
            CreditsTransactionType::ManualAdjustment,
            "zero".to_string(),
            None,
        );
        assert!(!txn.is_deduction(), "zero amount is not a deduction");
        assert!(!txn.is_addition(), "zero amount is not an addition");
    }

    // CreditsTransactionType Display / FromStr tests

    #[test]
    fn test_transaction_type_display_all_variants() {
        assert_eq!(CreditsTransactionType::Search.to_string(), "search");
        assert_eq!(CreditsTransactionType::Scrape.to_string(), "scrape");
        assert_eq!(CreditsTransactionType::Extract.to_string(), "extract");
        assert_eq!(CreditsTransactionType::Crawl.to_string(), "crawl");
        assert_eq!(
            CreditsTransactionType::ManualAdjustment.to_string(),
            "manual_adjustment"
        );
        assert_eq!(
            CreditsTransactionType::Subscription.to_string(),
            "subscription"
        );
        assert_eq!(CreditsTransactionType::Refund.to_string(), "refund");
    }

    #[test]
    fn test_transaction_type_from_str_valid() {
        assert_eq!(
            CreditsTransactionType::from_str("search").expect("valid"),
            CreditsTransactionType::Search
        );
        assert_eq!(
            CreditsTransactionType::from_str("scrape").expect("valid"),
            CreditsTransactionType::Scrape
        );
        assert_eq!(
            CreditsTransactionType::from_str("extract").expect("valid"),
            CreditsTransactionType::Extract
        );
        assert_eq!(
            CreditsTransactionType::from_str("crawl").expect("valid"),
            CreditsTransactionType::Crawl
        );
        assert_eq!(
            CreditsTransactionType::from_str("manual_adjustment").expect("valid"),
            CreditsTransactionType::ManualAdjustment
        );
        assert_eq!(
            CreditsTransactionType::from_str("subscription").expect("valid"),
            CreditsTransactionType::Subscription
        );
        assert_eq!(
            CreditsTransactionType::from_str("refund").expect("valid"),
            CreditsTransactionType::Refund
        );
    }

    #[test]
    fn test_transaction_type_from_str_invalid_returns_error() {
        let err = CreditsTransactionType::from_str("unknown_type")
            .expect_err("invalid type should error");
        assert!(
            err.contains("Invalid transaction type"),
            "error should describe invalid type: {}",
            err
        );
        assert!(
            err.contains("unknown_type"),
            "error should include the bad value"
        );
    }

    #[test]
    fn test_transaction_type_serde_roundtrip() {
        for ty in [
            CreditsTransactionType::Search,
            CreditsTransactionType::Scrape,
            CreditsTransactionType::Extract,
            CreditsTransactionType::Crawl,
            CreditsTransactionType::ManualAdjustment,
            CreditsTransactionType::Subscription,
            CreditsTransactionType::Refund,
        ] {
            let json = serde_json::to_string(&ty).expect("serialize");
            let back: CreditsTransactionType = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(ty, back, "roundtrip should preserve: {}", json);
        }
    }

    // CreditsError tests

    #[test]
    fn test_credits_error_insufficient_balance_display() {
        let err = CreditsError::InsufficientBalance {
            current: 50,
            requested: 100,
        };
        let msg = err.to_string();
        assert!(msg.contains("Insufficient balance"), "msg: {}", msg);
        assert!(msg.contains("50"), "should show current: {}", msg);
        assert!(msg.contains("100"), "should show requested: {}", msg);
    }

    #[test]
    fn test_credits_error_invalid_amount_display() {
        let err = CreditsError::InvalidAmount("negative".to_string());
        assert!(err.to_string().contains("Invalid amount"));
        assert!(err.to_string().contains("negative"));
    }

    #[test]
    fn test_credits_error_database_error_display() {
        let err = CreditsError::DatabaseError("conn lost".to_string());
        assert!(err.to_string().contains("Database error"));
        assert!(err.to_string().contains("conn lost"));
    }

    // Credits / CreditsTransaction serde roundtrip

    #[test]
    fn test_credits_serde_roundtrip() {
        let credits = Credits::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            750,
        );
        let json = serde_json::to_string(&credits).expect("serialize");
        let back: Credits = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(credits, back, "serde roundtrip should preserve credits");
    }

    #[test]
    fn test_credits_transaction_serde_roundtrip() {
        let txn = CreditsTransaction::new(
            crate::domain::id::Id::generate(),
            crate::domain::id::Id::generate(),
            -25,
            CreditsTransactionType::Crawl,
            "crawl cost".to_string(),
            Some(crate::domain::id::Id::generate()),
        );
        let json = serde_json::to_string(&txn).expect("serialize");
        let back: CreditsTransaction = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(txn, back, "serde roundtrip should preserve transaction");
    }
}