test(contracts,web): cover file attachment variant, drafts, and rendering
Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
4050afee4d
commit
f8da3b51bc
5 files changed
+366
No files matched your search
@@ -381,6 +381,88 @@ describe("MessagesTimeline", () => {
|
||||
expect(onAnchorSizeChanged).toHaveBeenCalledWith(secondEntry.message.id, 240);
|
||||
});
|
||||
|
||||
it("renders file attachments as chips with name and size instead of images", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const entry = {
|
||||
...buildUserTimelineEntry("Please review the attached report."),
|
||||
message: {
|
||||
...buildUserTimelineEntry("Please review the attached report.").message,
|
||||
attachments: [
|
||||
{
|
||||
type: "file" as const,
|
||||
id: "attachment-file-1",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 2_048,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline {...buildProps()} timelineEntries={[entry]} />,
|
||||
);
|
||||
|
||||
expect(markup).toContain("report.pdf");
|
||||
expect(markup).toContain("2.0 KB");
|
||||
expect(markup).toContain("lucide-file-text");
|
||||
expect(markup).not.toContain("<img");
|
||||
});
|
||||
|
||||
it("renders downloadable file attachment chips when a preview URL is available", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const entry = {
|
||||
...buildUserTimelineEntry("Here is the file."),
|
||||
message: {
|
||||
...buildUserTimelineEntry("Here is the file.").message,
|
||||
attachments: [
|
||||
{
|
||||
type: "file" as const,
|
||||
id: "attachment-file-2",
|
||||
name: "notes.txt",
|
||||
mimeType: "text/plain",
|
||||
sizeBytes: 512,
|
||||
previewUrl: "data:text/plain;base64,aGVsbG8=",
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline {...buildProps()} timelineEntries={[entry]} />,
|
||||
);
|
||||
|
||||
expect(markup).toContain('download="notes.txt"');
|
||||
expect(markup).toContain('href="data:text/plain;base64,aGVsbG8="');
|
||||
expect(markup).toContain("512 B");
|
||||
});
|
||||
|
||||
it("renders image attachments in the image grid without file chips", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const entry = {
|
||||
...buildUserTimelineEntry("Take a look."),
|
||||
message: {
|
||||
...buildUserTimelineEntry("Take a look.").message,
|
||||
attachments: [
|
||||
{
|
||||
type: "image" as const,
|
||||
id: "attachment-image-1",
|
||||
name: "screenshot.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 1,
|
||||
previewUrl: "data:image/png;base64,iVBORw0KGgo=",
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline {...buildProps()} timelineEntries={[entry]} />,
|
||||
);
|
||||
|
||||
expect(markup).toContain('src="data:image/png;base64,iVBORw0KGgo="');
|
||||
expect(markup).toContain('alt="screenshot.png"');
|
||||
expect(markup).not.toContain("lucide-file-text");
|
||||
expect(markup).not.toContain("download=");
|
||||
});
|
||||
|
||||
it("renders collapse controls for long user messages", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
|
||||
@@ -65,6 +65,7 @@ import {
|
||||
markPromotedDraftThreadByRef,
|
||||
markPromotedDraftThreads,
|
||||
markPromotedDraftThreadsByRef,
|
||||
type ComposerFileAttachment,
|
||||
type ComposerImageAttachment,
|
||||
useComposerDraftStore,
|
||||
DraftId,
|
||||
@@ -104,6 +105,33 @@ function makeImage(input: {
|
||||
};
|
||||
}
|
||||
|
||||
function makeFile(input: {
|
||||
id: string;
|
||||
previewUrl: string;
|
||||
name?: string;
|
||||
mimeType?: string;
|
||||
sizeBytes?: number;
|
||||
lastModified?: number;
|
||||
}): ComposerFileAttachment {
|
||||
const name = input.name ?? "report.pdf";
|
||||
const mimeType = input.mimeType ?? "application/pdf";
|
||||
const sizeBytes = input.sizeBytes ?? 4;
|
||||
const lastModified = input.lastModified ?? 1_700_000_000_000;
|
||||
const file = new File([new Uint8Array(sizeBytes).fill(1)], name, {
|
||||
type: mimeType,
|
||||
lastModified,
|
||||
});
|
||||
return {
|
||||
type: "file",
|
||||
id: input.id,
|
||||
name,
|
||||
mimeType,
|
||||
sizeBytes: file.size,
|
||||
previewUrl: input.previewUrl,
|
||||
file,
|
||||
};
|
||||
}
|
||||
|
||||
function makeTerminalContext(input: {
|
||||
id: string;
|
||||
text?: string;
|
||||
@@ -345,6 +373,123 @@ describe("composerDraftStore syncPersistedAttachments", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("composerDraftStore file attachments", () => {
|
||||
const threadId = ThreadId.make("thread-file-attachments");
|
||||
const threadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, threadId);
|
||||
|
||||
beforeEach(() => {
|
||||
removeLocalStorageItem(COMPOSER_DRAFT_STORAGE_KEY);
|
||||
resetComposerDraftStore();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
removeLocalStorageItem(COMPOSER_DRAFT_STORAGE_KEY);
|
||||
});
|
||||
|
||||
function mergePersistedDraft(attachment: Record<string, unknown>) {
|
||||
const persistApi = useComposerDraftStore.persist as unknown as {
|
||||
getOptions: () => {
|
||||
merge: (
|
||||
persistedState: unknown,
|
||||
currentState: ReturnType<typeof useComposerDraftStore.getState>,
|
||||
) => ReturnType<typeof useComposerDraftStore.getState>;
|
||||
};
|
||||
};
|
||||
return persistApi.getOptions().merge(
|
||||
{
|
||||
draftsByThreadId: {
|
||||
[threadId]: {
|
||||
prompt: "",
|
||||
attachments: [attachment],
|
||||
},
|
||||
},
|
||||
draftThreadsByThreadId: {},
|
||||
projectDraftThreadIdByProjectKey: {},
|
||||
},
|
||||
useComposerDraftStore.getInitialState(),
|
||||
);
|
||||
}
|
||||
|
||||
it("hydrates persisted attachments without a type as images (legacy drafts)", () => {
|
||||
const mergedState = mergePersistedDraft({
|
||||
id: "img-legacy",
|
||||
name: "legacy.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 5,
|
||||
dataUrl: "data:image/png;base64,aGVsbG8=",
|
||||
});
|
||||
|
||||
expect(mergedState.draftsByThreadKey[threadKeyFor(threadId)]?.images).toMatchObject([
|
||||
{
|
||||
type: "image",
|
||||
id: "img-legacy",
|
||||
name: "legacy.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 5,
|
||||
previewUrl: "data:image/png;base64,aGVsbG8=",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("hydrates persisted attachments with type file as file attachments", () => {
|
||||
const mergedState = mergePersistedDraft({
|
||||
type: "file",
|
||||
id: "file-persisted",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 5,
|
||||
dataUrl: "data:application/pdf;base64,aGVsbG8=",
|
||||
});
|
||||
|
||||
expect(mergedState.draftsByThreadKey[threadKeyFor(threadId)]?.images).toMatchObject([
|
||||
{
|
||||
type: "file",
|
||||
id: "file-persisted",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 5,
|
||||
previewUrl: "data:application/pdf;base64,aGVsbG8=",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("persists the file type for staged file attachments (round trip)", () => {
|
||||
const attachment = makeFile({
|
||||
id: "file-round-trip",
|
||||
previewUrl: "data:application/pdf;base64,aGVsbG8=",
|
||||
});
|
||||
useComposerDraftStore.getState().addImage(threadRef, attachment);
|
||||
useComposerDraftStore.getState().syncPersistedAttachments(threadRef, [
|
||||
{
|
||||
type: "file",
|
||||
id: attachment.id,
|
||||
name: attachment.name,
|
||||
mimeType: attachment.mimeType,
|
||||
sizeBytes: attachment.sizeBytes,
|
||||
dataUrl: attachment.previewUrl,
|
||||
},
|
||||
]);
|
||||
|
||||
const persistApi = useComposerDraftStore.persist as unknown as {
|
||||
getOptions: () => {
|
||||
partialize: (state: ReturnType<typeof useComposerDraftStore.getState>) => unknown;
|
||||
};
|
||||
};
|
||||
const persisted = persistApi.getOptions().partialize(useComposerDraftStore.getState()) as {
|
||||
draftsByThreadKey?: Record<string, { attachments?: Array<Record<string, unknown>> }>;
|
||||
};
|
||||
|
||||
expect(
|
||||
persisted.draftsByThreadKey?.[threadKeyFor(threadId, TEST_ENVIRONMENT_ID)]?.attachments?.[0],
|
||||
).toMatchObject({
|
||||
type: "file",
|
||||
id: "file-round-trip",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("composerDraftStore terminal contexts", () => {
|
||||
const threadId = ThreadId.make("thread-dedupe");
|
||||
const threadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, threadId);
|
||||
|
||||
@@ -136,4 +136,41 @@ describe("buildBootstrapInput", () => {
|
||||
expect(result.text).toContain("Attached image");
|
||||
expect(result.text).toContain("screenshot.png");
|
||||
});
|
||||
|
||||
it("captures mixed image and file attachment context in transcript blocks", () => {
|
||||
const result = buildBootstrapInput(
|
||||
[
|
||||
{
|
||||
id: messageId("u-mixed"),
|
||||
role: "user",
|
||||
text: "Compare the screenshot with the report.",
|
||||
attachments: [
|
||||
{
|
||||
type: "image",
|
||||
id: "img-1",
|
||||
name: "screenshot.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 2_048,
|
||||
},
|
||||
{
|
||||
type: "file",
|
||||
id: "file-1",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 4_096,
|
||||
},
|
||||
],
|
||||
createdAt: "2026-02-09T00:00:00.000Z",
|
||||
turnId: null,
|
||||
updatedAt: "2026-02-09T00:00:00.000Z",
|
||||
streaming: false,
|
||||
},
|
||||
],
|
||||
"What differs?",
|
||||
1_500,
|
||||
);
|
||||
|
||||
expect(result.text).toContain("[Attached image: screenshot.png]");
|
||||
expect(result.text).toContain("[Attached file: report.pdf]");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { formatAttachmentSizeLabel } from "./attachmentSize";
|
||||
|
||||
describe("formatAttachmentSizeLabel", () => {
|
||||
it("formats sub-kilobyte sizes in bytes", () => {
|
||||
expect(formatAttachmentSizeLabel(0)).toBe("0 B");
|
||||
expect(formatAttachmentSizeLabel(512)).toBe("512 B");
|
||||
expect(formatAttachmentSizeLabel(1_023)).toBe("1023 B");
|
||||
});
|
||||
|
||||
it("formats kilobyte sizes with one decimal", () => {
|
||||
expect(formatAttachmentSizeLabel(1_024)).toBe("1.0 KB");
|
||||
expect(formatAttachmentSizeLabel(2_048)).toBe("2.0 KB");
|
||||
expect(formatAttachmentSizeLabel(1_024 * 1_024 - 1)).toBe("1024.0 KB");
|
||||
});
|
||||
|
||||
it("formats megabyte sizes with one decimal", () => {
|
||||
expect(formatAttachmentSizeLabel(1_024 * 1_024)).toBe("1.0 MB");
|
||||
expect(formatAttachmentSizeLabel(2_202_010)).toBe("2.1 MB");
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,9 @@ import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import {
|
||||
ChatAttachment,
|
||||
ChatFileAttachment,
|
||||
ChatImageAttachment,
|
||||
DEFAULT_PROVIDER_INTERACTION_MODE,
|
||||
DEFAULT_RUNTIME_MODE,
|
||||
ModelSelection,
|
||||
@@ -743,3 +746,80 @@ it.effect("ModelSelection rejects malformed instance ids", () =>
|
||||
assert.strictEqual(result._tag, "Failure");
|
||||
}),
|
||||
);
|
||||
|
||||
// ── ChatAttachment: image/file union ──────────────────────────────────
|
||||
|
||||
const decodeChatAttachment = Schema.decodeUnknownEffect(ChatAttachment);
|
||||
const decodeChatAttachmentArray = Schema.decodeUnknownEffect(Schema.Array(ChatAttachment));
|
||||
const decodeChatImageAttachment = Schema.decodeUnknownEffect(ChatImageAttachment);
|
||||
const decodeChatFileAttachment = Schema.decodeUnknownEffect(ChatFileAttachment);
|
||||
|
||||
it.effect("ChatAttachment decodes a file attachment with a non-image mime type", () =>
|
||||
Effect.gen(function* () {
|
||||
const parsed = yield* decodeChatAttachment({
|
||||
type: "file",
|
||||
id: "file-1",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 2_048,
|
||||
});
|
||||
assert.strictEqual(parsed.type, "file");
|
||||
assert.strictEqual(parsed.name, "report.pdf");
|
||||
assert.strictEqual(parsed.mimeType, "application/pdf");
|
||||
assert.strictEqual(parsed.sizeBytes, 2_048);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("ChatAttachment arrays still decode legacy image-only attachments", () =>
|
||||
Effect.gen(function* () {
|
||||
const parsed = yield* decodeChatAttachmentArray([
|
||||
{
|
||||
type: "image",
|
||||
id: "img-1",
|
||||
name: "screenshot.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 1_024,
|
||||
},
|
||||
{
|
||||
type: "image",
|
||||
id: "img-2",
|
||||
name: "diagram.jpeg",
|
||||
mimeType: "image/jpeg",
|
||||
sizeBytes: 4_096,
|
||||
},
|
||||
]);
|
||||
assert.strictEqual(parsed.length, 2);
|
||||
assert.strictEqual(parsed[0]?.type, "image");
|
||||
assert.strictEqual(parsed[1]?.type, "image");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("ChatImageAttachment rejects non-image mime types", () =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Effect.exit(
|
||||
decodeChatImageAttachment({
|
||||
type: "image",
|
||||
id: "img-1",
|
||||
name: "report.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 2_048,
|
||||
}),
|
||||
);
|
||||
assert.strictEqual(result._tag, "Failure");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("ChatFileAttachment rejects sizeBytes over the 10MB limit", () =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Effect.exit(
|
||||
decodeChatFileAttachment({
|
||||
type: "file",
|
||||
id: "file-1",
|
||||
name: "big.bin",
|
||||
mimeType: "application/octet-stream",
|
||||
sizeBytes: 10 * 1024 * 1024 + 1,
|
||||
}),
|
||||
);
|
||||
assert.strictEqual(result._tag, "Failure");
|
||||
}),
|
||||
);
|
||||
Reference in new issue
Block a user