ui / src / components / agent-chat / agent-feed-attachment.test.tsxmain at d68d7e1
// @vitest-environment jsdom
// A picture's row has its height at its first paint: the list trusts a row's first measurement.
import { act } from "react";
import { createRoot } from "react-dom/client";
import { renderToStaticMarkup } from "react-dom/server";
import { expect, test } from "vitest";
import { attachmentBox, UserMessageBody } from "./agent-feed.tsx";
const picture = (path: string) => ({
kind: "user" as const,
id: path,
offset: 1,
text: "look",
timestampMs: 0,
files: [{ contentType: "image/png", filename: "shot.png", path, size: 10 }],
});
test("a picture's box is its own size, at most 256 px high in its proportions; none while unknown", () => {
expect(attachmentBox({})).toBeUndefined();
expect(attachmentBox({ width: 400, height: 91 })).toEqual({ width: 400, height: 91 });
expect(attachmentBox({ width: 2184, height: 552 })).toEqual({ width: 1013, height: 256 });
expect(attachmentBox({ width: 800, height: 1200 })).toEqual({ width: 171, height: 256 });
});
test("before its address is signed a picture already has its box, and no file chip", () => {
const markup = renderToStaticMarkup(
<UserMessageBody item={picture("/files/a.png")} signedUrl={() => new Promise(() => {})} />,
);
expect(markup).toContain('role="img"');
expect(markup).toContain("height:256px");
expect(markup).not.toContain("<img");
});
test("a row that mounts again signs nothing and draws the picture at the size it had", async () => {
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
let signings = 0;
const signedUrl = (path: string) => {
signings += 1;
return Promise.resolve(`https://files.test${path}?signed`);
};
const item = picture("/files/b.png");
const host = document.createElement("div");
const root = createRoot(host);
await act(async () => root.render(<UserMessageBody item={item} signedUrl={signedUrl} />));
const first = host.querySelector("img")!;
expect(first.style.height).toBe("256px");
// the picture loads: the page learns its size
Object.defineProperty(first, "naturalWidth", { value: 400 });
Object.defineProperty(first, "naturalHeight", { value: 91 });
await act(async () => void first.dispatchEvent(new Event("load")));
const sized = host.querySelector("img")!;
expect([sized.getAttribute("width"), sized.getAttribute("height"), sized.style.height]).toEqual([
"400",
"91",
"",
]);
await act(async () => root.render(null));
const again = renderToStaticMarkup(<UserMessageBody item={item} signedUrl={signedUrl} />);
expect(again).toContain("<img");
expect(again).toContain('width="400" height="91"');
expect(signings).toBe(1);
});