Lines
100 %
Functions
Branches
//! Symbol completion and the interrupt latch across forms.
use super::*;
use crate::ctx::ScriptLimits;
#[test]
fn completions_match_case_insensitively_and_skip_internal() {
let session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
// Lower-case input matches the upper-case folded symbol; the canonical
// upper-case name is returned.
let defuns = session.completions("def");
assert!(defuns.contains(&"DEFUN".to_string()), "got: {defuns:?}");
assert!(
defuns.iter().all(|n| n.starts_with("DEF")),
"got: {defuns:?}"
);
// Sorted; no internal `$`/`__` or `(SETF …)` place names.
let all = session.completions("");
assert!(all.windows(2).all(|w| w[0] <= w[1]), "must be sorted");
all.iter()
.all(|n| !n.starts_with('$') && !n.starts_with("__") && !n.starts_with("(SETF")),
"internal/setf symbols must be filtered: {all:?}"
}
#[tokio::test]
async fn completions_include_a_user_defined_symbol() {
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
let _ = session
.handle_form("(:id 1 :form (defun my-helper (x) x))")
.await;
// The defun's name is folded to upper-case; a lower-case prefix finds it.
let hits = session.completions("my-");
assert!(hits.contains(&"MY-HELPER".to_string()), "got: {hits:?}");
fn completions_unknown_prefix_is_empty() {
assert!(session.completions("zzz-no-such-symbol-").is_empty());
async fn interrupt_before_form_short_circuits_with_interrupted() {
let handle = session.interrupt_handle();
handle.interrupt();
let response = session.handle_form("(:id 11 :form (+ 1 2))").await;
assert!(response.contains(":id 11"));
assert!(response.contains(":code interrupted"));
async fn coalesced_interrupts_abort_one_form_each_in_order() {
// Two `C-g`s observed together before a form cancel THAT form and are
// coalesced (they do not pre-arm cancellation of a later one); a fresh,
// distinct `C-g` later still cancels the next form. This pins the REPL
// contract against both a "lost interrupt" and an "over-eager next-form
// abort" regression.
handle.interrupt(); // two presses before the form
let first = session.handle_form("(:id 60 :form (+ 1 2))").await;
first.contains(":code interrupted"),
"first form must abort: {first}"
// Both presses were coalesced into the one abort — the next form is clean.
let second = session.handle_form("(:id 61 :form (+ 1 2))").await;
assert_eq!(
second, "(:id 61 :value 3)",
"next form coalesced-poisoned: {second}"
// A fresh, distinct press still aborts the following form.
let third = session.handle_form("(:id 62 :form (+ 1 2))").await;
third.contains(":code interrupted"),
"a distinct later interrupt must still abort: {third}"
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn inflight_interrupt_does_not_poison_next_form() {
// The SLYNK reader arms the interrupt signal and then the epoch bump on
// `(:emacs-interrupt)` (mod.rs); that order is required. When it lands
// mid-eval the epoch trap cancels the running call; that interrupt
// generation must then be acked so the SAME `C-g` can't also abort the
// next request.
//
// The interrupt is sent only once the form's own PRINT is visible in the
// shared output, so it provably lands while Wasm is running; unbounded
// fuel and a huge loop keep the form from finishing or failing first.
let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
fuel: u64::MAX,
..ScriptLimits::default()
});
let mut session = Session::new(ctx).expect("Session::new");
let bumper = session.epoch_bumper();
let interrupt = session.interrupt_handle();
let output = Arc::clone(&session.output);
let cancel_task = tokio::spawn(async move {
let started = Instant::now();
while output.lock().map(|out| out.is_empty()).unwrap_or(true)
&& started.elapsed() < Duration::from_secs(60)
{
tokio::time::sleep(Duration::from_millis(1)).await;
interrupt.interrupt();
bumper.bump();
let cancelled = tokio::time::timeout(
std::time::Duration::from_secs(60),
session.handle_form(
"(:id 30 :form (let ((n 1073741823)) (print \"go\") (do ((i 0 (+ i 1))) ((>= i n) i))))",
),
)
.await
.expect("epoch bump did not cancel the eval within 60s");
cancel_task.await.expect("cancel task");
cancelled.contains(":code interrupted") && !cancelled.contains("before start"),
"in-flight eval should have been cancelled by the epoch trap: {cancelled}"
let next = session.handle_form("(:id 31 :form (+ 1 2))").await;
assert_eq!(next, "(:id 31 :value 3)", "next form was poisoned: {next}");
async fn non_interrupt_failure_still_consumes_a_concurrent_interrupt() {
// Race: an interrupt is signalled WHILE a form is in flight, but the
// form loses to its own terminal error (out-of-fuel) before any epoch
// bump could win. The in-flight form is finished, so the interrupt must
// be acked on the error path too — otherwise it poisons the next form.
// Enough fuel to run ~0.5s after its PRINT, so the latch provably
// lands mid-flight, yet the loop still ends in out-of-fuel.
fuel: 4_000_000_000,
let latch_task = tokio::spawn(async move {
// Latch only (no epoch bump), once the form is in flight: it trips
// out-of-fuel on its own.
let failed = session
.handle_form(
"(:id 50 :form (let ((n 1073741823)) (print \"go\") (do ((i 0 (+ i 1))) ((>= i n) i))))",
latch_task.await.unwrap();
failed.contains(":code runtime") || failed.contains(":code interrupted"),
"in-flight form should fail terminally: {failed}"
let next = session.handle_form("(:id 51 :form (+ 1 2))").await;
assert_eq!(next, "(:id 51 :value 3)", "next form was poisoned: {next}");
async fn interrupt_after_clean_eval_aborts_next_form() {
// The mirror of the above: after a form completes NORMALLY, an interrupt
// armed before the next form must still abort it. The in-flight cleanup
// only consumes the latch on an actual epoch-interrupt trap, so a `C-g`
// that lands post-completion is NOT swallowed.
let clean = session.handle_form("(:id 40 :form (+ 1 2))").await;
assert_eq!(clean, "(:id 40 :value 3)");
session.interrupt_handle().interrupt();
let interrupted = session.handle_form("(:id 41 :form (+ 4 5))").await;
interrupted.contains(":code interrupted"),
"post-completion interrupt must abort the next form: {interrupted}"
async fn epoch_bumper_cancels_inflight_long_eval() {
// The one test that pins the EPOCH-TRAP cancel path specifically.
// Two things can end an in-flight eval with `:code interrupted`: the
// pre-start `check_interrupt` latch, and the epoch trap inside Wasm.
// They are easy to confuse, and a test that accepts either silently
// stops covering the trap the moment timing drifts — which is exactly
// what a coverage-instrumented build does to it.
// This isolates the trap structurally rather than by timing:
// - the interrupt handle is never touched, so `check_interrupt` can
// not fire at all and the pre-start path is unreachable;
// - fuel is effectively unbounded and the loop bound is just under
// i32::MAX, so the form can neither finish nor exhaust fuel, ruling
// out `:code runtime`;
// - the bumper runs in a loop rather than after a fixed sleep, so a
// slow host widens the window instead of missing it.
// The assertions then pin the trap by its wire signature: EpochInterrupt
// carries a `:detail`, whereas the pre-start latch reports
// "evaluation interrupted before start" with none.
let done = Arc::new(std::sync::atomic::AtomicBool::new(false));
let bump_until_done = done.clone();
let bump_task = tokio::spawn(async move {
while !bump_until_done.load(std::sync::atomic::Ordering::Relaxed) {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
// Bounded: if epoch interruption regresses, the form runs to a bound just
// under i32::MAX with unbounded fuel and never returns. Without this the
// test would stop being a test and become a job that burns until the k8s
// deadline kills it — silently, since it produces no output meanwhile.
let response = tokio::time::timeout(
session.handle_form("(:id 22 :form (do ((i 0 (+ i 1))) ((>= i 1073741823) i)))"),
.expect("epoch bump did not cancel the eval within 60s — interruption has regressed");
done.store(true, std::sync::atomic::Ordering::Relaxed);
bump_task.await.unwrap();
assert!(response.contains(":id 22"), "{response}");
response.contains(":code interrupted"),
"epoch bump must cancel the in-flight eval: {response}"
!response.contains("evaluation interrupted before start"),
"took the pre-start latch path, so the epoch trap went untested: {response}"
response.contains(":detail"),
"EpochInterrupt must surface its detail; got: {response}"