TestCalculateTimeRemaining tests the calculateTimeRemaining function
(t *testing.T)
| 909 | |
| 910 | // TestCalculateTimeRemaining tests the calculateTimeRemaining function |
| 911 | func TestCalculateTimeRemaining(t *testing.T) { |
| 912 | tests := []struct { |
| 913 | name string |
| 914 | stopTimeStr string |
| 915 | expected string |
| 916 | }{ |
| 917 | { |
| 918 | name: "empty stop time", |
| 919 | stopTimeStr: "", |
| 920 | expected: "N/A", |
| 921 | }, |
| 922 | { |
| 923 | name: "invalid format", |
| 924 | stopTimeStr: "invalid-date-format", |
| 925 | expected: "Invalid", |
| 926 | }, |
| 927 | } |
| 928 | |
| 929 | for _, tt := range tests { |
| 930 | t.Run(tt.name, func(t *testing.T) { |
| 931 | result := calculateTimeRemaining(tt.stopTimeStr) |
| 932 | if result != tt.expected { |
| 933 | t.Errorf("calculateTimeRemaining(%q) = %q, want %q", tt.stopTimeStr, result, tt.expected) |
| 934 | } |
| 935 | }) |
| 936 | } |
| 937 | |
| 938 | // Test with future time - this will test the logic but the exact result depends on current time |
| 939 | t.Run("future time formatting", func(t *testing.T) { |
| 940 | // Create a time 2 hours and 30 minutes in the future |
| 941 | // Add a small buffer to account for execution time |
| 942 | futureTime := time.Now().Add(2*time.Hour + 30*time.Minute + 1*time.Second) |
| 943 | stopTimeStr := futureTime.Format("2006-01-02 15:04:05") |
| 944 | |
| 945 | result := calculateTimeRemaining(stopTimeStr) |
| 946 | |
| 947 | // Should contain "h" and "m" for hours and minutes |
| 948 | if !strings.Contains(result, "h") || !strings.Contains(result, "m") { |
| 949 | t.Errorf("calculateTimeRemaining() for future time should contain hours and minutes, got: %q", result) |
| 950 | } |
| 951 | |
| 952 | // Should not be "Expired", "Invalid", or "N/A" |
| 953 | if result == "Expired" || result == "Invalid" || result == "N/A" { |
| 954 | t.Errorf("calculateTimeRemaining() for future time should not be %q", result) |
| 955 | } |
| 956 | }) |
| 957 | |
| 958 | // Test with past time |
| 959 | t.Run("past time - expired", func(t *testing.T) { |
| 960 | // Create a time 1 hour in the past |
| 961 | pastTime := time.Now().Add(-1 * time.Hour) |
| 962 | stopTimeStr := pastTime.Format("2006-01-02 15:04:05") |
| 963 | |
| 964 | result := calculateTimeRemaining(stopTimeStr) |
| 965 | if result != "Expired" { |
| 966 | t.Errorf("calculateTimeRemaining() for past time = %q, want %q", result, "Expired") |
| 967 | } |
| 968 | }) |
nothing calls this directly
no test coverage detected