Using the ctx · 57 / 69
Read configuration with a fallback
YOUR TASK
Make this happen
Keep forwarding the key and default to the host. When the host returns false, return your local fallback instead of leaking its error as a setting. Check solution runs both cases. In the platform, configuration belongs to the account or the integration instance, depending on where the script runs.
Read the cases in
lesson.test.lua, then choose Check solution to see what needs to change.Update
subject.luauntil every test passes. Keep the supplied tests unchanged.
Read the starter and tests
subject.lua
exports.setting = function(key: string, fallback: string) local ok, value = ctx.config.get(key, fallback) return value end
lesson.test.lua
local t = require("test")
local subject = require("cococo.subject")
t.it("forwards the key and default", function()
t.mock.host("ctx.config.get"):returns(true, "production")
local s = subject.load()
t.expect(s.setting("environment", "sandbox")):eq("production")
t.expect(t.mock.spy("ctx.config.get")):called_with("environment", "sandbox")
end)
t.it("uses a local fallback when the host fails", function()
t.mock.host("ctx.config.get"):returns(false, "unavailable")
local s = subject.load()
t.expect(s.setting("environment", "sandbox")):eq("sandbox")
end)