// Copyright (C) 2026 Yota Hamada
// SPDX-License-Identifier: GPL-3.0-or-later

package cmd_test

import (
	"fmt"
	"net"
	"testing"

	"github.com/dagucloud/dagu/internal/cmd"
	"github.com/dagucloud/dagu/internal/test"
	"github.com/stretchr/testify/require"
)

func TestServerCommand(t *testing.T) {
	t.Run("StartServer", func(t *testing.T) {
		th := test.SetupCommand(t)
		cancelWhenLogContains(t, th, "Server is starting")
		port := findPort(t)
		th.RunCommand(t, cmd.Server(), test.CmdTest{
			Args:        []string{"server", fmt.Sprintf("--port=%s", port)},
			ExpectedOut: []string{"Server is starting", port},
		})

	})
	t.Run("StartServerWithConfig", func(t *testing.T) {
		th := test.SetupCommand(t)
		cancelWhenLogContains(t, th, "54321")
		th.RunCommand(t, cmd.Server(), test.CmdTest{
			Args:        []string{"server", "--config", test.TestdataPath(t, "cli/config_test.yaml")},
			ExpectedOut: []string{"54321"},
		})
	})
}

// findPort finds an available port.
func findPort(t *testing.T) string {
	t.Helper()
	ln, err := net.Listen("tcp", ":0")
	require.NoError(t, err)
	port := ln.Addr().(*net.TCPAddr).Port
	_ = ln.Close()
	return fmt.Sprintf("%d", port)
}
