optionchain_simulator 0.2.0

OptionChain-Simulator is a lightweight REST API service that simulates an evolving option chain with every request. It is designed for developers building or testing trading systems, backtesters, and visual tools that depend on option data streams but want to avoid relying on live data feeds.
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
//! The typed records the snapshot repository persists and returns.
//!
//! These are **infrastructure** types, not the domain snapshot. `src/domain` is
//! a private module and `SeriesSnapshot`/`ExpiryChain` are `pub(crate)` inside
//! it, so this layer cannot name them — and should not: a storage record has to
//! survive a schema migration and a driver upgrade without dragging the
//! simulation's internals along. The session layer, which can see both, owns
//! the conversion from a built snapshot into a [`SnapshotRecord`].
//!
//! Everything here stays typed — `Positive`, `Decimal`, `DateTime<Utc>`,
//! `Uuid` — up to the serialization boundary in
//! [`super::model`], which is the single place a value becomes a ClickHouse
//! column.
//!
//! # What identifies a snapshot
//!
//! `(simulation, generation, step)`. Deliberately **not** called a version: a
//! v2 session already has a `version`, the optimistic-concurrency revision that
//! changes on every advance and travels on the wire. Feeding that number in here
//! would fragment one tape across hundreds of coordinates and defeat the whole
//! retry-safe deduplication, so the field carries the name of what it actually
//! is — see [`CURRENT_SNAPSHOT_GENERATION`].
//!
//! Everything in this module treats two writes of the same triple as two
//! attempts at the *same* immutable snapshot.

use crate::utils::ChainError;
use chrono::{DateTime, Utc};
use positive::Positive;
use rust_decimal::Decimal;
use std::fmt;
use uuid::Uuid;

/// The generation every snapshot this build produces is written under.
///
/// A snapshot's coordinate is `(simulation, generation, step)`, and the
/// invariant the storage layer rests on is that the coordinate determines the
/// snapshot: two writes of it are two attempts at the same immutable rows, and
/// the second silently replaces the first.
///
/// The generation is what keeps that true across time. Bump it when a change to
/// this crate would make the *same* simulation and step produce *different*
/// values — a pricing fix, a walk-kernel resync, a change to the schedule
/// projection. Rows written under the old generation then stay addressable and
/// are never mixed with the new ones, rather than being half-overwritten by a
/// replay that no longer agrees with them.
///
/// A caller materialising snapshots passes this constant; a caller reading them
/// back passes the generation the rows were written under, which for everything
/// this build wrote is this one. It lives here, next to the records, so an
/// external consumer of the `pub` read API has a name to pass instead of a
/// hardcoded literal.
///
/// # History
///
/// - `1` — the shape 0.2.0 shipped. A historical walk priced every step at the
///   constant `volatility` the request supplied.
/// - `2` — a historical walk prices each step at a causal realized-volatility
///   estimate over the observations up to it. The price path is unchanged and
///   every other walk type is unaffected, but the premiums of a historical
///   simulation are not, so its rows are a different tape under the same
///   coordinates. Generation 1 rows stay in place until their retention expires
///   and are simply not read by this build.
pub const CURRENT_SNAPSHOT_GENERATION: u64 = 2;

/// The namespace every deterministic `snapshot_id` is derived under.
///
/// The ASCII bytes of `ocs-snapshot-v1`, padded to sixteen. Fixed forever: a
/// new namespace would give every already-persisted snapshot a second identity,
/// and every retry a duplicate.
const SNAPSHOT_NAMESPACE: Uuid = Uuid::from_u128(0x6f_63_73_2d_73_6e_61_70_73_68_6f_74_2d_76_31_00);

/// The deterministic identity of one materialised snapshot.
///
/// A UUID v5 over `(simulation, generation, step)` — the same primitive
/// [`crate::utils::UuidGenerator`] uses (`Uuid::new_v5`), applied to a
/// coordinate instead of to a counter, because a retry has to land on the id
/// the first attempt used. `UuidGenerator` itself cannot serve this: its name
/// is an internal counter, so two processes replaying the same step would
/// disagree.
///
/// # Examples
///
/// ```
/// use optionchain_simulator::infrastructure::{CURRENT_SNAPSHOT_GENERATION, snapshot_id};
/// use uuid::Uuid;
///
/// let simulation = Uuid::nil();
/// let generation = CURRENT_SNAPSHOT_GENERATION;
/// assert_eq!(
///     snapshot_id(simulation, generation, 7),
///     snapshot_id(simulation, generation, 7)
/// );
/// assert_ne!(
///     snapshot_id(simulation, generation, 7),
///     snapshot_id(simulation, generation + 1, 7)
/// );
/// ```
#[must_use]
#[inline]
pub fn snapshot_id(simulation: Uuid, generation: u64, step: usize) -> Uuid {
    // The separators matter: without them `(1, 23)` and `(12, 3)` would hash
    // to the same name.
    let name = format!("{simulation}:{generation}:{step}");
    Uuid::new_v5(&SNAPSHOT_NAMESPACE, name.as_bytes())
}

/// Which side of a strike a query is about.
///
/// Both sides live in one persisted row — they share the strike, the implied
/// volatility and the gamma — so the side is a projection at read time, never a
/// storage dimension.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ContractSide {
    /// The call side.
    Call,
    /// The put side.
    Put,
}

impl ContractSide {
    /// The side's lowercase name, for logs and query construction.
    #[must_use]
    #[inline]
    pub fn as_str(self) -> &'static str {
        match self {
            ContractSide::Call => "call",
            ContractSide::Put => "put",
        }
    }
}

impl fmt::Display for ContractSide {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// One strike of one expiration, both sides.
///
/// Mirrors upstream `OptionData` exactly as far as the v2 surface goes:
/// upstream carries delta per side and one shared gamma, and no other Greek, so
/// neither does this. A new upstream Greek is a schema change, deliberately.
#[derive(Debug, Clone, PartialEq)]
pub struct QuoteRow {
    /// The strike price.
    pub strike: Positive,
    /// The per-strike implied volatility, after skew and smile.
    pub implied_volatility: Positive,
    /// The call bid, when upstream quoted one.
    pub call_bid: Option<Positive>,
    /// The call ask, when upstream quoted one.
    pub call_ask: Option<Positive>,
    /// The call mid, when upstream quoted one.
    pub call_mid: Option<Positive>,
    /// The put bid, when upstream quoted one.
    pub put_bid: Option<Positive>,
    /// The put ask, when upstream quoted one.
    pub put_ask: Option<Positive>,
    /// The put mid, when upstream quoted one.
    pub put_mid: Option<Positive>,
    /// The call delta.
    pub delta_call: Option<Decimal>,
    /// The put delta.
    pub delta_put: Option<Decimal>,
    /// Gamma, shared by the call and the put.
    pub gamma: Option<Decimal>,
}

impl QuoteRow {
    /// Creates a quote row with no quotes and no Greeks.
    ///
    /// The builder-style setters exist for tests and for a caller that has only
    /// part of a chain; production callers construct the struct literally from
    /// an upstream `OptionData`.
    #[must_use]
    pub fn new(strike: Positive, implied_volatility: Positive) -> Self {
        Self {
            strike,
            implied_volatility,
            call_bid: None,
            call_ask: None,
            call_mid: None,
            put_bid: None,
            put_ask: None,
            put_mid: None,
            delta_call: None,
            delta_put: None,
            gamma: None,
        }
    }

    /// Sets the call side.
    #[must_use = "builders do nothing unless the value is used"]
    pub fn with_call(
        mut self,
        bid: Option<Positive>,
        ask: Option<Positive>,
        mid: Option<Positive>,
        delta: Option<Decimal>,
    ) -> Self {
        self.call_bid = bid;
        self.call_ask = ask;
        self.call_mid = mid;
        self.delta_call = delta;
        self
    }

    /// Sets the put side.
    #[must_use = "builders do nothing unless the value is used"]
    pub fn with_put(
        mut self,
        bid: Option<Positive>,
        ask: Option<Positive>,
        mid: Option<Positive>,
        delta: Option<Decimal>,
    ) -> Self {
        self.put_bid = bid;
        self.put_ask = ask;
        self.put_mid = mid;
        self.delta_put = delta;
        self
    }

    /// Sets the shared gamma.
    #[must_use = "builders do nothing unless the value is used"]
    pub fn with_gamma(mut self, gamma: Option<Decimal>) -> Self {
        self.gamma = gamma;
        self
    }
}

/// One live expiration at one step, with every strike it quotes.
#[derive(Debug, Clone, PartialEq)]
pub struct ExpirationRecord {
    /// The absolute expiration instant, in UTC. The authoritative expiration:
    /// it comes from the planner, never from a chain's calendar string.
    pub expires_at: DateTime<Utc>,
    /// Fractional days remaining, from the same `(simulated_at, expires_at)`
    /// pair the planner used.
    pub days_to_expiration: Positive,
    /// Every schedule rule this expiration satisfies, sorted. A date claimed by
    /// two rules appears once, with both labels.
    pub labels: Vec<String>,
    /// The strikes, ascending.
    pub quotes: Vec<QuoteRow>,
}

impl ExpirationRecord {
    /// Creates an expiration record.
    #[must_use]
    pub fn new(
        expires_at: DateTime<Utc>,
        days_to_expiration: Positive,
        labels: Vec<String>,
        quotes: Vec<QuoteRow>,
    ) -> Self {
        Self {
            expires_at,
            days_to_expiration,
            labels,
            quotes,
        }
    }
}

/// One materialised simulation step: the whole simulated market at one instant.
///
/// This is the unit the repository persists and returns. Reconstructing it from
/// storage must yield exactly what was written — the same ordering, the same
/// decimals, the same `None`s — which is what makes the persisted tape usable
/// as the source for #49's exports instead of a replay.
#[derive(Debug, Clone, PartialEq)]
pub struct SnapshotRecord {
    /// The simulation this snapshot belongs to.
    pub simulation: Uuid,
    /// The generation the snapshot was produced under —
    /// [`CURRENT_SNAPSHOT_GENERATION`] for anything this build materialises.
    /// Not the session's `version`, which is its CAS revision.
    pub generation: u64,
    /// The 0-based step.
    pub step: usize,
    /// The simulated instant, from the factor tape.
    pub simulated_at: DateTime<Utc>,
    /// The ticker symbol every chain in the snapshot prices.
    pub symbol: String,
    /// The underlying price shared by every chain.
    pub spot: Positive,
    /// The base implied volatility shared by every chain, before skew and smile
    /// shape it per strike.
    pub base_volatility: Positive,
    /// The live expirations, ordered by `expires_at` ascending.
    pub expirations: Vec<ExpirationRecord>,
}

impl SnapshotRecord {
    /// Creates a snapshot record.
    ///
    /// The fields are public as well, so the session layer can build one
    /// straight from a domain snapshot without an intermediate copy. Nothing is
    /// validated here — [`SnapshotRecord::validate`] runs at the moment of
    /// writing, which is the only place an invalid record could do damage.
    #[must_use]
    #[allow(
        clippy::too_many_arguments,
        reason = "a storage record is wide by nature"
    )]
    pub fn new(
        simulation: Uuid,
        generation: u64,
        step: usize,
        simulated_at: DateTime<Utc>,
        symbol: String,
        spot: Positive,
        base_volatility: Positive,
        expirations: Vec<ExpirationRecord>,
    ) -> Self {
        Self {
            simulation,
            generation,
            step,
            simulated_at,
            symbol,
            spot,
            base_volatility,
            expirations,
        }
    }

    /// This snapshot's deterministic identity.
    #[must_use]
    #[inline]
    pub fn snapshot_id(&self) -> Uuid {
        snapshot_id(self.simulation, self.generation, self.step)
    }

    /// How many quote rows this snapshot writes.
    ///
    /// The number the completion marker carries and every read verifies.
    #[must_use]
    pub fn quote_count(&self) -> usize {
        self.expirations
            .iter()
            .map(|expiration| expiration.quotes.len())
            .sum()
    }

    /// Checks the invariants the storage layer depends on.
    ///
    /// The fields are public, so a caller can assemble a record that never
    /// passed through a domain snapshot. Two properties matter enough to
    /// enforce before anything is written:
    ///
    /// * **Ordering.** Expirations ascend and strikes ascend within them. Reads
    ///   return rows in exactly this order, so a record written unordered would
    ///   come back reordered and fail its own round-trip.
    /// * **Uniqueness.** `(expires_at, strike)` identifies a row inside a
    ///   snapshot, and it is the deduplication key. Two rows sharing it would
    ///   collapse into one on merge, silently losing a strike.
    ///
    /// # Errors
    ///
    /// Returns [`ChainError::Validation`] naming the offending field.
    pub fn validate(&self) -> Result<(), ChainError> {
        if self.symbol.trim().is_empty() {
            return Err(ChainError::Validation {
                field: "symbol".to_string(),
                reason: "must not be empty".to_string(),
            });
        }

        let mut previous_expiry: Option<DateTime<Utc>> = None;
        for expiration in &self.expirations {
            if let Some(previous) = previous_expiry
                && expiration.expires_at <= previous
            {
                return Err(ChainError::Validation {
                    field: "expirations".to_string(),
                    reason: format!(
                        "must be strictly ascending by expires_at, got {} after {}",
                        expiration.expires_at, previous
                    ),
                });
            }
            previous_expiry = Some(expiration.expires_at);

            let mut previous_strike: Option<Positive> = None;
            for quote in &expiration.quotes {
                if let Some(previous) = previous_strike
                    && quote.strike <= previous
                {
                    return Err(ChainError::Validation {
                        field: "quotes".to_string(),
                        reason: format!(
                            "strikes must be strictly ascending within {}, got {} after {}",
                            expiration.expires_at, quote.strike, previous
                        ),
                    });
                }
                previous_strike = Some(quote.strike);
            }
        }

        Ok(())
    }
}

/// One `(expiration, strike, side)` at one step: a row of a contract's history.
///
/// What `contract_series` returns, ordered by simulated time — the shape a
/// frontend chart or a backtest consumes.
#[derive(Debug, Clone, PartialEq)]
pub struct ContractQuote {
    /// The step this quote came from.
    pub step: usize,
    /// The simulated instant of that step.
    pub simulated_at: DateTime<Utc>,
    /// The contract's absolute expiration.
    pub expires_at: DateTime<Utc>,
    /// Fractional days remaining at this step.
    pub days_to_expiration: Positive,
    /// The strike.
    pub strike: Positive,
    /// Which side these quotes describe.
    pub side: ContractSide,
    /// The per-strike implied volatility, shared by both sides.
    pub implied_volatility: Positive,
    /// The bid on this side.
    pub bid: Option<Positive>,
    /// The ask on this side.
    pub ask: Option<Positive>,
    /// The mid on this side.
    pub mid: Option<Positive>,
    /// The delta on this side.
    pub delta: Option<Decimal>,
    /// Gamma, shared by both sides.
    pub gamma: Option<Decimal>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;
    use positive::pos_or_panic;
    use rust_decimal_macros::dec;

    fn instant(day: u32) -> DateTime<Utc> {
        match Utc.with_ymd_and_hms(2026, 1, day, 14, 30, 0).single() {
            Some(instant) => instant,
            None => panic!("the test instant must be valid"),
        }
    }

    fn quote(strike: f64) -> QuoteRow {
        QuoteRow::new(pos_or_panic!(strike), pos_or_panic!(0.18))
            .with_call(
                Some(pos_or_panic!(1.0)),
                Some(pos_or_panic!(1.2)),
                Some(pos_or_panic!(1.1)),
                Some(dec!(0.51)),
            )
            .with_put(
                Some(pos_or_panic!(0.9)),
                Some(pos_or_panic!(1.1)),
                Some(pos_or_panic!(1.0)),
                Some(dec!(-0.49)),
            )
            .with_gamma(Some(dec!(0.0031)))
    }

    fn expiration(day: u32, strikes: &[f64]) -> ExpirationRecord {
        ExpirationRecord::new(
            instant(day),
            pos_or_panic!(f64::from(day)),
            vec!["weeklies".to_string()],
            strikes.iter().copied().map(quote).collect(),
        )
    }

    fn record(simulation: Uuid, generation: u64, step: usize) -> SnapshotRecord {
        SnapshotRecord::new(
            simulation,
            generation,
            step,
            instant(5),
            "SPX".to_string(),
            pos_or_panic!(5000.0),
            pos_or_panic!(0.18),
            vec![
                expiration(6, &[4975.0, 5000.0, 5025.0]),
                expiration(9, &[4975.0, 5000.0]),
            ],
        )
    }

    /// The same coordinate always yields the same id — the property a retry
    /// depends on.
    #[test]
    fn test_the_snapshot_id_is_deterministic() {
        let simulation = Uuid::from_u128(7);

        assert_eq!(
            snapshot_id(simulation, 3, 11),
            snapshot_id(simulation, 3, 11)
        );
    }

    /// Every component of the coordinate changes the id, including the two that
    /// a naive concatenation would confuse.
    #[test]
    fn test_the_snapshot_id_separates_its_components() {
        let simulation = Uuid::from_u128(7);
        let other = Uuid::from_u128(8);

        assert_ne!(snapshot_id(simulation, 3, 11), snapshot_id(other, 3, 11));
        assert_ne!(
            snapshot_id(simulation, 3, 11),
            snapshot_id(simulation, 4, 11)
        );
        assert_ne!(
            snapshot_id(simulation, 3, 11),
            snapshot_id(simulation, 3, 12)
        );
        // Without the separators, (1, 23) and (12, 3) would collide.
        assert_ne!(
            snapshot_id(simulation, 1, 23),
            snapshot_id(simulation, 12, 3)
        );
    }

    /// A caller with no reason to think about generations has a name to pass,
    /// rather than a literal it would have to guess.
    #[test]
    fn test_the_current_generation_is_addressable() {
        let simulation = Uuid::from_u128(7);

        assert_eq!(CURRENT_SNAPSHOT_GENERATION, 2);
        assert_eq!(
            record(simulation, CURRENT_SNAPSHOT_GENERATION, 0).snapshot_id(),
            snapshot_id(simulation, CURRENT_SNAPSHOT_GENERATION, 0)
        );
    }

    /// One step of one simulation is a different row in each generation.
    ///
    /// This is what a generation is for. The tables collapse on
    /// `(simulation, generation, step)`, so if two builds that price a step
    /// differently shared a generation, the later one would not sit beside the
    /// earlier tape — it would replace it, and a client replaying against
    /// either would get whichever landed last.
    #[test]
    fn test_a_generation_bump_addresses_a_different_row() {
        let simulation = Uuid::from_u128(7);

        let previous = snapshot_id(simulation, CURRENT_SNAPSHOT_GENERATION - 1, 3);
        let current = snapshot_id(simulation, CURRENT_SNAPSHOT_GENERATION, 3);

        assert_ne!(
            previous, current,
            "the same step under two generations must be two rows"
        );
        assert_eq!(
            current,
            snapshot_id(simulation, CURRENT_SNAPSHOT_GENERATION, 3),
            "and the current one must still be reproducible"
        );
    }

    /// The id is a name-based v5 UUID, which is what makes it reproducible
    /// across processes and restarts.
    #[test]
    fn test_the_snapshot_id_is_a_v5_uuid() {
        assert_eq!(
            snapshot_id(Uuid::from_u128(7), 1, 0).get_version(),
            Some(uuid::Version::Sha1)
        );
    }

    /// A record's id agrees with the free function.
    #[test]
    fn test_a_record_derives_its_own_id() {
        let simulation = Uuid::from_u128(9);
        let record = record(simulation, 2, 4);

        assert_eq!(record.snapshot_id(), snapshot_id(simulation, 2, 4));
    }

    /// The quote count is what the completion marker will carry.
    #[test]
    fn test_the_quote_count_sums_every_expiration() {
        assert_eq!(record(Uuid::from_u128(9), 1, 0).quote_count(), 5);
    }

    /// A well-formed record validates.
    #[test]
    fn test_a_well_formed_record_validates() {
        match record(Uuid::from_u128(9), 1, 0).validate() {
            Ok(()) => {}
            Err(error) => panic!("the reference record must validate: {error}"),
        }
    }

    /// An empty snapshot is legal: a simulation whose schedule yields nothing
    /// live at a step still has a step.
    #[test]
    fn test_an_empty_snapshot_is_legal() {
        let mut empty = record(Uuid::from_u128(9), 1, 0);
        empty.expirations.clear();

        assert_eq!(empty.quote_count(), 0);
        assert!(empty.validate().is_ok());
    }

    /// Out-of-order expirations are rejected: reads return them ascending, so an
    /// unordered write could never round-trip.
    #[test]
    fn test_unordered_expirations_are_rejected() {
        let mut unordered = record(Uuid::from_u128(9), 1, 0);
        unordered.expirations.reverse();

        match unordered.validate() {
            Err(ChainError::Validation { field, reason }) => {
                assert_eq!(field, "expirations");
                assert!(reason.contains("ascending"), "{reason}");
            }
            other => panic!("expected a validation error, got {other:?}"),
        }
    }

    /// A repeated expiration is rejected: it is the deduplication key, so the
    /// second one would silently replace the first.
    #[test]
    fn test_a_repeated_expiration_is_rejected() {
        let mut repeated = record(Uuid::from_u128(9), 1, 0);
        repeated.expirations = vec![expiration(6, &[5000.0]), expiration(6, &[5000.0])];

        assert!(repeated.validate().is_err());
    }

    /// Out-of-order strikes are rejected, for the same reason as expirations.
    #[test]
    fn test_unordered_strikes_are_rejected() {
        let mut unordered = record(Uuid::from_u128(9), 1, 0);
        unordered.expirations = vec![expiration(6, &[5025.0, 5000.0])];

        match unordered.validate() {
            Err(ChainError::Validation { field, reason }) => {
                assert_eq!(field, "quotes");
                assert!(reason.contains("ascending"), "{reason}");
            }
            other => panic!("expected a validation error, got {other:?}"),
        }
    }

    /// A repeated strike inside one expiration is rejected: it collapses on
    /// merge, losing a contract.
    #[test]
    fn test_a_repeated_strike_is_rejected() {
        let mut repeated = record(Uuid::from_u128(9), 1, 0);
        repeated.expirations = vec![expiration(6, &[5000.0, 5000.0])];

        assert!(repeated.validate().is_err());
    }

    /// An empty symbol is rejected.
    #[test]
    fn test_an_empty_symbol_is_rejected() {
        let mut blank = record(Uuid::from_u128(9), 1, 0);
        blank.symbol = "   ".to_string();

        match blank.validate() {
            Err(ChainError::Validation { field, .. }) => assert_eq!(field, "symbol"),
            other => panic!("expected a validation error, got {other:?}"),
        }
    }

    /// The side renders as the name the query builder and the logs use.
    #[test]
    fn test_the_contract_side_renders_its_name() {
        assert_eq!(ContractSide::Call.to_string(), "call");
        assert_eq!(ContractSide::Put.as_str(), "put");
    }
}