-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_format.sh
More file actions
executable file
·118 lines (99 loc) · 3.8 KB
/
Copy pathtest_format.sh
File metadata and controls
executable file
·118 lines (99 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
set -euo pipefail
# shellcheck source=_test_env.sh
source "$(dirname "${BASH_SOURCE[0]}")/_test_env.sh"
# Unit tests for formatting functions used by dashboard.sh and costs.sh.
# No Docker or API key required.
PASS=0
FAIL=0
assert_eq() {
local label="$1" expected="$2" actual="$3"
if [ "$expected" = "$actual" ]; then
echo " PASS: ${label}"
PASS=$((PASS + 1))
else
echo " FAIL: ${label}"
echo " expected: ${expected}"
echo " actual: ${actual}"
FAIL=$((FAIL + 1))
fi
}
# --- Functions under test (from dashboard.sh / costs.sh) ---
format_duration() {
local s=$1
if [ "$s" -ge 3600 ]; then
printf '%dh %02dm' $((s / 3600)) $(((s % 3600) / 60))
elif [ "$s" -ge 60 ]; then
printf '%dm %02ds' $((s / 60)) $((s % 60))
else
printf '%ds' "$s"
fi
}
format_duration_ms() {
format_duration $(( ${1:-0} / 1000 ))
}
format_tokens() {
local n=${1:-0}
if [ "$n" -ge 1000000 ]; then
printf '%.1fM' "$(echo "$n / 1000000" | bc -l)"
elif [ "$n" -ge 1000 ]; then
printf '%.0fk' "$(echo "$n / 1000" | bc -l)"
else
printf '%d' "$n"
fi
}
format_cost() {
printf '$%.2f' "${1:-0}"
}
# ============================================================
echo "=== 1. format_duration ==="
assert_eq "0 seconds" "0s" "$(format_duration 0)"
assert_eq "1 second" "1s" "$(format_duration 1)"
assert_eq "59 seconds" "59s" "$(format_duration 59)"
assert_eq "60 seconds" "1m 00s" "$(format_duration 60)"
assert_eq "61 seconds" "1m 01s" "$(format_duration 61)"
assert_eq "125 seconds" "2m 05s" "$(format_duration 125)"
assert_eq "3599 seconds" "59m 59s" "$(format_duration 3599)"
assert_eq "3600 seconds" "1h 00m" "$(format_duration 3600)"
assert_eq "3661 seconds" "1h 01m" "$(format_duration 3661)"
assert_eq "7200 seconds" "2h 00m" "$(format_duration 7200)"
assert_eq "7384 seconds" "2h 03m" "$(format_duration 7384)"
# ============================================================
echo ""
echo "=== 2. format_duration_ms ==="
assert_eq "0 ms" "0s" "$(format_duration_ms 0)"
assert_eq "500 ms" "0s" "$(format_duration_ms 500)"
assert_eq "1000 ms" "1s" "$(format_duration_ms 1000)"
assert_eq "5000 ms" "5s" "$(format_duration_ms 5000)"
assert_eq "65000 ms" "1m 05s" "$(format_duration_ms 65000)"
assert_eq "3600000 ms" "1h 00m" "$(format_duration_ms 3600000)"
assert_eq "empty" "0s" "$(format_duration_ms)"
# ============================================================
echo ""
echo "=== 3. format_tokens ==="
assert_eq "0" "0" "$(format_tokens 0)"
assert_eq "1" "1" "$(format_tokens 1)"
assert_eq "999" "999" "$(format_tokens 999)"
assert_eq "1000" "1k" "$(format_tokens 1000)"
assert_eq "1500" "2k" "$(format_tokens 1500)"
assert_eq "15000" "15k" "$(format_tokens 15000)"
assert_eq "999999" "1000k" "$(format_tokens 999999)"
assert_eq "1000000" "1.0M" "$(format_tokens 1000000)"
assert_eq "1500000" "1.5M" "$(format_tokens 1500000)"
assert_eq "25000000" "25.0M" "$(format_tokens 25000000)"
assert_eq "empty" "0" "$(format_tokens)"
# ============================================================
echo ""
echo "=== 4. format_cost ==="
assert_eq "zero" '$0.00' "$(format_cost 0)"
assert_eq "small" '$0.13' "$(format_cost 0.1292)"
assert_eq "round up" '$0.50' "$(format_cost 0.499)"
assert_eq "dollar" '$1.00' "$(format_cost 1)"
assert_eq "large" '$12.35' "$(format_cost 12.345)"
assert_eq "empty" '$0.00' "$(format_cost)"
# ============================================================
echo ""
echo "==============================="
echo " ${PASS} passed, ${FAIL} failed"
echo "==============================="
[ "$FAIL" -eq 0 ]