package bundler

import (
	"strings"
	"testing"
)

// CompileDev should Solid-compile the component AND wrap it with solid-refresh
// HMR instrumentation: an import from the solid-refresh runtime, a registry hot
// boundary, and an import.meta.hot accept (bundler:"esm"). None of these appear
// in the plain (prod) Compile output.
func TestCompileDevSolidRefresh(t *testing.T) {
	src := `import { createSignal } from "solid-js";
export default function Counter() {
    const [c, setC] = createSignal(0);
    return <button onclick={() => setC(c() + 1)}>Count: {c()}</button>;
}
`
	dev, err := CompileDev(src, "ui/Counter.tsx")
	if err != nil {
		t.Fatalf("CompileDev: %v", err)
	}
	t.Logf("DEV OUTPUT:\n%s", dev)

	for _, want := range []string{
		`solid-refresh`,   // runtime import
		"$$registry",      // HMR registry boundary
		"import.meta.hot", // esm bundler hot API
		"_tmpl$",          // still Solid-compiled
	} {
		if !strings.Contains(dev, want) {
			t.Errorf("dev output missing %q", want)
		}
	}

	// Prod compile of the same source must NOT carry refresh instrumentation,
	// and (regression for the mode-keyed cache) must differ from the dev output.
	prod, err := Compile(src, "ui/Counter.tsx")
	if err != nil {
		t.Fatalf("Compile: %v", err)
	}
	if strings.Contains(prod, "solid-refresh") || strings.Contains(prod, "import.meta.hot") {
		t.Errorf("prod output leaked HMR instrumentation:\n%s", prod)
	}
	if prod == dev {
		t.Errorf("mode-keyed cache broken: dev and prod output identical")
	}
}
