Refactor CLI and Twilio modules; add helper tests and comments
This commit is contained in:
20
src/auto-reply/claude.test.ts
Normal file
20
src/auto-reply/claude.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user