Skip to content

Commit ab06e2a

Browse files
saphidclaude
andauthored
fix(clients): attachment sizes just under 1 MB no longer read "1024 KB" (pingdotgg#29165)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8430db6 commit ab06e2a

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎packages/client-runtime/src/state/attachments.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import * as Effect from "effect/Effect";
99
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
1010

1111
import type { AtomCommand } from "./runtime.ts";
12-
import { runAttachmentUploadCycle, verifyPersistedAttachmentUpload } from "./attachments.ts";
12+
import {
13+
formatAttachmentSize,
14+
runAttachmentUploadCycle,
15+
verifyPersistedAttachmentUpload,
16+
} from "./attachments.ts";
1317

1418
const environmentId = EnvironmentId.make("environment-1");
1519
const registry = {} as AtomRegistry.AtomRegistry;
@@ -129,3 +133,14 @@ describe("verifyPersistedAttachmentUpload", () => {
129133
expect(lookups).toBe(2);
130134
});
131135
});
136+
137+
describe("formatAttachmentSize", () => {
138+
it.each([
139+
[1_047_552, "1023 KB"],
140+
[1_047_553, "1.0 MB"],
141+
[1_048_575, "1.0 MB"],
142+
[1_048_576, "1.0 MB"],
143+
])("formats %d bytes as %s", (sizeBytes, label) => {
144+
expect(formatAttachmentSize(sizeBytes)).toBe(label);
145+
});
146+
});

‎packages/client-runtime/src/state/attachments.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ export function clampFileAttachmentUploadBytes(advertisedMaxUploadBytes: number)
194194
}
195195

196196
export function formatAttachmentSize(sizeBytes: number): string {
197-
return sizeBytes >= 1024 * 1024
198-
? `${(sizeBytes / (1024 * 1024)).toFixed(1)} MB`
199-
: `${Math.max(1, Math.ceil(sizeBytes / 1024))} KB`;
197+
const kilobytes = Math.max(1, Math.ceil(sizeBytes / 1024));
198+
// Pick the unit after rounding, so 1023.5 KB reads "1.0 MB", not "1024 KB".
199+
return kilobytes >= 1024 ? `${(sizeBytes / (1024 * 1024)).toFixed(1)} MB` : `${kilobytes} KB`;
200200
}
201201

202202
export function fileAttachmentTooLargeMessage(name: string, maxUploadBytes: number): string {

0 commit comments

Comments
 (0)