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.
The file
Section titled “The file”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.optionsparams = { device = "BL1" } # captured path or topic values, for every message
[vars] # values the package reads with vars.getacme_region = "eu"[secrets] # and secrets.get; the server's are never usedacme_api_key = "test-key"
[[step]] # a message from the devicename = "punch upload"method = "POST" # default GET; PUBLISH for MQTT packagespath = "/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 itstatus = 200body = "OK" # or body_contains = [...] / body_lacks = [...]events = [{ type = "punch", data = { user_id = "1001", state_name = "check_in" } }]
[[step]] # a command for the device, by namedevice = "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 becamedevice = "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 stepnow = "2026-10-02T09:05:00Z" # optional: what time it is from this step onmethod = "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 } }]Steps and expectations
Section titled “Steps and expectations”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 fordevice, withpayloadsthe 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:
| Expectation | Steps | |
|---|---|---|
status, body, body_contains, body_lacks, route | message | The reply and the route that handled it. |
events | any | Every event the step stored, in order, each a subset. The gateway’s device.new and sync.member are left aside. |
device | any | A subset of the device’s info. |
pushed | any | The messages pushed to the device (push transports), each a subset. |
payload | command | The payload build returned. |
payloads | action, key, reconcile | The payloads queued, in order. |
error_contains | command, action, key | The step is refused with an error containing this text. |
sync | any | What the step left in the sync group (see below). |
events,pushedanddevicecompare 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’sbodyandquery. It also works in itsbody,body_containsandbody_lacksexpectations.{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 ownparamsbeside the endpoint’s. - An action step starts the action as the API does.
payloadsare 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 itsaction.result. It also storesdevice.held, when a gate did not pass. So that step’seventslist 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, forhourctl 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.
Details
Section titled “Details”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
nowis 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 = Nmakes a command the device asked to be sent again within N seconds due again. It moves nothing else.
Scenarios in a sync group
Section titled “Scenarios in a sync group”[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:
rows(registry rows).change_id(user id changes).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 3001punch_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
Section titled “Coverage”coverage routes commands actions [email protected] 3/3 9/9 7/7 [email protected] 13/13 45/45 24/24hourctl 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 asyncexpectation with alookup. - A command is covered when a step asserts its
payload. It is also covered when an action step’spayloadsorpushedinclude it. Internal commands count through the actions that use them. - An action is covered when an action step asserts its
payloadsorpushed. - For a package with
M.sync, each domain it declares is covered when a passing[sync]scenario with that domain checksexpect.sync. - For such a package, each report event it promises is covered when a passing scenario stores one. For example,
user.deletedfordeleted = 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.