Every chapter so far ended the same way: run the QA, look at the log, nod. That loop is perfect while you're learning — and it quietly falls apart the moment the QA grows. Change one line in a hundred-line file, and the log looks almost the same. Did the lamp still turn on at sunset? You'd have to watch it to know.
This chapter teaches the lamp to check itself. A small test file sits next to it, runs the same offline sim you already know, and answers with one word: pass or fail.
Create sunset_test.lua in your project folder:
--%%name:sunset_test
local Test = require("flua-test")
local t = Test.new()
-- the lamp's own logic: turnOn must light it
function QuickApp:turnOn()
self:updateProperty("value", true)
end
function QuickApp:onInit()
-- 1. the sim is honest: the device exists, under the id the QA sees
local dev = api.get("/devices/" .. _FLUA.qaId)
t:expect(dev.name == "sunset_test", "the device answers by name")
t:assert_eq(dev.id, _FLUA.qaId, "the id matches")
-- 2. our own logic, checked against the sim
self:turnOn()
t:expect(self.properties.value == true, "turnOn lights the lamp")
t:finish()
end
Three new names, all from flua's built-in test library:
require("flua-test") loads it — no download, nothing to install.t:expect(condition, "message") checks one thing and prints PASS: or
FAIL:.t:finish() prints the failure count and exits 1 if anything failed,
0 otherwise — so the exit code says pass or fail, not your eyes.You already know the rest: api.get is the sim (chapter 09),
updateProperty is how the lamp changes its own state (chapter 03), and
function QuickApp:turnOn() is the lamp's action (chapter 03 again).
One command runs every *_test.lua file in the folder:
flua test
PASS sunset_test.lua
flua test: 1 passed, 0 failed, 0 timed out
✅ You should see
PASS sunset_test.lua.
flua test finds the test files by name (anything ending in _test.lua),
runs each in its own fresh sim — so tests can't trip over each other — and
exits 0 only when everything passes. That last part matters more than it
looks: chapter 07's agents, and any computer, can now check your lamp
without reading a single log line.
Make the lamp's check wrong:
t:expect(self.properties.value == false, "turnOn lights the lamp")
Run the suite again:
FAIL sunset_test.lua (failures: 1)
flua test: 0 passed, 1 failed, 0 timed out
✅ You should see
FAILandfailures: 1.
Put the line back. This is the whole point: the test catches the mistake now, while you're two minutes from it — not after it has lived on your HC3 for a week.
A test QA must end itself with t:finish(). Forget that — say you delete
t:finish() and leave a repeating timer running — and the runner doesn't
hang your machine: it waits a while, then reports:
TIME sunset_test.lua (no exit within 5s — does the test end with t:finish()/exit(...)?)
(To see it: delete the t:finish() line and add
setInterval(function() end, 1000) somewhere, run flua test --timeout 5,
then put both back.)
Real QAs talk to real services — the lamp may grow a cloud API, a broker,
a player. The sim can host those services so the test stays offline:
the net.*Server family (a flua extension, no HC3 counterpart) runs inside
a QA and answers the client code:
local http = net.HTTPServer()
http:listen(0, function(req) return {status = 200, body = "ok"} end)
local ws = net.WebSocketServer()
ws:listen(0, function(message, isBinary, conn)
if message == "subscribe" then
setTimeout(function() ws:send(conn, "event:on") end, 50) -- push!
return "subscribed"
end
end)
ws:send / ws:broadcast), closes connections
(ws:disconnect), and reports connected/disconnected events — the
shape of event-driven services (subscribe, then pushed events).require("mocklib") — it runs
in the QA's own environment and is never packaged into the .fqa.One trap when tests meet the network: socket round-trips run in wall
time, timers in virtual time. At --speed 50 a 300 ms virtual delay is
~6 ms wall — shorter than a localhost reply — so never wait for network
effects with fixed delays; poll the condition with a generous timeout
(scaled with --speed).
require("flua-test") + t:expect/t:assert_eq/t:finish() — a test
file is just a QA that checks things and exits 0 on success, 1 on failure.flua test finds *_test.lua files, runs them isolated, and reports
PASS/FAIL/TIME — exit code 0 means everything holds.--%%file:$flua-test.lua,flua_test, the chapter-09 move) and the
same test code turns into no-ops on the controller.net.*Server mocks the outside world inside the sim — HTTP/TCP/UDP
request→reply, and WebSocket servers that push events and drop
connections like the real services do.That's the whole loop: write, run, look — and now prove. The lamp checks itself, the suite says so in one line, and every future change gets checked for free.
The rest is practice, the flua thread on the Fibaro forum, and the manual
(USAGE.md) for everything this tutorial deliberately skipped — record and
replay of your real house, the coverage audit, the VS Code extension's
toolbox. Happy automating — and mind the sunset.