import { test } from "node:test"; import assert from "node:assert/strict"; import { join } from "node:path"; import { bootConfig, loadSystem } from "../src/config.mjs"; import { writeJson } from "../../business/tests/helpers.mjs"; import { fixture, tmp } from "./helpers.mjs"; test("bootConfig builds the broker's boot message for one business, with no trackers key when no project names one", (t) => { const f = fixture(tmp(t)); const system = loadSystem({ env: f.env }); const warnings = []; const boot = bootConfig({ system, businessId: "acme", env: f.env, warn: (w) => warnings.push(w) }); assert.equal(boot.dataRoot, f.dataRoot); assert.deepEqual(Object.keys(boot.businesses), ["acme"]); assert.deepEqual(boot.launches, []); assert.deepEqual(boot.readers, ["acme"]); assert.equal("trackers" in boot, false); assert.match(warnings[0], /no project file sets tracker\.project/); }); test("trackers come from the tracker.* variables of the one project that names a tracker project", (t) => { const root = tmp(t); const f = fixture(root); writeJson(join(root, "project", ".mosaic", "project.json"), { projectVersion: 1, id: "stack", vars: { "tracker.project": 12 } }); const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env }); assert.deepEqual(boot.trackers, { acme: { baseUrl: "http://127.0.0.1:3456", project: 12, pollSeconds: 60, reconcileMinutes: 60 } }); }); test("with two projects, the one that sets tracker.project supplies the tracker and the other is no conflict", (t) => { const root = tmp(t); const f = fixture(root, "acme", (doc) => { doc.projects.web = { root: join(root, "web") }; return doc; }); writeJson(join(root, "project", ".mosaic", "project.json"), { projectVersion: 1, id: "stack", vars: { "tracker.project": 12 } }); writeJson(join(root, "web", ".mosaic", "project.json"), { projectVersion: 1, id: "web", vars: {} }); const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env }); assert.deepEqual(boot.trackers, { acme: { baseUrl: "http://127.0.0.1:3456", project: 12, pollSeconds: 60, reconcileMinutes: 60 } }); }); test("two projects that each name a tracker project refuse, since the boot shape holds one", (t) => { const root = tmp(t); const f = fixture(root, "acme", (doc) => { doc.projects.web = { root: join(root, "web") }; return doc; }); writeJson(join(root, "project", ".mosaic", "project.json"), { projectVersion: 1, id: "stack", vars: { "tracker.project": 12 } }); writeJson(join(root, "web", ".mosaic", "project.json"), { projectVersion: 1, id: "web", vars: { "tracker.project": 13 } }); assert.throws(() => bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env }), (e) => e.exitCode === 3 && /stack, web each set tracker\.project/.test(e.message)); }); test("a business without tracker.baseUrl gets no trackers entry", (t) => { const f = fixture(tmp(t), "acme", (doc) => { delete doc.vars["tracker.baseUrl"]; return doc; }); const warnings = []; const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env, warn: (w) => warnings.push(w) }); assert.equal("trackers" in boot, false); assert.deepEqual(warnings, []); }); test("an unknown business and a broken system config refuse with exit 3", (t) => { const f = fixture(tmp(t)); assert.throws(() => bootConfig({ system: loadSystem({ env: f.env }), businessId: "nope", env: f.env }), (e) => e.exitCode === 3); assert.throws(() => loadSystem({ env: { ...f.env, MOSAIC_CONFIG: join(f.dataRoot, "missing.json") } }), (e) => e.exitCode === 3); });