TypeScript
fix(clients): attachment sizes just under 1 MB no longer read "1024 KB" (#29165)
Alex Southwellab06e2a265db
diff --git a/packages/client-runtime/src/state/attachments.test.ts b/packages/client-runtime/src/state/attachments.test.tsindex c1376bc786f..a5396dcc0c6 100644--- a/packages/client-runtime/src/state/attachments.test.ts+++ b/packages/client-runtime/src/state/attachments.test.ts@@ -9,7 +9,11 @@ import * as Effect from "effect/Effect";import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";import type { AtomCommand } from "./runtime.ts";-import { runAttachmentUploadCycle, verifyPersistedAttachmentUpload } from "./attachments.ts";+import {+ formatAttachmentSize,+ runAttachmentUploadCycle,+ verifyPersistedAttachmentUpload,+} from "./attachments.ts";const environmentId = EnvironmentId.make("environment-1");const registry = {} as AtomRegistry.AtomRegistry;@@ -129,3 +133,14 @@ describe("verifyPersistedAttachmentUpload", () => {expect(lookups).toBe(2);});});++describe("formatAttachmentSize", () => {+ it.each([+ [1_047_552, "1023 KB"],+ [1_047_553, "1.0 MB"],+ [1_048_575, "1.0 MB"],+ [1_048_576, "1.0 MB"],+ ])("formats %d bytes as %s", (sizeBytes, label) => {+ expect(formatAttachmentSize(sizeBytes)).toBe(label);+ });+});diff --git a/packages/client-runtime/src/state/attachments.ts b/packages/client-runtime/src/state/attachments.tsindex a78113a2010..800393470ff 100644--- a/packages/client-runtime/src/state/attachments.ts+++ b/packages/client-runtime/src/state/attachments.ts@@ -194,9 +194,9 @@ export function clampFileAttachmentUploadBytes(advertisedMaxUploadBytes: number)}export function formatAttachmentSize(sizeBytes: number): string {- return sizeBytes >= 1024 * 1024- ? `${(sizeBytes / (1024 * 1024)).toFixed(1)} MB`- : `${Math.max(1, Math.ceil(sizeBytes / 1024))} KB`;+ const kilobytes = Math.max(1, Math.ceil(sizeBytes / 1024));+ // Pick the unit after rounding, so 1023.5 KB reads "1.0 MB", not "1024 KB".+ return kilobytes >= 1024 ? `${(sizeBytes / (1024 * 1024)).toFixed(1)} MB` : `${kilobytes} KB`;}export function fileAttachmentTooLargeMessage(name: string, maxUploadBytes: number): string {