package myfuzz

import (
	"testing"

	"linux-auth/internal/auth"
	"linux-auth/internal/db"
)

func FuzzAuth(f *testing.F) {
	err := db.Init("test_data.db")
	if err != nil {
		f.Fatalf("DB init error: %v", err)
	}
	f.Cleanup(func() {
		db.Close()
	})

	f.Add("admin", "admin123") // right admin
	f.Add("user1", "password1") // right user
	f.Add("admin", "admin") // wrong admin
	f.Add("user", "password") // wrong user
	f.Add("", "") // blank test

	f.Fuzz(func(t *testing.T, username string, password string) {
		ok, err := auth.Authenticate(username, password)
		if err != nil {
			t.Errorf("For username %q and password %q error: %v", username, password, err)
		}
		if ok {
			if (username == "admin" && password == "admin123") || (username == "user1" && password == "password1") {
				return
			}
			t.Errorf("Unexpected login for username %q and password %q", username, password)
		}
	})
}
