From 23ff43c92ee5852e4cf8f3608eddf889328803d0 Mon Sep 17 00:00:00 2001 From: kevin9327 <5299031+kevin9327@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:22:47 +0900 Subject: [PATCH] fix(ui): move attachment sizes to MB when rounding reaches 1024 KB Signed-off-by: kevin9327 <5299031+kevin9327@users.noreply.github.com> --- ui/src/api/chat/attachments.test.ts | 18 +++++++++++++++++- ui/src/api/chat/attachments.ts | 5 ++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ui/src/api/chat/attachments.test.ts b/ui/src/api/chat/attachments.test.ts index 71ec98a7be..6c9abeaa76 100644 --- a/ui/src/api/chat/attachments.test.ts +++ b/ui/src/api/chat/attachments.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { MAX_TOTAL_BYTES, isAllowedFile, mediaTypeOf, stageFiles, textLinks } from "./attachments"; +import { MAX_TOTAL_BYTES, formatBytes, isAllowedFile, mediaTypeOf, stageFiles, textLinks } from "./attachments"; const sized = (name: string, type: string, size: number) => ({ name, type, size }) as File; @@ -103,3 +103,19 @@ describe("stageFiles", () => { expect(stageFiles([], [sized("a.md", "", 10)]).error).toBeUndefined(); }); }); + +describe("formatBytes", () => { + it.each([ + [0, "0 B"], + [1023, "1023 B"], + [1024, "1 KB"], + [1536, "1.5 KB"], + [1048524, "1023.9 KB"], + [1048525, "1 MB"], + [1048575, "1 MB"], + [1048576, "1 MB"], + [5 * 1024 * 1024, "5 MB"], + ])("formats %d bytes as %s", (bytes, expected) => { + expect(formatBytes(bytes)).toBe(expected); + }); +}); diff --git a/ui/src/api/chat/attachments.ts b/ui/src/api/chat/attachments.ts index 97b42b230e..394616b490 100644 --- a/ui/src/api/chat/attachments.ts +++ b/ui/src/api/chat/attachments.ts @@ -118,7 +118,10 @@ export function fileNameFromUrl(url: string): string { export function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; - if (bytes < 1024 * 1024) return `${Number((bytes / 1024).toFixed(1))} KB`; + // Pick the unit after rounding: 1,048,525 bytes is 1023.95 KB, which + // rounds to "1024 KB" and should read as 1 MB. + const kb = Number((bytes / 1024).toFixed(1)); + if (kb < 1024) return `${kb} KB`; return `${Number((bytes / (1024 * 1024)).toFixed(1))} MB`; }