pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#![cfg(feature = "dap")]
//! REPLAY-002: Snapshot Serialization Tests
//! Sprint 75 - RED Phase
//!
//! Tests drive optimization requirements for snapshot serialization:
//! - Buffer reuse to minimize allocations
//! - Streaming serialization for large recordings
//! - Performance benchmarks
//! - Memory efficiency validation

// RED Test 1: Streaming serialization with writer
#[test]
fn test_streaming_serialization_to_writer() {
    // This test drives the requirement for streaming serialization
    // Expected: Can write snapshots incrementally without loading all in memory

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{RecordingWriter, Snapshot};
    // use std::io::Cursor;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "test_program", vec![])?;
    //
    // // Write snapshots incrementally
    // for i in 0..100 {
    //     let snapshot = Snapshot {
    //         frame_id: i,
    //         timestamp_relative_ms: i as u32 * 10,
    //         variables: HashMap::new(),
    //         stack_frames: vec![],
    //         instruction_pointer: 0x1000 + i * 8,
    //         memory_snapshot: None,
    //     };
    //     writer.write_snapshot(&snapshot)?;
    // }
    //
    // writer.finalize()?;
    //
    // // Verify file is valid
    // let bytes = buffer.into_inner();
    // let recording = Recording::from_bytes(&bytes)?;
    // assert_eq!(recording.snapshot_count(), 100);

    assert!(true, "Streaming serialization must be supported");
}

// RED Test 2: Buffer reuse to minimize allocations
#[test]
fn test_buffer_reuse_for_multiple_snapshots() {
    // This test drives memory efficiency requirements
    // Expected: Reuse internal buffers to avoid allocation churn

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{SnapshotSerializer, Snapshot};
    //
    // let mut serializer = SnapshotSerializer::with_capacity(1024);
    //
    // // Serialize multiple snapshots reusing buffer
    // for i in 0..1000 {
    //     let snapshot = Snapshot {
    //         frame_id: i,
    //         timestamp_relative_ms: i as u32,
    //         variables: HashMap::new(),
    //         stack_frames: vec![],
    //         instruction_pointer: 0x1000,
    //         memory_snapshot: None,
    //     };
    //
    //     let bytes = serializer.serialize(&snapshot)?;
    //     assert!(!bytes.is_empty(), "Serialization should produce bytes");
    //
    //     // Buffer should be reused internally, not reallocated
    // }
    //
    // // Verify buffer capacity didn't grow excessively
    // assert!(serializer.capacity() < 2048, "Buffer should not grow unbounded");

    assert!(true, "Buffer reuse must minimize allocations");
}

// RED Test 3: Large snapshot handling (1MB+ variables)
#[test]
fn test_large_snapshot_serialization() {
    // This test drives scalability requirements
    // Expected: Handle large snapshots (e.g., 1MB+ variable data) efficiently

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::Snapshot;
    //
    // // Create snapshot with 1MB of variable data
    // let mut variables = HashMap::new();
    // let large_string = "x".repeat(1_000_000); // 1MB string
    // variables.insert("large_var".to_string(), serde_json::Value::String(large_string));
    //
    // let snapshot = Snapshot {
    //     frame_id: 1,
    //     timestamp_relative_ms: 0,
    //     variables,
    //     stack_frames: vec![],
    //     instruction_pointer: 0x1000,
    //     memory_snapshot: None,
    // };
    //
    // // Should serialize without panic or excessive memory usage
    // let mut serializer = SnapshotSerializer::new();
    // let bytes = serializer.serialize(&snapshot)?;
    //
    // assert!(bytes.len() > 1_000_000, "Serialized size should reflect large data");

    assert!(true, "Large snapshots (1MB+) must serialize efficiently");
}

// RED Test 4: Memory snapshot compression
#[test]
fn test_memory_snapshot_compression() {
    // This test drives compression requirements for memory snapshots
    // Expected: Optional compression for large memory snapshots

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{Snapshot, CompressionLevel};
    //
    // // Create snapshot with large memory snapshot (heap state)
    // let memory_data = vec![0u8; 10_000_000]; // 10MB heap snapshot
    //
    // let snapshot = Snapshot {
    //     frame_id: 1,
    //     timestamp_relative_ms: 0,
    //     variables: HashMap::new(),
    //     stack_frames: vec![],
    //     instruction_pointer: 0x1000,
    //     memory_snapshot: Some(memory_data),
    // };
    //
    // // Serialize with compression
    // let mut serializer = SnapshotSerializer::with_compression(CompressionLevel::Balanced);
    // let compressed_bytes = serializer.serialize(&snapshot)?;
    //
    // // Serialize without compression
    // let mut no_compression = SnapshotSerializer::new();
    // let uncompressed_bytes = no_compression.serialize(&snapshot)?;
    //
    // assert!(
    //     compressed_bytes.len() < uncompressed_bytes.len(),
    //     "Compression should reduce size"
    // );

    assert!(true, "Memory snapshot compression must be supported");
}

// RED Test 5: Snapshot count accumulation
#[test]
fn test_snapshot_count_tracked_during_streaming() {
    // This test drives automatic snapshot counting
    // Expected: Writer tracks count automatically, writes correct count on finalize

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::RecordingWriter;
    // use std::io::Cursor;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "test", vec![])?;
    //
    // // Write 50 snapshots
    // for i in 0..50 {
    //     let snapshot = create_test_snapshot(i);
    //     writer.write_snapshot(&snapshot)?;
    // }
    //
    // assert_eq!(writer.snapshot_count(), 50, "Writer should track count");
    //
    // writer.finalize()?;
    //
    // // Verify recorded count matches
    // let bytes = buffer.into_inner();
    // let recording = Recording::from_bytes(&bytes)?;
    // assert_eq!(recording.snapshot_count(), 50);

    assert!(true, "Snapshot count must be tracked during streaming");
}

// RED Test 6: Incremental write validation
#[test]
fn test_incremental_write_produces_valid_file() {
    // This test drives correctness requirement for streaming writes
    // Expected: Incrementally written file is valid .pmat format

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{RecordingWriter, Recording};
    // use std::fs::File;
    //
    // let temp_file = tempfile::NamedTempFile::new()?;
    // let mut writer = RecordingWriter::new(temp_file.as_file(), "test", vec![])?;
    //
    // // Write snapshots incrementally
    // for i in 0..10 {
    //     let snapshot = create_test_snapshot(i);
    //     writer.write_snapshot(&snapshot)?;
    // }
    //
    // writer.finalize()?;
    //
    // // Load and validate
    // let recording = Recording::load_from_file(temp_file.path())?;
    // assert_eq!(recording.snapshot_count(), 10);
    // assert_eq!(&recording.snapshots()[0..4], b"PMAT", "Magic header present");

    assert!(true, "Incremental writes must produce valid .pmat files");
}

// RED Test 7: Performance - 1000 snapshots in <100ms
#[test]
fn test_serialize_1000_snapshots_performance() {
    // This test drives performance requirements
    // Expected: Serialize 1000 small snapshots in < 100ms

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{RecordingWriter, Snapshot};
    // use std::io::Cursor;
    // use std::time::Instant;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "perf_test", vec![])?;
    //
    // let start = Instant::now();
    //
    // for i in 0..1000 {
    //     let snapshot = Snapshot {
    //         frame_id: i,
    //         timestamp_relative_ms: i as u32,
    //         variables: HashMap::from([
    //             ("x".to_string(), serde_json::Value::Number(42.into())),
    //         ]),
    //         stack_frames: vec![],
    //         instruction_pointer: 0x1000,
    //         memory_snapshot: None,
    //     };
    //     writer.write_snapshot(&snapshot)?;
    // }
    //
    // writer.finalize()?;
    // let elapsed = start.elapsed();
    //
    // assert!(
    //     elapsed.as_millis() < 100,
    //     "1000 snapshots should serialize in < 100ms, took {}ms",
    //     elapsed.as_millis()
    // );

    assert!(
        true,
        "Performance: 1000 snapshots must serialize in < 100ms"
    );
}

// RED Test 8: Memory efficiency - bounded allocations
#[test]
fn test_bounded_memory_usage_during_streaming() {
    // This test drives memory efficiency
    // Expected: Memory usage stays bounded even with many snapshots

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::RecordingWriter;
    // use std::io::Cursor;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "memory_test", vec![])?;
    //
    // // Write 10,000 snapshots
    // for i in 0..10_000 {
    //     let snapshot = create_test_snapshot(i);
    //     writer.write_snapshot(&snapshot)?;
    //
    //     // Memory should not grow unbounded
    //     // (would need actual memory profiling in real test)
    // }
    //
    // writer.finalize()?;
    //
    // let recording = Recording::from_bytes(&buffer.into_inner())?;
    // assert_eq!(recording.snapshot_count(), 10_000);

    assert!(true, "Memory usage must stay bounded during streaming");
}

// RED Test 9: Error handling - write after finalize
#[test]
fn test_error_on_write_after_finalize() {
    // This test drives API safety requirements
    // Expected: Writing after finalize returns error

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::RecordingWriter;
    // use std::io::Cursor;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "test", vec![])?;
    //
    // let snapshot = create_test_snapshot(1);
    // writer.write_snapshot(&snapshot)?;
    // writer.finalize()?;
    //
    // // Attempt to write after finalize
    // let result = writer.write_snapshot(&snapshot);
    // assert!(result.is_err(), "Writing after finalize should fail");
    //
    // let err = result.unwrap_err();
    // assert!(
    //     err.to_string().contains("finalized"),
    //     "Error should mention finalization"
    // );

    assert!(true, "Writing after finalize must return error");
}

// RED Test 10: Metadata modification before finalize
#[test]
fn test_metadata_can_be_modified_before_finalize() {
    // This test drives flexibility requirement
    // Expected: Can update metadata (e.g., add environment vars) before finalize

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::RecordingWriter;
    // use std::io::Cursor;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "test", vec![])?;
    //
    // // Add environment variable after creation
    // writer.add_environment("PATH", "/usr/bin:/bin");
    // writer.add_environment("USER", "developer");
    //
    // writer.write_snapshot(&create_test_snapshot(1))?;
    // writer.finalize()?;
    //
    // // Verify metadata was written
    // let recording = Recording::from_bytes(&buffer.into_inner())?;
    // let metadata = recording.metadata();
    // assert_eq!(metadata.environment.get("PATH"), Some(&"/usr/bin:/bin".to_string()));
    // assert_eq!(metadata.environment.get("USER"), Some(&"developer".to_string()));

    assert!(true, "Metadata must be modifiable before finalize");
}

// RED Test 11: Partial write recovery
#[test]
fn test_partial_write_detection() {
    // This test drives robustness requirement
    // Expected: Detect incomplete writes (missing finalize)

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{RecordingWriter, Recording};
    // use std::io::Cursor;
    //
    // let mut buffer = Cursor::new(Vec::new());
    // let mut writer = RecordingWriter::new(&mut buffer, "test", vec![])?;
    //
    // writer.write_snapshot(&create_test_snapshot(1))?;
    // writer.write_snapshot(&create_test_snapshot(2))?;
    //
    // // Drop writer without finalizing (simulates crash)
    // drop(writer);
    //
    // // Attempt to load incomplete file
    // let result = Recording::from_bytes(&buffer.into_inner());
    // assert!(result.is_err(), "Incomplete file should fail to load");
    //
    // let err = result.unwrap_err();
    // assert!(
    //     err.to_string().contains("truncated") || err.to_string().contains("incomplete"),
    //     "Error should indicate incomplete file"
    // );

    assert!(true, "Incomplete writes (no finalize) must be detected");
}

// RED Test 12: Compression level selection
#[test]
fn test_compression_level_affects_output_size() {
    // This test drives compression configurability
    // Expected: Different compression levels produce different sizes

    // Will implement in GREEN phase:
    // use pmat::services::dap::recording::{RecordingWriter, CompressionLevel};
    // use std::io::Cursor;
    //
    // let snapshot = create_large_snapshot(); // Large compressible data
    //
    // // Write with no compression
    // let mut buffer_none = Cursor::new(Vec::new());
    // let mut writer_none = RecordingWriter::with_compression(
    //     &mut buffer_none,
    //     "test",
    //     vec![],
    //     CompressionLevel::None
    // )?;
    // writer_none.write_snapshot(&snapshot)?;
    // writer_none.finalize()?;
    //
    // // Write with fast compression
    // let mut buffer_fast = Cursor::new(Vec::new());
    // let mut writer_fast = RecordingWriter::with_compression(
    //     &mut buffer_fast,
    //     "test",
    //     vec![],
    //     CompressionLevel::Fast
    // )?;
    // writer_fast.write_snapshot(&snapshot)?;
    // writer_fast.finalize()?;
    //
    // // Write with best compression
    // let mut buffer_best = Cursor::new(Vec::new());
    // let mut writer_best = RecordingWriter::with_compression(
    //     &mut buffer_best,
    //     "test",
    //     vec![],
    //     CompressionLevel::Best
    // )?;
    // writer_best.write_snapshot(&snapshot)?;
    // writer_best.finalize()?;
    //
    // let size_none = buffer_none.into_inner().len();
    // let size_fast = buffer_fast.into_inner().len();
    // let size_best = buffer_best.into_inner().len();
    //
    // assert!(size_fast < size_none, "Fast compression should reduce size");
    // assert!(size_best < size_fast, "Best compression should be smaller than fast");

    assert!(true, "Compression level must affect output size");
}

/// Helper: Create test snapshot (will be implemented in GREEN phase)
#[allow(dead_code)]
fn create_test_snapshot(_frame_id: u64) {
    // Placeholder - will return Snapshot in GREEN phase
}

/// Helper: Create large snapshot for compression tests
#[allow(dead_code)]
fn create_large_snapshot() {
    // Placeholder - will return Snapshot with large data
}

// RED Test 13: File format documentation
#[test]
fn test_streaming_format_documented() {
    // This test drives documentation requirement
    // Expected: Streaming format is documented in specification

    // Will verify in GREEN phase that docs/specifications/components/infrastructure.md
    // includes streaming serialization details

    assert!(true, "Streaming serialization format must be documented");
}