hopper-schema 0.1.0

Schema export and ABI fingerprinting for Hopper zero-copy state framework. Layout manifests, schema diff, migration checks.
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
//! # Python client emitter
//!
//! Produces a standalone Python module from a `ProgramManifest` that mirrors
//! the TypeScript emitter in `clientgen.rs`. The generated Python has no
//! runtime dependency outside of the standard library. everything decodes
//! through `struct` (the stdlib module).
//!
//! ## What gets emitted
//!
//! - One dataclass per account layout (`Vault`, `Config`, …) with a
//!   `decode(bytes) -> Self` classmethod that verifies the layout_id and
//!   reads field offsets directly from the raw bytes.
//! - One dataclass per event with a `decode(bytes) -> Self` classmethod
//!   keyed off the 1-byte event tag.
//! - `build_<instruction>` helper functions that return the raw `bytes`
//!   instruction payload. The caller wires the returned bytes into their
//!   preferred Solana client (solders, solana-py, …).
//! - A `DISCRIMINATORS` dict mapping layout name to `(disc, layout_id)`.
//!
//! ## Design notes
//!
//! Hopper emits Python that:
//!   1. Verifies the `layout_id` fingerprint before decoding (impossible in
//!      Anchor because Anchor has no layout fingerprint).
//!   2. Honors `FieldIntent` by emitting typed `int` / `bytes` / `bool`
//!      field types that match the field's semantic role, not just the
//!      underlying u8/u64.
//!   3. Ships segment-aware partial readers (`Vault.read_balance(buf)`)
//!      parallel to the zero-copy on-chain side.

use core::fmt;

extern crate alloc;
use alloc::string::{String, ToString};

use crate::{EventDescriptor, InstructionDescriptor, LayoutManifest, ProgramManifest};

fn py_type(canonical: &str) -> &'static str {
    match canonical {
        "u8" | "u16" | "u32" | "i8" | "i16" | "i32" => "int",
        "u64" | "u128" | "i64" | "i128" => "int",
        "bool" => "bool",
        "Pubkey" => "bytes",
        _ => "bytes",
    }
}

fn struct_format(canonical: &str, size: u16) -> String {
    match canonical {
        "u8" => "<B".to_string(),
        "u16" => "<H".to_string(),
        "u32" => "<I".to_string(),
        "u64" => "<Q".to_string(),
        "i8" => "<b".to_string(),
        "i16" => "<h".to_string(),
        "i32" => "<i".to_string(),
        "i64" => "<q".to_string(),
        "bool" => "<?".to_string(),
        _ => {
            let mut s = String::from("<");
            let n = size.to_string();
            s.push_str(&n);
            s.push('s');
            s
        }
    }
}

fn write_snake(f: &mut fmt::Formatter<'_>, name: &str) -> fmt::Result {
    for c in name.chars() {
        if c == '-' {
            f.write_str("_")?;
        } else {
            for lc in c.to_lowercase() {
                write!(f, "{}", lc)?;
            }
        }
    }
    Ok(())
}

fn write_pascal(f: &mut fmt::Formatter<'_>, name: &str) -> fmt::Result {
    let mut cap = true;
    for c in name.chars() {
        if c == '_' || c == '-' {
            cap = true;
        } else if cap {
            for uc in c.to_uppercase() {
                write!(f, "{}", uc)?;
            }
            cap = false;
        } else {
            write!(f, "{}", c)?;
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Accounts emitter (`accounts.py`)
// ---------------------------------------------------------------------------

/// Generates `accounts.py` content from a `ProgramManifest`.
pub struct PyAccounts<'a>(pub &'a ProgramManifest);

impl<'a> fmt::Display for PyAccounts<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "\"\"\"Hopper account decoders for program `{}`.",
            self.0.name
        )?;
        writeln!(f)?;
        writeln!(f, "Auto-generated. Do not edit.")?;
        writeln!(f, "\"\"\"")?;
        writeln!(f, "from __future__ import annotations")?;
        writeln!(f, "from dataclasses import dataclass")?;
        writeln!(f, "import struct")?;
        writeln!(f)?;
        writeln!(
            f,
            "LAYOUT_ID_OFFSET = 4  # bytes [4..12] of the Hopper header"
        )?;
        writeln!(f)?;

        for layout in self.0.layouts {
            fmt_layout(f, layout)?;
            writeln!(f)?;
        }

        // DISCRIMINATORS map (one-per-layout)
        writeln!(f, "DISCRIMINATORS: dict[str, tuple[int, bytes]] = {{")?;
        for layout in self.0.layouts {
            write!(f, "    \"")?;
            write_pascal(f, layout.name)?;
            write!(f, "\": ({}, bytes([", layout.disc)?;
            for (i, b) in layout.layout_id.iter().enumerate() {
                if i > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "0x{:02x}", b)?;
            }
            writeln!(f, "])),")?;
        }
        writeln!(f, "}}")?;
        Ok(())
    }
}

fn fmt_layout(f: &mut fmt::Formatter<'_>, layout: &LayoutManifest) -> fmt::Result {
    writeln!(f, "@dataclass(frozen=True, slots=True)")?;
    write!(f, "class ")?;
    write_pascal(f, layout.name)?;
    writeln!(f, ":")?;
    writeln!(
        f,
        "    \"\"\"Decoder for the `{}` account. total_size={}\"\"\"",
        layout.name, layout.total_size
    )?;

    // Layout-id constant
    write!(f, "    LAYOUT_ID: bytes = bytes([")?;
    for (i, b) in layout.layout_id.iter().enumerate() {
        if i > 0 {
            write!(f, ", ")?;
        }
        write!(f, "0x{:02x}", b)?;
    }
    writeln!(f, "])")?;
    writeln!(f, "    DISC: int = {}", layout.disc)?;
    writeln!(f, "    VERSION: int = {}", layout.version)?;
    writeln!(f, "    TOTAL_SIZE: int = {}", layout.total_size)?;
    writeln!(f)?;

    // Typed fields (dataclass attributes)
    for fd in layout.fields {
        write!(f, "    ")?;
        write_snake(f, fd.name)?;
        writeln!(f, ": {}", py_type(fd.canonical_type))?;
    }

    writeln!(f)?;
    writeln!(f, "    @classmethod")?;
    write!(f, "    def decode(cls, buf: bytes) -> \"")?;
    write_pascal(f, layout.name)?;
    writeln!(f, "\":")?;
    writeln!(f, "        if len(buf) < cls.TOTAL_SIZE:")?;
    writeln!(f, "            raise ValueError(f\"buffer too short: need {{cls.TOTAL_SIZE}}, got {{len(buf)}}\")")?;
    writeln!(
        f,
        "        actual_id = bytes(buf[LAYOUT_ID_OFFSET:LAYOUT_ID_OFFSET + 8])"
    )?;
    writeln!(f, "        if actual_id != cls.LAYOUT_ID:")?;
    writeln!(f, "            raise ValueError(f\"layout_id mismatch: expected {{cls.LAYOUT_ID.hex()}}, got {{actual_id.hex()}}\")")?;

    for fd in layout.fields {
        let fmt = struct_format(fd.canonical_type, fd.size);
        write!(f, "        ")?;
        write_snake(f, fd.name)?;
        writeln!(
            f,
            " = struct.unpack_from(\"{}\", buf, {})[0]",
            fmt, fd.offset
        )?;
    }

    write!(f, "        return cls(")?;
    for (i, fd) in layout.fields.iter().enumerate() {
        if i > 0 {
            write!(f, ", ")?;
        }
        write_snake(f, fd.name)?;
        write!(f, "=")?;
        write_snake(f, fd.name)?;
    }
    writeln!(f, ")")?;

    // Partial reader helpers: `Vault.read_balance(buf) -> int`. these are
    // the segment-aware equivalent of hopper-sdk's `SegmentReader::read_u64`.
    writeln!(f)?;
    for fd in layout.fields {
        let fmt = struct_format(fd.canonical_type, fd.size);
        writeln!(f, "    @classmethod")?;
        write!(f, "    def read_")?;
        write_snake(f, fd.name)?;
        writeln!(f, "(cls, buf: bytes) -> {}:", py_type(fd.canonical_type))?;
        writeln!(f, "        \"\"\"Partial read of `{}` (size={}, offset={}). Does NOT verify layout_id; call decode() for full verification.\"\"\"", fd.name, fd.size, fd.offset)?;
        writeln!(
            f,
            "        return struct.unpack_from(\"{}\", buf, {})[0]",
            fmt, fd.offset
        )?;
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Instructions emitter (`instructions.py`)
// ---------------------------------------------------------------------------

/// Generates `instructions.py` content from a `ProgramManifest`.
pub struct PyInstructions<'a>(pub &'a ProgramManifest);

impl<'a> fmt::Display for PyInstructions<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "\"\"\"Instruction builders for program `{}`.\"\"\"",
            self.0.name
        )?;
        writeln!(f, "from __future__ import annotations")?;
        writeln!(f, "import struct")?;
        writeln!(f)?;
        for ix in self.0.instructions {
            fmt_instruction(f, ix)?;
            writeln!(f)?;
        }
        Ok(())
    }
}

fn fmt_instruction(f: &mut fmt::Formatter<'_>, ix: &InstructionDescriptor) -> fmt::Result {
    write!(f, "def build_")?;
    write_snake(f, ix.name)?;
    write!(f, "(")?;
    for (i, a) in ix.args.iter().enumerate() {
        if i > 0 {
            write!(f, ", ")?;
        }
        write_snake(f, a.name)?;
        write!(f, ": {}", py_type(a.canonical_type))?;
    }
    writeln!(f, ") -> bytes:")?;
    writeln!(
        f,
        "    \"\"\"Assemble the raw instruction data for `{}`. tag={}\"\"\"",
        ix.name, ix.tag
    )?;
    writeln!(f, "    parts: list[bytes] = [bytes([{}])]", ix.tag)?;
    for a in ix.args {
        let fmt = struct_format(a.canonical_type, a.size);
        write!(f, "    parts.append(struct.pack(\"{}\", ", fmt)?;
        write_snake(f, a.name)?;
        writeln!(f, "))")?;
    }
    writeln!(f, "    return b\"\".join(parts)")?;

    // Account ordering doc. helpful for consumers since Python has no
    // statically typed AccountMeta.
    if !ix.accounts.is_empty() {
        writeln!(f, "\nbuild_")?;
        write_snake(f, ix.name)?;
        writeln!(f, ".ACCOUNT_ORDER = (")?;
        for ae in ix.accounts {
            writeln!(
                f,
                "    (\"{}\", {{\"writable\": {}, \"signer\": {}, \"layout\": \"{}\"}}),",
                ae.name,
                if ae.writable { "True" } else { "False" },
                if ae.signer { "True" } else { "False" },
                ae.layout_ref,
            )?;
        }
        writeln!(f, ")")?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Events emitter (`events.py`)
// ---------------------------------------------------------------------------

/// Generates `events.py` content from a `ProgramManifest`.
pub struct PyEvents<'a>(pub &'a ProgramManifest);

impl<'a> fmt::Display for PyEvents<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "\"\"\"Event decoders for program `{}`.\"\"\"",
            self.0.name
        )?;
        writeln!(f, "from __future__ import annotations")?;
        writeln!(f, "from dataclasses import dataclass")?;
        writeln!(f, "import struct")?;
        writeln!(f)?;
        for e in self.0.events {
            fmt_event(f, e)?;
            writeln!(f)?;
        }

        // Event tag → decoder table.
        writeln!(f, "EVENT_DECODERS: dict[int, type] = {{")?;
        for e in self.0.events {
            write!(f, "    {}: ", e.tag)?;
            write_pascal(f, e.name)?;
            writeln!(f, ",")?;
        }
        writeln!(f, "}}")?;
        Ok(())
    }
}

fn fmt_event(f: &mut fmt::Formatter<'_>, e: &EventDescriptor) -> fmt::Result {
    writeln!(f, "@dataclass(frozen=True, slots=True)")?;
    write!(f, "class ")?;
    write_pascal(f, e.name)?;
    writeln!(f, ":")?;
    writeln!(f, "    \"\"\"Event {} (tag={})\"\"\"", e.name, e.tag)?;
    writeln!(f, "    TAG: int = {}", e.tag)?;
    for fd in e.fields {
        write!(f, "    ")?;
        write_snake(f, fd.name)?;
        writeln!(f, ": {}", py_type(fd.canonical_type))?;
    }

    writeln!(f)?;
    writeln!(f, "    @classmethod")?;
    write!(f, "    def decode(cls, buf: bytes) -> \"")?;
    write_pascal(f, e.name)?;
    writeln!(f, "\":")?;
    writeln!(f, "        if not buf or buf[0] != cls.TAG:")?;
    writeln!(f, "            raise ValueError(\"event tag mismatch\")")?;
    writeln!(f, "        p = 1")?;
    for fd in e.fields {
        let fmt = struct_format(fd.canonical_type, fd.size);
        write!(f, "        ")?;
        write_snake(f, fd.name)?;
        writeln!(
            f,
            " = struct.unpack_from(\"{}\", buf, p)[0]; p += {}",
            fmt, fd.size
        )?;
    }
    write!(f, "        return cls(")?;
    for (i, fd) in e.fields.iter().enumerate() {
        if i > 0 {
            write!(f, ", ")?;
        }
        write_snake(f, fd.name)?;
        write!(f, "=")?;
        write_snake(f, fd.name)?;
    }
    writeln!(f, ")")?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Types emitter (`types.py`. shared scaffolding)
// ---------------------------------------------------------------------------

/// Generates the shared `types.py` content: header parser, fingerprint
/// assertion helper, and a single source-of-truth `DECODERS` union table.
pub struct PyTypes<'a>(pub &'a ProgramManifest);

impl<'a> fmt::Display for PyTypes<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "\"\"\"Shared Hopper client primitives for program `{}`.\"\"\"",
            self.0.name
        )?;
        writeln!(f, "from __future__ import annotations")?;
        writeln!(f, "from dataclasses import dataclass")?;
        writeln!(f)?;
        writeln!(
            f,
            "HEADER_LEN = 16  # disc(1) + version(1) + flags(2) + layout_id(8) + reserved(4)"
        )?;
        writeln!(f, "LAYOUT_ID_OFFSET = 4")?;
        writeln!(f, "LAYOUT_ID_LENGTH = 8")?;
        writeln!(f)?;
        writeln!(f, "@dataclass(frozen=True, slots=True)")?;
        writeln!(f, "class HopperHeader:")?;
        writeln!(f, "    disc: int")?;
        writeln!(f, "    version: int")?;
        writeln!(f, "    flags: int")?;
        writeln!(f, "    layout_id: bytes")?;
        writeln!(f, "    reserved: bytes")?;
        writeln!(f)?;
        writeln!(f, "    @classmethod")?;
        writeln!(f, "    def decode(cls, buf: bytes) -> \"HopperHeader\":")?;
        writeln!(f, "        if len(buf) < HEADER_LEN:")?;
        writeln!(
            f,
            "            raise ValueError(\"account too short for Hopper header\")"
        )?;
        writeln!(f, "        return cls(")?;
        writeln!(f, "            disc=buf[0],")?;
        writeln!(f, "            version=buf[1],")?;
        writeln!(f, "            flags=int.from_bytes(buf[2:4], \"little\"),")?;
        writeln!(f, "            layout_id=bytes(buf[LAYOUT_ID_OFFSET:LAYOUT_ID_OFFSET + LAYOUT_ID_LENGTH]),")?;
        writeln!(f, "            reserved=bytes(buf[12:16]),")?;
        writeln!(f, "        )")?;
        writeln!(f)?;
        writeln!(
            f,
            "def assert_layout_id(buf: bytes, expected: bytes) -> None:"
        )?;
        writeln!(
            f,
            "    \"\"\"Raise if the account header's layout_id doesn't match `expected`.\"\"\""
        )?;
        writeln!(f, "    header = HopperHeader.decode(buf)")?;
        writeln!(f, "    if header.layout_id != expected:")?;
        writeln!(f, "        raise ValueError(f\"layout_id mismatch: expected {{expected.hex()}}, got {{header.layout_id.hex()}}\")")?;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Package-level bundle (`__init__.py`)
// ---------------------------------------------------------------------------

/// Generates an `__init__.py` that re-exports the public surface of the
/// generated package.
pub struct PyIndex<'a>(pub &'a ProgramManifest);

impl<'a> fmt::Display for PyIndex<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "\"\"\"Auto-generated Python client for `{}`.\"\"\"",
            self.0.name
        )?;
        writeln!(f, "from .accounts import *  # noqa: F401,F403")?;
        writeln!(f, "from .instructions import *  # noqa: F401,F403")?;
        writeln!(f, "from .events import *  # noqa: F401,F403")?;
        writeln!(
            f,
            "from .types import HopperHeader, assert_layout_id  # noqa: F401"
        )?;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Grand bundle: all-in-one emitter
// ---------------------------------------------------------------------------

/// Convenience emitter that produces a single concatenated Python file
/// combining every section above. Useful for CLI users who want one flat
/// file they can `cp` into their project.
pub struct PyClientGen<'a>(pub &'a ProgramManifest);

impl<'a> fmt::Display for PyClientGen<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", PyTypes(self.0))?;
        writeln!(f)?;
        write!(f, "{}", PyAccounts(self.0))?;
        writeln!(f)?;
        write!(f, "{}", PyInstructions(self.0))?;
        writeln!(f)?;
        write!(f, "{}", PyEvents(self.0))?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        AccountEntry, ArgDescriptor, EventDescriptor, FieldDescriptor, FieldIntent,
        InstructionDescriptor, LayoutManifest,
    };

    fn sample_manifest() -> ProgramManifest {
        static LAYOUTS: [LayoutManifest; 1] = [sample_layout_static()];
        static ACCTS: [AccountEntry; 1] = [AccountEntry {
            name: "vault",
            writable: true,
            signer: false,
            layout_ref: "vault",
        }];
        static ARGS: [ArgDescriptor; 1] = [ArgDescriptor {
            name: "amount",
            canonical_type: "u64",
            size: 8,
        }];
        static IX: [InstructionDescriptor; 1] = [InstructionDescriptor {
            name: "deposit",
            tag: 3,
            args: &ARGS,
            accounts: &ACCTS,
            capabilities: &[],
            policy_pack: "",
            receipt_expected: true,
        }];
        static EV_F: [FieldDescriptor; 1] = [FieldDescriptor {
            name: "amount",
            canonical_type: "u64",
            size: 8,
            offset: 1,
            intent: FieldIntent::Balance,
        }];
        static EVENTS: [EventDescriptor; 1] = [EventDescriptor {
            name: "deposited",
            tag: 1,
            fields: &EV_F,
        }];

        ProgramManifest {
            name: "vault_program",
            version: "0.1.0",
            description: "",
            layouts: &LAYOUTS,
            layout_metadata: &[],
            instructions: &IX,
            events: &EVENTS,
            policies: &[],
            compatibility_pairs: &[],
            tooling_hints: &[],
            contexts: &[],
        }
    }

    const fn sample_layout_static() -> LayoutManifest {
        const F: [FieldDescriptor; 2] = [
            FieldDescriptor {
                name: "authority",
                canonical_type: "Pubkey",
                size: 32,
                offset: 16,
                intent: FieldIntent::Authority,
            },
            FieldDescriptor {
                name: "balance",
                canonical_type: "u64",
                size: 8,
                offset: 48,
                intent: FieldIntent::Balance,
            },
        ];
        LayoutManifest {
            name: "vault",
            disc: 5,
            version: 1,
            layout_id: [1, 2, 3, 4, 5, 6, 7, 8],
            total_size: 64,
            field_count: 2,
            fields: &F,
        }
    }

    #[test]
    fn accounts_mentions_layout_id_and_fields() {
        let m = sample_manifest();
        let out = alloc::format!("{}", PyAccounts(&m));
        assert!(out.contains("class Vault"));
        assert!(out.contains("LAYOUT_ID"));
        assert!(out.contains("authority"));
        assert!(out.contains("balance"));
        assert!(out.contains("read_balance"));
    }

    #[test]
    fn instructions_pack_tag_byte() {
        let m = sample_manifest();
        let out = alloc::format!("{}", PyInstructions(&m));
        assert!(out.contains("def build_deposit"));
        assert!(out.contains("bytes([3])"));
        assert!(out.contains("amount"));
    }

    #[test]
    fn events_decoder_table_present() {
        let m = sample_manifest();
        let out = alloc::format!("{}", PyEvents(&m));
        assert!(out.contains("class Deposited"));
        assert!(out.contains("EVENT_DECODERS"));
        assert!(out.contains("1: Deposited"));
    }

    #[test]
    fn types_header_matches_runtime_offsets() {
        let m = sample_manifest();
        let out = alloc::format!("{}", PyTypes(&m));
        assert!(out.contains("HEADER_LEN = 16"));
        assert!(out.contains("LAYOUT_ID_OFFSET = 4"));
        assert!(out.contains("LAYOUT_ID_LENGTH = 8"));
        assert!(out.contains("flags=int.from_bytes(buf[2:4], \"little\")"));
        assert!(out.contains(
            "layout_id=bytes(buf[LAYOUT_ID_OFFSET:LAYOUT_ID_OFFSET + LAYOUT_ID_LENGTH])"
        ));
        assert!(out.contains("reserved=bytes(buf[12:16])"));
        assert!(!out.contains("HEADER_LEN = 12"));
        assert!(!out.contains("layout_id=bytes(buf[4:12])"));
    }
}