glacier-web 0.5.0

A batteries-included web framework for Rust, inspired by Rails, Phoenix and Laravel.
Documentation
//! The clock: `glacier::time::now()`, the helpers built on it, and moving
//! it in tests.

use std::sync::atomic::{AtomicUsize, Ordering};

use glacier::db::migration::Migration;
use glacier::db::{self, Repo};
use glacier::prelude::*;
use glacier::testing::{TestClient, freeze_time, travel, travel_back, travel_to};
use glacier::time::now;

#[glacier::model(timestamps = true)]
pub struct Note {
    body: String,
}

static RAN: AtomicUsize = AtomicUsize::new(0);

#[derive(serde::Serialize, serde::Deserialize)]
struct Remind;

impl Job for Remind {
    const NAME: &'static str = "remind";

    async fn perform(self) -> glacier::Result<()> {
        RAN.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}

fn at(text: &str) -> Timestamp {
    text.parse().unwrap()
}

#[glacier::test]
async fn travel_moves_the_clock() {
    let real = now();
    travel_to(at("2026-10-04T12:00:00Z"));
    assert_eq!(now(), at("2026-10-04T12:00:00Z"));
    assert_eq!(Timestamp::now(), now());
    assert_eq!(Date::today().to_string(), "2026-10-04");
    assert_eq!(30.days().ago(), at("2026-09-04T12:00:00Z"));
    assert_eq!(2.hours().from_now(), at("2026-10-04T14:00:00Z"));

    travel(15.days());
    assert_eq!(now(), at("2026-10-19T12:00:00Z"));
    travel(-(90.minutes()));
    assert_eq!(now(), at("2026-10-19T10:30:00Z"));

    travel_back();
    assert!(now() >= real);
    freeze_time();
    let frozen = now();
    tokio::task::yield_now().await;
    assert_eq!(now(), frozen);
}

#[glacier::test]
async fn records_and_jobs_follow_the_clock() {
    let repo = Repo::connect("sqlite::memory:").await.unwrap();
    db::with(&repo, async {
        for statement in Migration::new("m1_jobs", jobs_tables, |_| {}).up_sql(repo.dialect()) {
            db::sql(&statement).execute().await.unwrap();
        }
        db::sql(
            "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT NOT NULL, \
             inserted_at TEXT NOT NULL, updated_at TEXT NOT NULL)",
        )
        .execute()
        .await
        .unwrap();
    })
    .await;
    let client = TestClient::new(
        App::new()
            .database(repo.clone())
            .jobs(Jobs::new().register::<Remind>()),
    );
    db::with(&repo, async {
        travel_to(at("2026-10-04T09:00:00Z"));
        let note = Note::create(Note::changeset().set(|n| n.body, "hi"))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(note.inserted_at, at("2026-10-04T09:00:00Z"));

        Remind
            .enqueue_in(std::time::Duration::from_secs(3600))
            .await
            .unwrap();
        assert_eq!(client.perform_jobs().await.unwrap(), 0, "not due yet");
        travel(61.minutes());
        assert_eq!(client.perform_jobs().await.unwrap(), 1);
        assert_eq!(RAN.load(Ordering::SeqCst), 1);
    })
    .await;
}

#[test]
#[should_panic(expected = "travel_to works inside a #[glacier::test]")]
fn travel_needs_a_test_clock() {
    travel_to(at("2026-10-04T12:00:00Z"));
}

#[glacier::test]
async fn tests_see_what_was_enqueued() {
    let repo = Repo::connect("sqlite::memory:").await.unwrap();
    db::with(&repo, async {
        for statement in Migration::new("m1_jobs", jobs_tables, |_| {}).up_sql(repo.dialect()) {
            db::sql(&statement).execute().await.unwrap();
        }
        travel_to(at("2026-10-04T09:00:00Z"));
        assert!(glacier::testing::enqueued_jobs().await.unwrap().is_empty());
        Remind
            .enqueue_in(std::time::Duration::from_secs(60))
            .await
            .unwrap();
        let jobs = glacier::testing::enqueued_jobs().await.unwrap();
        assert_eq!(jobs.len(), 1);
        assert_eq!(jobs[0].name, "remind");
        assert_eq!(jobs[0].queue, "default");
        assert_eq!(jobs[0].args, serde_json::json!(null));
        assert_eq!(jobs[0].run_at, at("2026-10-04T09:01:00Z"));
    })
    .await;
}

/// The jobs tables as `glacier generate job` makes them.
fn jobs_tables(s: &mut glacier::db::migration::Schema) {
    glacier::jobs::create_table(s);
    glacier::jobs::upgrade_table(s);
}