refactor: reuse gateway output helpers

This commit is contained in:
Peter Steinberger
2026-01-07 01:43:02 +00:00
parent b88c4e9d20
commit 15b7560a9b
5 changed files with 94 additions and 15 deletions

View File

@@ -36,6 +36,9 @@ const deps: CliDeps = {
describe("pollCommand", () => {
beforeEach(() => {
callGatewayMock.mockReset();
runtime.log.mockReset();
runtime.error.mockReset();
runtime.exit.mockReset();
testConfig = {};
});
@@ -74,4 +77,34 @@ describe("pollCommand", () => {
| undefined;
expect(args?.url).toBeUndefined();
});
it("emits json output with gateway metadata", async () => {
callGatewayMock.mockResolvedValueOnce({ messageId: "p1", channelId: "C1" });
await pollCommand(
{
to: "channel:C1",
question: "hi?",
option: ["y", "n"],
provider: "discord",
json: true,
},
deps,
runtime,
);
const lastLog = runtime.log.mock.calls.at(-1)?.[0] as string | undefined;
expect(lastLog).toBeDefined();
const payload = JSON.parse(lastLog ?? "{}") as Record<string, unknown>;
expect(payload).toMatchObject({
provider: "discord",
via: "gateway",
to: "channel:C1",
messageId: "p1",
channelId: "C1",
mediaUrl: null,
question: "hi?",
options: ["y", "n"],
maxSelections: 1,
durationHours: null,
});
});
});

View File

@@ -1,6 +1,10 @@
import type { CliDeps } from "../cli/deps.js";
import { callGateway, randomIdempotencyKey } from "../gateway/call.js";
import { success } from "../globals.js";
import {
buildOutboundDeliveryJson,
formatGatewaySummary,
} from "../infra/outbound/format.js";
import { normalizePollInput, type PollInput } from "../polls.js";
import type { RuntimeEnv } from "../runtime.js";
@@ -74,19 +78,24 @@ export async function pollCommand(
runtime.log(
success(
`✅ Poll sent via gateway (${provider}). Message ID: ${result.messageId ?? "unknown"}`,
formatGatewaySummary({
action: "Poll sent",
provider,
messageId: result.messageId ?? null,
}),
),
);
if (opts.json) {
runtime.log(
JSON.stringify(
{
provider,
via: "gateway",
to: opts.to,
toJid: result.toJid ?? null,
channelId: result.channelId ?? null,
messageId: result.messageId,
...buildOutboundDeliveryJson({
provider,
via: "gateway",
to: opts.to,
result,
mediaUrl: null,
}),
question: normalized.question,
options: normalized.options,
maxSelections: normalized.maxSelections,

View File

@@ -5,6 +5,7 @@ import { success } from "../globals.js";
import { deliverOutboundPayloads } from "../infra/outbound/deliver.js";
import {
buildOutboundDeliveryJson,
formatGatewaySummary,
formatOutboundDeliverySummary,
} from "../infra/outbound/format.js";
import { resolveOutboundTarget } from "../infra/outbound/targets.js";
@@ -107,7 +108,7 @@ export async function sendCommand(
runtime.log(
success(
`✅ Sent via gateway. Message ID: ${result.messageId ?? "unknown"}`,
formatGatewaySummary({ provider, messageId: result.messageId ?? null }),
),
);
if (opts.json) {