refactor: cut cyclomatic complexity of five hot spots and pin their behavior with tests

Risk labels, menu callback parsing, settings callbacks, table cell width and
markdown link targets now use tables and small helpers (CC <= 10).
This commit is contained in:
evandro committed 2026-10-10 03:59:46 -03:00
1 parent bf4a2021e8
commit 6a6e75fe32
8 files changed
+648 -294

No files matched your search

+39 -31
View File
@@ -334,45 +334,53 @@ export type TelegramMenuCallbackAction =
value?: string;
};
const TELEGRAM_MENU_STATUS_ACTIONS = [
"model",
"thinking",
"queue",
"settings",
] as const;
const TELEGRAM_MENU_MODEL_ACTIONS = [
"noop",
"scope",
"page",
"pages",
"open",
"pick",
"pick-selected",
"scope-enable",
"scope-disable",
"scope-toggle",
] as const;
type TelegramMenuModelAction = Extract<
TelegramMenuCallbackAction,
{ kind: "model" }
>["action"];
export function parseTelegramMenuCallbackAction(
data: string | undefined,
): TelegramMenuCallbackAction {
if (data === "menu:model" || data === "status:model") {
return { kind: "status", action: "model" };
}
if (data === "menu:thinking" || data === "status:thinking") {
return { kind: "status", action: "thinking" };
}
if (data === "menu:queue" || data === "status:queue") {
return { kind: "status", action: "queue" };
}
if (data === "menu:settings" || data === "status:settings") {
return { kind: "status", action: "settings" };
}
if (data?.startsWith("thinking:set:")) {
if (!data) return { kind: "ignore" };
const statusAction = TELEGRAM_MENU_STATUS_ACTIONS.find(
(action) => data === `menu:${action}` || data === `status:${action}`,
);
if (statusAction) return { kind: "status", action: statusAction };
if (data.startsWith("thinking:set:")) {
return {
kind: "thinking:set",
level: data.slice("thinking:set:".length),
};
}
if (data?.startsWith("model:")) {
const [, action, value] = data.split(":");
if (
action === "noop" ||
action === "scope" ||
action === "page" ||
action === "pages" ||
action === "open" ||
action === "pick" ||
action === "pick-selected" ||
action === "scope-enable" ||
action === "scope-disable" ||
action === "scope-toggle"
) {
return { kind: "model", action, value };
}
}
return { kind: "ignore" };
if (!data.startsWith("model:")) return { kind: "ignore" };
const [, action, value] = data.split(":");
const modelAction = TELEGRAM_MENU_MODEL_ACTIONS.find(
(candidate): candidate is TelegramMenuModelAction => candidate === action,
);
return modelAction
? { kind: "model", action: modelAction, value }
: { kind: "ignore" };
}
export async function handleTelegramMenuCallbackEntry(