All files / src/commands send.ts

72.22% Statements 13/18
68.75% Branches 11/16
100% Functions 1/1
75% Lines 12/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                            2x 2x 2x   2x     2x       2x 1x     1x 1x     1x 1x 1x                  
import { info } from "../globals.js";
import type { CliDeps, Provider, RuntimeEnv } from "../index.js";
 
export async function sendCommand(
	opts: {
		to: string;
		message: string;
		wait: string;
		poll: string;
		provider: Provider;
	},
	deps: CliDeps,
	runtime: RuntimeEnv,
) {
	deps.assertProvider(opts.provider);
	const waitSeconds = Number.parseInt(opts.wait, 10);
	const pollSeconds = Number.parseInt(opts.poll, 10);
 
	Iif (Number.isNaN(waitSeconds) || waitSeconds < 0) {
		throw new Error("Wait must be >= 0 seconds");
	}
	Iif (Number.isNaN(pollSeconds) || pollSeconds <= 0) {
		throw new Error("Poll must be > 0 seconds");
	}
 
	if (opts.provider === "web") {
		Iif (waitSeconds !== 0) {
			runtime.log(info("Wait/poll are Twilio-only; ignored for provider=web."));
		}
		await deps.sendMessageWeb(opts.to, opts.message, { verbose: false });
		return;
	}
 
	const result = await deps.sendMessage(opts.to, opts.message, runtime);
	Iif (!result) return;
	Eif (waitSeconds === 0) return;
	await deps.waitForFinalStatus(
		result.client,
		result.sid,
		waitSeconds,
		pollSeconds,
		runtime,
	);
}