Skip to content

Commit 5f92a5e

Browse files
authored
Merge pull request #4 from mja00/feat/unit-tests
2 parents b4273ea + 4a5dcb2 commit 5f92a5e

18 files changed

Lines changed: 2658 additions & 45 deletions

.claude/settings.local.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
"Bash(npm run typecheck:*)",
1414
"Bash(npm run lint:*)",
1515
"Bash(npm run:*)",
16-
"WebFetch(domain:zipline.diced.sh)"
16+
"WebFetch(domain:zipline.diced.sh)",
17+
"Bash(npm test)",
18+
"Bash(npx tsc:*)"
1719
],
1820
"deny": [],
1921
"ask": []
2022
}
21-
}
23+
}

.eslintignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
/temp
99
/src/db/schema.js
1010
/src/db/schema.d.ts
11+
12+
# Config files
13+
vitest.config.ts
14+
vitest.config.js
15+
vitest.config.d.ts
16+
vitest.config.js.map

.eslintrc.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,17 @@
9191
}
9292
],
9393
"unicorn/prefer-node-protocol": "error"
94-
}
94+
},
95+
"overrides": [
96+
{
97+
"files": ["tests/**/*.ts"],
98+
"parserOptions": {
99+
"project": "./tsconfig.test.json"
100+
},
101+
"rules": {
102+
"@typescript-eslint/explicit-function-return-type": "off",
103+
"@typescript-eslint/typedef": "off"
104+
}
105+
}
106+
]
95107
}

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ jobs:
2929
- name: Type check
3030
run: npx tsc --noEmit
3131

32+
- name: Run tests
33+
run: npm run test
34+
3235
- name: Build project
3336
run: npm run build

CLAUDE.md

Lines changed: 136 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,175 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44

55
## Project Overview
66

7-
This is a TypeScript-based Markov chain text generation system with OpenAI integration. The project generates text using Markov chains trained on input data and can optionally use OpenAI's API for enhanced text generation and conversation handling.
7+
This is a Discord.js bot written in TypeScript, built on the Discord Bot TypeScript Template. The bot features a fishing game with an economy system, AI-powered image generation, and OpenAI integration for enhanced interactions.
88

99
## Development Commands
1010

1111
### Build and Development
1212
```bash
1313
npm run build # Compile TypeScript to JavaScript
14-
npm run dev # Run development server with hot reload
15-
npm start # Start the production server
14+
npm start # Start the bot (single instance)
15+
npm run start:manager # Start with shard manager for multiple shards
16+
npm run start:pm2 # Start with PM2 process manager
1617
```
1718

1819
### Code Quality
1920
```bash
2021
npm run lint # Run ESLint for code linting
2122
npm run lint:fix # Fix auto-fixable linting issues
22-
npm run typecheck # Run TypeScript type checking
23+
npm run format # Check code formatting with Prettier
24+
npm run format:fix # Fix code formatting issues
2325
```
2426

2527
### Testing
2628
```bash
2729
npm test # Run all tests
30+
npm run test:unit # Run unit tests only
31+
npm run test:integration # Run integration tests only
2832
npm run test:watch # Run tests in watch mode
33+
npm run test:coverage # Run tests with coverage report
2934
```
3035

31-
## Architecture Overview
32-
33-
### Core Components
36+
### Database Management
37+
```bash
38+
npm run db:generate # Generate database migrations
39+
npm run db:push # Push schema changes to database
40+
npm run db:migrate # Run database migrations
41+
npm run db:seed # Seed database with initial data
42+
npm run db:setup # Push schema and seed data
43+
npm run db:studio # Open Drizzle Studio for database management
44+
```
3445

35-
- **MarkovChain** (`src/MarkovChain.ts`): The main class implementing Markov chain text generation
36-
- Handles state transitions and probability calculations
37-
- Supports configurable order (n-gram size) for chain complexity
38-
- Provides text generation with customizable parameters
46+
### Command Management
47+
```bash
48+
npm run commands:view [GUILD_ID] # View registered commands
49+
npm run commands:register [GUILD_ID] # Register commands (guild-specific if ID provided)
50+
npm run commands:clear [GUILD_ID] # Clear registered commands
51+
npm run commands:rename # Rename a command
52+
npm run commands:delete # Delete a specific command
53+
```
3954

40-
- **OpenAI Integration** (`src/openai.ts`): Handles OpenAI API interactions
41-
- Thread management for conversation context
42-
- Response generation and processing
43-
- Credit tracking and usage monitoring
55+
## Architecture Overview
4456

45-
- **Type Definitions** (`src/types.ts`): Core TypeScript interfaces and types
46-
- `MarkovState`: Represents chain states and transitions
47-
- `GenerationOptions`: Configuration for text generation
48-
- OpenAI-related types for API integration
57+
### Core Components
4958

50-
- **Utilities** (`src/utils.ts`): Helper functions for data processing
51-
- Text preprocessing and tokenization
52-
- File I/O operations
53-
- Data validation and sanitization
59+
- **Bot Initialization** (`src/start-bot.ts`, `src/start-manager.ts`): Entry points for bot and shard manager
60+
- Bot setup and configuration
61+
- Command registration
62+
- Event handler initialization
63+
- Service initialization (OpenAI, Database)
64+
65+
- **Commands** (`src/commands/`): Discord slash commands
66+
- **Chat Commands**: `/fish`, `/fishing`, `/shop`, `/buy`, `/inventory`, `/generate-image`, `/help`, `/info`, `/test`, `/dev`
67+
- **Context Menu Commands**: View date sent (message), View date joined (user)
68+
- Command metadata and argument definitions in `metadata.ts` and `args.ts`
69+
70+
- **Services** (`src/services/`): Business logic layer
71+
- **FishingService**: Core fishing mechanics and catch logic
72+
- **UserService**: User data management
73+
- **GuildService**: Server settings and configuration
74+
- **DatabaseService**: Database connection and query management (Drizzle ORM)
75+
- **OpenAIService**: OpenAI API integration for AI features
76+
- **ImageUpload**: Image upload to fal.ai for AI-generated images
77+
- **ItemEffectsService**: Item effect calculations (rarity boosts, worth multipliers)
78+
- **FishingCooldownService**: Rate limiting for fishing commands
79+
- **ShopService**: Shop and purchase management
80+
81+
- **Database Schema** (`src/db/schema.ts`): PostgreSQL schema using Drizzle ORM
82+
- **users**: Discord user profiles with money and auto-fishing status
83+
- **catchables**: Fish and items that can be caught (rarity, worth, images)
84+
- **catches**: Record of user catches
85+
- **items**: Purchasable items with effects (consumable/passive)
86+
- **shop**: Items available for purchase
87+
- **purchases**: Purchase history
88+
- **inventory**: User item inventory
89+
- **guilds**: Server-specific settings (cooldown limits)
90+
91+
- **Events** (`src/events/`): Discord event handlers
92+
- **CommandHandler**: Slash command execution
93+
- **ButtonHandler**: Button interaction handling
94+
- **MessageHandler**: Message event processing
95+
- **ReactionHandler**: Reaction event processing
96+
- **TriggerHandler**: Custom trigger system
97+
- **GuildJoinHandler**: New server welcome messages
98+
- **GuildLeaveHandler**: Server leave cleanup
99+
100+
- **Utilities** (`src/utils/`): Helper functions
101+
- **ClientUtils**: Discord client helpers
102+
- **CommandUtils**: Command processing utilities
103+
- **MessageUtils**: Message formatting and sending
104+
- **PermissionUtils**: Permission checking
105+
- **DbUtils**: Database query helpers
106+
- **FormatUtils**: Data formatting
107+
- **StringUtils**: String manipulation
108+
- **MathUtils**: Mathematical calculations
109+
- **RandomUtils**: Random number generation
110+
111+
- **Models** (`src/models/`): Data models and type definitions
112+
- **ConfigModels**: Configuration types
113+
- **InternalModels**: Internal data structures
114+
- **API Models**: REST API request/response types for cluster API
115+
116+
### Key Features
117+
118+
1. **Fishing Game**
119+
- Catch various fish/items with different rarities
120+
- Economy system with money and purchasable items
121+
- Item effects (rarity boosts, worth multipliers)
122+
- Auto-fishing capability
123+
- Cooldown system to prevent spam
124+
125+
2. **AI Integration**
126+
- OpenAI API for enhanced interactions
127+
- Image generation via fal.ai
128+
- Thread management for conversation context
129+
130+
3. **Scalability**
131+
- Sharding support for large bot deployments (2500+ servers)
132+
- Clustering support for multi-machine deployment
133+
- PM2 process manager integration
134+
- Cluster API for cross-shard communication
135+
136+
4. **Developer Features**
137+
- TypeScript with strict type checking
138+
- ESM modules for modern JavaScript
139+
- Comprehensive testing with Vitest
140+
- Database migrations with Drizzle Kit
141+
- Localization support via `lang/` directory
54142

55143
### Data Flow
56144

57-
1. Input text is processed and tokenized in utilities
58-
2. MarkovChain builds state transition tables from processed data
59-
3. Text generation uses probability-based state transitions
60-
4. OpenAI integration can enhance or supplement generated content
61-
5. Results are processed and returned to the caller
145+
1. User invokes slash command in Discord
146+
2. CommandHandler receives interaction and routes to appropriate Command
147+
3. Command validates permissions and arguments
148+
4. Command calls relevant Service(s) for business logic
149+
5. Service queries/updates database via DatabaseService
150+
6. Service applies game logic (e.g., fishing mechanics, item effects)
151+
7. Results formatted and sent back to Discord user via MessageUtils
62152

63153
### Key Design Patterns
64154

65-
- **Probabilistic State Machine**: Markov chains use statistical transitions between states
66-
- **Strategy Pattern**: Different generation strategies (pure Markov vs OpenAI-enhanced)
67-
- **Builder Pattern**: Configurable generation options and chain parameters
155+
- **Handler Pattern**: Event handlers process Discord events and route to appropriate components
156+
- **Service Layer**: Business logic separated from command/event handling
157+
- **Repository Pattern**: DatabaseService abstracts database operations
158+
- **Factory Pattern**: Command and event handler registration
159+
- **Singleton Pattern**: Services like OpenAIService and DatabaseService
68160

69161
## Configuration
70162

71-
- TypeScript configuration in `tsconfig.json` with strict type checking
72-
- ESLint configuration for code quality enforcement
73-
- Build output to `dist/` directory
163+
- **TypeScript**: `tsconfig.json` with strict type checking, ESM modules
164+
- **ESLint**: `.eslintrc.json` for code quality
165+
- **Prettier**: `.prettierrc.json` for code formatting
166+
- **Vitest**: `vitest.config.ts` for testing configuration
167+
- **Drizzle**: `src/drizzle.config.ts` for database configuration
168+
- **Bot Config**: `config/config.json` (see `config/*.example.json` for templates)
169+
- **Build Output**: `dist/` directory
74170

75171
## Environment Variables
76172

77-
The application expects OpenAI API configuration through environment variables or configuration files for AI-enhanced features.
173+
Required environment variables (typically in `.env`):
174+
- Discord bot token and client ID
175+
- Database connection string (PostgreSQL)
176+
- OpenAI API key (for AI features)
177+
- fal.ai API key (for image generation)
178+
- Bot developer Discord user ID(s)

0 commit comments

Comments
 (0)