1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { formatFileSize, formatUploadSpeed, formatCost, formatCWLResourceSize } from "./formatters";
7 describe('formatFileSize', () => {
8 it('should pick the largest unit', () => {
11 {input: 0, output: '0 B'},
12 {input: 1, output: '1 B'},
13 {input: 1023, output: '1023 B'},
14 {input: base, output: '1.0 KiB'},
15 {input: 1.1 * base, output: '1.1 KiB'},
16 {input: 1.5 * base, output: '1.5 KiB'},
17 {input: base ** 2, output: '1.0 MiB'},
18 {input: 1.5 * (base ** 2), output: '1.5 MiB'},
19 {input: base ** 3, output: '1.0 GiB'},
20 {input: base ** 4, output: '1.0 TiB'},
23 for (const { input, output } of testCases) {
24 expect(formatFileSize(input)).toBe(output);
28 it('should handle accidental empty string or undefined input', () => {
29 expect(formatFileSize('')).toBe('-');
30 expect(formatFileSize(undefined)).toBe('-');
33 it('should handle accidental non-empty string input', () => {
34 expect(formatFileSize('foo')).toBe('0 B');
38 describe('formatCWLResourceSize', () => {
39 it('should format bytes as MiB', () => {
40 const base = 1024 ** 2;
43 {input: 0, output: '0 MiB'},
44 {input: 1, output: '0 MiB'},
45 {input: base - 1, output: '1 MiB'},
46 {input: 2 * base, output: '2 MiB'},
47 {input: 1024 * base, output: '1024 MiB'},
48 {input: 10000 * base, output: '10000 MiB'},
51 for (const { input, output } of testCases) {
52 expect(formatCWLResourceSize(input)).toBe(output);
57 describe('formatUploadSpeed', () => {
58 it('should show speed less than 1MB/s', () => {
63 const result = formatUploadSpeed(0, speed, 0, 1);
66 expect(result).toBe('0.90 MB/s');
69 it('should show 5MB/s', () => {
74 const result = formatUploadSpeed(0, speed, 0, 1);
77 expect(result).toBe('5.23 MB/s');
81 describe('formatContainerCost', () => {
82 it('should correctly round to tenth of a cent', () => {
83 expect(formatCost(0.0)).toBe('$0');
84 expect(formatCost(0.125)).toBe('$0.125');
85 expect(formatCost(0.1254)).toBe('$0.125');
86 expect(formatCost(0.1255)).toBe('$0.126');
89 it('should round up any smaller value to 0.001', () => {
90 expect(formatCost(0.0)).toBe('$0');
91 expect(formatCost(0.001)).toBe('$0.001');
92 expect(formatCost(0.0001)).toBe('$0.001');
93 expect(formatCost(0.00001)).toBe('$0.001');