Skip to content

Commit

Permalink
test(cardinal): add test for create, start, stop, purge (#32)
Browse files Browse the repository at this point in the history
Closes: WORLD-362

## Overview

Adding unit test for create, start, stop, purge command

## Brief Changelog

- Added new test that cover create, start, stop, and purge command

## Testing and Verifying

Test will do this scenario
- run create command to clone starter-game-template
- start the cardinal
- hit `/health` endpoint and verify the response
- stop the cardinal
- hit `/health` endpoint and make sure it will be error
- purge the cardinal
  • Loading branch information
zulkhair committed Jan 26, 2024
1 parent 445e2c3 commit 7fe2073
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cmd/world/cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cardinal

import (
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/dependency"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/tea/style"
Expand All @@ -10,7 +11,6 @@ import (
func init() {
// Register subcommands - `world cardinal [subcommand]`
BaseCmd.AddCommand(startCmd, devCmd, restartCmd, purgeCmd, stopCmd)

// Add --debug flag
logger.AddLogFlag(startCmd, devCmd, restartCmd, purgeCmd, stopCmd)
}
Expand All @@ -22,7 +22,7 @@ var BaseCmd = &cobra.Command{
Short: "Manage your Cardinal game shard project",
Long: style.CLIHeader("World CLI — CARDINAL", "Manage your Cardinal game shard project"),
GroupID: "Core",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
return dependency.Check(
dependency.Go,
dependency.Git,
Expand Down
36 changes: 20 additions & 16 deletions cmd/world/root/create.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package root

import (
"github.com/spf13/cobra"
"io"
"pkg.world.dev/world-cli/common/logger"
"strings"

"github.com/charmbracelet/bubbles/textinput"
"github.com/spf13/cobra"

tea "github.com/charmbracelet/bubbletea"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/tea_cmd"
"pkg.world.dev/world-cli/tea/component/steps"
"pkg.world.dev/world-cli/tea/style"
Expand All @@ -22,21 +22,25 @@ const TemplateGitUrl = "https://github.com/Argus-Labs/starter-game-template.git"

// createCmd creates a new World Engine project based on starter-game-template
// Usage: `world cardinal create [directory_name]`
var createCmd = &cobra.Command{
Use: "create [directory_name]",
Short: "Create a World Engine game shard from scratch",
Long: `Create a World Engine game shard based on https://github.com/Argus-Labs/starter-game-template.
func getCreateCmd(writer io.Writer) *cobra.Command {
createCmd := &cobra.Command{
Use: "create [directory_name]",
Short: "Create a World Engine game shard from scratch",
Long: `Create a World Engine game shard based on https://github.com/Argus-Labs/starter-game-template.
If [directory_name] is set, it will automatically clone the starter project into that directory.
Otherwise, it will prompt you to enter a directory name.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
logger.SetDebugMode(cmd)
p := tea.NewProgram(NewWorldCreateModel(args))
if _, err := p.Run(); err != nil {
return err
}
return nil
},
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
logger.SetDebugMode(cmd)
p := tea.NewProgram(NewWorldCreateModel(args), tea.WithOutput(writer))
if _, err := p.Run(); err != nil {
return err
}
return nil
},
}

return createCmd
}

//////////////////////
Expand Down
3 changes: 2 additions & 1 deletion cmd/world/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func init() {

// Register base commands
doctorCmd := getDoctorCmd(os.Stdout)
rootCmd.AddCommand(doctorCmd, versionCmd)
createCmd := getCreateCmd(os.Stdout)
rootCmd.AddCommand(createCmd, doctorCmd, versionCmd)

// Register subcommands
rootCmd.AddCommand(cardinal.BaseCmd)
Expand Down
55 changes: 55 additions & 0 deletions cmd/world/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package root

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cobra"
"net/http"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -95,3 +98,55 @@ func TestExecuteDoctorCommand(t *testing.T) {
assert.Check(t, count > 0, "dependencies %q is not listed in dependencies checking", dep)
}
}

func TestCreateStartStopPurge(t *testing.T) {
// Create Cardinal
gameDir, err := os.MkdirTemp("", "game-template")
assert.NilError(t, err)

// Remove dir
defer os.RemoveAll(gameDir)

// set tea ouput to variable
teaOut := &bytes.Buffer{}
createCmd := getCreateCmd(teaOut)
createCmd.SetArgs([]string{gameDir})

err = createCmd.Execute()
assert.NilError(t, err)

// Start cardinal
os.Chdir(gameDir)
rootCmd.SetArgs([]string{"cardinal", "start", "--build", "--detach"})
err = rootCmd.Execute()
assert.NilError(t, err)

// Check cardinal health
resp, err := http.Get("http://127.0.0.1:3333/health")
assert.NilError(t, err)
assert.Equal(t, resp.StatusCode, 200)
var healthResponse struct {
IsServerRunning bool
IsGameLoopRunning bool
}
err = json.NewDecoder(resp.Body).Decode(&healthResponse)
assert.NilError(t, err)
assert.Assert(t, healthResponse.IsServerRunning)
assert.Assert(t, healthResponse.IsGameLoopRunning)

// Stop cardinal
rootCmd.SetArgs([]string{"cardinal", "stop"})
err = rootCmd.Execute()
assert.NilError(t, err)

// Check cardinal health
_, err = http.Get("http://127.0.0.1:3333/health")
assert.Error(t, err,
"Get \"http://127.0.0.1:3333/health\": dial tcp 127.0.0.1:3333: connect: connection refused")

// Purge cardinal
rootCmd.SetArgs([]string{"cardinal", "purge"})
err = rootCmd.Execute()
assert.NilError(t, err)

}

0 comments on commit 7fe2073

Please sign in to comment.