package coverart

import (
	"errors"
	"os"
	"path/filepath"
	"testing"
)

func TestExtractEmbeddedAlbumArt_NonexistentFile(t *testing.T) {
	_, err := ExtractEmbeddedAlbumArt("/nonexistent/path/track.mp3")
	if err == nil {
		t.Fatal("err = nil, want non-nil for nonexistent file")
	}
	if errors.Is(err, ErrNoEmbeddedArt) {
		t.Errorf("err is ErrNoEmbeddedArt; want a wrapped open error instead")
	}
}

func TestExtractEmbeddedAlbumArt_EmptyFile(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "empty.mp3")
	if err := os.WriteFile(path, []byte{}, 0o644); err != nil {
		t.Fatalf("seed: %v", err)
	}
	_, err := ExtractEmbeddedAlbumArt(path)
	if err == nil {
		t.Fatal("err = nil, want non-nil for empty file (tag.ReadFrom should fail)")
	}
	if errors.Is(err, ErrNoEmbeddedArt) {
		t.Errorf("err is ErrNoEmbeddedArt; want a wrapped tag-read error instead")
	}
}

func TestExtractEmbeddedAlbumArt_NonAudioFile(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "not-audio.mp3")
	if err := os.WriteFile(path, []byte("this is not an audio file"), 0o644); err != nil {
		t.Fatalf("seed: %v", err)
	}
	_, err := ExtractEmbeddedAlbumArt(path)
	if err == nil {
		t.Fatal("err = nil, want non-nil for non-audio bytes")
	}
	if errors.Is(err, ErrNoEmbeddedArt) {
		t.Errorf("err is ErrNoEmbeddedArt; want a wrapped tag-read error instead")
	}
}
