package audiostore import ( "context" "errors" "io" "net/http" "net/http/httptest" "testing" ) func TestNewNeedsAllOrNothing(t *testing.T) { if s, err := New(Config{}); s != nil || err != nil { t.Errorf("empty config = %v, %v; want nil, nil", s, err) } if _, err := New(Config{Endpoint: "http://garage:3900"}); err == nil { t.Error("half a config should be an error") } } // A fake S3 that holds objects in a map, addressed by path as Garage is. func TestPutGetDelete(t *testing.T) { objs := map[string][]byte{} types := map[string]string{} srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPut: b, _ := io.ReadAll(r.Body) objs[r.URL.Path], types[r.URL.Path] = b, r.Header.Get("Content-Type") case http.MethodGet: b, ok := objs[r.URL.Path] if !ok { w.Header().Set("Content-Type", "application/xml") w.WriteHeader(404) io.WriteString(w, `NoSuchKeygone`) return } w.Header().Set("Content-Type", types[r.URL.Path]) w.Write(b) case http.MethodDelete: delete(objs, r.URL.Path) w.WriteHeader(204) } })) defer srv.Close() s, err := New(Config{Endpoint: srv.URL, Region: "garage", Bucket: "usaco-audio", AccessKeyID: "GK1", SecretAccessKey: "s"}) if err != nil { t.Fatal(err) } ctx := context.Background() if err := s.Put(ctx, "moocast/2026-10-20-153000.mp3", "audio/mpeg", []byte("ID3")); err != nil { t.Fatal(err) } if string(objs["/usaco-audio/moocast/2026-10-20-153000.mp3"]) != "ID3" { t.Fatalf("objects = %v", objs) } o, err := s.Get(ctx, "moocast/2026-10-20-153000.mp3") if err != nil { t.Fatal(err) } b, _ := io.ReadAll(o.Body) o.Body.Close() if string(b) != "ID3" || o.ContentType != "audio/mpeg" { t.Errorf("get = %q %q", b, o.ContentType) } if err := s.Delete(ctx, "moocast/2026-10-20-153000.mp3"); err != nil { t.Fatal(err) } if _, err := s.Get(ctx, "moocast/2026-10-20-153000.mp3"); !errors.Is(err, ErrNotFound) { t.Errorf("get after delete: %v, want ErrNotFound", err) } }