Files
herdrMario/tests/send-args.test.mjs

64 lines
2.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { sendArgs, parseSendArgs, sendNow } from "../herdr/herdr-composer/mods/herdr-composer/delivery.js";
const ok = { code: 0, stdout: "{}", stderr: "" };
test("sendArgs puts the text after `--`, so hc cannot read it as an option", () => {
assert.deepEqual(sendArgs("t2", "hello"), ["send", "--json", "t2", "--", "hello"]);
});
test("a text that looks like an option still goes through as the text", async () => {
for (const text of ["- fix", "--help", "--force x", "-", "--"]) {
const calls = [];
const result = await sendNow({ terminal: "t2", hc: async (args) => (calls.push(args), ok) }, text);
assert.equal(result.ok, true, text);
const args = calls[0];
const at = args.indexOf("--");
assert.equal(args[at + 1], text, text);
assert.equal(args.length, at + 2, text);
assert.deepEqual(args.slice(0, at), ["send", "--json", "t2"], text);
}
});
test("parseSendArgs reads the named fields back", () => {
for (const text of ["hello", "- fix", "--help", "--force x", "z \"q\" ęą"]) {
assert.deepEqual(parseSendArgs(sendArgs("t7", text)), { terminal: "t7", text, force: false });
}
});
test("parseSendArgs: not a send, or a send of another shape, is nothing", () => {
assert.equal(parseSendArgs(["get", "t2", "--json"]), null);
assert.equal(parseSendArgs(["send", "t2", "text", "--json"]), null);
assert.equal(parseSendArgs(null), null);
assert.equal(parseSendArgs([]), null);
});
test("--force is only there when asked for, and before the `--`", async () => {
assert.deepEqual(sendArgs("t2", "yes", { force: true }), ["send", "--json", "--force", "t2", "--", "yes"]);
assert.deepEqual(sendArgs("t2", "yes", { force: false }), sendArgs("t2", "yes"));
assert.deepEqual(parseSendArgs(sendArgs("t2", "--force x", { force: true })), { terminal: "t2", text: "--force x", force: true });
assert.equal(parseSendArgs(sendArgs("t2", "--force")).force, false);
const calls = [];
const ctx = { terminal: "t2", hc: async (args) => (calls.push(args), ok) };
await sendNow(ctx, "yes", { force: true });
await sendNow(ctx, "yes");
assert.equal(calls[0].includes("--force"), true);
assert.equal(calls[1].includes("--force"), false);
});
test("the queue never forces: drain sends without --force", async () => {
const { createState, reduce } = await import("../herdr/herdr-composer/mods/herdr-composer/state.js");
const { drain } = await import("../herdr/herdr-composer/mods/herdr-composer/delivery.js");
const state = reduce(createState(), { type: "queue", agent: "t2", item: { id: "1", text: "q", createdAt: 1 } });
const calls = [];
const result = await drain(
{ agent: "t2", terminal: "t2", status: () => "idle", gate: () => true, hc: async (args) => (calls.push(args), ok) },
state,
);
assert.equal(result.sent, true);
assert.equal(calls.length, 1);
assert.equal(calls[0].includes("--force"), false);
});