Tdd

From Miosa-osa/canopy · View interactive version

Tdd is an AI agent skill from the GitHub repository Miosa-osa/canopy. Enforces Test-Driven Development discipline with RED-GREEN-REFACTOR cycle

How to use this skill

Skill file

TDD Enforcer Skill

Enforces strict Test-Driven Development discipline. You cannot prompt your way into TDD discipline - you need forcing functions that make TDD the path of least resistance.

Activation

This skill activates when:

TDD Cycle

Phase 1: RED (Write Failing Test First)

Before writing ANY implementation code:

  1. Understand the requirement
  1. Write the test FIRST

```

// The test must:

// - Define expected behavior

// - Be specific and focused

// - FAIL when run (no implementation yet)

```

  1. Run the test - confirm it FAILS

Phase 2: GREEN (Minimum Implementation)

  1. Write MINIMUM code to pass
  1. Run the test - confirm it PASSES

Phase 3: REFACTOR (Improve Code)

  1. Refactor while keeping tests green

Enforcement Rules

Pre-Implementation Check

Before writing any feature code, verify:

Blocking Conditions

BLOCK implementation if:

Quality Gates

Framework Detection

JavaScript/TypeScript

# Jest
npm test -- --coverage

# Vitest
npx vitest run --coverage

# Playwright (E2E)
npx playwright test

Go

# Unit tests with coverage
go test -cover -coverprofile=coverage.out ./...

# View coverage
go tool cover -html=coverage.out

Python

# pytest with coverage
pytest --cov=. --cov-report=html

Example Workflow

## Task: Add user authentication

### 1. RED: Write failing test

// auth.test.ts

describe('authenticateUser', () => {

it('should return token for valid credentials', async () => {

const result = await authenticateUser('user@test.com', 'password123');

expect(result.token).toBeDefined();

expect(result.expiresIn).toBe(3600);

});

it('should throw error for invalid credentials', async () => {

await expect(

authenticateUser('user@test.com', 'wrong')

).rejects.toThrow('Invalid credentials');

});

});


### 2. Run test → FAILS (function doesn't exist)

### 3. GREEN: Minimum implementation

// auth.ts

export async function authenticateUser(email: string, password: string) {

const user = await findUserByEmail(email);

if (!user || !verifyPassword(password, user.passwordHash)) {

throw new Error('Invalid credentials');

}

return {

token: generateToken(user),

expiresIn: 3600

};

}


### 4. Run test → PASSES

### 5. REFACTOR: Improve code quality
- Extract constants
- Add input validation
- Improve error messages

Integration with Hooks

Pre-commit Hook

#!/bin/bash
# Block commits without test coverage

# Get changed files
changed_files=$(git diff --cached --name-only --diff-filter=ACM)

# Check for test files
for file in $changed_files; do
  if [[ $file =~ \.(ts|js|go|py)$ ]] && [[ ! $file =~ (test|spec) ]]; then
    # Implementation file - check for corresponding test
    test_file="${file%.*}.test.${file##*.}"
    if ! git diff --cached --name-only | grep -q "$test_file"; then
      echo "ERROR: No test file for $file"
      echo "TDD requires tests FIRST. Add $test_file"
      exit 1
    fi
  fi
done

Metrics Tracked

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.