A message's attachments persist as metadata (messaging.Message.Media) and
cross the gateway wire, but the dashboard transcript rendered text only, so
a photo was invisible to the operator while the model had seen it.
The transcript view now carries the persisted metadata, and the browser
labels each attachment in the placeholder vocabulary the channels already
write into a message's text ('[photo]', '[document: report.pdf]'), with the
surviving metadata (mime, name, size, dimensions, duration) in the chip's
tooltip.
Bytes are never rendered: they are turn-scoped and stripped before a record
is stored, so there is nothing to fetch and no broken image or 404 link. The
platform file id is deliberately not served either -- it is a download
handle, not an address -- so the chip links out only when the attachment
itself carried a URL. A message with no attachments omits the media key, so
transcripts written before this change keep the payload they had.
74 lines
2.9 KiB
TypeScript
74 lines
2.9 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import { readFile } from "node:fs/promises";
|
||
import test from "node:test";
|
||
|
||
import { mediaDetail, mediaLabel, type ChatMedia } from "../src/chat/attachment.ts";
|
||
|
||
// The transcript cannot show an attachment's bytes -- they are turn-scoped and
|
||
// never reach a browser -- so the label is the entire rendering. It reuses the
|
||
// placeholder vocabulary the channels write into a message's text, so a photo
|
||
// reads the same whether its metadata survived or only its note did.
|
||
test("an attachment is labelled in the channel's own placeholder vocabulary", () => {
|
||
const cases: Array<[ChatMedia, string]> = [
|
||
[{ type: "image", mime_type: "image/jpeg" }, "[photo]"],
|
||
[{ type: "video" }, "[video]"],
|
||
[{ type: "audio" }, "[audio]"],
|
||
[{ type: "document", file_name: "report.pdf" }, "[document: report.pdf]"],
|
||
[{ type: "document" }, "[document]"],
|
||
[{ type: "sticker" }, "[sticker]"],
|
||
[{ type: "" }, "[attachment]"],
|
||
];
|
||
for (const [attachment, want] of cases) {
|
||
assert.equal(mediaLabel(attachment), want, JSON.stringify(attachment));
|
||
}
|
||
});
|
||
|
||
// The label says what arrived; the detail says what is known about it, which
|
||
// is the metadata persistence kept. Nothing here is fetchable, and nothing
|
||
// here is invented from a file id.
|
||
test("the detail names the metadata that survived persistence", () => {
|
||
assert.equal(
|
||
mediaDetail({
|
||
type: "image",
|
||
mime_type: "image/jpeg",
|
||
file_name: "beach.jpg",
|
||
file_size: 2024,
|
||
width: 1280,
|
||
height: 960,
|
||
}),
|
||
"image/jpeg · beach.jpg · 2024 bytes · 1280×960",
|
||
);
|
||
assert.equal(mediaDetail({ type: "audio", duration: 12 }), "12 s");
|
||
assert.equal(mediaDetail({ type: "document" }), "");
|
||
});
|
||
|
||
// A file id is a platform download handle, not an address: a chip that linked
|
||
// it would 404. The chip links out only when the attachment itself carried a
|
||
// URL, and the transcript reaches the label and the URL through this module.
|
||
test("the transcript renders attachments from metadata, never from a file id", async () => {
|
||
const chip = await readFile(
|
||
new URL("../src/chat/ChatAttachment.vue", import.meta.url),
|
||
"utf8",
|
||
);
|
||
assert.match(chip, /mediaLabel\(attachment\)/, "the chip shows the label");
|
||
assert.match(chip, /mediaDetail\(attachment\)/, "the chip carries the metadata detail");
|
||
assert.match(chip, /:href="attachment\.url"/, "the only link target is a URL the attachment had");
|
||
assert.doesNotMatch(chip, /file_?id/i, "a file id is a handle, not a link target");
|
||
|
||
const bubble = await readFile(
|
||
new URL("../src/chat/ChatBubble.vue", import.meta.url),
|
||
"utf8",
|
||
);
|
||
assert.match(bubble, /ChatAttachment/, "the bubble renders the attachment chips");
|
||
|
||
const transcript = await readFile(
|
||
new URL("../src/chat/ChatTranscript.vue", import.meta.url),
|
||
"utf8",
|
||
);
|
||
assert.match(
|
||
transcript,
|
||
/media: message\.media \|\| \[\]/,
|
||
"a recorded message's attachments reach the bubble",
|
||
);
|
||
});
|