soma.cell

Multi-page static site: home, about, and a dynamic greeting route

Demonstrates html() pages, path captures, and a 404 fallthrough.

A Soma program from the web part of the verified corpus: 42 lines, cells Site, SiteTest. It passes soma check with soma 3.2.4.

How it is built

The cell Site declares 2 signals in its face: request(method: String, path: String, body: String) -> String and greet(name: String) -> String. Handlers: greet (line 18) and request (line 23). Private helpers: _page.

The file also holds 1 test cell (SiteTest) with 4 assertions, run by soma test.

Generated from soma describe on this file.

The source

soma.toml

[package]
name = "web-corpus"
version = "0.1.0"
entry = "todo_state.cell"

[verify]
cells = ["TodoService"]
deadlock_free = true
eventually = ["Done", "Cancelled"]

site_router.cell

// Multi-page static site: home, about, and a dynamic greeting route.
// Demonstrates html() pages, path captures, and a 404 fallthrough.

cell Site {
    face {
        signal request(method: String, path: String, body: String) -> String
        signal greet(name: String) -> String
        promise "static pages plus one dynamic route"
    }

    on _page(title: String, body: String) {
        return concat(
            concat("<!DOCTYPE html><html><head><title>", title),
            concat("</title></head><body><nav><a href='/'>home</a> | <a href='/about'>about</a></nav>",
                   concat(body, "</body></html>")))
    }

    on greet(name: String) {
        let safe = escape_html(name)
        return _page("Hello", "<h1>Hello, {safe}!</h1>")
    }

    on request(method: String, path: String, body: String) {
        let req = map("method", method, "path", path)
        match req {
            {method: "GET", path: "/"}             -> html(_page("Home", "<h1>Welcome</h1><p>A site in Soma.</p>"))
            {method: "GET", path: "/about"}        -> html(_page("About", "<h1>About</h1><p>Built with cells.</p>"))
            {method: "GET", path: "/hello/" + nm}  -> html(greet(nm))
            _                                      -> html(404, _page("Not Found", "<h1>404</h1>"))
        }
    }
}

cell test SiteTest {
    rules {
        assert contains(greet("Ada"), "Hello, Ada!")
        assert contains(greet("<x>"), "&lt;x&gt;")
        assert contains(_page("T", "B"), "<title>T</title>")
        assert contains(_page("T", "B"), "<a href='/about'>about</a>")
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check site_router.cell
note: promise on 'Site' is documentation (not machine-verifiable): "static pages plus one dynamic route"
  --> site_router.cell:8:17
  |
8 |         promise "static pages plus one dynamic route"
  |                 ^
✓ All checks passed (1 note above).
$ soma test site_router.cell
test SiteTest ...
  ✓ assert contains(greet("Ada"), "Hello, Ada!")
  ✓ assert contains(greet("<x>"), "&lt;x&gt;")
  ✓ assert contains(_page("T", "B"), "<title>T</title>")
  ✓ assert contains(_page("T", "B"), "<a href='/about'>about</a>")

4 tests: 4 passed, 0 failed
$ soma verify site_router.cell
Verifying site_router.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
note: the soma.toml beside this file declares [verify] properties; none applies here (no state machine)
VERIFY OK (vacuous — no state machine)

Builtins used

concat, contains, escape_html, html, map

Source file: /corpus/web/site_router.cell · examples/corpus/web/site_router.cell