Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(files): display zero-byte file sizes (#3608)
Tony Guane98249a624d4
diff --git a/src/commands/files.ts b/src/commands/files.tsindex d38bd5dcf0..391644699f 100644--- a/src/commands/files.ts+++ b/src/commands/files.ts@@ -16,7 +16,7 @@ interface FileRecord {filename: string;storage_path: string;mime_type: string | null;- size_bytes: number;+ size_bytes: number | bigint | string | null;content_hash: string;metadata: Record<string, unknown>;created_at: string;@@ -42,6 +42,14 @@ function fileHash(filePath: string): string {return createHash('sha256').update(content).digest('hex');}+export function formatFileSizeKb(rawSizeBytes: number | bigint | string | null): string {+ if (rawSizeBytes == null) return '?';+ const sizeBytes = Number(rawSizeBytes);+ return Number.isFinite(sizeBytes) && sizeBytes >= 0+ ? `${Math.round(sizeBytes / 1024)}KB`+ : '?';+}+export async function runFiles(engine: BrainEngine, args: string[]) {const subcommand = args[0];@@ -116,7 +124,7 @@ async function listFiles(engine: BrainEngine, slug?: string) {console.log(`${rows.length} file(s):`);for (const row of rows) {- const size = row.size_bytes ? `${Math.round(Number(row.size_bytes) / 1024)}KB` : '?';+ const size = formatFileSizeKb(row.size_bytes as FileRecord['size_bytes']);console.log(` ${row.page_slug || '(unlinked)'} / ${row.filename} [${size}, ${row.mime_type || '?'}]`);}}diff --git a/test/files.test.ts b/test/files.test.tsindex 8d8a7e58a3..897c5f4119 100644--- a/test/files.test.ts+++ b/test/files.test.ts@@ -4,7 +4,7 @@ import { join, basename } from 'path';import { createHash } from 'crypto';import { extname } from 'path';import { tmpdir } from 'os';-import { collectFiles } from '../src/commands/files.ts';+import { collectFiles, formatFileSizeKb } from '../src/commands/files.ts';import { operationsByName } from '../src/core/operations.ts';import * as db from '../src/core/db.ts';@@ -51,6 +51,25 @@ afterAll(() => {rmSync(TMP, { recursive: true, force: true });});+describe('formatFileSizeKb', () => {+ test('formats number, bigint, and string database values', () => {+ expect(formatFileSizeKb(35 * 1024)).toBe('35KB');+ expect(formatFileSizeKb(35n * 1024n)).toBe('35KB');+ expect(formatFileSizeKb('35840')).toBe('35KB');+ });++ test('preserves zero-byte files instead of reporting an unknown size', () => {+ expect(formatFileSizeKb(0)).toBe('0KB');+ expect(formatFileSizeKb(0n)).toBe('0KB');+ });++ test('reports missing or invalid sizes as unknown', () => {+ expect(formatFileSizeKb(null)).toBe('?');+ expect(formatFileSizeKb('not-a-number')).toBe('?');+ expect(formatFileSizeKb(-1)).toBe('?');+ });+});+describe('getMimeType', () => {test('returns correct MIME for .jpg', () => {expect(getMimeType('photo.jpg')).toBe('image/jpeg');