# AI-Assisted Test Generation Exercises

## Exercise 1: Write a Test Generation Prompt

**Task:** For this function, write a prompt you'd use to generate unit tests. Include: framework (Vitest), desired cases, and any style preferences.

```javascript
function slugify(text) {
  return String(text)
    .toLowerCase()
    .trim()
    .replace(/\s+/g, '-')
    .replace(/[^a-z0-9-]/g, '');
}
```

**Validation:**
- [ ] Prompt includes the function (or clear reference)
- [ ] Framework specified
- [ ] At least 3 case types (e.g., happy, empty, special chars)
- [ ] Prompt is specific enough to get usable output

**Hints:**
1. What happens with "Hello World", "", "UPPERCASE", "a—b"?
2. Specify assert style: toBe, toEqual, toMatch
3. Include "trim and replace" behavior in the prompt

---

## Exercise 2: Critique and Fix a Generated Test

**Task:** This test was AI-generated. Identify 2 problems and fix them.

```javascript
test('fetchUser works', async () => {
  const result = await fetchUser(1);
  expect(result).toBeTruthy();
});
```

**Validation:**
- [ ] Identifies shallow assertion (toBeTruthy)
- [ ] Identifies missing mock/setup if fetchUser hits network
- [ ] Proposes concrete improvements (e.g., expect(result.name).toBe('Alice'))

**Hints:**
1. What does toBeTruthy actually verify?
2. Does fetchUser need a mock? What would happen without it?
3. What would a strong assertion look like?

---

## Exercise 3: Generate and Review

**Task:** Use Claude (or another AI) to generate tests for `slugify` from Exercise 1. Run them. Then review using the checklist: assertions verify behavior? Edge cases covered? Tests independent? List 2 improvements you'd make.

**Validation:**
- [ ] Tests were generated and run
- [ ] At least one improvement identified
- [ ] Improvement is actionable (specific change, not vague)

**Hints:**
1. Try the prompt you wrote in Exercise 1
2. Run with `npx vitest run` or your project's test command
3. Look for toBeDefined, toBeTruthy, or missing edge cases

---

## Exercise 4: AI for Integration Test Setup

**Task:** You have an API endpoint `POST /users` that creates a user. Writing integration tests requires: DB setup, request body, and assertions. Write a prompt that asks AI to generate one integration test, including setup and teardown. Specify your stack (e.g., Node, Express, Jest, supertest).

**Validation:**
- [ ] Prompt specifies stack and tools
- [ ] Asks for setup (DB, server) and teardown
- [ ] Asks for clear assertion (status, body shape)
- [ ] Understands AI may need refinement—integration is complex

**Hints:**
1. "Generate integration test for POST /users with Jest and supertest"
2. Include: how to start/stop server, reset DB
3. Ask for status 201 and body containing id, name, email

---

## Exercise 5: Build a Prompt Template

**Task:** Create a reusable prompt template for unit test generation. Use placeholders like [FUNCTION], [FRAMEWORK], [CASES]. Write 2 example fills (different functions) showing how you'd use it.

**Validation:**
- [ ] Template has clear placeholders
- [ ] Template includes context that improves output (framework, style, cases)
- [ ] Both examples would produce usable prompts

**Hints:**
1. Start with: "Generate [FRAMEWORK] unit tests for [FUNCTION]..."
2. Add: "Include [CASES]. Use [STYLE]."
3. Example: [FUNCTION]=parsePrice, [CASES]=happy, null, invalid
