package buildverify

import (
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestMakefileSeparatesPureGoAndElectronBuilds(t *testing.T) {
	makefile := readFile(t, filepath.Join(desktopRoot(t), "Makefile"))
	build := targetBody(t, makefile, "build")
	require.Contains(t, build, "build-daemon")
	require.NotContains(t, build, "build-renderer")
	require.NotContains(t, build, "build-electron")

	daemon := targetBody(t, makefile, "build-daemon")
	require.Contains(t, daemon, "go build")
	require.NotContains(t, daemon, "npm")
	require.NotContains(t, daemon, "webassets")

	renderer := targetBody(t, makefile, "build-renderer")
	require.Contains(t, renderer, "npm run build:renderer")
}

func TestRootMakefileExposesExplicitProductionBuildEntrypoints(t *testing.T) {
	makefile := readFile(t, filepath.Join(repoRoot(t), "Makefile"))

	desktop := targetBody(t, makefile, `build\:desktop`)
	require.Contains(t, desktop, "$(MAKE) -C desktop desktop")

	daemon := targetBody(t, makefile, `build\:daemon`)
	require.Contains(t, daemon, "$(MAKE) -C desktop build-daemon")

	server := targetBody(t, makefile, `build\:server`)
	require.Contains(t, server, "scripts/agentteam-go-buildinfo-ldflags.sh")
	require.Contains(t, server, "./cmd/agentteam-server")
}

func TestDesktopMakefileBuildsOneExplicitElectronTarget(t *testing.T) {
	makefile := readFile(t, filepath.Join(desktopRoot(t), "Makefile"))
	desktop := targetBody(t, makefile, "desktop")

	require.Contains(t, desktop, "npm run dist")
	require.Contains(t, makefile, "https://github.com/anomalyco/opencode/releases/latest")
	require.Contains(t, makefile, "git describe --tags --abbrev=0")
	require.Contains(t, desktop, "AGENTTEAM_MANAGED_OPENCODE_VERSION")
	require.Contains(t, desktop, "models.json")
	require.Contains(t, desktop, "darwin/arm64")
	require.Contains(t, desktop, "darwin/x64")
	require.Contains(t, desktop, "win32/x64")
	require.NotContains(t, desktop, "build-daemon")
}

func TestMakefileGoTestsHaveNoFrontendPrerequisite(t *testing.T) {
	makefile := readFile(t, filepath.Join(desktopRoot(t), "Makefile"))
	for _, target := range []string{"test", "ci-required"} {
		body := targetBody(t, makefile, target)
		require.Contains(t, body, "go test ./...")
		require.NotContains(t, body, "npm")
		require.NotContains(t, body, "webassets")
		require.NotContains(t, body, "ensure-web")
	}
}
