forked from AppFlowy-IO/AppFlowy-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-test-environment.sh
More file actions
executable file
·582 lines (468 loc) · 16 KB
/
setup-test-environment.sh
File metadata and controls
executable file
·582 lines (468 loc) · 16 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/bin/bash
################################################################################
# AppFlowy E2E Test Environment Setup Script
#
# This script automates the complete setup of the AppFlowy Web Premium
# E2E testing environment, including Docker services, web server, and tests.
#
# Usage: ./setup-test-environment.sh [command]
# Commands:
# setup - Complete environment setup
# start - Start all services
# stop - Stop all services
# test - Run all E2E tests
# test:specific <spec> - Run specific test
# clean - Clean up Docker volumes and containers
# status - Check service status
# context - Show AI context information
# help - Show this help message
################################################################################
set -e # Exit on error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Project paths
APPFLOWY_WEB_DIR="/Users/weidongfu/Documents/AF/AppFlowy-Web-Premium"
APPFLOWY_CLOUD_DIR="/Users/weidongfu/Documents/AF/AppFlowy-Cloud-Premium"
# Environment variables for external/frontend use
export APPFLOWY_BASE_URL="http://localhost"
export APPFLOWY_WS_BASE_URL="ws://localhost/ws/v2"
# Note: APPFLOWY_GOTRUE_BASE_URL is set in .env file for internal Docker communication
export APPFLOWY_WEB_VERSION="local-$(cd $APPFLOWY_WEB_DIR && git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
# Test configuration
CYPRESS_BASE_URL="http://localhost:3000"
WEB_DEV_SERVER_PID_FILE="/tmp/appflowy-web-dev.pid"
################################################################################
# Helper Functions
################################################################################
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_command() {
if ! command -v $1 &> /dev/null; then
log_error "$1 is not installed"
return 1
fi
log_success "$1 is installed"
return 0
}
check_prerequisites() {
log_info "Checking prerequisites..."
local all_good=true
check_command "node" || all_good=false
check_command "npm" || all_good=false
check_command "docker" || all_good=false
check_command "docker" || all_good=false
if [ "$all_good" = false ]; then
log_error "Some prerequisites are missing. Please install them first."
exit 1
fi
log_success "All prerequisites are installed"
}
check_directories() {
log_info "Checking project directories..."
if [ ! -d "$APPFLOWY_WEB_DIR" ]; then
log_error "AppFlowy-Web-Premium directory not found at: $APPFLOWY_WEB_DIR"
exit 1
fi
if [ ! -d "$APPFLOWY_CLOUD_DIR" ]; then
log_error "AppFlowy-Cloud-Premium directory not found at: $APPFLOWY_CLOUD_DIR"
exit 1
fi
log_success "Project directories found"
}
################################################################################
# Docker Management
################################################################################
setup_docker_env() {
log_info "Setting up Docker environment..."
cd "$APPFLOWY_CLOUD_DIR"
# Copy .env.nginx to .env if it doesn't exist
if [ ! -f .env ]; then
if [ -f .env.nginx ]; then
cp .env.nginx .env
log_success "Created .env from .env.nginx"
else
log_error ".env.nginx not found in AppFlowy-Cloud-Premium"
exit 1
fi
fi
# Set required environment variables in .env
sed -i.bak "s|APPFLOWY_WEB_VERSION=.*|APPFLOWY_WEB_VERSION=$APPFLOWY_WEB_VERSION|g" .env 2>/dev/null || true
}
start_docker_services() {
log_info "Starting Docker services..."
cd "$APPFLOWY_CLOUD_DIR"
# Stop any existing services
docker compose down 2>/dev/null || true
# Clean up orphan containers
docker compose down --remove-orphans 2>/dev/null || true
# Start services
docker compose up -d
log_info "Waiting for services to be healthy..."
# Wait for postgres
local retries=30
while [ $retries -gt 0 ]; do
if docker compose ps | grep -q "postgres.*healthy"; then
log_success "Postgres is healthy"
break
fi
sleep 2
retries=$((retries - 1))
done
# Wait for gotrue
retries=30
while [ $retries -gt 0 ]; do
if docker compose ps | grep -q "gotrue.*healthy"; then
log_success "GoTrue is healthy"
break
fi
sleep 2
retries=$((retries - 1))
done
# Additional wait for all services to stabilize
sleep 10
log_success "Docker services are running"
}
stop_docker_services() {
log_info "Stopping Docker services..."
cd "$APPFLOWY_CLOUD_DIR"
docker compose down
log_success "Docker services stopped"
}
clean_docker_environment() {
log_warning "This will remove all Docker containers and volumes for AppFlowy"
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
cd "$APPFLOWY_CLOUD_DIR"
docker compose down -v
docker system prune -f
log_success "Docker environment cleaned"
else
log_info "Clean operation cancelled"
fi
}
################################################################################
# Web Server Management
################################################################################
start_web_dev_server() {
log_info "Starting web development server..."
cd "$APPFLOWY_WEB_DIR"
# Kill any existing dev server
stop_web_dev_server
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
log_info "Installing npm dependencies..."
npm install
fi
# Start dev server in background
npm run dev > /tmp/appflowy-web-dev.log 2>&1 &
echo $! > "$WEB_DEV_SERVER_PID_FILE"
# Wait for server to be ready
log_info "Waiting for web server to be ready..."
local retries=30
while [ $retries -gt 0 ]; do
if curl -s http://localhost:3000 > /dev/null 2>&1; then
log_success "Web development server is running on port 3000"
return 0
fi
sleep 2
retries=$((retries - 1))
done
log_error "Web server failed to start. Check /tmp/appflowy-web-dev.log"
return 1
}
stop_web_dev_server() {
if [ -f "$WEB_DEV_SERVER_PID_FILE" ]; then
local pid=$(cat "$WEB_DEV_SERVER_PID_FILE")
if kill -0 $pid 2>/dev/null; then
kill $pid
log_info "Stopped web development server (PID: $pid)"
fi
rm "$WEB_DEV_SERVER_PID_FILE"
fi
# Also kill any process on port 3000
lsof -ti:3000 | xargs kill -9 2>/dev/null || true
}
################################################################################
# Test Execution
################################################################################
run_all_tests() {
log_info "Running all E2E tests..."
cd "$APPFLOWY_WEB_DIR"
# Ensure services are running
check_services_status || start_all_services
# Run tests
npm run test:integration
}
run_specific_test() {
local test_spec=$1
log_info "Running specific test: $test_spec"
cd "$APPFLOWY_WEB_DIR"
# Ensure services are running
check_services_status || start_all_services
# Run specific test
npx cypress run --spec "$test_spec"
}
run_test_headed() {
log_info "Opening Cypress Test Runner..."
cd "$APPFLOWY_WEB_DIR"
# Ensure services are running
check_services_status || start_all_services
# Open Cypress
npm run cypress:open
}
################################################################################
# Status and Monitoring
################################################################################
check_services_status() {
local all_running=true
echo "================================"
echo "Service Status Check"
echo "================================"
# Check Docker services
cd "$APPFLOWY_CLOUD_DIR"
if docker compose ps | grep -q "running"; then
echo -e "${GREEN}✓${NC} Docker services: Running"
docker compose ps --format "table {{.Name}}\t{{.Status}}"
else
echo -e "${RED}✗${NC} Docker services: Not running"
all_running=false
fi
echo ""
# Check web dev server
if curl -s http://localhost:3000 > /dev/null 2>&1; then
echo -e "${GREEN}✓${NC} Web dev server: Running on port 3000"
else
echo -e "${RED}✗${NC} Web dev server: Not running"
all_running=false
fi
echo "================================"
if [ "$all_running" = true ]; then
return 0
else
return 1
fi
}
################################################################################
# AI Context Information
################################################################################
show_ai_context() {
cat << 'EOF'
================================================================================
AI ASSISTANT CONTEXT INFORMATION
================================================================================
PROJECT STRUCTURE:
------------------
- AppFlowy-Web-Premium: /Users/weidongfu/Documents/AF/AppFlowy-Web-Premium
- Main web application with React/TypeScript
- Cypress E2E tests in cypress/e2e/
- Test support files in cypress/support/
- AppFlowy-Cloud-Premium: /Users/weidongfu/Documents/AF/AppFlowy-Cloud-Premium
- Backend services (Postgres, Redis, GoTrue, etc.)
- Docker Compose orchestration
- Nginx reverse proxy configuration
KEY TEST FILES:
---------------
1. cypress/support/selectors.ts
- Centralized test ID selectors
- Avoids hardcoding data-testid in tests
- Main selectors: PageSelectors, SpaceSelectors, ModalSelectors, etc.
2. cypress/support/page/page-actions.ts
- Page action utilities (delete, rename, etc.)
- Handles both published (with modal) and unpublished pages
3. cypress/support/page/flows.ts
- Complex test flows (createPageAndAddContent, etc.)
- Page creation sets title after navigation
IMPORTANT PATTERNS:
-------------------
1. Selector Usage:
- ALWAYS use selector helpers from selectors.ts
- Example: PageSelectors.nameContaining(text) instead of cy.get('[data-testid="page-name"]')
2. Delete Confirmation:
- Published pages show confirmation modal
- Unpublished pages delete immediately
- Code handles both cases automatically
3. Wait Patterns:
- Use waitForReactUpdate() from selectors.ts
- Default wait times: 500-1000ms for UI updates, 3000ms for navigation
KNOWN ISSUES & FIXES:
---------------------
1. Multiple Elements Error:
- Solution: Add .first() to selectors
- Already fixed in PageSelectors.itemByName()
2. Pointer Events None:
- Solution: Use { force: true } for interactions
- Common with modals and overlays
3. Page Creation Flow:
- Creates page → Navigates automatically → Sets title
- Don't try to find page in sidebar after creation
4. Space Expansion:
- expandSpace() takes numeric index (0-based), not string
- Default is 0 (first space)
TEST COMMANDS:
--------------
- Run all tests: npm run test:integration
- Run specific: npx cypress run --spec 'cypress/e2e/page/create-delete-page.cy.ts'
- Run headed: npm run cypress:open
- Test subsets:
- npm run test:integration:page:create-delete
- npm run test:integration:page:edit
- npm run test:integration:user
ENVIRONMENT VARIABLES:
----------------------
- APPFLOWY_BASE_URL=http://localhost
- APPFLOWY_WS_BASE_URL=ws://localhost/ws/v2
- APPFLOWY_GOTRUE_BASE_URL=http://gotrue:9999 (internal Docker) / http://localhost/gotrue (external)
- CYPRESS_BASE_URL=http://localhost:3000
DOCKER SERVICES:
----------------
- postgres (port 5432)
- redis (port 6379)
- gotrue (port 9999)
- nginx (port 80)
- appflowy_cloud (port 8000)
- appflowy_web (served via nginx)
- minio (ports 9000, 9001)
TROUBLESHOOTING:
----------------
1. "No space left on device":
- Run: docker system prune -a --volumes -f
2. Port already in use:
- Kill process: lsof -ti:3000 | xargs kill -9
3. Tests timeout finding elements:
- Check if services are healthy: ./setup-test-environment.sh status
- Restart services: ./setup-test-environment.sh stop && ./setup-test-environment.sh start
4. WebSocket connection issues:
- Ensure APPFLOWY_WS_BASE_URL is set correctly
- Check nginx is running and healthy
CURRENT TEST STATUS:
--------------------
✅ Passing (3/5):
- page/create-delete-page.cy.ts
- page/edit-page.cy.ts
- user/user.cy.ts
❌ Failing (2/5):
- page/more-page-action.cy.ts (space expansion and rename issues)
- page/publish-page.cy.ts (page creation flow needs adjustment)
================================================================================
EOF
}
################################################################################
# Main Functions
################################################################################
setup_complete_environment() {
log_info "Setting up complete test environment..."
check_prerequisites
check_directories
setup_docker_env
start_docker_services
start_web_dev_server
log_success "Test environment setup complete!"
echo ""
check_services_status
echo ""
log_info "You can now run tests with: $0 test"
}
start_all_services() {
log_info "Starting all services..."
start_docker_services
start_web_dev_server
log_success "All services started"
}
stop_all_services() {
log_info "Stopping all services..."
stop_web_dev_server
stop_docker_services
log_success "All services stopped"
}
show_help() {
cat << EOF
AppFlowy E2E Test Environment Setup Script
Usage: $0 [command] [options]
Commands:
setup Complete environment setup (first time)
start Start all services (Docker + Web server)
stop Stop all services
status Check service status
test Run all E2E tests
test:spec PATH Run specific test file
test:headed Open Cypress Test Runner (interactive)
clean Clean up Docker volumes and containers
context Show AI context information
help Show this help message
Examples:
$0 setup # First time setup
$0 start # Start services
$0 test # Run all tests
$0 test:spec cypress/e2e/page/create-delete-page.cy.ts
$0 status # Check if everything is running
$0 context # Show context for AI assistants
Environment:
Web App: $APPFLOWY_WEB_DIR
Cloud: $APPFLOWY_CLOUD_DIR
Base URL: $APPFLOWY_BASE_URL
Note: GoTrue URL is configured in .env for internal Docker communication
EOF
}
################################################################################
# Main Script Entry Point
################################################################################
main() {
case "${1:-help}" in
setup)
setup_complete_environment
;;
start)
start_all_services
;;
stop)
stop_all_services
;;
status)
check_services_status
;;
test)
run_all_tests
;;
test:spec)
if [ -z "$2" ]; then
log_error "Please specify a test file path"
exit 1
fi
run_specific_test "$2"
;;
test:headed)
run_test_headed
;;
clean)
clean_docker_environment
;;
context)
show_ai_context
;;
help|--help|-h)
show_help
;;
*)
log_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"