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) } }