package gui

import (
	"io/fs"
	"strings"
	"testing"
)

// TestEmbed verifies that frontend/dist actually got embedded — easy to
// silently miss this and end up with a Wails window that 404s on every
// asset.
func TestEmbed(t *testing.T) {
	sub, err := fs.Sub(Assets, "frontend/dist")
	if err != nil {
		t.Fatalf("fs.Sub: %v", err)
	}
	var files []string
	fs.WalkDir(sub, ".", func(p string, d fs.DirEntry, err error) error {
		if err != nil || d.IsDir() {
			return err
		}
		files = append(files, p)
		return nil
	})
	if len(files) == 0 {
		t.Fatal("frontend/dist embed is empty — did you forget `npm run build`?")
	}
	if !sliceContains(files, "index.html") {
		t.Fatalf("no index.html in embed; got %v", files)
	}
	hasJS := false
	for _, f := range files {
		if strings.HasSuffix(f, ".js") {
			hasJS = true
			break
		}
	}
	if !hasJS {
		t.Fatalf("no .js bundle in embed; got %v", files)
	}
	t.Logf("embed contains %d files (looks healthy):", len(files))
	for _, f := range files {
		t.Logf("  %s", f)
	}
}

func sliceContains(xs []string, x string) bool {
	for _, v := range xs {
		if v == x {
			return true
		}
	}
	return false
}
