basalt-db 0.1.6

CLI-first local SQL workspaces for structured data and coding agents
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::atomic::{AtomicU64, Ordering};

use serde_json::Value;

struct TempDir {
    path: PathBuf,
}

static TEMP_DIR_SEQUENCE: AtomicU64 = AtomicU64::new(0);

impl TempDir {
    fn new() -> Self {
        let path = std::env::temp_dir().join(format!(
            "basalt-workspace-test-{}-{}",
            std::process::id(),
            unique_suffix()
        ));
        fs::create_dir(&path).unwrap();
        Self { path }
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}

fn run(args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_basalt"))
        .args(args)
        .output()
        .expect("Basalt should run")
}

fn run_with_env(args: &[&str], key: &str) -> Output {
    Command::new(env!("CARGO_BIN_EXE_basalt"))
        .args(args)
        .env(key, "1")
        .output()
        .expect("Basalt should run")
}

fn path_arg(path: &Path) -> &str {
    path.to_str().expect("test paths should be UTF-8")
}

fn inspect(workspace: &Path) -> Value {
    let output = run(&["workspace", "inspect", "--json", path_arg(workspace)]);
    assert!(output.status.success(), "inspect failed: {output:?}");
    serde_json::from_slice(&output.stdout).expect("inspect should emit JSON")
}

#[test]
fn initializes_and_inspects_a_workspace() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let output = run(&["init", path_arg(&workspace)]);
    assert!(output.status.success(), "init failed: {output:?}");
    assert!(workspace.join("workspace.json").is_file());
    assert!(workspace.join("data.basalt").is_file());

    let report = inspect(&workspace);
    assert_eq!(report["format_version"], 1);
    assert_eq!(report["database"], "data.basalt");
    assert_eq!(report["tables"].as_array().unwrap().len(), 0);
}

#[test]
fn imports_csv_exports_common_formats_and_reopens() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("events.csv");
    let csv = "id,name,note\n1,\"Ada, Lovelace\",\n2,Bob,\"hello\"\n";
    fs::write(&source, csv).unwrap();
    assert!(
        run(&["workspace", "init", path_arg(&workspace)])
            .status
            .success()
    );

    let output = run(&[
        "workspace",
        "import",
        path_arg(&workspace),
        path_arg(&source),
    ]);
    assert!(output.status.success(), "import failed: {output:?}");
    let report = inspect(&workspace);
    assert_eq!(report["tables"][0]["name"], "events");
    assert_eq!(report["tables"][0]["rows"], 2);
    assert_eq!(report["tables"][0]["columns"][0]["data_type"], "INTEGER");

    let query = run(&[
        "workspace",
        "query",
        "--json",
        path_arg(&workspace),
        "SELECT id, name, note FROM events ORDER BY id",
    ]);
    assert!(query.status.success(), "query failed: {query:?}");
    let lines: Vec<Value> = String::from_utf8(query.stdout)
        .unwrap()
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(lines[0]["rows"][0][1], "Ada, Lovelace");
    assert_eq!(lines[0]["rows"][0][2], "");

    let rejected = run(&[
        "workspace",
        "query",
        path_arg(&workspace),
        "DELETE FROM events",
    ]);
    assert!(!rejected.status.success());

    let csv_output = temp.path().join("roundtrip.csv");
    let export = run(&[
        "workspace",
        "export",
        path_arg(&workspace),
        "events",
        path_arg(&csv_output),
    ]);
    assert!(export.status.success(), "CSV export failed: {export:?}");
    assert_eq!(
        fs::read_to_string(&csv_output).unwrap(),
        "id,name,note\n1,\"Ada, Lovelace\",\n2,Bob,hello\n"
    );

    let jsonl = run(&[
        "workspace",
        "export",
        "--format",
        "jsonl",
        path_arg(&workspace),
        "events",
        "-",
    ]);
    assert!(jsonl.status.success(), "JSONL export failed: {jsonl:?}");
    let exported: Vec<Value> = String::from_utf8(jsonl.stdout)
        .unwrap()
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(exported[0]["id"], 1);
    assert_eq!(exported[0]["name"], "Ada, Lovelace");
    assert_eq!(exported[0]["note"], "");
}

#[test]
fn machine_readable_import_and_export_reports_are_unambiguous() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("events.csv");
    let export_path = temp.path().join("events.jsonl");
    let source_bytes = b"id,name\n1,Ada\n";
    fs::write(&source, source_bytes).unwrap();
    assert!(run(&["init", path_arg(&workspace)]).status.success());

    let imported = run(&[
        "workspace",
        "import",
        "--json",
        "--table",
        "events",
        path_arg(&workspace),
        path_arg(&source),
    ]);
    assert!(imported.status.success(), "import failed: {imported:?}");
    let imported: Value = serde_json::from_slice(&imported.stdout).unwrap();
    assert_eq!(imported["operation"], "import");
    assert_eq!(imported["format"], "csv");
    assert_eq!(imported["table"], "events");
    assert_eq!(imported["bytes"], source_bytes.len());
    assert_eq!(imported["summary"], "table events (1 rows, 2 columns)");
    assert!(imported["change_id"].as_str().is_some());

    let history = run(&["workspace", "history", "--json", path_arg(&workspace)]);
    assert!(history.status.success(), "history failed: {history:?}");
    let history: Value = serde_json::from_slice(&history.stdout).unwrap();
    assert_eq!(history.as_array().unwrap().len(), 1);
    assert_eq!(history[0]["status"], "committed");
    assert_eq!(history[0]["import"]["format"], "csv");
    assert_eq!(history[0]["import"]["table"], "events");

    let exported = run(&[
        "workspace",
        "export",
        "--json",
        "--format",
        "jsonl",
        path_arg(&workspace),
        "events",
        path_arg(&export_path),
    ]);
    assert!(exported.status.success(), "export failed: {exported:?}");
    let exported: Value = serde_json::from_slice(&exported.stdout).unwrap();
    assert_eq!(exported["operation"], "export");
    assert_eq!(exported["format"], "jsonl");
    assert_eq!(exported["table"], "events");
    assert_eq!(exported["rows"], 1);
    assert_eq!(exported["bytes"], fs::metadata(&export_path).unwrap().len());

    let ambiguous = run(&[
        "workspace",
        "export",
        "--json",
        "--format",
        "jsonl",
        path_arg(&workspace),
        "events",
        "-",
    ]);
    assert!(!ambiguous.status.success());
    assert!(
        String::from_utf8_lossy(&ambiguous.stderr)
            .contains("--json cannot be combined with stdout export")
    );
}

#[test]
fn refuses_export_paths_that_alias_workspace_metadata() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("events.csv");
    fs::write(&source, "id,name\n1,Ada\n").unwrap();
    assert!(run(&["init", path_arg(&workspace)]).status.success());
    assert!(
        run(&[
            "workspace",
            "import",
            "--table",
            "events",
            path_arg(&workspace),
            path_arg(&source),
        ])
        .status
        .success()
    );

    for protected_file in [
        "data.basalt",
        "workspace.json",
        ".workspace.lock",
        "data.basalt.wal",
        "data.basalt.lock",
        "data.basalt.tmp",
    ] {
        let alias = workspace.join("..").join("workspace").join(protected_file);
        let output = run(&[
            "workspace",
            "export",
            "--format",
            "csv",
            path_arg(&workspace),
            "events",
            path_arg(&alias),
        ]);
        assert!(
            !output.status.success(),
            "export unexpectedly succeeded: {output:?}"
        );
        assert!(
            String::from_utf8_lossy(&output.stderr)
                .contains("refusing to overwrite workspace metadata, locks, database, or history"),
            "unexpected export error: {output:?}"
        );
    }

    let history_output = workspace.join("history").join("changes").join("backup.csv");
    let output = run(&[
        "workspace",
        "export",
        "--format",
        "csv",
        path_arg(&workspace),
        "events",
        path_arg(&history_output),
    ]);
    assert!(
        !output.status.success(),
        "history export unexpectedly succeeded: {output:?}"
    );

    assert_eq!(inspect(&workspace)["tables"][0]["rows"], 1);
}

#[test]
fn rejects_preview_with_too_many_mutating_statements() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    assert!(run(&["init", path_arg(&workspace)]).status.success());
    let sql = (0..33)
        .map(|index| format!("CREATE TABLE table_{index} (id INTEGER)"))
        .collect::<Vec<_>>()
        .join("; ");

    let output = run(&["workspace", "preview", path_arg(&workspace), sql.as_str()]);
    assert!(
        !output.status.success(),
        "preview unexpectedly succeeded: {output:?}"
    );
    assert!(
        String::from_utf8_lossy(&output.stderr)
            .contains("preview accepts at most 32 mutating statements"),
        "unexpected preview error: {output:?}"
    );
}

#[test]
fn json_import_and_sql_roundtrip_are_deterministic() {
    let temp = TempDir::new();
    let workspace = temp.path().join("json-workspace");
    let source = temp.path().join("records.json");
    fs::write(
        &source,
        r#"[{"name":"Ada","active":true,"score":3.5},{"name":"Grace","score":9}]"#,
    )
    .unwrap();
    assert!(
        run(&["workspace", "init", path_arg(&workspace)])
            .status
            .success()
    );
    assert!(
        run(&[
            "workspace",
            "import",
            "--table",
            "records",
            path_arg(&workspace),
            path_arg(&source),
        ])
        .status
        .success()
    );

    let dump = temp.path().join("records.sql");
    assert!(
        run(&[
            "workspace",
            "export",
            "--format",
            "sql",
            path_arg(&workspace),
            "records",
            path_arg(&dump),
        ])
        .status
        .success()
    );
    let first_dump = fs::read(&dump).unwrap();
    assert_eq!(first_dump, fs::read(&dump).unwrap());

    let restored = temp.path().join("restored");
    assert!(
        run(&["workspace", "init", path_arg(&restored)])
            .status
            .success()
    );
    let output = run(&["workspace", "import", path_arg(&restored), path_arg(&dump)]);
    assert!(output.status.success(), "SQL import failed: {output:?}");
    let report = inspect(&restored);
    assert_eq!(report["tables"][0]["name"], "records");
    assert_eq!(report["tables"][0]["rows"], 2);
}

#[test]
fn failed_sql_import_does_not_leave_a_partial_table() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("broken.sql");
    fs::write(
        &source,
        "CREATE TABLE broken (id INTEGER); INSERT INTO broken VALUES (1); INSERT INTO broken VALUES ('bad');",
    )
    .unwrap();
    assert!(
        run(&["workspace", "init", path_arg(&workspace)])
            .status
            .success()
    );
    let output = run(&[
        "workspace",
        "import",
        path_arg(&workspace),
        path_arg(&source),
    ]);
    assert!(!output.status.success());
    assert!(inspect(&workspace)["tables"].as_array().unwrap().is_empty());
    let history = run(&["workspace", "history", "--json", path_arg(&workspace)]);
    assert!(history.status.success(), "history failed: {history:?}");
    let history: Value = serde_json::from_slice(&history.stdout).unwrap();
    assert_eq!(history.as_array().unwrap().len(), 1);
    assert_eq!(history[0]["status"], "failed");
    assert_eq!(history[0]["import"]["format"], "sql");
}

#[test]
fn cli_imports_are_recoverable_for_row_data_and_sql_dumps() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let csv_source = temp.path().join("users.csv");
    let sql_source = temp.path().join("events.sql");
    fs::write(&csv_source, "id,name\n1,Ada\n").unwrap();
    fs::write(
        &sql_source,
        "CREATE TABLE events (id INTEGER); INSERT INTO events VALUES (1);",
    )
    .unwrap();
    assert!(
        run(&["workspace", "init", path_arg(&workspace)])
            .status
            .success()
    );

    let csv_import = run(&[
        "workspace",
        "import",
        "--json",
        "--table",
        "users",
        path_arg(&workspace),
        path_arg(&csv_source),
    ]);
    assert!(
        csv_import.status.success(),
        "CSV import failed: {csv_import:?}"
    );
    let csv_import: Value = serde_json::from_slice(&csv_import.stdout).unwrap();
    let csv_change_id = csv_import["change_id"].as_str().unwrap();
    let csv_undo = run(&[
        "workspace",
        "undo",
        "--json",
        path_arg(&workspace),
        csv_change_id,
    ]);
    assert!(csv_undo.status.success(), "CSV undo failed: {csv_undo:?}");
    assert!(inspect(&workspace)["tables"].as_array().unwrap().is_empty());

    let sql_import = run(&[
        "workspace",
        "import",
        "--json",
        path_arg(&workspace),
        path_arg(&sql_source),
    ]);
    assert!(
        sql_import.status.success(),
        "SQL import failed: {sql_import:?}"
    );
    let sql_import: Value = serde_json::from_slice(&sql_import.stdout).unwrap();
    assert!(sql_import["table"].is_null());
    assert_eq!(sql_import["summary"], "2 statements from SQL");
    let sql_change_id = sql_import["change_id"].as_str().unwrap();
    assert!(
        run(&[
            "workspace",
            "undo",
            "--json",
            path_arg(&workspace),
            sql_change_id,
        ])
        .status
        .success()
    );
    assert!(inspect(&workspace)["tables"].as_array().unwrap().is_empty());
}

#[test]
fn previews_applies_diffs_and_undoes_one_change() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("users.csv");
    fs::write(&source, "id,name\n1,Ada\n").unwrap();
    assert!(run(&["init", path_arg(&workspace)]).status.success());
    assert!(
        run(&[
            "workspace",
            "import",
            "--table",
            "users",
            path_arg(&workspace),
            path_arg(&source),
        ])
        .status
        .success()
    );

    let preview = run(&[
        "workspace",
        "preview",
        "--json",
        path_arg(&workspace),
        "UPDATE users SET name = 'Grace' WHERE id = 1",
    ]);
    assert!(preview.status.success(), "preview failed: {preview:?}");
    let preview: Value = serde_json::from_slice(&preview.stdout).unwrap();
    assert_eq!(preview["mutating_statements"], 1);
    assert_eq!(preview["statements"][0]["rows_affected"], 1);
    let plan_id = preview["plan_id"].as_str().unwrap();
    assert_eq!(
        preview["sql"],
        "UPDATE users SET name = 'Grace' WHERE id = 1"
    );

    let loaded_plan = run(&["workspace", "plan", "--json", path_arg(&workspace), plan_id]);
    assert!(
        loaded_plan.status.success(),
        "plan lookup failed: {loaded_plan:?}"
    );
    let loaded_plan: Value = serde_json::from_slice(&loaded_plan.stdout).unwrap();
    assert_eq!(loaded_plan["plan_id"], plan_id);
    assert_eq!(loaded_plan["statements"][0]["rows_affected"], 1);
    assert_eq!(
        loaded_plan["sql"],
        "UPDATE users SET name = 'Grace' WHERE id = 1"
    );
    let apply = run(&[
        "workspace",
        "apply",
        "--json",
        path_arg(&workspace),
        plan_id,
    ]);
    assert!(apply.status.success(), "apply failed: {apply:?}");
    let apply: Value = serde_json::from_slice(&apply.stdout).unwrap();
    let change_id = apply["change_id"].as_str().unwrap();
    let apply_generation = apply["generation"].as_u64().unwrap();

    let retried_apply = run(&[
        "workspace",
        "apply",
        "--json",
        path_arg(&workspace),
        plan_id,
    ]);
    assert!(
        retried_apply.status.success(),
        "retrying apply failed: {retried_apply:?}"
    );
    let retried_apply: Value = serde_json::from_slice(&retried_apply.stdout).unwrap();
    assert_eq!(retried_apply["change_id"], change_id);

    let query = run(&[
        "workspace",
        "query",
        "--json",
        path_arg(&workspace),
        "SELECT name FROM users",
    ]);
    let query: Value = serde_json::from_slice(&query.stdout).unwrap();
    assert_eq!(query["rows"][0][0], "Grace");

    let diff = run(&[
        "workspace",
        "diff",
        "--json",
        path_arg(&workspace),
        change_id,
    ]);
    assert!(diff.status.success(), "diff failed: {diff:?}");
    let diff: Value = serde_json::from_slice(&diff.stdout).unwrap();
    assert_eq!(
        diff["precision"],
        "table schema and row-multiset comparison"
    );
    assert_eq!(diff["tables"][0]["data_changed"], true);
    assert_eq!(diff["tables"][0]["added_rows"], 1);
    assert_eq!(diff["tables"][0]["removed_rows"], 1);

    let undo = run(&[
        "workspace",
        "undo",
        "--json",
        path_arg(&workspace),
        change_id,
    ]);
    assert!(undo.status.success(), "undo failed: {undo:?}");
    let undo: Value = serde_json::from_slice(&undo.stdout).unwrap();
    assert_eq!(undo["undone_change_id"], change_id);
    assert!(undo["generation"].as_u64().unwrap() > apply_generation);

    let retried_undo = run(&[
        "workspace",
        "undo",
        "--json",
        path_arg(&workspace),
        change_id,
    ]);
    assert!(
        retried_undo.status.success(),
        "retrying undo failed: {retried_undo:?}"
    );
    let retried_undo: Value = serde_json::from_slice(&retried_undo.stdout).unwrap();
    assert_eq!(retried_undo["undone_change_id"], change_id);

    let query = run(&[
        "workspace",
        "query",
        "--json",
        path_arg(&workspace),
        "SELECT name FROM users",
    ]);
    let query: Value = serde_json::from_slice(&query.stdout).unwrap();
    assert_eq!(query["rows"][0][0], "Ada");
}

#[test]
fn stale_plans_and_non_latest_undo_are_rejected() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("users.csv");
    fs::write(&source, "id,name\n1,Ada\n").unwrap();
    assert!(
        run(&["workspace", "init", path_arg(&workspace)])
            .status
            .success()
    );
    assert!(
        run(&[
            "workspace",
            "import",
            "--table",
            "users",
            path_arg(&workspace),
            path_arg(&source),
        ])
        .status
        .success()
    );
    let first = run(&[
        "workspace",
        "preview",
        "--json",
        path_arg(&workspace),
        "UPDATE users SET name = 'Grace' WHERE id = 1",
    ]);
    let first: Value = serde_json::from_slice(&first.stdout).unwrap();
    let first_plan = first["plan_id"].as_str().unwrap();
    let second = run(&[
        "workspace",
        "preview",
        "--json",
        path_arg(&workspace),
        "UPDATE users SET name = 'Linus' WHERE id = 1",
    ]);
    let second: Value = serde_json::from_slice(&second.stdout).unwrap();
    let second_plan = second["plan_id"].as_str().unwrap();
    let second_apply = run(&[
        "workspace",
        "apply",
        "--json",
        path_arg(&workspace),
        second_plan,
    ]);
    assert!(second_apply.status.success());
    let second_apply: Value = serde_json::from_slice(&second_apply.stdout).unwrap();
    let second_change = second_apply["change_id"].as_str().unwrap();

    let stale = run(&["workspace", "apply", path_arg(&workspace), first_plan]);
    assert!(!stale.status.success());
    let third = run(&[
        "workspace",
        "preview",
        "--json",
        path_arg(&workspace),
        "UPDATE users SET name = 'Alan' WHERE id = 1",
    ]);
    let third: Value = serde_json::from_slice(&third.stdout).unwrap();
    let third_plan = third["plan_id"].as_str().unwrap();
    let third_apply = run(&[
        "workspace",
        "apply",
        "--json",
        path_arg(&workspace),
        third_plan,
    ]);
    assert!(third_apply.status.success());
    let third_apply: Value = serde_json::from_slice(&third_apply.stdout).unwrap();
    let third_change = third_apply["change_id"].as_str().unwrap();
    let old_undo = run(&["workspace", "undo", path_arg(&workspace), second_change]);
    assert!(!old_undo.status.success());
    let latest_undo = run(&[
        "workspace",
        "undo",
        "--json",
        path_arg(&workspace),
        third_change,
    ]);
    assert!(latest_undo.status.success(), "undo failed: {latest_undo:?}");

    let moved_preview = run(&[
        "workspace",
        "preview",
        "--json",
        path_arg(&workspace),
        "UPDATE users SET name = 'Ada' WHERE id = 1",
    ]);
    let moved_preview: Value = serde_json::from_slice(&moved_preview.stdout).unwrap();
    let moved_plan = moved_preview["plan_id"].as_str().unwrap();
    let moved_apply = run(&[
        "workspace",
        "apply",
        "--json",
        path_arg(&workspace),
        moved_plan,
    ]);
    assert!(moved_apply.status.success());

    let replay = run(&["workspace", "apply", path_arg(&workspace), second_plan]);
    assert!(!replay.status.success());
    assert!(
        String::from_utf8_lossy(&replay.stderr).contains("will not be replayed"),
        "unexpected replay error: {replay:?}"
    );
}

#[test]
fn interrupted_apply_and_undo_are_reconciled_after_restart() {
    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let source = temp.path().join("users.csv");
    fs::write(&source, "id,name\n1,Ada\n").unwrap();
    assert!(
        run(&["workspace", "init", path_arg(&workspace)])
            .status
            .success()
    );
    assert!(
        run(&[
            "workspace",
            "import",
            "--table",
            "users",
            path_arg(&workspace),
            path_arg(&source),
        ])
        .status
        .success()
    );
    let preview = run(&[
        "workspace",
        "preview",
        "--json",
        path_arg(&workspace),
        "UPDATE users SET name = 'Grace' WHERE id = 1",
    ]);
    let preview: Value = serde_json::from_slice(&preview.stdout).unwrap();
    let plan_id = preview["plan_id"].as_str().unwrap();

    let crashed_apply = run_with_env(
        &[
            "--crash-test-workspace-apply",
            path_arg(&workspace),
            plan_id,
        ],
        "BASALT_CRASH_TEST_AFTER_APPLY_CHECKPOINT",
    );
    assert!(!crashed_apply.status.success());
    let history = run(&["workspace", "history", "--json", path_arg(&workspace)]);
    assert!(history.status.success(), "history failed: {history:?}");
    let history: Value = serde_json::from_slice(&history.stdout).unwrap();
    let recovered_apply = history
        .as_array()
        .unwrap()
        .iter()
        .find(|entry| entry["kind"] == "apply" && entry["status"] == "recovered")
        .expect("crashed apply should be recovered");
    let change_id = recovered_apply["change_id"].as_str().unwrap();

    let crashed_undo = run_with_env(
        &[
            "--crash-test-workspace-undo",
            path_arg(&workspace),
            change_id,
        ],
        "BASALT_CRASH_TEST_AFTER_UNDO_RESTORE",
    );
    assert!(!crashed_undo.status.success());
    let history = run(&["workspace", "history", "--json", path_arg(&workspace)]);
    let history: Value = serde_json::from_slice(&history.stdout).unwrap();
    assert!(
        history
            .as_array()
            .unwrap()
            .iter()
            .any(|entry| entry["kind"] == "undo" && entry["status"] == "recovered")
    );
    let query = run(&[
        "workspace",
        "query",
        "--json",
        path_arg(&workspace),
        "SELECT name FROM users",
    ]);
    let query: Value = serde_json::from_slice(&query.stdout).unwrap();
    assert_eq!(query["rows"][0][0], "Ada");
}

#[cfg(unix)]
#[test]
fn rejects_symlinked_history_directory() {
    use std::os::unix::fs::symlink;

    let temp = TempDir::new();
    let workspace = temp.path().join("workspace");
    let outside = temp.path().join("outside");
    fs::create_dir(&outside).unwrap();
    assert!(run(&["init", path_arg(&workspace)]).status.success());
    symlink(&outside, workspace.join("history")).unwrap();

    let output = run(&["workspace", "history", path_arg(&workspace)]);
    assert!(!output.status.success());
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("symbolic link"),
        "unexpected error: {output:?}"
    );
    assert!(fs::read_dir(&outside).unwrap().next().is_none());
}

fn unique_suffix() -> u128 {
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let sequence = TEMP_DIR_SEQUENCE.fetch_add(1, Ordering::Relaxed) as u128;
    timestamp * 1_000_000 + sequence
}