Files
AgentCompany/builtin-skills/software-engineer/clean-architecture/references/test-testable-design.md
T
ZacharyZhang-NY e74dafcd36 Silence render-disposed spam, unbreak bundled skills, drop board wake
Three independently ugly bugs that all fed the same log flood:

* Electron's `WebFrameMain.send` writes every "Render frame was disposed"
  to stderr via `console.error`. When the worker fans a second
  agent-event past a reloading renderer, we produced thousands of
  identical lines and stalled the main loop on stderr I/O. Added a
  `mainFrame.detached` guard so the send is short-circuited before
  Electron's own catch block fires, and installed a targeted filter in
  `suppress-warnings.ts` (now actually imported from the entry point)
  so the specific `console.error("Error sending from webFrameMain:"…)`
  noise never reaches the terminal. Any other payload still surfaces.

* `dispatchAgentMessage` accepted the human-reader sentinel as a wake
  target when an agent replied to `__board__` — `wakeAgentIfPossible`
  then called `db.getAgent("__board__")`, threw, and the dispatcher
  kept repeating on every send. Strip `HUMAN_SENDER_ID` at
  `resolveMessageWakeTargets`, and keep a defense-in-depth guard in the
  heartbeat so a stray caller (future automation fan-out) cannot
  reintroduce the crash.

* Pi's skills loader rejects any SKILL.md whose frontmatter `name`
  diverges from its parent directory. The four bundled `my-writing-style`
  copies declared `name: writing-style-skill`, so every install emitted
  a warning and dropped the skill. They also lived as orphan gitlinks
  (mode 160000 without .gitmodules), and the global `*.md` gitignore
  hid every other SKILL.md from version control. Fixed the frontmatter
  to match the dir, converted the dangling submodules to plain trees,
  and allowlisted `builtin-skills/**/*.md` so a fresh clone carries the
  catalogue instead of silently losing it.
2026-04-20 08:13:48 -04:00

105 lines
3.3 KiB
Markdown

---
title: Design for Testability From the Start
impact: LOW-MEDIUM
impactDescription: enables comprehensive testing without test-induced damage
tags: test, testability, design, interfaces
---
## Design for Testability From the Start
Clean architecture makes testing easy by design. If something is hard to test, it's a sign of architectural coupling. Don't compromise architecture for testability; fix the coupling.
**Incorrect (hard to test, requiring workarounds):**
```java
public class OrderProcessor {
public void process(Order order) {
// Hard to test: direct instantiation
PaymentService payment = new PaymentService();
// Hard to test: static call
if (InventoryChecker.isAvailable(order.getItems())) {
// Hard to test: singleton
payment.charge(order.getTotal());
NotificationService.getInstance().sendConfirmation(order);
// Hard to test: current time
order.setProcessedAt(new Date());
}
}
}
// Test requires PowerMock, static mocking, singleton reset
// Test is slow, brittle, and complicated
@Test
@PrepareForTest({InventoryChecker.class, NotificationService.class})
public void testProcess() {
PowerMockito.mockStatic(InventoryChecker.class);
PowerMockito.when(InventoryChecker.isAvailable(any())).thenReturn(true);
// ... 20 more lines of mock setup
}
```
**Correct (testable by design):**
```java
public class OrderProcessor {
private final InventoryChecker inventory;
private final PaymentGateway payment;
private final NotificationPort notification;
private final Clock clock;
// All dependencies injected - easy to substitute
public OrderProcessor(
InventoryChecker inventory,
PaymentGateway payment,
NotificationPort notification,
Clock clock
) {
this.inventory = inventory;
this.payment = payment;
this.notification = notification;
this.clock = clock;
}
public ProcessResult process(Order order) {
if (!inventory.isAvailable(order.getItems())) {
return ProcessResult.unavailable();
}
PaymentResult paymentResult = payment.charge(order.getTotal());
if (!paymentResult.isSuccessful()) {
return ProcessResult.paymentFailed(paymentResult.getError());
}
order.markProcessed(clock.now());
notification.sendConfirmation(order);
return ProcessResult.success(order);
}
}
@Test
void processesOrderWhenInventoryAvailable() {
var inventory = StubInventory.withAvailability(true);
var payment = FakePaymentGateway.alwaysSucceeds();
var notification = new SpyNotification();
var clock = Clock.fixed(Instant.parse("2024-01-15T10:00:00Z"), ZoneOffset.UTC);
var processor = new OrderProcessor(inventory, payment, notification, clock);
var result = processor.process(order);
assertThat(result.isSuccessful()).isTrue();
}
```
**Testability checklist:**
- [ ] No `new` for services (inject dependencies)
- [ ] No static method calls for behavior (use interfaces)
- [ ] No singletons (pass instances)
- [ ] No direct time/random access (inject Clock, Random)
- [ ] No hidden dependencies (everything in constructor)
Reference: [Growing Object-Oriented Software, Guided by Tests](http://www.growing-object-oriented-software.com/)