All files / src/scripts sendSmokeMessage.ts

100% Statements 44/44
100% Branches 9/9
100% Functions 3/3
100% Lines 44/44

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 451x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 1x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 3x  
import { resolveTwitchRuntimeEnv } from "@workspace/env/twitch"
import { createLogger } from "@workspace/logger"
 
import { TwitchApiService } from "../services/TwitchApiService"
import { ensureBotGrant, GrantStore } from "../auth/grantStore"
 
export async function sendSmokeMessage(
  env: Record<string, string | undefined>,
  message = "dude is online.",
  requestFetch: typeof fetch = fetch
): Promise<string> {
  const config = resolveTwitchRuntimeEnv(env)
  if (!config.TWITCH_BOOTSTRAP_BROADCASTER_USER_ID)
    throw new Error("An explicit smoke broadcaster is required.")
  const api = new TwitchApiService(config, requestFetch)
  const botUserToken = await ensureBotGrant(
    api,
    new GrantStore(config.TWITCH_BOT_GRANT_PATH),
    config
  )
  const appToken = await api.acquireAppToken()
  await api.broadcasterExists(
    appToken,
    config.TWITCH_BOOTSTRAP_BROADCASTER_USER_ID
  )
  return api.sendChatMessage(
    botUserToken,
    config.TWITCH_BOOTSTRAP_BROADCASTER_USER_ID,
    message
  )
}
 
export async function main(args = process.argv.slice(2)): Promise<void> {
  const logger = createLogger("twitch-smoke")
  try {
    if (args.length > 1)
      throw new Error("Expected at most one message argument.")
    const messageId = await sendSmokeMessage(process.env, args[0])
    logger.info("Explicit Twitch smoke message sent", { messageId })
  } catch {
    logger.error("Twitch smoke failed. No automatic retry was attempted.")
    process.exitCode = 1
  }
}