Skip to content

Scenario tests

A scenario is a scripted conversation with a device. It is kept in a package’s tests/ folder.

It is how you prove a package reads what a clock sends and builds what the clock expects. hourctl pkg test fails while any route, command or action is untested. A package whose scenarios fail is not built.

Each tests/*.toml file runs through the real gateway pipeline. It uses no sockets or hardware. It runs on a fresh gateway with one endpoint bound to the package. Devices in it register themselves and are let in.

description = "Punches are stored before OK"
now = "2026-10-02T09:00:00Z" # optional: the instant it runs at
[endpoint]
options = { timezone = "-05:00" } # the endpoint's options, checked against M.options
params = { device = "BL1" } # captured path or topic values, for every message
[vars] # values the package reads with vars.get
acme_region = "eu"
[secrets] # and secrets.get; the server's are never used
acme_api_key = "test-key"
[[step]] # a message from the device
name = "punch upload"
method = "POST" # default GET; PUBLISH for MQTT packages
path = "/acme/punch" # default "/"
query = "page=1"
headers = { "x-acme-device" = "T1" }
body = '{"id":"p1","user":1001,"at":"2026-10-01 08:00:00","direction":"in"}'
store_fails = false # true: the gateway cannot store the events
[step.expect]
route = "punch" # which route handled it
status = 200
body = "OK" # or body_contains = [...] / body_lacks = [...]
events = [{ type = "punch", data = { user_id = "1001", state_name = "check_in" } }]
[[step]] # a command for the device, by name
device = "T1"
command = { action = "add_user", params = { user_id = "7", name = "Ann" } }
[step.expect]
payload = '{"code":"","id":"7","name":"Ann","op":"user.put"}'
[[step]] # an action: every command it became
device = "T1"
action = { name = "upsert_user", params = { user_id = "8", name = "Bo" } }
[step.expect]
payloads = ['{"code":"","id":"8","name":"Bo","op":"user.put"}']
[[step]] # the device is given a key ("" takes it away)
device = "T1"
key = "s3cret"
[step.expect]
payloads = ['{"key":"s3cret","op":"key"}'] # what set_key queued, in order
[[step]]
name = "the device asks again later"
wait = 10 # seconds that pass before the step
now = "2026-10-02T09:05:00Z" # optional: what time it is from this step on
method = "POST"
path = "/acme/result"
headers = { "x-acme-device" = "T1" }
body = '{"id":{command:1},"busy":true}'
[step.expect]
events = [{ type = "command.result", data = { retry_in = 10 } }]

A step is one of these:

  • A device message.
  • A command.
  • An action.
  • A key.
  • reconcile = true. This is one pass of the scenario’s sync group for device, with payloads the commands it queued.

Command, action, key and reconcile steps need device. It must be a device that has already sent a message in the scenario. These steps take only the expectations of their kind:

ExpectationSteps
status, body, body_contains, body_lacks, routemessageThe reply and the route that handled it.
eventsanyEvery event the step stored, in order, each a subset. The gateway’s device.new and sync.member are left aside.
deviceanyA subset of the device’s info.
pushedanyThe messages pushed to the device (push transports), each a subset.
payloadcommandThe payload build returned.
payloadsaction, key, reconcileThe payloads queued, in order.
error_containscommand, action, keyThe step is refused with an error containing this text.
syncanyWhat the step left in the sync group (see below).
  • events, pushed and device compare as subsets. Objects need only the keys you list. Arrays must have the same length.
  • A key given as { absent = true } must not be there. TOML has no null. For example: state_name = { absent = true }.
  • {last_command_id} stands for the id of the most recently queued command. It works in a message step’s body and query. It also works in its body, body_contains and body_lacks expectations.
  • {command:N} stands for the Nth command queued in the scenario, counted from 1, whatever queued it.
  • {*} in an expected payload stands for any text, such as a time the package fills in.
  • A message that fails fails the step, unless the step expects a status of 400 or more. A message fails when there is no route, when there is a package error, or when events could not be stored.
  • A message step may give a body that is not text as body_base64. It may also give its own params beside the endpoint’s.
  • An action step starts the action as the API does. payloads are all its steps, gates and the steps waiting behind them included. A gate goes out on its own, and what the device answers settles it. The step that finishes it stores its action.result. It also stores device.held, when a gate did not pass. So that step’s events list them.
  • Once a key step has given a device a key and the device has presented it, a message without it is refused with 401.
  • The runner checks every scenario for two events with the same id and different data, and fails it. Consumers deduplicate on id, so the second would be lost.
  • package = "name@version" names the package a capture was recorded with, for hourctl pkg replay (captures).

Take expected payloads from an independent source, such as the vendor’s specification or a capture from a real device. Do not take them from the package’s own output. Otherwise the test only shows the package agrees with itself.

A scenario starts at one instant. That is its now, or 2026-10-03T00:00:00Z. time.now in the package reads that instant. So a payload that carries the time can be expected exactly.

The clock stands still unless a step moves it.

  • A step’s own now is what time it is from that step on. It takes the place of the scenario’s or the step before’s.
  • Each step says what time it is. A time before the last one is allowed.
  • What a device asked to be sent again (queue.retry) by then is due.
  • wait = N makes a command the device asked to be sent again within N seconds due again. It moves nothing else.

[sync] puts the scenario’s endpoint in a sync group. It takes every key a sync group takes (domains, people, punches, …). id defaults to hq.

It also takes the data the group starts with. This data is seeded in this order:

  1. rows (registry rows).
  2. change_id (user id changes).
  3. punch_state (latest punches taken elsewhere).
[sync]
domains = ["punches"]
rows = [{ domain = "people", kind = "person", key = "1001", fields = { name = "Ann" } }]
change_id = [{ old = "2001", new = "3001" }] # 2001 stays an alias of 3001
punch_state = [{ user_id = "1001", time = "2026-10-02T13:01:12Z", state_name = "check_in",
endpoint = "berlin", device = "B1" }]
[[step]]
path = "/acme/last"
query = "user=1001"
headers = { "x-acme-device" = "T1" }
[step.expect]
sync = { lookup = { last_punch = "1001" } } # what the route asked; {} for nothing
# sync = { rows = [...], device_rows = [...] } # the group's rows and the device's rows (subsets)

Seeded punches dated 2 October 2026 count as recent, because the scenario runs on 3 October.

coverage routes commands actions
[email protected] 3/3 9/9 7/7
[email protected] 13/13 45/45 24/24

hourctl pkg test runs every scenario under packages (or -p <folder>). Per package, it reports what passing steps covered:

  • A route is covered when a step it handled has at least one expectation of the message’s outcome. Those are status, body, body_contains, body_lacks, route, events, device, pushed, or a sync expectation with a lookup.
  • A command is covered when a step asserts its payload. It is also covered when an action step’s payloads or pushed include it. Internal commands count through the actions that use them.
  • An action is covered when an action step asserts its payloads or pushed.
  • For a package with M.sync, each domain it declares is covered when a passing [sync] scenario with that domain checks expect.sync.
  • For such a package, each report event it promises is covered when a passing scenario stores one. For example, user.deleted for deleted = true.

It exits non-zero on a failed scenario and on anything untested. --allow-untested lets untested items pass while a package is in progress.

hourctl pkg test <filter> runs only the scenarios whose package id or path contains the filter. It then checks no coverage.