# fix: deduplicate distractors, replace tautological test

## Problem

Two issues in `createGameSession` and its test suite:

1. If `getDistractors` returned the correct answer as one of the distractors, `createGameSession` would include it in the options array without filtering it out. `indexOf` would then find the first occurrence, which might not be the one intended as the correct answer — producing a question where the correct answer appears twice and the stored `correctOptionId` is wrong.

2. The test `"distractors are never the correct answer"` was tautological — it filtered the correct answer out of the options array, then asserted the remaining items were not the correct answer. It was testing that `Array.filter()` works. It could never fail.

## Options considered

### Option A — Filter duplicates after fetching, request extra distractors as buffer ✅

Filter out any distractor that matches the correct answer after fetching. Request 6 distractors instead of 3 to ensure enough remain after deduplication. Take the first 3 valid ones with `slice(0, 3)`.

Chosen because: deduplication at the service layer is the right place — `getDistractors` shouldn't need to know what the correct answer is. Requesting extra provides a buffer against collisions.

### Option B — Fix `getDistractors` to never return the correct answer

Add a NOT filter in the database query.

Not chosen for this ticket — the database query is in `@lila/db` and is a separate concern. The service layer should be defensive regardless of what the model layer returns.

## Solution

- Filter distractors against the correct answer before building options:

  ```ts
  const uniqueDistractors = distractorTexts.filter((t) => t !== term.targetText);
  const optionTexts = [term.targetText, ...uniqueDistractors.slice(0, 3)];
  ```

- Request 6 distractors instead of 3 to account for potential duplicates
- Replaced tautological test with a test that actually exercises the duplicate case:

  ```ts
  it("correct answer appears exactly once even if getDistractors returns a duplicate", ...)
  ```

- Added distractor failure propagation test:

  ```ts
  it("propagates getDistractors failure", ...)
  ```

## Files changed

- `apps/api/src/services/gameService.ts` — deduplication logic, distractor count increased to 6
- `apps/api/src/services/gameService.test.ts` — tautological test replaced, failure test added

## Commit

`fix: deduplicate distractors, replace tautological test, add distractor failure test`
