-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-site.sh
More file actions
executable file
·431 lines (351 loc) · 11.5 KB
/
test-site.sh
File metadata and controls
executable file
·431 lines (351 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/bash
# ObjectDocs Site Testing Script
# Tests the complete lifecycle: init -> create -> startup -> build -> run
# 测试站点完整流程:初始化 -> 创建 -> 启动 -> 编译 -> 运行
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
TEST_DIR=$(mktemp -d -t objectdocs-test.XXXXXXXXXX)
LOG_DIR=$(mktemp -d -t objectdocs-logs.XXXXXXXXXX)
PORT="${PORT:-7777}" # Allow override via environment variable
BUILD_TIMEOUT="${BUILD_TIMEOUT:-300}" # 5 minutes for build
DEV_TIMEOUT="${DEV_TIMEOUT:-30}" # 30 seconds for dev server to start
# Cleanup function
cleanup() {
echo ""
echo -e "${BLUE}Cleaning up...${NC}"
# Kill any running dev/start servers (gracefully first, then forcefully)
if [ ! -z "$DEV_PID" ]; then
kill $DEV_PID 2>/dev/null || true
sleep 2
kill -9 $DEV_PID 2>/dev/null || true
fi
if [ ! -z "$START_PID" ]; then
kill $START_PID 2>/dev/null || true
sleep 2
kill -9 $START_PID 2>/dev/null || true
fi
# Kill any process using the test port (gracefully)
if lsof -ti:$PORT >/dev/null 2>&1; then
lsof -ti:$PORT | xargs kill 2>/dev/null || true
sleep 1
if lsof -ti:$PORT >/dev/null 2>&1; then
lsof -ti:$PORT | xargs kill -9 2>/dev/null || true
fi
fi
# Remove test directory (force recursive delete)
if [ -d "$TEST_DIR" ]; then
rm -rf "$TEST_DIR" 2>/dev/null || true
fi
# Remove log directory
if [ -d "$LOG_DIR" ]; then
rm -rf "$LOG_DIR" 2>/dev/null || true
fi
echo -e "${BLUE}Cleanup complete${NC}"
}
# Register cleanup on exit
trap cleanup EXIT
# Print section header
print_section() {
echo ""
echo "================================================"
echo -e "${BLUE}$1${NC}"
echo "================================================"
}
# Print success message
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
# Print error message
print_error() {
echo -e "${RED}✗${NC} $1"
}
# Print warning message
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
# Print info message
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Wait for server to be ready
wait_for_server() {
local port=$1
local timeout=$2
local elapsed=0
print_info "Waiting for server on port $port (timeout: ${timeout}s)..."
while [ $elapsed -lt $timeout ]; do
if curl -s http://localhost:$port > /dev/null 2>&1; then
return 0
fi
sleep 2
elapsed=$((elapsed + 2))
done
return 1
}
# Main test script
main() {
print_section "ObjectDocs Complete Lifecycle Test"
echo "Test directory: $TEST_DIR"
echo "Log directory: $LOG_DIR"
echo "Port: $PORT"
echo ""
# Create log directory
mkdir -p "$LOG_DIR"
# Check prerequisites
print_section "Step 0: Checking Prerequisites"
if ! command_exists node; then
print_error "Node.js is not installed"
exit 1
fi
print_success "Node.js installed: $(node --version)"
if ! command_exists pnpm; then
print_error "pnpm is not installed"
exit 1
fi
print_success "pnpm installed: $(pnpm --version)"
if ! command_exists curl; then
print_error "curl is not installed"
exit 1
fi
print_success "curl installed"
# Step 1: Create test directory and initialize project
print_section "Step 1: Initializing New Project"
mkdir -p "$TEST_DIR"
cd "$TEST_DIR"
print_success "Created test directory: $TEST_DIR"
# Initialize package.json
print_info "Creating package.json..."
echo "{}" > package.json
print_success "package.json created"
# Install @objectdocs/cli as dev dependency
print_info "Installing @objectdocs/cli..."
# Get the absolute path to the monorepo root (where script is located)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MONOREPO_ROOT="${SCRIPT_DIR}"
# Install CLI from workspace or npm
if [ -d "$MONOREPO_ROOT/packages/cli" ]; then
print_info "Installing CLI from local workspace..."
pnpm add -D "$MONOREPO_ROOT/packages/cli"
print_success "@objectdocs/cli installed from local workspace"
else
print_info "Installing CLI from npm..."
pnpm add -D @objectdocs/cli
print_success "@objectdocs/cli installed from npm"
fi
# Step 2: Configure scripts
print_section "Step 2: Configuring Package Scripts"
# Add scripts to package.json using pnpm pkg
pnpm pkg set scripts.dev="objectdocs dev"
pnpm pkg set scripts.build="objectdocs build"
pnpm pkg set scripts.start="objectdocs start"
print_success "Scripts configured in package.json"
# Step 3: Initialize ObjectDocs
print_section "Step 3: Running ObjectDocs Init"
print_info "Running: pnpm objectdocs init"
if pnpm objectdocs init; then
print_success "ObjectDocs initialized successfully"
else
print_error "ObjectDocs init failed"
exit 1
fi
# Verify init created necessary files
if [ -d "content/.fumadocs" ]; then
print_success "Site engine copied to content/.fumadocs"
else
print_warning "content/.fumadocs not found (might use different structure)"
fi
# Step 4: Create content
print_section "Step 4: Creating Documentation Content"
mkdir -p content/docs
print_success "Created content/docs directory"
# Create docs.site.json
cat > content/docs.site.json << 'EOF'
{
"branding": {
"name": "Test Site",
"description": "ObjectDocs Test Site"
},
"links": [
{ "text": "Home", "url": "/" },
{ "text": "Docs", "url": "/docs" }
]
}
EOF
print_success "Created content/docs.site.json"
# Create meta.json
cat > content/docs/meta.json << 'EOF'
{
"title": "Documentation",
"pages": ["index", "getting-started"]
}
EOF
print_success "Created content/docs/meta.json"
# Create index.mdx
cat > content/docs/index.mdx << 'EOF'
---
title: Welcome to ObjectDocs
description: Getting started with ObjectDocs
---
# Welcome to ObjectDocs
This is a test documentation site created to validate the ObjectDocs setup process.
## Features
- Fast and modern documentation engine
- Built on Next.js 14
- Powered by Fumadocs
- Configuration as Code
## Next Steps
Check out the [Getting Started](/docs/getting-started) guide to learn more.
EOF
print_success "Created content/docs/index.mdx"
# Create getting-started.mdx
cat > content/docs/getting-started.mdx << 'EOF'
---
title: Getting Started
description: Quick start guide for ObjectDocs
---
# Getting Started
Welcome to the ObjectDocs quick start guide!
## Installation
```bash
pnpm add -D @objectdocs/cli
```
## Usage
Start the development server:
```bash
pnpm dev
```
Build for production:
```bash
pnpm build
```
Start production server:
```bash
pnpm start
```
EOF
print_success "Created content/docs/getting-started.mdx"
# Step 5: Test development server
print_section "Step 5: Testing Development Server"
print_info "Starting dev server..."
pnpm dev > "$LOG_DIR/dev-server.log" 2>&1 &
DEV_PID=$!
if wait_for_server $PORT $DEV_TIMEOUT; then
print_success "Dev server started successfully on http://localhost:$PORT"
# Test if we can fetch the homepage
if curl -s http://localhost:$PORT > /dev/null; then
print_success "Homepage is accessible"
else
print_warning "Homepage returned unexpected response"
fi
# Test if we can fetch a docs page
if curl -s http://localhost:$PORT/docs > /dev/null; then
print_success "Docs page is accessible"
else
print_warning "Docs page returned unexpected response"
fi
else
print_error "Dev server failed to start within ${DEV_TIMEOUT}s"
print_info "Server log output:"
cat "$LOG_DIR/dev-server.log" || true
exit 1
fi
# Stop dev server
print_info "Stopping dev server..."
kill $DEV_PID 2>/dev/null || true
sleep 2
kill -9 $DEV_PID 2>/dev/null || true
wait $DEV_PID 2>/dev/null || true
DEV_PID=""
sleep 2
print_success "Dev server stopped"
# Step 6: Test build process
print_section "Step 6: Testing Build Process"
print_info "Running build (this may take a few minutes)..."
# Check if timeout command is available
if command_exists timeout; then
if timeout $BUILD_TIMEOUT pnpm build; then
print_success "Build completed successfully"
else
print_error "Build failed or timed out"
exit 1
fi
else
# Fallback without timeout (macOS compatibility)
print_warning "timeout command not available, running without timeout protection"
if pnpm build; then
print_success "Build completed successfully"
else
print_error "Build failed"
exit 1
fi
fi
# Check if build output exists
if [ -d "content/.fumadocs/.next" ] || [ -d ".next" ]; then
print_success "Build output directory created"
else
print_warning "Build output directory not found in expected locations"
fi
# Step 7: Test production server
print_section "Step 7: Testing Production Server"
print_info "Starting production server..."
pnpm start > "$LOG_DIR/start-server.log" 2>&1 &
START_PID=$!
if wait_for_server $PORT $DEV_TIMEOUT; then
print_success "Production server started successfully on http://localhost:$PORT"
# Test if we can fetch the homepage
if curl -s http://localhost:$PORT > /dev/null; then
print_success "Production homepage is accessible"
else
print_warning "Production homepage returned unexpected response"
fi
# Test if we can fetch a docs page
if curl -s http://localhost:$PORT/docs > /dev/null; then
print_success "Production docs page is accessible"
else
print_warning "Production docs page returned unexpected response"
fi
else
print_error "Production server failed to start within ${DEV_TIMEOUT}s"
print_info "Server log output:"
cat "$LOG_DIR/start-server.log" || true
exit 1
fi
# Stop production server
print_info "Stopping production server..."
kill $START_PID 2>/dev/null || true
sleep 2
kill -9 $START_PID 2>/dev/null || true
wait $START_PID 2>/dev/null || true
START_PID=""
sleep 2
print_success "Production server stopped"
# Final summary
print_section "Test Summary"
echo ""
echo -e "${GREEN}✅ All tests passed successfully!${NC}"
echo ""
echo "Tested lifecycle stages:"
echo " ✓ Project initialization (pnpm init)"
echo " ✓ CLI installation (@objectdocs/cli)"
echo " ✓ ObjectDocs initialization (objectdocs init)"
echo " ✓ Content creation (MDX files, configuration)"
echo " ✓ Development server (pnpm dev)"
echo " ✓ Production build (pnpm build)"
echo " ✓ Production server (pnpm start)"
echo ""
echo "Test directory: $TEST_DIR"
echo ""
print_info "Test directory will be cleaned up on exit"
}
# Run main function
main "$@"