neovm-core 0.0.1

Core runtime structures for NeoVM
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
use super::super::eval::Context;
use super::super::intern::intern;
use super::*;

// -- ThreadManager unit tests -------------------------------------------

#[test]
fn thread_manager_new_has_main_thread() {
    crate::test_utils::init_test_tracing();
    let mgr = ThreadManager::new();
    assert!(mgr.is_thread(0));
    assert_eq!(mgr.current_thread_id(), 0);
    assert!(mgr.thread_alive_p(0));
    assert_eq!(mgr.thread_name(0), None);
    assert_eq!(mgr.thread_buffer_disposition(0), Some(Value::NIL));
}

#[test]
fn create_thread_assigns_unique_ids() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id1 = mgr.create_thread(Value::NIL, Some("t1".into()));
    let id2 = mgr.create_thread(Value::NIL, Some("t2".into()));
    assert_ne!(id1, id2);
    assert!(mgr.is_thread(id1));
    assert!(mgr.is_thread(id2));
    assert!(!mgr.is_thread(999));
}

#[test]
fn thread_lifecycle_created_running_finished() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_thread(Value::NIL, None);
    assert_eq!(mgr.get_thread(id).unwrap().status, ThreadStatus::Created);
    assert!(mgr.thread_alive_p(id));

    mgr.start_thread(id);
    assert_eq!(mgr.get_thread(id).unwrap().status, ThreadStatus::Running);
    assert!(mgr.thread_alive_p(id));

    mgr.finish_thread(id, Value::fixnum(42));
    assert_eq!(mgr.get_thread(id).unwrap().status, ThreadStatus::Finished);
    assert!(!mgr.thread_alive_p(id));
    assert_eq!(mgr.thread_result(id).as_int(), Some(42));
}

#[test]
fn thread_signal_records_error() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_thread(Value::NIL, None);
    mgr.start_thread(id);
    mgr.signal_thread(id, Value::symbol("test-error"));
    assert_eq!(mgr.get_thread(id).unwrap().status, ThreadStatus::Signaled);
    assert!(!mgr.thread_alive_p(id));
}

#[test]
fn all_thread_ids_includes_main_and_created() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_thread(Value::NIL, None);
    let ids = mgr.all_thread_ids();
    assert!(ids.len() >= 2);
    assert!(ids.contains(&0));
    assert!(ids.contains(&id));
}

#[test]
fn all_thread_ids_excludes_finished_thread() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_thread(Value::NIL, None);
    let before_join = mgr.all_thread_ids();
    assert!(before_join.contains(&id));

    mgr.finish_thread(id, Value::fixnum(1));
    let after_join = mgr.all_thread_ids();
    assert!(!after_join.contains(&id));
    assert!(after_join.contains(&0));
}

#[test]
fn thread_buffer_disposition_round_trips() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_thread(Value::NIL, None);
    assert_eq!(mgr.thread_buffer_disposition(id), Some(Value::NIL));
    assert!(mgr.set_thread_buffer_disposition(id, Value::symbol("silently")));
    assert_eq!(
        mgr.thread_buffer_disposition(id),
        Some(Value::symbol("silently"))
    );
}

#[test]
fn thread_manager_tracks_current_buffer_and_blocker_state() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_thread(Value::NIL, None);
    let buffer_id = crate::buffer::BufferId(99);
    assert!(mgr.set_thread_current_buffer(id, Some(buffer_id)));
    assert_eq!(mgr.thread_current_buffer(id), Some(buffer_id));
    assert!(mgr.set_thread_blocker(id, Value::symbol("test-blocker")));
    assert_eq!(mgr.thread_blocker(id), Some(Value::symbol("test-blocker")));
    assert!(mgr.clear_thread_blocker(id));
    assert_eq!(mgr.thread_blocker(id), Some(Value::NIL));
}

#[test]
fn last_error_get_and_cleanup() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    mgr.record_last_error(Value::symbol("oops"));

    let err = mgr.last_error(false);
    assert!(err.is_truthy());
    // Still there after no cleanup
    let err2 = mgr.last_error(true);
    assert!(err2.is_truthy());
    // Now gone
    let err3 = mgr.last_error(false);
    assert!(err3.is_nil());
}

// -- Mutex unit tests ---------------------------------------------------

#[test]
fn mutex_create_and_lookup() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_mutex(Some("my-lock".into()));
    assert!(mgr.is_mutex(id));
    assert_eq!(mgr.mutex_name(id), Some("my-lock"));
    assert!(!mgr.is_mutex(999));
}

#[test]
fn mutex_lock_unlock_cycle() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_mutex(None);
    assert!(mgr.mutex_lock(id));
    assert!(mgr.mutex_unlock(id));
    // Unlocking when not locked is fine
    assert!(mgr.mutex_unlock(id));
}

#[test]
fn mutex_recursive_lock() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let id = mgr.create_mutex(None);
    assert!(mgr.mutex_lock(id));
    assert!(mgr.mutex_lock(id));
    // lock_count is 2
    assert!(mgr.mutex_unlock(id));
    // Still locked (count=1)
    let m = mgr.mutexes.get(&id).unwrap();
    assert!(m.owner.is_some());
    assert!(mgr.mutex_unlock(id));
    // Now fully unlocked
    let m = mgr.mutexes.get(&id).unwrap();
    assert!(m.owner.is_none());
}

// -- Condition variable unit tests --------------------------------------

#[test]
fn condition_variable_create() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let mx = mgr.create_mutex(None);
    let cv = mgr.create_condition_variable(mx, Some("cv1".into()));
    assert!(cv.is_some());
    let cv_id = cv.unwrap();
    assert!(mgr.is_condition_variable(cv_id));
    assert_eq!(mgr.condition_variable_name(cv_id), Some("cv1"));
    assert_eq!(mgr.condition_variable_mutex(cv_id), Some(mx));
}

#[test]
fn condition_variable_requires_valid_mutex() {
    crate::test_utils::init_test_tracing();
    let mut mgr = ThreadManager::new();
    let cv = mgr.create_condition_variable(999, None);
    assert!(cv.is_none());
}

// -- Builtin-level tests -----------------------------------------------

#[test]
fn test_builtin_make_thread_runs_function() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    // Define a simple function that returns 42
    eval.set_variable("thread-test-result", Value::NIL);
    eval.set_function(
        "thread-test-fn",
        Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![].into(), // empty body → nil
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        }),
    );

    let result = builtin_make_thread(
        &mut eval,
        vec![Value::symbol("thread-test-fn"), Value::string("worker")],
    );
    assert!(result.is_ok());
    let tid = result.unwrap();
    assert_eq!(tagged_object_id(&tid, "thread"), Some(1));
}

#[test]
fn test_builtin_make_thread_accepts_buffer_disposition_arg() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_make_thread(
        &mut eval,
        vec![
            Value::make_lambda(super::super::value::LambdaData {
                params: super::super::value::LambdaParams::simple(vec![]),
                body: vec![].into(),
                env: None,
                docstring: None,
                doc_form: None,
                interactive: None,
            }),
            Value::string("worker"),
            Value::symbol("silently"),
        ],
    );
    assert!(result.is_ok());
    let thread = result.unwrap();
    let thread_id = tagged_object_id(&thread, "thread").unwrap();
    assert_eq!(thread_id, 1);
    assert_eq!(
        eval.threads.thread_buffer_disposition(thread_id),
        Some(Value::symbol("silently"))
    );
}

#[test]
fn test_builtin_make_thread_rejects_more_than_three_args() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_make_thread(
        &mut eval,
        vec![
            Value::make_lambda(super::super::value::LambdaData {
                params: super::super::value::LambdaParams::simple(vec![]),
                body: vec![].into(),
                env: None,
                docstring: None,
                doc_form: None,
                interactive: None,
            }),
            Value::NIL,
            Value::NIL,
            Value::NIL,
        ],
    );
    assert!(matches!(
        result,
        Err(Flow::Signal(sig)) if sig.symbol_name() == "wrong-number-of-arguments"
    ));
}

#[test]
fn test_builtin_threadp() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();

    let r = builtin_threadp(&mut eval, vec![current]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_truthy());

    let r = builtin_threadp(&mut eval, vec![Value::fixnum(0)]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let r = builtin_threadp(&mut eval, vec![Value::string("nope")]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let fake = Value::cons(Value::symbol("thread"), Value::fixnum(999));
    let r = builtin_threadp(&mut eval, vec![fake]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let forged_main = Value::cons(Value::symbol("thread"), Value::fixnum(0));
    let r = builtin_threadp(&mut eval, vec![forged_main]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());
}

#[test]
fn test_builtin_current_thread() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_current_thread(&mut eval, vec![]);
    assert!(result.is_ok());
    assert_eq!(tagged_object_id(&result.unwrap(), "thread"), Some(0));
}

#[test]
fn test_builtin_current_thread_returns_stable_handle_identity() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let first = builtin_current_thread(&mut eval, vec![]).unwrap();
    let second = builtin_current_thread(&mut eval, vec![]).unwrap();
    assert!(eq_value(&first, &second));
}

#[test]
fn test_main_thread_variable_matches_current_thread() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();
    let main_thread = eval.obarray.symbol_value("main-thread").copied().unwrap();
    assert!(eq_value(&current, &main_thread));
}

#[test]
fn test_main_thread_tracks_current_buffer() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let original = eval.buffers.current_buffer_id().expect("current buffer");
    assert_eq!(eval.threads.thread_current_buffer(0), Some(original));
    let other = eval.buffers.create_buffer("thread-main-other");
    eval.switch_current_buffer(other)
        .expect("switch current buffer");
    assert_eq!(eval.threads.thread_current_buffer(0), Some(other));
}

#[test]
fn test_builtin_thread_yield() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_thread_yield(&mut eval, vec![]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());
}

#[test]
fn test_builtin_thread_name_main() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();
    let result = builtin_thread_name(&mut eval, vec![current]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());
}

#[test]
fn test_builtin_thread_live_p_main() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();
    let result = builtin_thread_live_p(&mut eval, vec![current]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_truthy());
}

#[test]
fn test_builtin_all_threads_includes_main() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_all_threads(&mut eval, vec![]);
    assert!(result.is_ok());
    let list = super::super::value::list_to_vec(&result.unwrap()).unwrap();
    assert!(!list.is_empty());
    assert!(
        list.iter()
            .any(|v| tagged_object_id(v, "thread") == Some(0))
    );
}

#[test]
fn test_builtin_all_threads_excludes_finished_worker() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let worker = builtin_make_thread(
        &mut eval,
        vec![Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![].into(),
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        })],
    )
    .unwrap();
    let result = builtin_all_threads(&mut eval, vec![]).unwrap();
    let list = super::super::value::list_to_vec(&result).unwrap();
    assert!(!list.iter().any(|value| eq_value(value, &worker)));
}

#[test]
fn test_builtin_thread_join_finished() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    // Create and run a thread
    let tid_val = builtin_make_thread(
        &mut eval,
        vec![Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![].into(),
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        })],
    )
    .unwrap();

    // Join it
    let result = builtin_thread_join(&mut eval, vec![tid_val]);
    assert!(result.is_ok());
}

#[test]
fn test_builtin_thread_join_current_thread_errors() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();
    let result = builtin_thread_join(&mut eval, vec![current]);
    match result {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "error");
            assert_eq!(sig.data.len(), 1);
            assert_eq!(sig.data[0].as_str(), Some("Cannot join current thread"));
        }
        other => panic!("expected error signal for self-join, got {other:?}"),
    }
}

#[test]
fn test_builtin_thread_signal_non_current_is_noop() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let tid_val = builtin_make_thread(
        &mut eval,
        vec![Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![].into(),
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        })],
    )
    .unwrap();

    let result = builtin_thread_signal(
        &mut eval,
        vec![tid_val, Value::symbol("test-error"), Value::string("oops")],
    );
    assert!(result.is_ok());

    // Signaling an already-finished non-current thread does not set global last-error.
    let err = builtin_thread_last_error(&mut eval, vec![]);
    assert!(err.is_ok());
    assert!(err.unwrap().is_nil());
}

#[test]
fn test_builtin_thread_signal_current_thread_raises() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();
    let result = builtin_thread_signal(
        &mut eval,
        vec![current, Value::symbol("foo"), Value::fixnum(1)],
    );
    match result {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "foo");
            assert_eq!(sig.raw_data, Some(Value::fixnum(1)));
        }
        other => panic!("expected signal from thread-signal current thread, got {other:?}"),
    }
}

#[test]
fn test_builtin_thread_last_error_cleanup() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    eval.threads
        .record_last_error(Value::list(vec![Value::symbol("err"), Value::fixnum(1)]));

    let e1 = builtin_thread_last_error(&mut eval, vec![Value::NIL]).unwrap();
    assert!(e1.is_truthy());

    // Cleanup
    let e2 = builtin_thread_last_error(&mut eval, vec![Value::T]).unwrap();
    assert!(e2.is_truthy());

    // Should be gone now
    let e3 = builtin_thread_last_error(&mut eval, vec![]).unwrap();
    assert!(e3.is_nil());
}

#[test]
fn test_builtin_thread_blocker_reads_runtime_state() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let current = builtin_current_thread(&mut eval, vec![]).unwrap();
    eval.threads
        .set_thread_blocker(0, Value::symbol("vm-blocked"));
    assert_eq!(
        builtin_thread_blocker(&mut eval, vec![current]).unwrap(),
        Value::symbol("vm-blocked")
    );
    eval.threads.clear_thread_blocker(0);
    assert_eq!(
        builtin_thread_blocker(&mut eval, vec![current]).unwrap(),
        Value::NIL
    );
}

#[test]
fn test_builtin_thread_buffer_disposition_round_trips() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let worker = builtin_make_thread(
        &mut eval,
        vec![Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![].into(),
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        })],
    )
    .unwrap();

    assert_eq!(
        builtin_thread_buffer_disposition(&mut eval, vec![worker]).unwrap(),
        Value::NIL
    );
    assert_eq!(
        builtin_thread_set_buffer_disposition(&mut eval, vec![worker, Value::T]).unwrap(),
        Value::T
    );
    assert_eq!(
        builtin_thread_buffer_disposition(&mut eval, vec![worker]).unwrap(),
        Value::T
    );
}

#[test]
fn test_builtin_thread_set_buffer_disposition_rejects_non_nil_main_thread_value() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let main_thread = builtin_current_thread(&mut eval, vec![]).unwrap();
    match builtin_thread_set_buffer_disposition(&mut eval, vec![main_thread, Value::T]) {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "wrong-type-argument");
            assert_eq!(sig.data, vec![Value::symbol("null"), Value::T]);
        }
        other => panic!("expected wrong-type-argument signal, got {other:?}"),
    }
}

#[test]
fn test_builtin_make_thread_preserves_caller_current_buffer() {
    crate::test_utils::init_test_tracing();
    use super::super::expr::Expr;

    let mut eval = Context::new();
    let main_buffer = eval.buffers.current_buffer_id().expect("current buffer");
    let worker_buffer = eval.buffers.create_buffer("thread-worker-buffer");

    eval.set_function(
        "thread-switch-buffer",
        Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![
                Expr::List(vec![
                    Expr::Symbol(intern("set-buffer")),
                    Expr::OpaqueValueRef(
                        super::super::eval::OPAQUE_POOL.with(|pool| {
                            pool.borrow_mut().insert(Value::make_buffer(worker_buffer))
                        }),
                    ),
                ]),
                Expr::List(vec![Expr::Symbol(intern("current-buffer"))]),
            ]
            .into(),
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        }),
    );

    let thread = builtin_make_thread(&mut eval, vec![Value::symbol("thread-switch-buffer")])
        .expect("make-thread");
    let thread_id = tagged_object_id(&thread, "thread").expect("thread id");
    let joined = builtin_thread_join(&mut eval, vec![thread]).expect("thread-join");

    assert_eq!(joined, Value::make_buffer(worker_buffer));
    assert_eq!(eval.buffers.current_buffer_id(), Some(main_buffer));
    assert_eq!(
        eval.threads.thread_current_buffer(thread_id),
        Some(worker_buffer)
    );
    assert_eq!(eval.threads.thread_current_buffer(0), Some(main_buffer));
}

// -- Mutex builtin tests ------------------------------------------------

#[test]
fn test_builtin_make_mutex() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_make_mutex(&mut eval, vec![Value::string("my-mutex")]);
    assert!(result.is_ok());
    let mx = result.unwrap();
    assert_eq!(tagged_object_id(&mx, "mutex"), Some(1));
}

#[test]
fn test_builtin_mutexp() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();

    let r = builtin_mutexp(&mut eval, vec![mx]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_truthy());

    let r = builtin_mutexp(&mut eval, vec![Value::fixnum(1)]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let r = builtin_mutexp(&mut eval, vec![Value::NIL]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let forged = Value::cons(Value::symbol("mutex"), Value::fixnum(1));
    let r = builtin_mutexp(&mut eval, vec![forged]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());
}

#[test]
fn test_builtin_mutex_name() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![Value::string("named-mx")]).unwrap();
    let result = builtin_mutex_name(&mut eval, vec![mx]);
    assert!(result.is_ok());
    assert_eq!(result.unwrap().as_str(), Some("named-mx"));
}

#[test]
fn test_builtin_mutex_lock_unlock() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let lock_result = builtin_mutex_lock(&mut eval, vec![mx]);
    assert!(lock_result.is_ok());
    let unlock_result = builtin_mutex_unlock(&mut eval, vec![mx]);
    assert!(unlock_result.is_ok());
}

// -- Condition variable builtin tests -----------------------------------

#[test]
fn test_builtin_make_condition_variable() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let result = builtin_make_condition_variable(&mut eval, vec![mx, Value::string("my-cv")]);
    assert!(result.is_ok());
    assert!(tagged_object_id(&result.unwrap(), "condition-variable").is_some());
}

#[test]
fn test_builtin_condition_variable_p() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let cv = builtin_make_condition_variable(&mut eval, vec![mx]).unwrap();

    let r = builtin_condition_variable_p(&mut eval, vec![cv]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_truthy());

    let r = builtin_condition_variable_p(&mut eval, vec![Value::fixnum(1)]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let r = builtin_condition_variable_p(&mut eval, vec![Value::NIL]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());

    let forged = Value::cons(Value::symbol("condition-variable"), Value::fixnum(1));
    let r = builtin_condition_variable_p(&mut eval, vec![forged]);
    assert!(r.is_ok());
    assert!(r.unwrap().is_nil());
}

#[test]
fn test_builtin_condition_name() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let unnamed = builtin_make_condition_variable(&mut eval, vec![mx]).unwrap();
    let named =
        builtin_make_condition_variable(&mut eval, vec![mx, Value::string("cv-compat-name")])
            .unwrap();

    let unnamed_name = builtin_condition_name(&mut eval, vec![unnamed]).unwrap();
    assert!(unnamed_name.is_nil());

    let named_name = builtin_condition_name(&mut eval, vec![named]).unwrap();
    assert_eq!(named_name, Value::string("cv-compat-name"));
}

#[test]
fn test_builtin_condition_mutex() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let cv = builtin_make_condition_variable(&mut eval, vec![mx]).unwrap();
    let result = builtin_condition_mutex(&mut eval, vec![cv]).unwrap();
    assert!(eq_value(&result, &mx));
}

#[test]
fn test_builtin_condition_name_wrong_type_argument() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_condition_name(&mut eval, vec![Value::NIL]);
    match result {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "wrong-type-argument");
            assert_eq!(
                sig.data,
                vec![Value::symbol("condition-variable-p"), Value::NIL]
            );
        }
        other => panic!("expected wrong-type-argument signal, got {other:?}"),
    }
}

#[test]
fn test_builtin_condition_mutex_wrong_type_argument() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_condition_mutex(&mut eval, vec![Value::fixnum(1)]);
    match result {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "wrong-type-argument");
            assert_eq!(
                sig.data,
                vec![Value::symbol("condition-variable-p"), Value::fixnum(1)]
            );
        }
        other => panic!("expected wrong-type-argument signal, got {other:?}"),
    }
}

#[test]
fn test_builtin_condition_wait_noop() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let cv = builtin_make_condition_variable(&mut eval, vec![mx]).unwrap();
    let owner_error = builtin_condition_wait(&mut eval, vec![cv]);
    assert!(owner_error.is_err());
    let lock = builtin_mutex_lock(&mut eval, vec![mx]);
    assert!(lock.is_ok());
    let result = builtin_condition_wait(&mut eval, vec![cv]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());
    let unlock = builtin_mutex_unlock(&mut eval, vec![mx]);
    assert!(unlock.is_ok());
}

#[test]
fn test_builtin_condition_notify_noop() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let cv = builtin_make_condition_variable(&mut eval, vec![mx]).unwrap();
    let owner_error = builtin_condition_notify(&mut eval, vec![cv]);
    assert!(owner_error.is_err());
    let lock = builtin_mutex_lock(&mut eval, vec![mx]);
    assert!(lock.is_ok());
    let result = builtin_condition_notify(&mut eval, vec![cv]);
    assert!(result.is_ok());
    let unlock = builtin_mutex_unlock(&mut eval, vec![mx]);
    assert!(unlock.is_ok());
}

// -- with-mutex special form tests --------------------------------------

#[test]
fn test_sf_with_mutex_executes_body() {
    crate::test_utils::init_test_tracing();
    use super::super::expr::Expr;

    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let mx_id = tagged_object_id(&mx, "mutex").unwrap();

    // Store the mutex id in a variable so the special form can look it up
    eval.set_variable("test-mx", mx);

    // (with-mutex test-mx 42)
    let tail = vec![Expr::Symbol(intern("test-mx")), Expr::Int(42)];
    let result = sf_with_mutex(&mut eval, &tail);
    assert!(result.is_ok());
    assert_eq!(result.unwrap().as_int(), Some(42));

    // Mutex should be unlocked after with-mutex completes
    let m = eval.threads.mutexes.get(&mx_id).unwrap();
    assert!(m.owner.is_none());
}

#[test]
fn test_sf_with_mutex_unlocks_on_error() {
    crate::test_utils::init_test_tracing();
    use super::super::expr::Expr;
    use crate::emacs_core::value::ValueKind;

    let mut eval = Context::new();
    let mx = builtin_make_mutex(&mut eval, vec![]).unwrap();
    let mx_id = tagged_object_id(&mx, "mutex").unwrap();
    eval.set_variable("test-mx2", mx);

    // (with-mutex test-mx2 (/ 1 0))  -- will signal arith-error
    let tail = vec![
        Expr::Symbol(intern("test-mx2")),
        Expr::List(vec![Expr::Symbol(intern("/")), Expr::Int(1), Expr::Int(0)]),
    ];
    let result = sf_with_mutex(&mut eval, &tail);
    // Should propagate the error
    assert!(result.is_err());

    // But the mutex should still be unlocked
    let m = eval.threads.mutexes.get(&mx_id).unwrap();
    assert!(m.owner.is_none());
}

#[test]
fn test_sf_with_mutex_wrong_args() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    // No arguments at all
    let result = sf_with_mutex(&mut eval, &[]);
    match result {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "wrong-number-of-arguments");
            assert_eq!(
                sig.data,
                vec![
                    Value::cons(Value::fixnum(1), Value::fixnum(1)),
                    Value::fixnum(0)
                ]
            );
        }
        other => panic!("expected wrong-number-of-arguments signal, got {other:?}"),
    }
}

// -- Arity / type error tests -------------------------------------------

#[test]
fn test_thread_yield_wrong_args() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_thread_yield(&mut eval, vec![Value::fixnum(1)]);
    assert!(result.is_err());
}

#[test]
fn test_current_thread_wrong_args() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_current_thread(&mut eval, vec![Value::fixnum(1)]);
    assert!(result.is_err());
}

#[test]
fn test_make_thread_non_callable_returns_thread_object() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let result = builtin_make_thread(&mut eval, vec![Value::fixnum(42)]).unwrap();
    let is_thread = builtin_threadp(&mut eval, vec![result]).unwrap();
    assert!(is_thread.is_truthy());
}

#[test]
fn test_make_thread_non_callable_last_error_shape() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let thread = builtin_make_thread(&mut eval, vec![Value::fixnum(1)]).unwrap();
    let result = builtin_thread_join(&mut eval, vec![thread]);
    assert!(matches!(result, Err(Flow::Signal(_))));
    let err = builtin_thread_last_error(&mut eval, vec![]).unwrap();
    assert_eq!(
        super::super::print::print_value(&err),
        "(invalid-function 1)"
    );
}

#[test]
fn test_thread_last_error_is_published_when_signaled_thread_exits() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let _ = builtin_thread_last_error(&mut eval, vec![Value::T]).unwrap();

    let thread = builtin_make_thread(&mut eval, vec![Value::symbol("car")]).unwrap();
    let published = builtin_thread_last_error(&mut eval, vec![]).unwrap();
    assert_eq!(
        super::super::print::print_value(&published),
        "(wrong-number-of-arguments #<subr car> 0)"
    );

    let join_result = builtin_thread_join(&mut eval, vec![thread]);
    assert!(matches!(join_result, Err(Flow::Signal(_))));

    let _ = builtin_thread_last_error(&mut eval, vec![Value::T]).unwrap();
    let cleared = builtin_thread_last_error(&mut eval, vec![]).unwrap();
    assert!(cleared.is_nil());
}

#[test]
fn test_thread_signal_noncurrent_thread_changes_join_outcome_without_publishing_last_error() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let _ = builtin_thread_last_error(&mut eval, vec![Value::T]).unwrap();
    let thread = builtin_make_thread(
        &mut eval,
        vec![Value::make_lambda(super::super::value::LambdaData {
            params: super::super::value::LambdaParams::simple(vec![]),
            body: vec![super::super::expr::Expr::Int(42)].into(),
            env: None,
            docstring: None,
            doc_form: None,
            interactive: None,
        })],
    )
    .unwrap();

    assert_eq!(
        builtin_thread_signal(
            &mut eval,
            vec![
                thread,
                Value::symbol("error"),
                Value::list(vec![Value::string("oops")])
            ],
        )
        .unwrap(),
        Value::NIL
    );

    let join_result = builtin_thread_join(&mut eval, vec![thread]);
    match join_result {
        Err(Flow::Signal(sig)) => {
            assert_eq!(sig.symbol_name(), "error");
            assert_eq!(sig.data, vec![Value::string("oops")]);
        }
        other => panic!("expected thread-join to re-signal stored thread error, got {other:?}"),
    }

    let last_error = builtin_thread_last_error(&mut eval, vec![]).unwrap();
    assert!(last_error.is_nil());
}

#[test]
fn test_thread_name_nonexistent() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let fake = Value::cons(Value::symbol("thread"), Value::fixnum(999));
    let result = builtin_thread_name(&mut eval, vec![fake]);
    assert!(result.is_err());
}

#[test]
fn test_mutex_lock_nonexistent() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let fake = Value::cons(Value::symbol("mutex"), Value::fixnum(999));
    let result = builtin_mutex_lock(&mut eval, vec![fake]);
    assert!(result.is_err());
}

#[test]
fn test_condition_wait_nonexistent() {
    crate::test_utils::init_test_tracing();
    let mut eval = Context::new();
    let fake = Value::cons(Value::symbol("condition-variable"), Value::fixnum(999));
    let result = builtin_condition_wait(&mut eval, vec![fake]);
    assert!(result.is_err());
}