// Row 141 (#1635, REQ-MSG-2): `mosaic name` and `mosaic names`, through the in-process broker from // helpers.mjs. The broker's own checks are in packages/bus/tests/names.test.mjs. import { test } from "node:test"; import assert from "node:assert/strict"; import { main, USAGE } from "../src/cli.mjs"; import { CliError } from "../src/errors.mjs"; import { broker, io, tmp } from "./helpers.mjs"; function setup(t) { const bus = broker(t); const dataRoot = tmp(t); const run = async (argv, x = io()) => { try { await main(argv, x, { system: { dataRoot }, transport: bus.transport }); return { code: 0, io: x }; } catch (e) { if (!(e instanceof CliError)) throw e; return { code: e.exitCode, error: e.message, io: x }; } }; return { ...bus, run }; } test("the human names an instance, lists it, and talk's message.send reaches it by that name", async (t) => { const s = setup(t); const named = await s.run(["name", "coder", "rex", "--business", "demo"]); assert.equal(named.code, 0, named.error); assert.equal(named.io.out.text, "named coder rex\n"); assert.deepEqual(s.calls.at(-1), { business: "demo", verb: "name.set", args: { instance: "coder", name: "rex" } }); const listed = await s.run(["names", "--business", "demo"]); assert.equal(listed.code, 0, listed.error); assert.equal(listed.io.out.text, "coder rex\n"); const json = await s.run(["names", "--json", "--business", "demo"]); assert.deepEqual( JSON.parse(json.io.out.text).map(({ instance, name }) => ({ instance, name })), [{ instance: "coder", name: "rex" }], ); const sent = await s.run(["talk", "rex", "hello", "--wait", "0", "--business", "demo"]); assert.equal(sent.code, 0, sent.error); assert.match(sent.io.out.text, /^sent [0-9a-f]{8} to rex\n/); const [m] = s.b.request(s.coder, { verb: "message.receive" }); assert.equal(m.to_role, "coder"); assert.equal(m.to_name, "rex"); }); test("name and names refuse the wrong number of arguments with the usage", async (t) => { const s = setup(t); for (const argv of [["name", "coder"], ["name", "coder", "rex", "extra"], ["names", "coder"]]) { const r = await s.run([...argv, "--business", "demo"]); assert.equal(r.code, 4, argv.join(" ")); assert.ok(r.error.endsWith(USAGE), r.error); } assert.equal(s.calls.length, 0); assert.match(USAGE, /^ {2}mosaic name \[--business \] \[--tester\]$/m); assert.match(USAGE, /^ {2}mosaic names \[--business \] \[--json\] \[--tester\]$/m); });