Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1782-social-size-rollover.fixed.md
Original file line number Diff line number Diff line change
@@ -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)).
8 changes: 7 additions & 1 deletion plugins/social/src/social.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions plugins/social/test/size-label.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
Loading