↓ Skip to main content

Auditing AI-Written Godot Tests: Fixing Brittle Mocks Before a Refactor

Author
David Watson
Terraforming enterprise clouds by day, terraforming worlds by night. With an unhealthy urge to over-engineer both.

Midway through my five-day strategy game sprint, I was gearing up to break apart a bloated 2,700+ line root simulation script. Before touching it, I glanced at my test runner: 42 headless test suites and more than 1,800 passing assertions (running on a lightweight custom headless GDScript runner, though everything here applies equally to GUT or gdUnit4).

I thought I was safe to refactor aggressively. Then I actually opened a few of the test files my AI agents had written over the previous few days.

A surprising chunk of them weren’t testing the game at all. They were testing mocks, hardcoded theme colours, and raw internal dictionaries—the exact kind of tests that stay green right up until you touch the architecture, and then shatter all at once in a wall of false failures.


Why AI Test Counts Can Mislead You
#

In my earlier posts on local GPUs and direct-to-disk generation, I talked about how quickly my RTX 4070 Ti can spit out unit tests. When a test file takes a few seconds to generate, it’s easy to mistake a high assertion count for a good safety net.

When you ask a model to write tests for a feature, its main goal is getting green checkmarks quickly. If your production class doesn’t expose a clean setup method, the model won’t pause and add one—it will poke private fields directly, stuff raw dictionaries into internal arrays, or copy exact hex colours straight out of your theme script.


The Anti-Patterns I Kept Finding
#

Auditing 21 test files before starting the refactor turned up the same patterns across the repo:

Brittle UI & Theme Assertions
#

Models love asserting on exact pixel borders, font sizes, and hex strings copied straight from the theme builder. One theme test had grown past 80 lines checking exact styling values, so every minor visual tweak broke it. Replacing those with structural and contrast checks cut it down to around 35 lines:

# BAD: Breaks on every visual polish tweak
assert_eq(stylebox.border_width_left, 1)
assert_true(stylebox.bg_color.is_equal_approx(Color("0d1c26f5")))
assert_eq(theme.get_font_size("font_size", "ActionBtn"), 10)

# GOOD: Tests structural contract & semantic contrast
assert_true(theme.get_stylebox("panel", "PrimaryCard") is StyleBoxFlat)
assert_gt(cyan_fill.bg_color.b, cyan_fill.bg_color.r)   # cyan stays cool-toned
assert_gt(amber_fill.bg_color.r, amber_fill.bg_color.b) # amber stays warm-toned

Raw-Dictionary Mocks Skipping Real Code
#

Across the logistics, combat, economy, and recycling suites, tests were mutating internal slots dictionaries directly instead of activating buildings through a public method. That bypassed activation side effects and guaranteed every test would break the moment I replaced those dictionaries with a typed class:

# BAD: Bypasses activation hooks and breaks when dict becomes a typed class
node.slots[0]["building"] = "REACTOR"
node.slots[0]["status"] = "ACTIVE"
node.slots[0]["build_progress"] = 1.0

# GOOD: Exercises the real public domain API
assert_true(node.activate_slot(0, "REACTOR"))

Faking Entities by Nulling Internal Fields
#

To set up edge cases, the AI would sometimes grab an existing object and null out internal fields to fake a different entity type—like taking a moon and clearing its parent reference to pretend it was an isolated planet:

# BAD: Mutates a moon into a fake planet by nulling internal fields
var fake_planet := sim.asteroids[0]
fake_planet.parent_body = null

# GOOD: Uses a real planet from the generated system
var target_planet := sim.planets[2]

Real Bugs the Audit Caught Before the Refactor
#

Cleaning up those 21 suites didn’t just make the refactor safer—it flushed out three real bugs in the game code:

  • Colonising a planet or building a station didn’t always initialise its faction owner automatically.
  • Clearing all slots on a body wasn’t firing deactivation hooks for active extractors.
  • Host classes didn’t expose a clean public method to activate a slot with all of its side effects—which is why the AI had fallen back to mutating raw dictionaries in the first place.

That last one stuck with me: awkward AI tests are often pointing at an awkward production API. Whenever a model has to reach into private fields to arrange a test, adding a small public helper on the class usually fixes both.


By the Numbers
#

21 / 42

Test Suites Audited

Half the test suite tightened up before touching the root simulation script.

86 → 34

LOC on Brittle UI Test

Hardcoded hex colours and pixel borders replaced with behavioural checks.

3

Real Production Bugs

Hidden state-initialisation and cleanup bugs surfaced during the audit.

Next Up
#

With the test suite exercising real public APIs instead of mocks, I finally felt safe breaking up that 2,700+ line root simulation script—which is up next.