@ktmcp-cli/nowpayments 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Configuration management for NOWPayments CLI
3
+ *
4
+ * Handles API keys, environment selection, and persistent configuration.
5
+ */
6
+
7
+ const fs = require('fs');
8
+ const path = require('path');
9
+ const os = require('os');
10
+
11
+ const CONFIG_DIR = path.join(os.homedir(), '.nowpayments');
12
+ const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
13
+
14
+ /**
15
+ * Default configuration
16
+ */
17
+ const DEFAULT_CONFIG = {
18
+ apiKey: null,
19
+ sandbox: false,
20
+ defaultCurrency: 'USD',
21
+ defaultPayCurrency: 'BTC'
22
+ };
23
+
24
+ /**
25
+ * Ensure config directory exists
26
+ */
27
+ function ensureConfigDir() {
28
+ if (!fs.existsSync(CONFIG_DIR)) {
29
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Load configuration from file
35
+ * @returns {Object} Configuration object
36
+ */
37
+ function loadConfig() {
38
+ try {
39
+ ensureConfigDir();
40
+ if (fs.existsSync(CONFIG_FILE)) {
41
+ const data = fs.readFileSync(CONFIG_FILE, 'utf8');
42
+ return { ...DEFAULT_CONFIG, ...JSON.parse(data) };
43
+ }
44
+ } catch (error) {
45
+ // Return defaults if config file doesn't exist or is invalid
46
+ }
47
+ return { ...DEFAULT_CONFIG };
48
+ }
49
+
50
+ /**
51
+ * Save configuration to file
52
+ * @param {Object} config - Configuration to save
53
+ */
54
+ function saveConfig(config) {
55
+ ensureConfigDir();
56
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf8');
57
+ }
58
+
59
+ /**
60
+ * Get a configuration value
61
+ * @param {string} key - Configuration key
62
+ * @returns {*} Configuration value
63
+ */
64
+ function getConfig(key) {
65
+ const config = loadConfig();
66
+ return config[key];
67
+ }
68
+
69
+ /**
70
+ * Set a configuration value
71
+ * @param {string} key - Configuration key
72
+ * @param {*} value - Configuration value
73
+ */
74
+ function setConfig(key, value) {
75
+ const config = loadConfig();
76
+ config[key] = value;
77
+ saveConfig(config);
78
+ }
79
+
80
+ /**
81
+ * Get API key from config, environment, or command option
82
+ * @param {Object} program - Commander program instance
83
+ * @returns {string|null} API key
84
+ */
85
+ function getApiKey(program) {
86
+ // Priority: command option > environment > config file
87
+ return program._apiKey || process.env.NOWPAYMENTS_API_KEY || getConfig('apiKey');
88
+ }
89
+
90
+ /**
91
+ * Get base URL based on environment
92
+ * @param {boolean} sandbox - Whether to use sandbox environment
93
+ * @returns {string} Base URL
94
+ */
95
+ function getBaseUrl(sandbox = false) {
96
+ return sandbox
97
+ ? 'https://api-sandbox.nowpayments.io/v1'
98
+ : 'https://api.nowpayments.io/v1';
99
+ }
100
+
101
+ module.exports = {
102
+ CONFIG_DIR,
103
+ CONFIG_FILE,
104
+ loadConfig,
105
+ saveConfig,
106
+ getConfig,
107
+ setConfig,
108
+ getApiKey,
109
+ getBaseUrl
110
+ };
package/test.sh ADDED
@@ -0,0 +1,250 @@
1
+ #!/bin/bash
2
+
3
+ ##
4
+ # NOWPayments CLI Test Suite
5
+ #
6
+ # Tests all major functionality of the CLI without requiring an actual API key.
7
+ # Uses mock responses and validates command structure.
8
+ ##
9
+
10
+ set -e
11
+
12
+ CLI="node bin/nowpayments.js"
13
+ ERRORS=0
14
+ TESTS=0
15
+
16
+ # Colors
17
+ GREEN='\033[0;32m'
18
+ RED='\033[0;31m'
19
+ YELLOW='\033[1;33m'
20
+ NC='\033[0m' # No Color
21
+
22
+ echo "========================================"
23
+ echo " NOWPayments CLI Test Suite"
24
+ echo "========================================"
25
+ echo ""
26
+
27
+ # Helper functions
28
+ pass() {
29
+ echo -e "${GREEN}✓${NC} $1"
30
+ TESTS=$((TESTS + 1))
31
+ }
32
+
33
+ fail() {
34
+ echo -e "${RED}✗${NC} $1"
35
+ ERRORS=$((ERRORS + 1))
36
+ TESTS=$((TESTS + 1))
37
+ }
38
+
39
+ info() {
40
+ echo -e "${YELLOW}ℹ${NC} $1"
41
+ }
42
+
43
+ # Test 1: CLI executable
44
+ echo "Testing CLI structure..."
45
+ if [ -f "bin/nowpayments.js" ]; then
46
+ pass "CLI entry point exists"
47
+ else
48
+ fail "CLI entry point not found"
49
+ fi
50
+
51
+ # Test 2: Help output
52
+ if $CLI --help > /dev/null 2>&1; then
53
+ pass "Help command works"
54
+ else
55
+ fail "Help command failed"
56
+ fi
57
+
58
+ # Test 3: Version output
59
+ if $CLI --version > /dev/null 2>&1; then
60
+ pass "Version command works"
61
+ else
62
+ fail "Version command failed"
63
+ fi
64
+
65
+ # Test 4: Command structure
66
+ echo ""
67
+ echo "Testing command structure..."
68
+
69
+ commands=("auth" "status" "currencies" "estimate" "payment" "invoice" "payout")
70
+
71
+ for cmd in "${commands[@]}"; do
72
+ if $CLI "$cmd" --help > /dev/null 2>&1; then
73
+ pass "Command '$cmd' registered"
74
+ else
75
+ fail "Command '$cmd' not found"
76
+ fi
77
+ done
78
+
79
+ # Test 5: Auth commands
80
+ echo ""
81
+ echo "Testing auth commands..."
82
+
83
+ if $CLI auth --help | grep -q "set"; then
84
+ pass "Auth 'set' subcommand exists"
85
+ else
86
+ fail "Auth 'set' subcommand missing"
87
+ fi
88
+
89
+ if $CLI auth --help | grep -q "show"; then
90
+ pass "Auth 'show' subcommand exists"
91
+ else
92
+ fail "Auth 'show' subcommand missing"
93
+ fi
94
+
95
+ # Test 6: Payment commands
96
+ echo ""
97
+ echo "Testing payment commands..."
98
+
99
+ payment_cmds=("create" "get" "list" "update-estimate")
100
+
101
+ for cmd in "${payment_cmds[@]}"; do
102
+ if $CLI payment --help | grep -q "$cmd"; then
103
+ pass "Payment '$cmd' subcommand exists"
104
+ else
105
+ fail "Payment '$cmd' subcommand missing"
106
+ fi
107
+ done
108
+
109
+ # Test 7: Estimate commands
110
+ echo ""
111
+ echo "Testing estimate commands..."
112
+
113
+ if $CLI estimate --help | grep -q "convert"; then
114
+ pass "Estimate 'convert' subcommand exists"
115
+ else
116
+ fail "Estimate 'convert' subcommand missing"
117
+ fi
118
+
119
+ if $CLI estimate --help | grep -q "min"; then
120
+ pass "Estimate 'min' subcommand exists"
121
+ else
122
+ fail "Estimate 'min' subcommand missing"
123
+ fi
124
+
125
+ # Test 8: Library files
126
+ echo ""
127
+ echo "Testing library structure..."
128
+
129
+ libs=("src/lib/api.js" "src/lib/auth.js" "src/lib/config.js")
130
+
131
+ for lib in "${libs[@]}"; do
132
+ if [ -f "$lib" ]; then
133
+ pass "Library file '$lib' exists"
134
+ else
135
+ fail "Library file '$lib' not found"
136
+ fi
137
+ done
138
+
139
+ # Test 9: Command files
140
+ echo ""
141
+ echo "Testing command files..."
142
+
143
+ cmd_files=("auth.js" "status.js" "currencies.js" "estimate.js" "payment.js" "invoice.js" "payout.js")
144
+
145
+ for file in "${cmd_files[@]}"; do
146
+ if [ -f "src/commands/$file" ]; then
147
+ pass "Command file '$file' exists"
148
+ else
149
+ fail "Command file '$file' not found"
150
+ fi
151
+ done
152
+
153
+ # Test 10: Documentation
154
+ echo ""
155
+ echo "Testing documentation..."
156
+
157
+ docs=("README.md" "AGENT.md" "OPENCLAW.md" "INSTALL.md")
158
+
159
+ for doc in "${docs[@]}"; do
160
+ if [ -f "$doc" ]; then
161
+ pass "Documentation '$doc' exists"
162
+ else
163
+ fail "Documentation '$doc' not found"
164
+ fi
165
+ done
166
+
167
+ # Test 11: Package configuration
168
+ echo ""
169
+ echo "Testing package configuration..."
170
+
171
+ if [ -f "package.json" ]; then
172
+ pass "package.json exists"
173
+
174
+ if grep -q '"name": "@ktmcp-cli/nowpayments"' package.json; then
175
+ pass "Package name is correct"
176
+ else
177
+ fail "Package name is incorrect"
178
+ fi
179
+
180
+ if grep -q '"bin"' package.json; then
181
+ pass "Binary entry point configured"
182
+ else
183
+ fail "Binary entry point not configured"
184
+ fi
185
+ else
186
+ fail "package.json not found"
187
+ fi
188
+
189
+ # Test 12: Examples
190
+ echo ""
191
+ echo "Testing example files..."
192
+
193
+ examples=("basic-payment.js" "monitor-payment.js" "invoice-flow.js")
194
+
195
+ for example in "${examples[@]}"; do
196
+ if [ -f "examples/$example" ]; then
197
+ pass "Example '$example' exists"
198
+ else
199
+ fail "Example '$example' not found"
200
+ fi
201
+ done
202
+
203
+ # Test 13: Required options validation
204
+ echo ""
205
+ echo "Testing option validation..."
206
+
207
+ # Payment create should fail without required options
208
+ if ! $CLI payment create 2>&1 | grep -q "required option"; then
209
+ fail "Missing required option validation"
210
+ else
211
+ pass "Required option validation works"
212
+ fi
213
+
214
+ # Test 14: File permissions
215
+ echo ""
216
+ echo "Testing file permissions..."
217
+
218
+ if [ -x "bin/nowpayments.js" ]; then
219
+ pass "CLI binary is executable"
220
+ else
221
+ fail "CLI binary is not executable"
222
+ fi
223
+
224
+ # Summary
225
+ echo ""
226
+ echo "========================================"
227
+ echo " Test Summary"
228
+ echo "========================================"
229
+ echo "Total tests: $TESTS"
230
+ echo -e "Passed: ${GREEN}$((TESTS - ERRORS))${NC}"
231
+ echo -e "Failed: ${RED}$ERRORS${NC}"
232
+
233
+ if [ $ERRORS -eq 0 ]; then
234
+ echo ""
235
+ echo -e "${GREEN}All tests passed! ✓${NC}"
236
+ echo ""
237
+ echo "The CLI structure is correct and ready for use."
238
+ echo ""
239
+ echo "Next steps:"
240
+ echo " 1. Install dependencies: npm install"
241
+ echo " 2. Set API key: $CLI auth set YOUR_KEY"
242
+ echo " 3. Test connection: $CLI status"
243
+ echo ""
244
+ exit 0
245
+ else
246
+ echo ""
247
+ echo -e "${RED}Some tests failed.${NC}"
248
+ echo "Please review the errors above."
249
+ exit 1
250
+ fi