|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +This document provides comprehensive information about testing the Langbase SDK. |
| 4 | + |
| 5 | +## Test Environment Setup |
| 6 | + |
| 7 | +The SDK uses **Vitest** for testing with multiple configurations: |
| 8 | + |
| 9 | +- **Node.js environment** (`vitest.node.config.js`) - For server-side testing |
| 10 | +- **Edge runtime environment** (`vitest.edge.config.js`) - For edge computing platforms |
| 11 | +- **React UI environment** (`vitest.ui.react.config.js`) - For React components with JSDOM |
| 12 | + |
| 13 | +## Running Tests |
| 14 | + |
| 15 | +### Local Development |
| 16 | + |
| 17 | +Run all tests locally using our comprehensive script: |
| 18 | + |
| 19 | +```bash |
| 20 | +./scripts/test-all.sh |
| 21 | +``` |
| 22 | + |
| 23 | +Or run specific test suites: |
| 24 | + |
| 25 | +```bash |
| 26 | +cd packages/langbase |
| 27 | + |
| 28 | +# Node.js environment tests |
| 29 | +pnpm test:node |
| 30 | + |
| 31 | +# Edge runtime tests |
| 32 | +pnpm test:edge |
| 33 | + |
| 34 | +# React UI component tests |
| 35 | +pnpm test:ui:react |
| 36 | + |
| 37 | +# Run all tests |
| 38 | +pnpm test |
| 39 | +``` |
| 40 | + |
| 41 | +### Individual Test Files |
| 42 | + |
| 43 | +Run specific test files: |
| 44 | + |
| 45 | +```bash |
| 46 | +# Test specific module |
| 47 | +pnpm vitest src/langbase/langbase.test.ts --config vitest.node.config.js --run |
| 48 | + |
| 49 | +# Test with watch mode |
| 50 | +pnpm vitest src/common/request.test.ts --config vitest.node.config.js |
| 51 | +``` |
| 52 | + |
| 53 | +### GitHub Actions / CI |
| 54 | + |
| 55 | +Tests automatically run on: |
| 56 | +- Push to `main` or `develop` branches |
| 57 | +- Pull requests to `main` or `develop` branches |
| 58 | + |
| 59 | +The CI pipeline runs: |
| 60 | +1. **Linting** with ESLint |
| 61 | +2. **Type checking** with TypeScript |
| 62 | +3. **Unit tests** in Node.js environment |
| 63 | +4. **Unit tests** in Edge runtime environment |
| 64 | +5. **UI tests** for React components |
| 65 | +6. **Ecosystem tests** (Node ESM/CJS, Bun, Deno) - PR only |
| 66 | + |
| 67 | +## Test Structure |
| 68 | + |
| 69 | +### Core Test Files |
| 70 | + |
| 71 | +- **`src/langbase/langbase.test.ts`** - Main SDK class tests |
| 72 | +- **`src/common/request.test.ts`** - HTTP client and error handling |
| 73 | +- **`src/common/stream.test.ts`** - Streaming functionality |
| 74 | +- **`src/common/errors.test.ts`** - Error class hierarchy |
| 75 | +- **`src/langbase/threads.test.ts`** - Thread operations |
| 76 | +- **`src/langbase/workflows.test.ts`** - Workflow execution engine |
| 77 | +- **`src/lib/helpers/index.test.ts`** - Helper utilities |
| 78 | +- **`src/lib/utils/doc-to-formdata.test.ts`** - Document conversion utilities |
| 79 | + |
| 80 | +### Test Coverage Areas |
| 81 | + |
| 82 | +#### 1. Core SDK Operations |
| 83 | +- **Pipes**: Create, update, list, run (streaming and non-streaming) |
| 84 | +- **Memories**: Create, delete, list, retrieve, document operations |
| 85 | +- **Tools**: Web search, crawling |
| 86 | +- **Agent**: Run with various configurations |
| 87 | +- **Threads**: Full thread lifecycle management |
| 88 | +- **Workflows**: Step execution, retries, timeouts, tracing |
| 89 | + |
| 90 | +#### 2. HTTP Client (`Request` class) |
| 91 | +- All HTTP methods (GET, POST, PUT, DELETE) |
| 92 | +- Error handling for various HTTP status codes |
| 93 | +- Request/response processing |
| 94 | +- Stream handling |
| 95 | +- Header management |
| 96 | +- Raw response processing |
| 97 | + |
| 98 | +#### 3. Error Handling |
| 99 | +- API error types and inheritance |
| 100 | +- Network connection errors |
| 101 | +- Timeout handling |
| 102 | +- Proper error messages and status codes |
| 103 | + |
| 104 | +#### 4. Streaming |
| 105 | +- Server-Sent Events (SSE) processing |
| 106 | +- ReadableStream handling |
| 107 | +- Stream tee/split operations |
| 108 | +- Error handling in streams |
| 109 | + |
| 110 | +#### 5. Utilities |
| 111 | +- Document to FormData conversion |
| 112 | +- Tool extraction from streams |
| 113 | +- Helper functions for OpenAI integration |
| 114 | + |
| 115 | +## Test Patterns |
| 116 | + |
| 117 | +### Mocking External Dependencies |
| 118 | + |
| 119 | +Tests use Vitest's mocking capabilities: |
| 120 | + |
| 121 | +```typescript |
| 122 | +// Mock external modules |
| 123 | +vi.mock('../common/request'); |
| 124 | + |
| 125 | +// Mock global functions |
| 126 | +global.fetch = vi.fn(); |
| 127 | + |
| 128 | +// Mock class methods |
| 129 | +const mockPost = vi.fn().mockResolvedValue(mockResponse); |
| 130 | +(langbase as any).request = {post: mockPost}; |
| 131 | +``` |
| 132 | + |
| 133 | +### Testing Async Operations |
| 134 | + |
| 135 | +```typescript |
| 136 | +it('should handle async operations', async () => { |
| 137 | + const result = await langbase.pipes.run(options); |
| 138 | + expect(result).toEqual(expectedResponse); |
| 139 | +}); |
| 140 | +``` |
| 141 | + |
| 142 | +### Error Testing |
| 143 | + |
| 144 | +```typescript |
| 145 | +it('should throw appropriate errors', async () => { |
| 146 | + await expect( |
| 147 | + langbase.pipes.run(invalidOptions) |
| 148 | + ).rejects.toThrow('Expected error message'); |
| 149 | +}); |
| 150 | +``` |
| 151 | + |
| 152 | +### Stream Testing |
| 153 | + |
| 154 | +```typescript |
| 155 | +it('should handle streams correctly', async () => { |
| 156 | + const stream = createMockStream(); |
| 157 | + const results = []; |
| 158 | + |
| 159 | + for await (const chunk of stream) { |
| 160 | + results.push(chunk); |
| 161 | + } |
| 162 | + |
| 163 | + expect(results).toEqual(expectedChunks); |
| 164 | +}); |
| 165 | +``` |
| 166 | + |
| 167 | +## Continuous Integration |
| 168 | + |
| 169 | +### GitHub Actions Workflow |
| 170 | + |
| 171 | +The CI workflow (`.github/workflows/tests.yml`) includes: |
| 172 | + |
| 173 | +```yaml |
| 174 | +strategy: |
| 175 | + matrix: |
| 176 | + node-version: [18.x, 20.x] # Test multiple Node.js versions |
| 177 | +``` |
| 178 | +
|
| 179 | +### Test Jobs |
| 180 | +
|
| 181 | +1. **Main Test Job** |
| 182 | + - Install dependencies |
| 183 | + - Run linting |
| 184 | + - Run type checking |
| 185 | + - Test in Node.js environment |
| 186 | + - Test in Edge runtime environment |
| 187 | + - Test React UI components |
| 188 | +
|
| 189 | +2. **Ecosystem Test Job** (PR only) |
| 190 | + - Test with Node.js ESM/CJS |
| 191 | + - Test with Bun runtime |
| 192 | + - Test with Deno runtime |
| 193 | +
|
| 194 | +### Performance Considerations |
| 195 | +
|
| 196 | +- Tests use timeouts to prevent hanging |
| 197 | +- Ecosystem tests have 30-second timeouts |
| 198 | +- Parallel execution where possible |
| 199 | +- Efficient mocking to reduce test time |
| 200 | +
|
| 201 | +## Contributing |
| 202 | +
|
| 203 | +When adding new features: |
| 204 | +
|
| 205 | +1. **Write tests first** (TDD approach recommended) |
| 206 | +2. **Cover edge cases** and error scenarios |
| 207 | +3. **Mock external dependencies** properly |
| 208 | +4. **Test in multiple environments** if applicable |
| 209 | +5. **Update documentation** as needed |
| 210 | +
|
| 211 | +### Test Checklist |
| 212 | +
|
| 213 | +- [ ] Unit tests for new functionality |
| 214 | +- [ ] Error handling tests |
| 215 | +- [ ] Edge case coverage |
| 216 | +- [ ] Integration tests where appropriate |
| 217 | +- [ ] Performance considerations |
| 218 | +- [ ] Documentation updates |
| 219 | +
|
| 220 | +## Debugging Tests |
| 221 | +
|
| 222 | +### Local Debugging |
| 223 | +
|
| 224 | +```bash |
| 225 | +# Run with verbose output |
| 226 | +pnpm vitest --reporter=verbose |
| 227 | + |
| 228 | +# Run single test with debugging |
| 229 | +pnpm vitest src/path/to/test.ts --reporter=verbose |
| 230 | + |
| 231 | +# Watch mode for development |
| 232 | +pnpm vitest --watch |
| 233 | +``` |
| 234 | + |
| 235 | +### Common Issues |
| 236 | + |
| 237 | +1. **Path Resolution**: Ensure `@` alias is configured in vitest configs |
| 238 | +2. **Mocking**: Use `vi.resetAllMocks()` in `beforeEach` |
| 239 | +3. **Async**: Always `await` async operations in tests |
| 240 | +4. **Global Objects**: Mock globals like `fetch`, `FormData`, etc. |
| 241 | + |
| 242 | +## Best Practices |
| 243 | + |
| 244 | +- **Keep tests focused** - One concept per test |
| 245 | +- **Use descriptive test names** - Describe what should happen |
| 246 | +- **Mock external dependencies** - Keep tests isolated |
| 247 | +- **Test error scenarios** - Don't just test the happy path |
| 248 | +- **Clean up after tests** - Reset mocks and global state |
| 249 | +- **Use proper TypeScript types** - Maintain type safety in tests |
0 commit comments