All files / src/lib/app/routes debug.ts

100% Statements 20/20
100% Branches 14/14
100% Functions 4/4
100% Lines 20/20

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                    6x 2x     4x 4x       6x 4x     2x       25x 8x 1x     7x 7x 6x 6x   1x       6x 6x 6x   6x                         6x      
import type { Hono } from "hono";
import type { AppEnv } from "../types";
import { notifyDeveloperOfError } from "../../functions/errors/notifyDeveloper";
 
interface TestEmailRequestBody {
  message?: string;
  context?: Record<string, unknown>;
}
 
function resolveMessage(body: TestEmailRequestBody | null): string {
  if (!body?.message) {
    return "Kick bot test notification";
  }
 
  const trimmed = body.message.trim();
  return trimmed.length > 0 ? trimmed : "Kick bot test notification";
}
 
function resolveContext(body: TestEmailRequestBody | null) {
  if (!body?.context || typeof body.context !== "object") {
    return undefined;
  }
 
  return body.context;
}
 
export function registerDiagnosticsRoute(app: Hono<AppEnv>): void {
  app.post("/debug/test-email", async (c) => {
    if (!c.env.DEVELOPER_EMAIL) {
      return c.json({ error: "Developer email not configured" }, 400);
    }
 
    let body: TestEmailRequestBody | null = null;
    if (c.req.header("Content-Type")?.includes("application/json")) {
      try {
        body = (await c.req.json()) as TestEmailRequestBody;
      } catch (error) {
        return c.json({ error: "Invalid JSON payload" }, 400);
      }
    }
 
    const message = resolveMessage(body);
    const context = resolveContext(body);
    const timestamp = new Date().toISOString();
 
    await notifyDeveloperOfError(
      c.env,
      {
        message,
        status: 200,
        context: {
          trigger: "debug/test-email",
          ...(context ?? {}),
        },
      },
      timestamp
    );
 
    return c.json({ status: "sent" });
  });
}