Refactor CLI and Twilio modules; add helper tests and comments

This commit is contained in:
Peter Steinberger
2025-11-25 03:11:39 +01:00
parent c71abf13a1
commit afdaa7ef98
17 changed files with 996 additions and 734 deletions

View File

@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { parseClaudeJsonText } from "./claude.js";
describe("claude JSON parsing", () => {
it("extracts text from single JSON object", () => {
const out = parseClaudeJsonText('{"text":"hello"}');
expect(out).toBe("hello");
});
it("extracts from newline-delimited JSON", () => {
const out = parseClaudeJsonText('{"irrelevant":1}\n{"text":"there"}');
expect(out).toBe("there");
});
it("returns undefined on invalid JSON", () => {
expect(parseClaudeJsonText("not json")).toBeUndefined();
});
});

View File

@@ -1,5 +1,6 @@
// Helpers specific to Claude CLI output/argv handling.
// Preferred binary name for Claude CLI invocations.
export const CLAUDE_BIN = "claude";
function extractClaudeText(payload: unknown): string | undefined {
@@ -45,7 +46,7 @@ function extractClaudeText(payload: unknown): string | undefined {
}
export function parseClaudeJsonText(raw: string): string | undefined {
// Handle a single JSON blob or newline-delimited JSON; return the first extracted text.
// Handle a single JSON blob or newline-delimited JSON; return the first extracted text.
const candidates = [raw, ...raw.split(/\n+/).map((s) => s.trim()).filter(Boolean)];
for (const candidate of candidates) {
try {

View File

@@ -11,8 +11,8 @@ export type TemplateContext = MsgContext & {
IsNewSession?: string;
};
// Simple {{Placeholder}} interpolation using inbound message context.
export function applyTemplate(str: string, ctx: TemplateContext) {
// Simple {{Placeholder}} interpolation using inbound message context.
return str.replace(/{{\s*(\w+)\s*}}/g, (_, key) => {
const value = (ctx as Record<string, unknown>)[key];
return value == null ? "" : String(value);