Merge branch '21654-net-imap-downgrade'. Refs #21654
[arvados.git] / services / workbench2 / src / common / formatters.test.ts
index 048779727e4865724e1bdcd67e862d3012e6a361..cde1a4f9d253f987c3c83962c83cdb197bf5dc9b 100644 (file)
@@ -2,7 +2,57 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { formatUploadSpeed, formatContainerCost } 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', () => {
@@ -30,16 +80,16 @@ describe('formatUploadSpeed', () => {
 
 describe('formatContainerCost', () => {
     it('should correctly round to tenth of a cent', () => {
-        expect(formatContainerCost(0.0)).toBe('$0');
-        expect(formatContainerCost(0.125)).toBe('$0.125');
-        expect(formatContainerCost(0.1254)).toBe('$0.125');
-        expect(formatContainerCost(0.1255)).toBe('$0.126');
+        expect(formatCost(0.0)).toBe('$0');
+        expect(formatCost(0.125)).toBe('$0.125');
+        expect(formatCost(0.1254)).toBe('$0.125');
+        expect(formatCost(0.1255)).toBe('$0.126');
     });
 
     it('should round up any smaller value to 0.001', () => {
-        expect(formatContainerCost(0.0)).toBe('$0');
-        expect(formatContainerCost(0.001)).toBe('$0.001');
-        expect(formatContainerCost(0.0001)).toBe('$0.001');
-        expect(formatContainerCost(0.00001)).toBe('$0.001');
+        expect(formatCost(0.0)).toBe('$0');
+        expect(formatCost(0.001)).toBe('$0.001');
+        expect(formatCost(0.0001)).toBe('$0.001');
+        expect(formatCost(0.00001)).toBe('$0.001');
     });
 });