//go:build dev

package jsbundler

import (
	"strings"
	"testing"

	esbuild "github.com/evanw/esbuild/pkg/api"
)

// hmrClientJS is a hand-written JS blob served to the browser; a syntax slip in it
// silently breaks every hot update, and the headless-browser smoke test is skipped
// off Linux/macOS. Parse it as an ES module here so a bad edit fails at test time,
// and assert the recompile-indicator hooks are wired.
func TestHMRClientJSValid(t *testing.T) {
	res := esbuild.Transform(hmrClientJS, esbuild.TransformOptions{
		Loader:   esbuild.LoaderJS,
		Format:   esbuild.FormatESModule,
		LogLevel: esbuild.LogLevelSilent,
	})
	if len(res.Errors) > 0 {
		msgs := esbuild.FormatMessages(res.Errors, esbuild.FormatMessagesOptions{})
		t.Fatalf("HMR client JS failed to parse:\n%s", strings.Join(msgs, "\n"))
	}
	for _, want := range []string{"createHotContext", "hmrBusyStart", "hmrBusyEnd", "recompiling",
		"showErrorOverlay", "clearErrorOverlay", "errorEpoch"} {
		if !strings.Contains(hmrClientJS, want) {
			t.Errorf("HMR client missing %q", want)
		}
	}
}
