21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / workbench2 / src / common / formatters.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { formatFileSize, formatUploadSpeed, formatCost, formatCWLResourceSize } from "./formatters";
6
7 describe('formatFileSize', () => {
8     it('should pick the largest unit', () => {
9         const base = 1024;
10         const testCases = [
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'},
21         ];
22
23         for (const { input, output } of testCases) {
24             expect(formatFileSize(input)).toBe(output);
25         }
26     });
27
28     it('should handle accidental empty string or undefined input', () => {
29         expect(formatFileSize('')).toBe('-');
30         expect(formatFileSize(undefined)).toBe('-');
31     });
32
33     it('should handle accidental non-empty string input', () => {
34         expect(formatFileSize('foo')).toBe('0 B');
35     });
36 });
37
38 describe('formatCWLResourceSize', () => {
39     it('should format bytes as MiB', () => {
40         const base = 1024 ** 2;
41
42         const testCases = [
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'},
49         ];
50
51         for (const { input, output } of testCases) {
52             expect(formatCWLResourceSize(input)).toBe(output);
53         }
54     });
55 });
56
57 describe('formatUploadSpeed', () => {
58     it('should show speed less than 1MB/s', () => {
59         // given
60         const speed = 900;
61
62         // when
63         const result = formatUploadSpeed(0, speed, 0, 1);
64
65         // then
66         expect(result).toBe('0.90 MB/s');
67     });
68
69     it('should show 5MB/s', () => {
70         // given
71         const speed = 5230;
72
73         // when
74         const result = formatUploadSpeed(0, speed, 0, 1);
75
76         // then
77         expect(result).toBe('5.23 MB/s');
78     });
79 });
80
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');
87     });
88
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');
94     });
95 });