local pkg = require("assay.pkg")
local function check(cond, msg)
if not cond then error(msg, 2) end
end
local catalog_dir = "tests/fixtures/pkg_catalog"
local templates_dir = "tests/fixtures/pkg_templates"
local cat = pkg.catalog.load({ catalog_dir })
local tpl = pkg.templates.load({ templates_dir }, cat.entries)
check(type(tpl) == "table" and tpl.entries and tpl.errors,
"templates.load returns {entries, errors}")
local def = tpl.entries["default"]
check(def, "default template should load")
check(def.packages[1] == "test-apt-only", "packages preserved")
local bad = tpl.entries["bad"]
check(bad == nil, "template referencing missing catalog id should be rejected")
local found_err = false
for _, e in ipairs(tpl.errors) do
if e.template_id == "bad" and e.message:find("does-not-exist", 1, true) then
found_err = true; break
end
end
check(found_err, "expected error mentioning the missing catalog id")
local listed = pkg.templates.list(tpl.entries)
check(#listed == 1, "only the default template should list")
check(def._origin == "built-in",
"first-layer template entries should be tagged built-in, got: " .. tostring(def._origin))
print("templates.lua OK")