19675: Add CWL size formatter and display MiB max ram/disk request alongside
[arvados.git] / services / workbench2 / src / common / formatters.test.ts
index 7f9ffa0c35618007f32cf3b2de0b9623a22d00fd..cde1a4f9d253f987c3c83962c83cdb197bf5dc9b 100644 (file)
@@ -2,7 +2,57 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { formatUploadSpeed, formatCost } from "./formatters";
+import { formatFileSize, formatUploadSpeed, formatCost, formatCWLResourceSize } from "./formatters";
+
+describe('formatFileSize', () => {
+    it('should pick the largest unit', () => {
+        const base = 1024;
+        const testCases = [
+            {input: 0, output: '0 B'},
+            {input: 1, output: '1 B'},
+            {input: 1023, output: '1023 B'},
+            {input: base, output: '1.0 KiB'},
+            {input: 1.1 * base, output: '1.1 KiB'},
+            {input: 1.5 * base, output: '1.5 KiB'},
+            {input: base ** 2, output: '1.0 MiB'},
+            {input: 1.5 * (base ** 2), output: '1.5 MiB'},
+            {input: base ** 3, output: '1.0 GiB'},
+            {input: base ** 4, output: '1.0 TiB'},
+        ];
+
+        for (const { input, output } of testCases) {
+            expect(formatFileSize(input)).toBe(output);
+        }
+    });
+
+    it('should handle accidental empty string or undefined input', () => {
+        expect(formatFileSize('')).toBe('-');
+        expect(formatFileSize(undefined)).toBe('-');
+    });
+
+    it('should handle accidental non-empty string input', () => {
+        expect(formatFileSize('foo')).toBe('0 B');
+    });
+});
+
+describe('formatCWLResourceSize', () => {
+    it('should format bytes as MiB', () => {
+        const base = 1024 ** 2;
+
+        const testCases = [
+            {input: 0, output: '0 MiB'},
+            {input: 1, output: '0 MiB'},
+            {input: base - 1, output: '1 MiB'},
+            {input: 2 * base, output: '2 MiB'},
+            {input: 1024 * base, output: '1024 MiB'},
+            {input: 10000 * base, output: '10000 MiB'},
+        ];
+
+        for (const { input, output } of testCases) {
+            expect(formatCWLResourceSize(input)).toBe(output);
+        }
+    });
+});
 
 describe('formatUploadSpeed', () => {
     it('should show speed less than 1MB/s', () => {