chkpt-core 0.3.1

Core library for chkpt – a fast, content-addressable checkpoint system
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
use chkpt_core::error::ChkpttError;
use chkpt_core::ops::delete::delete;
use chkpt_core::ops::list::list;
use chkpt_core::ops::restore::{restore, RestoreOptions};
use chkpt_core::ops::save::{save, SaveOptions};
use std::fs;
use tempfile::TempDir;

fn unique_prefix(snapshot_id: &str, other_snapshot_id: &str) -> String {
    let shared = snapshot_id
        .chars()
        .zip(other_snapshot_id.chars())
        .take_while(|(left, right)| left == right)
        .count();
    snapshot_id.chars().take(shared + 1).collect()
}

/// Full lifecycle: save -> list -> restore -> verify
#[test]
fn test_e2e_save_list_restore() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("readme.md"), "# Hello").unwrap();
    fs::create_dir_all(workspace.path().join("src")).unwrap();
    fs::write(workspace.path().join("src/main.rs"), "fn main() {}").unwrap();

    // Save
    let r = save(
        workspace.path(),
        SaveOptions {
            message: Some("initial".into()),
            ..Default::default()
        },
    )
    .unwrap();

    // List (returns newest first)
    let snapshots = list(workspace.path(), None).unwrap();
    assert_eq!(snapshots.len(), 1);
    assert_eq!(snapshots[0].id, r.snapshot_id);
    assert_eq!(snapshots[0].message.as_deref(), Some("initial"));

    // Modify workspace
    fs::write(workspace.path().join("readme.md"), "# Modified").unwrap();
    fs::write(workspace.path().join("new.txt"), "new file").unwrap();

    // Restore
    let rr = restore(workspace.path(), &r.snapshot_id, RestoreOptions::default()).unwrap();
    assert!(rr.files_changed > 0 || rr.files_removed > 0);

    // Verify restored state matches original
    assert_eq!(
        fs::read_to_string(workspace.path().join("readme.md")).unwrap(),
        "# Hello"
    );
    assert_eq!(
        fs::read_to_string(workspace.path().join("src/main.rs")).unwrap(),
        "fn main() {}"
    );
    assert!(!workspace.path().join("new.txt").exists());
}

/// Multiple saves and selective restore to earlier snapshots
#[test]
fn test_e2e_multiple_saves_selective_restore() {
    let workspace = TempDir::new().unwrap();

    fs::write(workspace.path().join("a.txt"), "version 1").unwrap();
    let r1 = save(
        workspace.path(),
        SaveOptions {
            message: Some("v1".into()),
            ..Default::default()
        },
    )
    .unwrap();

    fs::write(workspace.path().join("a.txt"), "version 2").unwrap();
    fs::write(workspace.path().join("b.txt"), "added in v2").unwrap();
    let r2 = save(
        workspace.path(),
        SaveOptions {
            message: Some("v2".into()),
            ..Default::default()
        },
    )
    .unwrap();

    fs::write(workspace.path().join("a.txt"), "version 3").unwrap();

    // Restore to v1
    restore(workspace.path(), &r1.snapshot_id, RestoreOptions::default()).unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("a.txt")).unwrap(),
        "version 1"
    );
    assert!(!workspace.path().join("b.txt").exists());

    // Restore to v2
    restore(workspace.path(), &r2.snapshot_id, RestoreOptions::default()).unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("a.txt")).unwrap(),
        "version 2"
    );
    assert_eq!(
        fs::read_to_string(workspace.path().join("b.txt")).unwrap(),
        "added in v2"
    );
}

/// Delete a snapshot; remaining snapshots still work
#[test]
fn test_e2e_delete_gc_preserves_valid() {
    let workspace = TempDir::new().unwrap();

    fs::write(workspace.path().join("shared.txt"), "shared content").unwrap();
    let r1 = save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("unique.txt"), "only in v2").unwrap();
    let r2 = save(workspace.path(), SaveOptions::default()).unwrap();

    // Delete first snapshot
    delete(workspace.path(), &r1.snapshot_id).unwrap();

    // Second snapshot should still be listed and restorable
    let snaps = list(workspace.path(), None).unwrap();
    assert_eq!(snaps.len(), 1);
    assert_eq!(snaps[0].id, r2.snapshot_id);

    // Remove files from workspace, then restore from the surviving snapshot
    fs::remove_file(workspace.path().join("shared.txt")).unwrap();
    fs::remove_file(workspace.path().join("unique.txt")).unwrap();
    restore(workspace.path(), &r2.snapshot_id, RestoreOptions::default()).unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("shared.txt")).unwrap(),
        "shared content"
    );
    assert_eq!(
        fs::read_to_string(workspace.path().join("unique.txt")).unwrap(),
        "only in v2"
    );
}

/// Dry-run reports changes without modifying workspace
#[test]
fn test_e2e_dry_run_no_modification() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("a.txt"), "original").unwrap();
    let r = save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("a.txt"), "changed").unwrap();
    fs::write(workspace.path().join("extra.txt"), "extra").unwrap();

    let result = restore(
        workspace.path(),
        &r.snapshot_id,
        RestoreOptions {
            dry_run: true,
            ..Default::default()
        },
    )
    .unwrap();

    // Should report changes
    assert!(result.files_changed + result.files_added + result.files_removed > 0);

    // But workspace should be unchanged
    assert_eq!(
        fs::read_to_string(workspace.path().join("a.txt")).unwrap(),
        "changed"
    );
    assert!(workspace.path().join("extra.txt").exists());
}

/// Large file count scenario with deduplication
#[test]
fn test_e2e_many_files() {
    let workspace = TempDir::new().unwrap();

    // Create 200 files across 10 directories
    for i in 0..200 {
        let dir = workspace.path().join(format!("dir_{}", i / 20));
        fs::create_dir_all(&dir).unwrap();
        fs::write(
            dir.join(format!("file_{}.txt", i)),
            format!("content-{}", i),
        )
        .unwrap();
    }

    let r = save(workspace.path(), SaveOptions::default()).unwrap();
    assert_eq!(r.stats.total_files, 200);
    assert_eq!(r.stats.new_objects, 200);

    // Second save with no changes: all deduped
    let r2 = save(workspace.path(), SaveOptions::default()).unwrap();
    assert_eq!(r2.stats.new_objects, 0);

    // Modify some files
    for i in 0..10 {
        let dir = workspace.path().join(format!("dir_{}", i / 20));
        fs::write(
            dir.join(format!("file_{}.txt", i)),
            format!("modified-{}", i),
        )
        .unwrap();
    }

    let r3 = save(workspace.path(), SaveOptions::default()).unwrap();
    assert_eq!(r3.stats.new_objects, 10);

    // Restore to original
    restore(workspace.path(), &r.snapshot_id, RestoreOptions::default()).unwrap();

    // Verify a few files
    let content = fs::read_to_string(workspace.path().join("dir_0/file_0.txt")).unwrap();
    assert_eq!(content, "content-0");
    let content5 = fs::read_to_string(workspace.path().join("dir_0/file_5.txt")).unwrap();
    assert_eq!(content5, "content-5");
    let content199 = fs::read_to_string(workspace.path().join("dir_9/file_199.txt")).unwrap();
    assert_eq!(content199, "content-199");
}

/// Save respects .chkptignore patterns
#[test]
fn test_e2e_chkptignore() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("keep.txt"), "keep").unwrap();
    fs::write(workspace.path().join("ignore.log"), "ignore").unwrap();
    fs::create_dir_all(workspace.path().join("build")).unwrap();
    fs::write(workspace.path().join("build/output.o"), "binary").unwrap();
    fs::write(workspace.path().join(".chkptignore"), "*.log\nbuild/\n").unwrap();

    let r = save(workspace.path(), SaveOptions::default()).unwrap();
    // Should only save keep.txt and .chkptignore (ignore.log and build/ excluded)
    assert_eq!(r.stats.total_files, 2);
}

/// Empty workspace produces a valid snapshot
#[test]
fn test_e2e_empty_workspace() {
    let workspace = TempDir::new().unwrap();
    let r = save(workspace.path(), SaveOptions::default()).unwrap();
    assert_eq!(r.stats.total_files, 0);
    assert_eq!(r.stats.new_objects, 0);

    // Should be listable
    let snaps = list(workspace.path(), None).unwrap();
    assert_eq!(snaps.len(), 1);

    // Should be restorable (no-op)
    let rr = restore(workspace.path(), &r.snapshot_id, RestoreOptions::default()).unwrap();
    assert_eq!(rr.files_added, 0);
    assert_eq!(rr.files_changed, 0);
    assert_eq!(rr.files_removed, 0);
}

/// Restore "latest" alias picks the most recent snapshot
#[test]
fn test_e2e_restore_latest() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("a.txt"), "v1").unwrap();
    save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("a.txt"), "v2").unwrap();
    save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("a.txt"), "v3").unwrap();

    restore(workspace.path(), "latest", RestoreOptions::default()).unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("a.txt")).unwrap(),
        "v2"
    );
}

#[test]
fn test_e2e_restore_unique_prefix() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("a.txt"), "v1").unwrap();
    let r1 = save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("a.txt"), "v2").unwrap();
    let r2 = save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("a.txt"), "v3").unwrap();

    let prefix = unique_prefix(&r1.snapshot_id, &r2.snapshot_id);
    restore(workspace.path(), &prefix, RestoreOptions::default()).unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("a.txt")).unwrap(),
        "v1"
    );

    fs::write(workspace.path().join("a.txt"), "v4").unwrap();
    let latest_prefix = unique_prefix(&r2.snapshot_id, &r1.snapshot_id);
    restore(workspace.path(), &latest_prefix, RestoreOptions::default()).unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("a.txt")).unwrap(),
        "v2"
    );
}

#[test]
fn test_e2e_restore_ambiguous_prefix_errors() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("a.txt"), "v1").unwrap();
    let r1 = save(workspace.path(), SaveOptions::default()).unwrap();

    fs::write(workspace.path().join("a.txt"), "v2").unwrap();
    let r2 = save(workspace.path(), SaveOptions::default()).unwrap();

    let shared_prefix: String = r1
        .snapshot_id
        .chars()
        .zip(r2.snapshot_id.chars())
        .take_while(|(left, right)| left == right)
        .map(|(ch, _)| ch)
        .collect();
    assert!(!shared_prefix.is_empty());

    let err = restore(
        workspace.path(),
        &shared_prefix,
        RestoreOptions {
            dry_run: true,
            ..Default::default()
        },
    )
    .unwrap_err();
    assert!(
        matches!(err, ChkpttError::Other(message) if message.contains("Ambiguous snapshot prefix"))
    );
}

#[test]
fn test_e2e_repeated_restore_save_delete_lifecycle() {
    let workspace = TempDir::new().unwrap();

    fs::create_dir_all(workspace.path().join("src")).unwrap();
    fs::write(
        workspace.path().join("src/main.rs"),
        "fn main() { println!(\"v1\"); }",
    )
    .unwrap();
    let first = save(
        workspace.path(),
        SaveOptions {
            message: Some("first".into()),
            ..Default::default()
        },
    )
    .unwrap();

    fs::write(
        workspace.path().join("src/main.rs"),
        "fn main() { println!(\"v2\"); }",
    )
    .unwrap();
    fs::write(workspace.path().join("README.md"), "# v2").unwrap();
    let second = save(
        workspace.path(),
        SaveOptions {
            message: Some("second".into()),
            ..Default::default()
        },
    )
    .unwrap();

    restore(
        workspace.path(),
        &first.snapshot_id,
        RestoreOptions::default(),
    )
    .unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("src/main.rs")).unwrap(),
        "fn main() { println!(\"v1\"); }"
    );
    assert!(!workspace.path().join("README.md").exists());

    let resaved = save(
        workspace.path(),
        SaveOptions {
            message: Some("resaved".into()),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(resaved.stats.new_objects, 0);

    delete(workspace.path(), &first.snapshot_id).unwrap();
    delete(workspace.path(), &resaved.snapshot_id).unwrap();

    fs::write(
        workspace.path().join("src/main.rs"),
        "fn main() { println!(\"mutated\"); }",
    )
    .unwrap();
    restore(
        workspace.path(),
        &second.snapshot_id,
        RestoreOptions::default(),
    )
    .unwrap();
    assert_eq!(
        fs::read_to_string(workspace.path().join("src/main.rs")).unwrap(),
        "fn main() { println!(\"v2\"); }"
    );
    assert_eq!(
        fs::read_to_string(workspace.path().join("README.md")).unwrap(),
        "# v2"
    );
}

/// Deleting all snapshots leaves an empty list
#[test]
fn test_e2e_delete_all() {
    let workspace = TempDir::new().unwrap();
    fs::write(workspace.path().join("a.txt"), "data").unwrap();

    let r1 = save(workspace.path(), SaveOptions::default()).unwrap();
    let r2 = save(workspace.path(), SaveOptions::default()).unwrap();

    delete(workspace.path(), &r1.snapshot_id).unwrap();
    delete(workspace.path(), &r2.snapshot_id).unwrap();

    let snaps = list(workspace.path(), None).unwrap();
    assert_eq!(snaps.len(), 0);
}