agentleFS
brendadeeznuts1111brendadeeznuts1111/betting-brain-v3/.cursor/rules/analytics-testing.mdc

betting-brain-v3 / rules

Cursor rule8 starsChanged 14 months agoMITView on GitHub

Analytics testing patterns and best practices

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
  1. Distinctive:

    No test pollution between runs

    line 119
  2. Very specific:

    Use --randomize for reliability

    line 123
  3. Distinctive:

    Use descriptive test names

    line 133
  4. Distinctive:

    Document complex analytics scenarios

    line 134

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
analytics-testing.mdc
---
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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.