fix: strip trailing punctuation from MEDIA tokens and add tests

This commit is contained in:
Peter Steinberger
2025-11-25 06:07:11 +01:00
parent 9fbeb2ccd3
commit ad55832cda
2 changed files with 35 additions and 4 deletions

View File

@@ -223,6 +223,31 @@ describe("config and templating", () => {
expect(result?.mediaUrl).toBe("/tmp/pic.png"); expect(result?.mediaUrl).toBe("/tmp/pic.png");
}); });
it("captures MEDIA token with trailing JSON characters", async () => {
const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({
stdout: 'MEDIA:/tmp/pic.png"} trailing',
stderr: "",
code: 0,
signal: null,
killed: false,
});
const cfg = {
inbound: {
reply: {
mode: "command" as const,
command: ["echo", "{{Body}}"],
},
},
};
const result = await index.getReplyFromConfig(
{ Body: "hi", From: "+1", To: "+2" },
undefined,
cfg,
runSpy,
);
expect(result?.mediaUrl).toBe("/tmp/pic.png");
});
it("ignores invalid MEDIA lines with whitespace", async () => { it("ignores invalid MEDIA lines with whitespace", async () => {
const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({ const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({
stdout: "hello\nMEDIA: not a url with spaces\nrest\n", stdout: "hello\nMEDIA: not a url with spaces\nrest\n",

View File

@@ -17,15 +17,21 @@ export function splitMediaFromOutput(raw: string): {
let text = trimmedRaw; let text = trimmedRaw;
let mediaUrl: string | undefined; let mediaUrl: string | undefined;
const mediaLine = trimmedRaw.split("\n").find((line) => MEDIA_LINE_RE.test(line)); let mediaLine = trimmedRaw.split("\n").find((line) => MEDIA_LINE_RE.test(line));
if (!mediaLine) { let mediaMatch = mediaLine?.match(MEDIA_TOKEN_RE) ?? trimmedRaw.match(MEDIA_TOKEN_RE);
if (!mediaMatch) {
return { text: trimmedRaw }; return { text: trimmedRaw };
} }
if (!mediaLine && mediaMatch) {
mediaLine = mediaMatch[0];
}
let isValidMedia = false; let isValidMedia = false;
const mediaMatch = mediaLine.match(MEDIA_TOKEN_RE);
if (mediaMatch?.[1]) { if (mediaMatch?.[1]) {
const candidate = normalizeMediaSource(mediaMatch[1]); const cleaned = mediaMatch[1]
.replace(/^[`"'[{(]+/, "")
.replace(/[`"'\\})\],]+$/, "");
const candidate = normalizeMediaSource(cleaned);
const looksLikeUrl = /^https?:\/\//i.test(candidate); const looksLikeUrl = /^https?:\/\//i.test(candidate);
const looksLikePath = candidate.startsWith("/") || candidate.startsWith("./"); const looksLikePath = candidate.startsWith("/") || candidate.startsWith("./");
const hasWhitespace = /\s/.test(candidate); const hasWhitespace = /\s/.test(candidate);