From 0ff98f1f1f432095fea7b6be9116db99bb71e81d Mon Sep 17 00:00:00 2001 From: Alex Savin Date: Wed, 2 Jul 2025 12:07:03 +0300 Subject: [PATCH] Add tests for timestamp, URL generation, VIN validation, and vehicle charging functionality - Implement unit tests for timestamp generation to ensure it returns numeric and increasing values. - Add tests for URL transformation based on generation type. - Create tests for VIN validation, including valid, invalid check digits, and incorrect lengths. - Introduce tests for vehicle charging functionality, covering scenarios for electric vehicles, non-electric vehicles, and missing remote features. - Log errors for JSON parsing issues in the client. --- utils_test.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 utils_test.go diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..707e8f5 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,121 @@ +package mysubaru + +import ( + "regexp" + "strconv" + "testing" + "time" +) + +func TestTimestamp(t *testing.T) { + ts1 := timestamp() + time.Sleep(1 * time.Millisecond) + ts2 := timestamp() + + // Should be numeric + if _, err := strconv.ParseInt(ts1, 10, 64); err != nil { + t.Errorf("timestamp() returned non-numeric string: %s", ts1) + } + // Should be increasing + if ts1 >= ts2 { + t.Errorf("timestamp() not increasing: %s >= %s", ts1, ts2) + } +} + +func TestUrlToGen(t *testing.T) { + tests := []struct { + url, gen, want string + }{ + {"https://host/api_gen/endpoint", "g1", "https://host/g1/endpoint"}, + {"https://host/api_gen/endpoint", "g2", "https://host/g2/endpoint"}, + {"https://host/api_gen/endpoint", "g3", "https://host/g2/endpoint"}, // g3 special case + {"https://host/api_gen/api_gen", "g1", "https://host/g1/g1"}, + {"https://host/other/endpoint", "g1", "https://host/other/endpoint"}, + } + for _, tt := range tests { + got := urlToGen(tt.url, tt.gen) + if got != tt.want { + t.Errorf("urlToGen(%q, %q) = %q, want %q", tt.url, tt.gen, got, tt.want) + } + } +} + +func TestVinCheck_Valid(t *testing.T) { + // Example valid VIN: 1HGCM82633A004352 (check digit is '3') + vin := "1HGCM82633A004352" + valid, corrected := vinCheck(vin) + if !valid { + t.Errorf("vinCheck(%q) = false, want true", vin) + } + if corrected != vin { + t.Errorf("vinCheck(%q) corrected VIN = %q, want %q", vin, corrected, vin) + } +} + +func TestVinCheck_InvalidCheckDigit(t *testing.T) { + vin := "1HGCM82633A004352" + // Change check digit (9th char) to '9' + badVin := vin[:8] + "9" + vin[9:] + valid, corrected := vinCheck(badVin) + if valid { + t.Errorf("vinCheck(%q) = true, want false", badVin) + } + // Should correct to original VIN + if corrected != vin { + t.Errorf("vinCheck(%q) corrected VIN = %q, want %q", badVin, corrected, vin) + } +} + +func TestVinCheck_WrongLength(t *testing.T) { + vin := "1234567890123456" // 16 chars + valid, corrected := vinCheck(vin) + if valid { + t.Errorf("vinCheck(%q) = true, want false", vin) + } + if corrected != "" { + t.Errorf("vinCheck(%q) corrected VIN = %q, want empty string", vin, corrected) + } +} + +func TestTranscodeDigits(t *testing.T) { + // Use a known VIN and manually compute the sum + vin := "1HGCM82633A004352" + sum := transcodeDigits(vin) + // Precomputed sum for this VIN is 311 (from online VIN calculator) + want := 311 + if sum != want { + t.Errorf("transcodeDigits(%q) = %d, want %d", vin, sum, want) + } +} + +func TestVinCheck_XCheckDigit(t *testing.T) { + // VIN with check digit 'X' + vin := "1M8GDM9AXKP042788" + valid, corrected := vinCheck(vin) + if !valid { + t.Errorf("vinCheck(%q) = false, want true", vin) + } + if corrected != vin { + t.Errorf("vinCheck(%q) corrected VIN = %q, want %q", vin, corrected, vin) + } +} + +func TestUrlToGen_NoApiGen(t *testing.T) { + url := "https://host/endpoint" + gen := "g1" + got := urlToGen(url, gen) + if got != url { + t.Errorf("urlToGen(%q, %q) = %q, want %q", url, gen, got, url) + } +} + +func TestTimestamp_Format(t *testing.T) { + ts := timestamp() + matched, err := regexp.MatchString(`^\d+$`, ts) + if err != nil { + t.Fatalf("regexp error: %v", err) + } + if !matched { + t.Errorf("timestamp() = %q, want only digits", ts) + } +} \ No newline at end of file