Back to blog
·5 min read

OddSmith #3: from domain to screen, doing TDD until I paused it

Building the OddSmith UI with React and Vite: Red/Green on components, results derived from state, and an honest account of what went wrong.

OddSmith #3: from domain to screen, doing TDD until I paused it

In the previous post I closed the domain: resolveSequence to chain steps and resolveAttack to join what happens before and after the damage. This time it’s the interface’s turn.

Where I actually was

Before writing a single line of UI I wanted to be sure where I stood. git fetch and git status told me my local copy was 3 commits ahead of GitHub: exactly the ones containing resolveAttack, which I had never pushed. The blog described a finished domain and the repository didn’t have it. A small lesson: before resuming a project, check what the remote really contains.

There was no debate about the stack. The repository already used React, TypeScript, Vite and Vitest, and that fits well: the logic is plain TypeScript with no framework dependency, and an editable list of steps is the textbook use case for React. I only added Testing Library and jsdom to test components.

The Red/Green cycle on components

I kept the rhythm I used for the domain: a failing test, then the minimum code to make it pass.

  1. StepRow: shows a step with its threshold and reports changes.
  2. StepList: renders one row per step, and lets you edit, add and remove them.
  3. App: state lives here, and the result is computed from it.

Two design decisions worth highlighting:

  • The result is not state. It’s a constant computed on every render from attacks and steps. There is no second copy to keep in sync, so the number on screen can’t go stale, which was the bug that made me rebuild the project in the first place.
  • Controlled components. StepRow and StepList store nothing: they receive data and hand back the modified list through onChange. That’s why key={index} is safe here: rows have no internal state that could get mixed up when one is removed.

What went wrong (and what it teaches)

This part had more stumbles than I’d like to admit, which is exactly why I’m telling it:

  • An empty test file. Vitest said No test suite found. It looks like a Red, but it isn’t: a proper Red fails with Failed to resolve import "./StepRow". The file was empty because it hadn’t been saved. A failing test only counts if it fails for the right reason.
  • Commits with broken tests, twice. I typed vitest, git add and git commit on separate lines, and git happily committed even though the test failed. The fix is to chain them with &&, so the commit only happens if everything passes.
  • Code in the wrong file. I pasted the tests into StepList.tsx instead of StepList.test.tsx. The symptom was Element type is invalid: got undefined. Since then I check the editor tab name before saving.
  • vite/config vs vitest/config. The tests passed, but tsc -b complained because defineConfig came from the wrong package and the test field didn’t exist in its types. Tests don’t catch configuration mistakes; the typechecker does.

Pausing TDD, and saying so

At some point I wanted to see the app running. I decided to stop doing TDD and build the visual part directly, and there’s nothing wrong with admitting it: it’s a conscious decision, with a debt written down (see below). What I did keep was the rule that every commit command comes after green tests, lint and typecheck.

The model on screen

What started as “a list of steps” ended up with a structure that mirrors the rules of the game. The UI is in Spanish, so I’ll give the labels in parentheses:

  • Attacker (Atacante): step 1 (Hit, Impactar) and step 2 (Wound, Herir). They’re always there, and only their threshold is editable.
  • Defender (Defensor): step 3 (Save, Salvación), always present, and an optional step 4 (FNP, Añadir FNP) that appears when you ask for it. There is no step 5.

This forced me to split the state into attackSteps and defenceSteps, and to recombine them right before calling the domain, which didn’t change:

const [save, ...fnp] = defenceSteps
const result = resolveAttack(attacks, [...attackSteps, save], damage, fnp)

The type of each step (success or failure) stopped being a UI control: the data fixes it. Hit and wound are successes; the save and the FNP are failures from the attacker’s point of view. And the save starts at 7, which means “no defence”, so it doesn’t change the result until you touch it.

A refactor that paid for itself

To show, on each row, the probability that the attack carries on, I didn’t want to duplicate the success/failure logic in the UI. I moved applyStep out of the loop (the refactor I had pending) and exposed it as a domain function, with its own tests:

export function stepProbability(step: Step): number {
    return applyStep[step.countsAs](successProbability(step.threshold));
}

resolveSequence reuses it, and the existing tests stayed green without touching them. With the default values, 10 attacks with hit and wound at 4+ give 2.50. Add an FNP at 5+ and it drops to 1.67.

TDD debt

What I built without tests and have to cover when I come back:

  • minSteps and maxSteps in StepList
  • addLabel, firstNumber and newStep
  • editableCountsAs (hiding the selector)
  • The damage field and the attacker/defender split in App

What’s next

Move the damage field between the two cards, so the order on screen follows the real sequence of an attack, and show a step-by-step breakdown of the result (out of 10 attacks, how many hit, how many wound…).