Specialist tdd guide

From VenTheZone/pi-dots · View interactive version

Specialist tdd guide is an AI agent skill from the GitHub repository VenTheZone/pi-dots. Test-driven development specialist enforcing write-tests-first workflows with comprehensive coverage. Use when developing new features, fixing bugs with regression tests, guiding developers through the red-green-refactor cycle, or ensuring 80%+ unit, integration, and E2E coverage.

How to use this skill

Skill file

Specialist: TDD Guide

Tests-before-code specialist ensuring all code is developed test-first with comprehensive, meaningful coverage. Enforces the red-green-refactor cycle and catches edge cases before implementation.

Workflow

  1. Write failing test first (RED) — define the expected behavior as a test; run it and confirm it fails
  2. Write minimal implementation (GREEN) — add just enough code to make the test pass
  3. Refactor (IMPROVE) — remove duplication, clarify names, optimize under a green suite
  4. Verify coverage — run the coverage report and confirm thresholds (80%+ lines/branches/functions/statements)
  5. Review edge cases — ensure null/empty/invalid/error/race cases are covered, not just the happy path

Write the Test First

// ALWAYS start with a failing test
describe('searchMarkets', () => {
  it('returns semantically similar markets', async () => {
    const results = await searchMarkets('election')

    expect(results).toHaveLength(5)
    expect(results[0].name).toContain('Trump')
    expect(results[1].name).toContain('Biden')
  })
})

Run it — it must fail before any implementation exists:

npm test
# Test should fail - we haven't implemented yet

Minimal Implementation (GREEN)

export async function searchMarkets(query: string) {
  const embedding = await generateEmbedding(query)
  const results = await vectorSearch(embedding)
  return results
}

Re-run the test — this time it passes. Then refactor while the suite stays green.

Test Types

1. Unit Tests (Mandatory)

Functions in isolation — fast, deterministic, no I/O:

import { calculateSimilarity } from './utils'

describe('calculateSimilarity', () => {
  it('returns 1.0 for identical embeddings', () => {
    const embedding = [0.1, 0.2, 0.3]
    expect(calculateSimilarity(embedding, embedding)).toBe(1.0)
  })

  it('returns 0.0 for orthogonal embeddings', () => {
    const a = [1, 0, 0]
    const b = [0, 1, 0]
    expect(calculateSimilarity(a, b)).toBe(0.0)
  })

  it('handles null gracefully', () => {
    expect(() => calculateSimilarity(null, [])).toThrow()
  })
})

2. Integration Tests (Mandatory)

API endpoints and database operations:

import { NextRequest } from 'next/server'
import { GET } from './route'

describe('GET /api/markets/search', () => {
  it('returns 200 with valid results', async () => {
    const request = new NextRequest('http://localhost/api/markets/search?q=trump')
    const response = await GET(request, {})
    const data = await response.json()

    expect(response.status).toBe(200)
    expect(data.success).toBe(true)
    expect(data.results.length).toBeGreaterThan(0)
  })

  it('returns 400 for missing query', async () => {
    const request = new NextRequest('http://localhost/api/markets/search')
    const response = await GET(request, {})
    expect(response.status).toBe(400)
  })
})

3. E2E Tests (Critical Flows)

Complete user journeys with Playwright:

import { test, expect } from '@playwright/test'

test('user can search and view market', async ({ page }) => {
  await page.goto('/')

  // Search for market
  await page.fill('input[placeholder="Search markets"]', 'election')
  await page.waitForTimeout(600) // Debounce

  // Verify results
  const results = page.locator('[data-testid="market-card"]')
  await expect(results).toHaveCount(5, { timeout: 5000 })

  // Click first result
  await results.first().click()

  // Verify market page loaded
  await expect(page).toHaveURL(/\/markets\//)
  await expect(page.locator('h1')).toBeVisible()
})

Edge Cases You MUST Test

  1. Null/Undefined — what if input is null?
  2. Empty — empty array/string
  3. Invalid Types — wrong type passed
  4. Boundaries — min/max values
  5. Errors — network failures, database errors
  6. Race Conditions — concurrent operations
  7. Large Data — 10k+ items
  8. Special Characters — unicode, emojis, SQL characters

Anti-Patterns (Test Smells)

Testing implementation details instead of behavior:

// DON'T test internal state
expect(component.state.count).toBe(5)

// DO test what users see
expect(screen.getByText('Count: 5')).toBeInTheDocument()

Tests depending on each other:

// DON'T rely on previous test
test('creates user', () => { /* ... */ })
test('updates same user', () => { /* needs previous test */ })

// DO setup data in each test
test('updates user', () => {
  const user = createTestUser()
  // Test logic
})

Coverage

# Run tests with coverage
npm run test:coverage

# View HTML report
open coverage/lcov-report/index.html

Required thresholds: Branches 80%, Functions 80%, Lines 80%, Statements 80%.

Test Quality Checklist

Remember: No code without tests. Tests are the safety net enabling confident refactoring and production reliability.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.