betting-brain-v3 / rules
Cursor rule8 starsChanged 14 months agoMITView on GitHub
Analytics testing patterns and best practices
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository27 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .cursor/rules/99-floor.mdcCursor rule
- .cursor/rules/ai-friendly-testing.mdcCursor rule
- .cursor/rules/analytics-stub-api.mdcCursor rule
- .cursor/rules/api-patterns.mdcCursor rule
- .cursor/rules/browser-extension.mdcCursor rule
- .cursor/rules/bun-runtime.mdcCursor rule
- .cursor/rules/cache-optimization.mdcCursor rule
- .cursor/rules/ci-integration.mdcCursor rule
- .cursor/rules/ci-patterns.mdcCursor rule
- .cursor/rules/cloudflare-infrastructure.mdcCursor rule
- .cursor/rules/cloudflare-workers.mdcCursor rule
- .cursor/rules/code-searchability.mdcCursor rule
- .cursor/rules/coverage-thresholds.mdcCursor rule
- .cursor/rules/database-patterns.mdcCursor rule
- .cursor/rules/documentation.mdcCursor rule
- .cursor/rules/endpoint-routing.mdcCursor rule
- .cursor/rules/file-naming.mdcCursor rule
- .cursor/rules/mcp-integration.mdcCursor rule
- .cursor/rules/process-management.mdcCursor rule
- .cursor/rules/production-security.mdcCursor rule
- .cursor/rules/quality-standards.mdcCursor rule
- .cursor/rules/root-organization.mdcCursor rule
- .cursor/rules/security-patterns.mdcCursor rule
- .cursor/rules/testing.mdcCursor rule
- .cursor/rules/testing-patterns.mdcCursor rule
- .cursor/rules/test-setup-patterns.mdcCursor rule
The file
We split this analytics-testing.mdc into the rules it gives an agent
4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 119
No test pollution between runs
- Very specific: line 123
Use --randomize for reliability
- Distinctive: line 133
Use descriptive test names
- Distinctive: line 134
Document complex analytics scenarios
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
version: "1.1.0"
alwaysApply: true
description: "Analytics testing patterns and best practices"
lastUpdated: "2025-10-08"
dependencies: ["quality-standards", "testing-patterns"]
---
# Analytics Testing Rules
## Core Principles
**Analytics Engine calls are first-class citizens in tests**
- No network dependencies
- No flakes
- No waiting
## Required Patterns
### 1. Use AnalyticsEngineStub for All Analytics Testing
```typescript
// ✅ CORRECT: Use the global stub
import { createMockEnv } from '../utils/test-helpers';
const { env: mockEnv, analytics } = createMockEnv();
// Make analytics calls
await mockEnv.ANALYTICS_ENGINE.writeDataPoint({
blobs: ['event_type', 'data'],
doubles: [value1, value2],
tags: { source: 'test' }
});
// Assert with analytics.getCalls()
expect(analytics.callCount()).toBe(1);
expect(analytics.getCalls()[0].blobs).toEqual(['event_type', 'data']);
```
### 2. Never Use toHaveBeenCalledWith for Analytics
```typescript
// ❌ WRONG: Generic mock assertions
expect(mockAnalytics.writeDataPoint).toHaveBeenCalledWith({
blobs: ['event_type', 'data']
});
// ✅ CORRECT: First-class analytics assertions
expect(analytics.wasCalledWith({
blobs: ['event_type', 'data']
})).toBe(true);
```
### 3. Use Helper Functions for Common Assertions
```typescript
import {
expectAnalyticsCall,
expectAnalyticsCallCount,
expectAnalyticsDoubles
} from '../utils/test-helpers';
// Clean, readable assertions
expectAnalyticsCall(analytics, {
blobs: ['event_type', 'data'],
doubles: [1.5, 1000]
});
expectAnalyticsCallCount(analytics, 1);
expectAnalyticsDoubles(analytics, [1.5, 1000], 0.001);
```
### 4. Test Name Detection for Auto-Flush
```typescript
// ✅ CORRECT: Set test name for auto-flush
test('should track analytics calls with stub', async () => {
setCurrentTestName('should track analytics calls with stub');
// Test implementation
});
```
## File Organization
### Analytics Test Files
- Location: `tests/unit/analytics-*.test.ts`
- Pattern: `analytics-[feature].test.ts`
- Example: `analytics-tracking.test.ts`
### Test Utilities
- `tests/utils/analytics-engine-stub.ts` - Core stub implementation
- `tests/utils/test-helpers.ts` - Helper functions and assertions
- `tests/setup/test-setup.ts` - Global test configuration
## Configuration
### Dual Coverage Thresholds
- **Main suite**: 80% coverage threshold (enforced)
- **Analytics suite**: 0% coverage threshold (flexible)
### Package.json Scripts
```json
{
"analytics:test": "bun test tests/unit/analytics-testing-example.test.ts --randomize",
"analytics:test:coverage": "bun test --coverage --randomize tests/unit/analytics-testing-example.test.ts --config bunfig.analytics.toml",
"analytics:ci": "CI=true bun run analytics:test:coverage"
}
```
## Best Practices
### 1. Test Isolation
- Each test gets a fresh analytics stub
- Auto-flush only on analytics tests
- No test pollution between runs
### 2. Performance
- Tests run in ~20ms (no network calls)
- Use `--randomize` for reliability
- Coverage calculation is fast with stub
### 3. CI Integration
- Analytics tests use `--config bunfig.analytics.toml`
- Main suite enforces 80% coverage
- Nightly jobs validate real binding
### 4. Documentation
- Reference [ANALYTICS_TESTING_GUIDE.md](mdc:docs/testing/ANALYTICS_TESTING_GUIDE.md)
- Use descriptive test names
- Document complex analytics scenarios
## Anti-Patterns
### ❌ Don't Do These
```typescript
// Don't use real Analytics Engine in tests
const realAnalytics = env.ANALYTICS_ENGINE;
// Don't use generic mock assertions
expect(mockAnalytics).toHaveBeenCalledWith();
// Don't forget to set test name for auto-flush
test('my test', async () => {
// Missing setCurrentTestName()
});
// Don't create duplicate stubs
const analytics1 = createAnalyticsEngineStub();
const analytics2 = createAnalyticsEngineStub(); // Wrong!
```
### ✅ Do These Instead
```typescript
// Use the global stub
const { env: mockEnv, analytics } = createMockEnv();
// Use first-class analytics assertions
expect(analytics.getCalls()).toHaveLength(1);
// Set test name for auto-flush
test('my test', async () => {
setCurrentTestName('my test');
});
// Use the global stub consistently
const analytics = getGlobalAnalyticsStub();
```
## Related Rules
- [Test Setup Patterns](mdc:.cursor/rules/test-setup-patterns.mdc) - Test configuration and setup
- [Analytics Stub API](mdc:.cursor/rules/analytics-stub-api.mdc) - AnalyticsEngineStub API reference
- [Coverage Thresholds](mdc:.cursor/rules/coverage-thresholds.mdc) - Dual threshold system
- [CI Integration](mdc:.cursor/rules/ci-integration.mdc) - CI/CD automation patterns
## Related Files
- [analytics-engine-stub.ts](mdc:tests/utils/analytics-engine-stub.ts) - Core stub implementation
- [test-helpers.ts](mdc:tests/utils/test-helpers.ts) - Helper functions
- [test-setup.ts](mdc:tests/setup/test-setup.ts) - Global configuration
- [bunfig.analytics.toml](mdc:bunfig.analytics.toml) - Analytics-specific config
- [ANALYTICS_TESTING_GUIDE.md](mdc:docs/testing/ANALYTICS_TESTING_GUIDE.md) - Complete guide
---
**Status:** Production Ready ✅
**Last Updated:** 2025-10-07
**Score:** 9.5/10
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

