libgrite-git 0.5.3

Git WAL, sync, and snapshot operations for grite
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
//! CBOR chunk encoding/decoding for portable event storage
//!
//! Chunk format:
//! - Magic: `GRITECNK` (8 bytes)
//! - Version: u16 (little-endian)
//! - Codec length: u8
//! - Codec: "cbor-v1"
//! - Payload: CBOR array of events

use blake2::digest::consts::U32;
use blake2::{Blake2b, Digest};
use ciborium::Value;
use libgrite_core::types::event::{DependencyType, Event, EventKind, IssueState, SymbolInfo};
use libgrite_core::types::ids::{ActorId, EventId, IssueId};

use crate::GitError;

/// Magic bytes at start of chunk
pub const CHUNK_MAGIC: &[u8; 8] = b"GRITECNK";

/// Current chunk format version
pub const CHUNK_VERSION: u16 = 1;

/// Codec identifier
pub const CHUNK_CODEC: &str = "cbor-v1";

/// Encode a list of events into a chunk
pub fn encode_chunk(events: &[Event]) -> Result<Vec<u8>, GitError> {
    let mut buf = Vec::new();

    // Magic
    buf.extend_from_slice(CHUNK_MAGIC);

    // Version (little-endian u16)
    buf.extend_from_slice(&CHUNK_VERSION.to_le_bytes());

    // Codec length and codec string
    let codec_bytes = CHUNK_CODEC.as_bytes();
    buf.push(codec_bytes.len() as u8);
    buf.extend_from_slice(codec_bytes);

    // Encode events as CBOR array
    let events_value = events_to_cbor(events);
    ciborium::into_writer(&events_value, &mut buf)
        .map_err(|e| GitError::CborDecode(format!("Failed to encode events: {}", e)))?;

    Ok(buf)
}

/// Decode a chunk into a list of events
pub fn decode_chunk(data: &[u8]) -> Result<Vec<Event>, GitError> {
    // Check minimum size
    if data.len() < 8 + 2 + 1 {
        return Err(GitError::InvalidChunk("Chunk too small".to_string()));
    }

    // Verify magic
    if &data[0..8] != CHUNK_MAGIC {
        return Err(GitError::InvalidChunk("Invalid magic bytes".to_string()));
    }

    // Read version
    let version = u16::from_le_bytes([data[8], data[9]]);
    if version != CHUNK_VERSION {
        return Err(GitError::InvalidChunk(format!(
            "Unsupported chunk version: {}",
            version
        )));
    }

    // Read codec
    let codec_len = data[10] as usize;
    if data.len() < 11 + codec_len {
        return Err(GitError::InvalidChunk(
            "Chunk truncated at codec".to_string(),
        ));
    }
    let codec = std::str::from_utf8(&data[11..11 + codec_len])
        .map_err(|_| GitError::InvalidChunk("Invalid codec string".to_string()))?;
    if codec != CHUNK_CODEC {
        return Err(GitError::InvalidChunk(format!(
            "Unsupported codec: {}",
            codec
        )));
    }

    // Parse CBOR payload
    let payload_start = 11 + codec_len;
    let value: Value = ciborium::from_reader(&data[payload_start..])
        .map_err(|e| GitError::CborDecode(format!("Failed to decode CBOR: {}", e)))?;

    cbor_to_events(value)
}

/// Compute BLAKE2b-256 hash of chunk data
pub fn chunk_hash(data: &[u8]) -> [u8; 32] {
    let mut hasher = Blake2b::<U32>::new();
    hasher.update(data);
    hasher.finalize().into()
}

/// Convert events to CBOR value
fn events_to_cbor(events: &[Event]) -> Value {
    let events_array: Vec<Value> = events.iter().map(event_to_cbor).collect();
    Value::Array(events_array)
}

/// Convert a single event to CBOR
/// Format: [event_id, issue_id, actor, ts, parent, kind_tag, kind_payload, sig]
fn event_to_cbor(event: &Event) -> Value {
    let (kind_tag, kind_payload) = libgrite_core::hash::kind_to_tag_and_payload(&event.kind);

    let parent_value = match &event.parent {
        Some(p) => Value::Bytes(p.to_vec()),
        None => Value::Null,
    };

    let sig_value = match &event.sig {
        Some(s) => Value::Bytes(s.clone()),
        None => Value::Null,
    };

    Value::Array(vec![
        Value::Bytes(event.event_id.to_vec()),
        Value::Bytes(event.issue_id.to_vec()),
        Value::Bytes(event.actor.to_vec()),
        Value::Integer(event.ts_unix_ms.into()),
        parent_value,
        Value::Integer(kind_tag.into()),
        kind_payload,
        sig_value,
    ])
}

/// Convert CBOR value to events
fn cbor_to_events(value: Value) -> Result<Vec<Event>, GitError> {
    let array = match value {
        Value::Array(arr) => arr,
        _ => {
            return Err(GitError::InvalidChunk(
                "Expected array of events".to_string(),
            ))
        }
    };

    array.into_iter().map(cbor_to_event).collect()
}

/// Convert a single CBOR value to an Event
fn cbor_to_event(value: Value) -> Result<Event, GitError> {
    let array = match value {
        Value::Array(arr) => arr,
        _ => return Err(GitError::InvalidEvent("Expected event array".to_string())),
    };

    if array.len() != 8 {
        return Err(GitError::InvalidEvent(format!(
            "Expected 8 elements, got {}",
            array.len()
        )));
    }

    let mut iter = array.into_iter();

    // event_id
    let event_id: EventId = extract_bytes(&next_item(&mut iter, "event_id")?, "event_id", 32)?
        .try_into()
        .map_err(|_| GitError::InvalidEvent("Invalid event_id length".to_string()))?;

    // issue_id
    let issue_id: IssueId = extract_bytes(&next_item(&mut iter, "issue_id")?, "issue_id", 16)?
        .try_into()
        .map_err(|_| GitError::InvalidEvent("Invalid issue_id length".to_string()))?;

    // actor
    let actor: ActorId = extract_bytes(&next_item(&mut iter, "actor")?, "actor", 16)?
        .try_into()
        .map_err(|_| GitError::InvalidEvent("Invalid actor length".to_string()))?;

    // ts_unix_ms
    let ts_unix_ms = extract_u64(&next_item(&mut iter, "ts_unix_ms")?, "ts_unix_ms")?;

    // parent
    let parent_value = next_item(&mut iter, "parent")?;
    let parent: Option<EventId> = match parent_value {
        Value::Null => None,
        Value::Bytes(b) => {
            let arr: EventId = b
                .try_into()
                .map_err(|_| GitError::InvalidEvent("Invalid parent length".to_string()))?;
            Some(arr)
        }
        _ => return Err(GitError::InvalidEvent("Invalid parent type".to_string())),
    };

    // kind_tag
    let kind_tag = extract_u32(&next_item(&mut iter, "kind_tag")?, "kind_tag")?;

    // kind_payload
    let kind_payload = next_item(&mut iter, "kind_payload")?;

    // sig
    let sig_value = next_item(&mut iter, "sig")?;
    let sig: Option<Vec<u8>> = match sig_value {
        Value::Null => None,
        Value::Bytes(b) => Some(b),
        _ => return Err(GitError::InvalidEvent("Invalid sig type".to_string())),
    };

    // Parse kind from tag and payload
    let kind = parse_event_kind(kind_tag, kind_payload)?;

    Ok(Event {
        event_id,
        issue_id,
        actor,
        ts_unix_ms,
        parent,
        kind,
        sig,
    })
}

/// Parse EventKind from tag and payload
fn parse_event_kind(tag: u32, payload: Value) -> Result<EventKind, GitError> {
    let array = match payload {
        Value::Array(arr) => arr,
        _ => {
            return Err(GitError::InvalidEvent(
                "Expected kind payload array".to_string(),
            ))
        }
    };

    match tag {
        1 => {
            // IssueCreated { title, body, labels }
            if array.len() != 3 {
                return Err(GitError::InvalidEvent(
                    "IssueCreated expects 3 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let title = extract_string(&next_item(&mut iter, "title")?, "title")?;
            let body = extract_string(&next_item(&mut iter, "body")?, "body")?;
            let labels = extract_string_array(&next_item(&mut iter, "labels")?, "labels")?;
            Ok(EventKind::IssueCreated {
                title,
                body,
                labels,
            })
        }
        2 => {
            // IssueUpdated { title, body }
            if array.len() != 2 {
                return Err(GitError::InvalidEvent(
                    "IssueUpdated expects 2 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let title = extract_optional_string(&next_item(&mut iter, "title")?, "title")?;
            let body = extract_optional_string(&next_item(&mut iter, "body")?, "body")?;
            Ok(EventKind::IssueUpdated { title, body })
        }
        3 => {
            // CommentAdded { body }
            if array.len() != 1 {
                return Err(GitError::InvalidEvent(
                    "CommentAdded expects 1 field".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let body = extract_string(&next_item(&mut iter, "body")?, "body")?;
            Ok(EventKind::CommentAdded { body })
        }
        4 => {
            // LabelAdded { label }
            if array.len() != 1 {
                return Err(GitError::InvalidEvent(
                    "LabelAdded expects 1 field".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let label = extract_string(&next_item(&mut iter, "label")?, "label")?;
            Ok(EventKind::LabelAdded { label })
        }
        5 => {
            // LabelRemoved { label }
            if array.len() != 1 {
                return Err(GitError::InvalidEvent(
                    "LabelRemoved expects 1 field".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let label = extract_string(&next_item(&mut iter, "label")?, "label")?;
            Ok(EventKind::LabelRemoved { label })
        }
        6 => {
            // StateChanged { state }
            if array.len() != 1 {
                return Err(GitError::InvalidEvent(
                    "StateChanged expects 1 field".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let state_str = extract_string(&next_item(&mut iter, "state")?, "state")?;
            let state = match state_str.as_str() {
                "open" => IssueState::Open,
                "closed" => IssueState::Closed,
                _ => {
                    return Err(GitError::InvalidEvent(format!(
                        "Invalid state: {}",
                        state_str
                    )))
                }
            };
            Ok(EventKind::StateChanged { state })
        }
        7 => {
            // LinkAdded { url, note }
            if array.len() != 2 {
                return Err(GitError::InvalidEvent(
                    "LinkAdded expects 2 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let url = extract_string(&next_item(&mut iter, "url")?, "url")?;
            let note = extract_optional_string(&next_item(&mut iter, "note")?, "note")?;
            Ok(EventKind::LinkAdded { url, note })
        }
        8 => {
            // AssigneeAdded { user }
            if array.len() != 1 {
                return Err(GitError::InvalidEvent(
                    "AssigneeAdded expects 1 field".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let user = extract_string(&next_item(&mut iter, "user")?, "user")?;
            Ok(EventKind::AssigneeAdded { user })
        }
        9 => {
            // AssigneeRemoved { user }
            if array.len() != 1 {
                return Err(GitError::InvalidEvent(
                    "AssigneeRemoved expects 1 field".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let user = extract_string(&next_item(&mut iter, "user")?, "user")?;
            Ok(EventKind::AssigneeRemoved { user })
        }
        10 => {
            // AttachmentAdded { name, sha256, mime }
            if array.len() != 3 {
                return Err(GitError::InvalidEvent(
                    "AttachmentAdded expects 3 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let name = extract_string(&next_item(&mut iter, "name")?, "name")?;
            let sha256: [u8; 32] = extract_bytes(&next_item(&mut iter, "sha256")?, "sha256", 32)?
                .try_into()
                .map_err(|_| GitError::InvalidEvent("Invalid sha256 length".to_string()))?;
            let mime = extract_string(&next_item(&mut iter, "mime")?, "mime")?;
            Ok(EventKind::AttachmentAdded { name, sha256, mime })
        }
        11 => {
            // DependencyAdded { target, dep_type }
            if array.len() != 2 {
                return Err(GitError::InvalidEvent(
                    "DependencyAdded expects 2 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let target: IssueId = extract_bytes(&next_item(&mut iter, "target")?, "target", 16)?
                .try_into()
                .map_err(|_| GitError::InvalidEvent("Invalid target length".to_string()))?;
            let dep_type_str = extract_string(&next_item(&mut iter, "dep_type")?, "dep_type")?;
            let dep_type = DependencyType::from_str(&dep_type_str).ok_or_else(|| {
                GitError::InvalidEvent(format!("Invalid dep_type: {}", dep_type_str))
            })?;
            Ok(EventKind::DependencyAdded { target, dep_type })
        }
        12 => {
            // DependencyRemoved { target, dep_type }
            if array.len() != 2 {
                return Err(GitError::InvalidEvent(
                    "DependencyRemoved expects 2 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let target: IssueId = extract_bytes(&next_item(&mut iter, "target")?, "target", 16)?
                .try_into()
                .map_err(|_| GitError::InvalidEvent("Invalid target length".to_string()))?;
            let dep_type_str = extract_string(&next_item(&mut iter, "dep_type")?, "dep_type")?;
            let dep_type = DependencyType::from_str(&dep_type_str).ok_or_else(|| {
                GitError::InvalidEvent(format!("Invalid dep_type: {}", dep_type_str))
            })?;
            Ok(EventKind::DependencyRemoved { target, dep_type })
        }
        13 => {
            // ContextUpdated { path, language, symbols, summary, content_hash }
            if array.len() != 5 {
                return Err(GitError::InvalidEvent(
                    "ContextUpdated expects 5 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let path = extract_string(&next_item(&mut iter, "path")?, "path")?;
            let language = extract_string(&next_item(&mut iter, "language")?, "language")?;
            let symbols_value = next_item(&mut iter, "symbols")?;
            let symbols = parse_symbols(symbols_value)?;
            let summary = extract_string(&next_item(&mut iter, "summary")?, "summary")?;
            let content_hash: [u8; 32] =
                extract_bytes(&next_item(&mut iter, "content_hash")?, "content_hash", 32)?
                    .try_into()
                    .map_err(|_| {
                        GitError::InvalidEvent("Invalid content_hash length".to_string())
                    })?;
            Ok(EventKind::ContextUpdated {
                path,
                language,
                symbols,
                summary,
                content_hash,
            })
        }
        14 => {
            // ProjectContextUpdated { key, value }
            if array.len() != 2 {
                return Err(GitError::InvalidEvent(
                    "ProjectContextUpdated expects 2 fields".to_string(),
                ));
            }
            let mut iter = array.into_iter();
            let key = extract_string(&next_item(&mut iter, "key")?, "key")?;
            let value = extract_string(&next_item(&mut iter, "value")?, "value")?;
            Ok(EventKind::ProjectContextUpdated { key, value })
        }
        _ => Err(GitError::InvalidEvent(format!("Unknown kind tag: {}", tag))),
    }
}

/// Parse a CBOR array of symbols into Vec<SymbolInfo>
fn parse_symbols(value: Value) -> Result<Vec<SymbolInfo>, GitError> {
    let array = match value {
        Value::Array(arr) => arr,
        _ => return Err(GitError::InvalidEvent("symbols must be array".to_string())),
    };
    array
        .into_iter()
        .map(|sym_value| {
            let sym_arr = match sym_value {
                Value::Array(arr) => arr,
                _ => return Err(GitError::InvalidEvent("symbol must be array".to_string())),
            };
            if sym_arr.len() != 4 {
                return Err(GitError::InvalidEvent(
                    "symbol expects 4 fields".to_string(),
                ));
            }
            let mut iter = sym_arr.into_iter();
            let name = extract_string(&next_item(&mut iter, "symbol.name")?, "symbol.name")?;
            let kind = extract_string(&next_item(&mut iter, "symbol.kind")?, "symbol.kind")?;
            let line_start = extract_u32(
                &next_item(&mut iter, "symbol.line_start")?,
                "symbol.line_start",
            )?;
            let line_end =
                extract_u32(&next_item(&mut iter, "symbol.line_end")?, "symbol.line_end")?;
            Ok(SymbolInfo {
                name,
                kind,
                line_start,
                line_end,
            })
        })
        .collect()
}

// Helper functions for extracting values from CBOR

fn next_item(iter: &mut impl Iterator<Item = Value>, field: &str) -> Result<Value, GitError> {
    iter.next()
        .ok_or_else(|| GitError::InvalidEvent(format!("missing field: {}", field)))
}

fn extract_bytes(value: &Value, field: &str, expected_len: usize) -> Result<Vec<u8>, GitError> {
    match value {
        Value::Bytes(b) => {
            if b.len() != expected_len {
                return Err(GitError::InvalidEvent(format!(
                    "{} has wrong length: expected {}, got {}",
                    field,
                    expected_len,
                    b.len()
                )));
            }
            Ok(b.clone())
        }
        _ => Err(GitError::InvalidEvent(format!("{} must be bytes", field))),
    }
}

fn extract_u64(value: &Value, field: &str) -> Result<u64, GitError> {
    match value {
        Value::Integer(i) => {
            let n: i128 = (*i).into();
            if n < 0 || n > u64::MAX as i128 {
                return Err(GitError::InvalidEvent(format!("{} out of range", field)));
            }
            Ok(n as u64)
        }
        _ => Err(GitError::InvalidEvent(format!("{} must be integer", field))),
    }
}

fn extract_u32(value: &Value, field: &str) -> Result<u32, GitError> {
    match value {
        Value::Integer(i) => {
            let n: i128 = (*i).into();
            if n < 0 || n > u32::MAX as i128 {
                return Err(GitError::InvalidEvent(format!("{} out of range", field)));
            }
            Ok(n as u32)
        }
        _ => Err(GitError::InvalidEvent(format!("{} must be integer", field))),
    }
}

fn extract_string(value: &Value, field: &str) -> Result<String, GitError> {
    match value {
        Value::Text(s) => Ok(s.clone()),
        _ => Err(GitError::InvalidEvent(format!("{} must be string", field))),
    }
}

fn extract_optional_string(value: &Value, field: &str) -> Result<Option<String>, GitError> {
    match value {
        Value::Null => Ok(None),
        Value::Text(s) => Ok(Some(s.clone())),
        _ => Err(GitError::InvalidEvent(format!(
            "{} must be string or null",
            field
        ))),
    }
}

fn extract_string_array(value: &Value, field: &str) -> Result<Vec<String>, GitError> {
    match value {
        Value::Array(arr) => arr.iter().map(|v| extract_string(v, field)).collect(),
        _ => Err(GitError::InvalidEvent(format!("{} must be array", field))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use libgrite_core::hash::compute_event_id;
    use libgrite_core::types::ids::generate_issue_id;

    fn make_test_event(kind: EventKind) -> Event {
        let issue_id = generate_issue_id();
        let actor = [1u8; 16];
        let ts_unix_ms = 1700000000000u64;
        let event_id = compute_event_id(&issue_id, &actor, ts_unix_ms, None, &kind);
        Event::new(event_id, issue_id, actor, ts_unix_ms, None, kind)
    }

    #[test]
    fn test_chunk_roundtrip_issue_created() {
        let event = make_test_event(EventKind::IssueCreated {
            title: "Test Issue".to_string(),
            body: "Test body".to_string(),
            labels: vec!["bug".to_string(), "p0".to_string()],
        });

        let chunk = encode_chunk(std::slice::from_ref(&event)).unwrap();

        // Verify magic
        assert_eq!(&chunk[0..8], CHUNK_MAGIC);

        // Decode and verify
        let decoded = decode_chunk(&chunk).unwrap();
        assert_eq!(decoded.len(), 1);
        assert_eq!(decoded[0].event_id, event.event_id);
        assert_eq!(decoded[0].issue_id, event.issue_id);
        assert_eq!(decoded[0].actor, event.actor);
        assert_eq!(decoded[0].ts_unix_ms, event.ts_unix_ms);

        if let EventKind::IssueCreated {
            title,
            body,
            labels,
        } = &decoded[0].kind
        {
            assert_eq!(title, "Test Issue");
            assert_eq!(body, "Test body");
            assert!(labels.contains(&"bug".to_string()));
            assert!(labels.contains(&"p0".to_string()));
        } else {
            panic!("Wrong event kind");
        }
    }

    #[test]
    fn test_chunk_roundtrip_all_kinds() {
        let events = vec![
            make_test_event(EventKind::IssueCreated {
                title: "Test".to_string(),
                body: "Body".to_string(),
                labels: vec![],
            }),
            make_test_event(EventKind::IssueUpdated {
                title: Some("New Title".to_string()),
                body: None,
            }),
            make_test_event(EventKind::CommentAdded {
                body: "A comment".to_string(),
            }),
            make_test_event(EventKind::LabelAdded {
                label: "bug".to_string(),
            }),
            make_test_event(EventKind::LabelRemoved {
                label: "wip".to_string(),
            }),
            make_test_event(EventKind::StateChanged {
                state: IssueState::Closed,
            }),
            make_test_event(EventKind::LinkAdded {
                url: "https://example.com".to_string(),
                note: Some("ref".to_string()),
            }),
            make_test_event(EventKind::AssigneeAdded {
                user: "alice".to_string(),
            }),
            make_test_event(EventKind::AssigneeRemoved {
                user: "bob".to_string(),
            }),
            make_test_event(EventKind::AttachmentAdded {
                name: "file.txt".to_string(),
                sha256: [0u8; 32],
                mime: "text/plain".to_string(),
            }),
            make_test_event(EventKind::DependencyAdded {
                target: [0xAA; 16],
                dep_type: DependencyType::Blocks,
            }),
            make_test_event(EventKind::DependencyRemoved {
                target: [0xBB; 16],
                dep_type: DependencyType::DependsOn,
            }),
            make_test_event(EventKind::ContextUpdated {
                path: "src/main.rs".to_string(),
                language: "rust".to_string(),
                symbols: vec![SymbolInfo {
                    name: "main".to_string(),
                    kind: "function".to_string(),
                    line_start: 1,
                    line_end: 10,
                }],
                summary: "Entry point".to_string(),
                content_hash: [0xCC; 32],
            }),
            make_test_event(EventKind::ProjectContextUpdated {
                key: "framework".to_string(),
                value: "actix-web".to_string(),
            }),
        ];

        let chunk = encode_chunk(&events).unwrap();
        let decoded = decode_chunk(&chunk).unwrap();

        assert_eq!(decoded.len(), events.len());
        for (orig, dec) in events.iter().zip(decoded.iter()) {
            assert_eq!(orig.event_id, dec.event_id);
            assert_eq!(orig.kind, dec.kind);
        }
    }

    #[test]
    fn test_chunk_hash_deterministic() {
        let event = make_test_event(EventKind::IssueCreated {
            title: "Test".to_string(),
            body: "Body".to_string(),
            labels: vec![],
        });

        let chunk1 = encode_chunk(std::slice::from_ref(&event)).unwrap();
        let chunk2 = encode_chunk(&[event]).unwrap();

        let hash1 = chunk_hash(&chunk1);
        let hash2 = chunk_hash(&chunk2);

        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_invalid_chunk_magic() {
        let data = b"BADMAGIC\x01\x00\x07cbor-v1";
        let result = decode_chunk(data);
        assert!(matches!(result, Err(GitError::InvalidChunk(_))));
    }

    #[test]
    fn test_invalid_chunk_version() {
        let mut data = Vec::new();
        data.extend_from_slice(CHUNK_MAGIC);
        data.extend_from_slice(&99u16.to_le_bytes()); // Bad version
        data.push(7);
        data.extend_from_slice(b"cbor-v1");

        let result = decode_chunk(&data);
        assert!(matches!(result, Err(GitError::InvalidChunk(_))));
    }
}