/// The session store: an actor holding the users and sessions /// (gen_server style). It is linked to the web server that starts it, so /// the supervisor restarts both if either crashes. import app.{type RegisterError, type User, NameTaken, User} import argon2 import std/actor.{Continue, Stop} import std/dict.{type Dict} import std/option import std/process.{type Subject, type proc} import std/random.{type random} import std/result import std/string import std/test.{assert, assert_equal} pub type Msg { FindUser(name: String, reply: Subject>) , AddUser(user: User, reply: Subject>) , NewSession(name: String, reply: Subject) , SessionUser(token: String, reply: Subject>) } type State { State(users: Dict, sessions: Dict) } /// What the store's messages and tests may perform (ADR 0044). effect alias store_fx = proc, random /// Start a store, linked to the calling actor, with one user: `ada`, /// password `lovelace`. pub fn start_linked :: -> Subject / { let ada = match argon2.hash_password "lovelace" (app.hashing ()) { Ok(hash) -> User("ada", hash) , Error(e) -> panic "cannot hash the seed password: {e}" } let users = dict.insert (dict.empty ()) "ada" ada // A lambda, so `on_message` declares only the effects it uses. let store = actor.start State(users, dict.empty ()) fn state msg { on_message state msg } process.link (process.owner store) store } fn on_message :: state: State msg: Msg -> actor.Next / { match msg { FindUser(name, reply) -> { process.send reply (dict.get state.users name) Continue(state) } , AddUser ( user , reply ) -> if dict.has state.users user.name { process.send reply Error(NameTaken) Continue(state) } else { process.send reply Ok(()) Continue(State(..state, users: dict.insert state.users user.name user)) } , NewSession(name, reply) -> { let token = random.token 16 process.send reply token Continue(State(..state, sessions: dict.insert state.sessions token name)) } , SessionUser(token, reply) -> { process.send reply (dict.get state.sessions token) Continue(state) } } } // The tests below call `on_message` directly, with a reply subject of the // test's own, and read the reply back. /// The reply `send` gets, and the state after it. fn ask :: state: State send: fn(Subject) -> Msg -> (r, State) / { let reply = process.subject () match on_message state (send reply) { Continue(next) -> match process.receive reply 0 { Some(answer) -> (answer, next), None -> panic "no reply" } , Stop -> panic "the store stopped" } } /// A store with one user, `ada`, whose hash is "h1". fn with_ada :: -> State { State(dict.insert (dict.empty ()) "ada" User("ada", "h1"), dict.empty ()) } @test fn a_taken_name_keeps_its_first_user :: -> () / { let (added, state) = ask (with_ada ()) AddUser(User("ada", "h2"), _) assert (added |> result.is_error) "refused" let (found, _) = ask state FindUser("ada", _) assert_equal (found |> option.map fn u { u.password_hash }) Some("h1") } @test fn a_new_user_can_be_found :: -> () / { let (added, state) = ask (with_ada ()) AddUser(User("bob", "h2"), _) assert (added |> result.is_ok) "added" let (found, _) = ask state FindUser("bob", _) assert_equal (found |> option.map fn u { u.password_hash }) Some("h2") } @test fn a_new_session_names_its_user :: -> () / { let (token, state) = ask (with_ada ()) NewSession("ada", _) assert_equal (string.length token) 32 let (user, _) = ask state SessionUser(token, _) assert_equal user Some("ada") let (nobody, _) = ask state SessionUser("not{token}", _) assert_equal nobody None }