diff --git a/changelog.d/1782-social-size-rollover.fixed.md b/changelog.d/1782-social-size-rollover.fixed.md new file mode 100644 index 000000000..52e94c773 --- /dev/null +++ b/changelog.d/1782-social-size-rollover.fixed.md @@ -0,0 +1 @@ +Bot Social no longer prints `1024.0 KB` for a file just under a megabyte: a size that rounds up to 1024 KB is shown as `1.0 MB` ([#1782](https://github.com/ThinkFlowLab/FlyBot/issues/1782)). diff --git a/plugins/social/src/social.js b/plugins/social/src/social.js index 762e2dc13..867bc16f2 100644 --- a/plugins/social/src/social.js +++ b/plugins/social/src/social.js @@ -94,7 +94,13 @@ const sha256 = bytes => createHash('sha256').update(bytes).digest('hex') const MIME = { '.txt': 'text/plain', '.md': 'text/markdown', '.csv': 'text/csv', '.tsv': 'text/tab-separated-values', '.json': 'application/json', '.pdf': 'application/pdf', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation' } export const mimeOf = name => MIME[extname(String(name)).toLowerCase()] ?? 'application/octet-stream' const TEXTUAL = new Set(['text/plain', 'text/markdown', 'text/csv', 'text/tab-separated-values', 'application/json']) -const sizeLabel = n => (n < 1024 ? `${n} B` : n < 1048576 ? `${(n / 1024).toFixed(1)} KB` : `${(n / 1048576).toFixed(1)} MB`) +// The unit comes from the rounded KB figure: a size that rounds up to 1024 KB prints as MB, +// never as "1024.0 KB" (#1782). +export const sizeLabel = (n) => { + if (n < 1024) return `${n} B` + const kb = (n / 1024).toFixed(1) + return Number(kb) >= 1024 ? `${(n / 1048576).toFixed(1)} MB` : `${kb} KB` +} // Remote strings that reach model-visible text: one line, no brackets or markup, capped. export const cleanText = (value, fallback = '') => String(value ?? '').replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, ' ').replace(/[[\]"<>`]/g, '').replace(/\s+/g, ' ').trim().slice(0, 60) || fallback // Owner-only draft body: keep punctuation the friend will see; strip controls and cap. diff --git a/plugins/social/test/size-label.test.js b/plugins/social/test/size-label.test.js new file mode 100644 index 000000000..2249eeedd --- /dev/null +++ b/plugins/social/test/size-label.test.js @@ -0,0 +1,17 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { sizeLabel } from '../src/social.js' + +// #1782: just under a MiB the KB branch rounded to "1024.0 KB". The unit is chosen from the +// rounded KB figure, so a size that rounds to 1024 KB is printed as MB instead. +test('#1782: a size that rounds to 1024 KB rolls over to MB', () => { + assert.equal(sizeLabel(1_048_570), '1.0 MB') + assert.equal(sizeLabel(1_048_575), '1.0 MB') +}) + +test('#1782: the MB boundary and ordinary sizes keep their unit', () => { + assert.equal(sizeLabel(1_048_576), '1.0 MB') + assert.equal(sizeLabel(1_048_000), '1023.4 KB') + assert.equal(sizeLabel(1023), '1023 B') + assert.equal(sizeLabel(3_145_728), '3.0 MB') +})