go-blockchain/commands_test.go
Virgil 9780153778 refactor(blockchain): reject unexpected chain args
Co-Authored-By: Charon <charon@lethean.io>
2026-04-04 10:00:05 +00:00

104 lines
3.4 KiB
Go

// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// SPDX-License-Identifier: EUPL-1.2
package blockchain
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddChainCommands_Good_RegistersParent(t *testing.T) {
root := &cobra.Command{Use: "test"}
AddChainCommands(root)
chainCmd, _, err := root.Find([]string{"chain"})
require.NoError(t, err)
assert.Equal(t, "chain", chainCmd.Name())
assert.Equal(t, "Manage the Lethean blockchain node", chainCmd.Short)
assert.Equal(t, "Manage the Lethean blockchain node: sync and explore.", chainCmd.Long)
assert.Equal(t, "core-chain chain explorer --data-dir ~/.lethean/chain\ncore-chain chain sync --daemon\ncore-chain chain sync --stop", chainCmd.Example)
}
func TestAddChainCommands_Good_HasSubcommands(t *testing.T) {
root := &cobra.Command{Use: "test"}
AddChainCommands(root)
chainCmd, _, _ := root.Find([]string{"chain"})
var names []string
for _, sub := range chainCmd.Commands() {
names = append(names, sub.Name())
}
assert.Contains(t, names, "explorer")
assert.Contains(t, names, "sync")
}
func TestAddChainCommands_Good_PersistentFlags(t *testing.T) {
root := &cobra.Command{Use: "test"}
AddChainCommands(root)
chainCmd, _, _ := root.Find([]string{"chain"})
assert.NotNil(t, chainCmd.PersistentFlags().Lookup("data-dir"))
assert.NotNil(t, chainCmd.PersistentFlags().Lookup("seed"))
assert.NotNil(t, chainCmd.PersistentFlags().Lookup("testnet"))
}
func TestNewChainExplorerCommand_Good_Metadata(t *testing.T) {
dataDir := ""
seed := ""
testnet := false
cmd := newChainExplorerCommand(&dataDir, &seed, &testnet)
assert.Equal(t, "explorer", cmd.Use)
assert.Equal(t, "Inspect blocks in a terminal UI", cmd.Short)
assert.Equal(t, "Browse blocks and transactions in a terminal UI while the node syncs.", cmd.Long)
assert.Equal(t, "core-chain chain explorer --data-dir ~/.lethean/chain", cmd.Example)
assert.Equal(t, "Chain Explorer", chainExplorerFrameTitle)
assert.ErrorContains(t, cmd.Args(cmd, []string{"unexpected"}), "unknown command")
}
func TestNewChainSyncCommand_Good_Metadata(t *testing.T) {
dataDir := ""
seed := ""
testnet := false
cmd := newChainSyncCommand(&dataDir, &seed, &testnet)
assert.Equal(t, "sync", cmd.Use)
assert.Equal(t, "Synchronise from P2P peers", cmd.Short)
assert.Equal(t, "Synchronise the blockchain from P2P peers in foreground or daemon mode.", cmd.Long)
assert.Equal(t, "core-chain chain sync\ncore-chain chain sync --daemon\ncore-chain chain sync --stop", cmd.Example)
assert.ErrorContains(t, cmd.Args(cmd, []string{"unexpected"}), "unknown command")
}
func TestAddChainCommands_Bad_RejectsUnexpectedArgs(t *testing.T) {
root := &cobra.Command{Use: "test"}
AddChainCommands(root)
chainCmd, _, err := root.Find([]string{"chain"})
require.NoError(t, err)
require.ErrorContains(t, chainCmd.Args(chainCmd, []string{"unexpected"}), "unknown command")
}
func TestNewChainSyncCommand_Bad_RejectsConflictingFlags(t *testing.T) {
dataDir := ""
seed := ""
testnet := false
cmd := newChainSyncCommand(&dataDir, &seed, &testnet)
require.NoError(t, cmd.Flags().Set("daemon", "true"))
require.NoError(t, cmd.Flags().Set("stop", "true"))
err := cmd.RunE(cmd, nil)
require.Error(t, err)
assert.EqualError(t, err, "blockchain: --daemon and --stop are mutually exclusive")
}