Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion apps/web/src/chat/threads-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import { ChatApiError, sendToWorkbench } from "./threads-api";
import { buildMailFrame } from "@corbits/mailbox";

import { ChatApiError, frameBody, sendToWorkbench } from "./threads-api";
import { appendRoster, stripRoster } from "./workbench-roster";

const fetchSpy = spyOn(globalThis, "fetch");
afterEach(() => {
Expand Down Expand Up @@ -42,3 +45,25 @@ describe("sendToWorkbench failures", () => {
});
}
});

describe("frameBody", () => {
test("a roster-bearing frame built by the mailbox hub reads back without the roster", () => {
const body = appendRoster("Hello there", [
{ name: "Ada", address: "ada@example.com", kind: "person" },
{ name: "Echo", address: "echo@example.com", kind: "agent" },
]);
const frame = buildMailFrame({
from: "ada@example.com",
to: "echo@example.com",
subject: "Hi",
body,
messageId: "<one@example.com>",
});
const raw = btoa(String.fromCharCode(...frame));

const text = frameBody(raw);

expect(text).not.toContain("\r");
expect(stripRoster(text)).toBe("Hello there");
});
});
2 changes: 1 addition & 1 deletion apps/web/src/chat/threads-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export function frameBody(raw: string): string {
const part = frameParts(raw, "chat_frame_body").find(
(leaf) => leaf.filename === undefined && leaf.contentType === "text/plain",
);
return part === undefined ? "" : utf8.decode(part.content).trim();
return part === undefined ? "" : utf8.decode(part.content).replaceAll("\r\n", "\n").trim();
}

/** Every named MIME leaf of a frame, decoded. A part counts as an
Expand Down
Loading