Files
AgentCompany/builtin-skills/software-engineer/clean-architecture/references/test-layer-isolation.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

122 lines
3.3 KiB
Markdown

---
title: Test Each Layer in Isolation
impact: LOW-MEDIUM
impactDescription: fast feedback, precise failure location
tags: test, isolation, layers, unit
---
## Test Each Layer in Isolation
Each architectural layer should be testable independently. Domain tests need no infrastructure. Use case tests need no web framework. This enables fast, focused testing.
**Incorrect (everything tested through API):**
```typescript
// All tests go through HTTP - slow, imprecise failures
describe('Order API', () => {
let app: Express
let db: Database
beforeEach(async () => {
db = await Database.connect()
await db.migrate()
await db.seed()
app = createApp(db)
})
afterEach(async () => {
await db.clean()
await db.close()
})
it('rejects order with insufficient inventory', async () => {
// 500ms+ per test
const response = await request(app)
.post('/orders')
.send({ items: [{ productId: 'p1', quantity: 1000 }] })
expect(response.status).toBe(400)
expect(response.body.error).toBe('Insufficient inventory')
})
// 50 more tests like this - test suite takes 5 minutes
// When one fails, unclear if it's domain, use case, or HTTP issue
})
```
**Correct (layered testing):**
```typescript
// Domain layer tests - instant, no dependencies
describe('Order', () => {
it('calculates total from items', () => {
const order = new Order()
order.addItem(new Product('p1', Money.dollars(10)), 2)
order.addItem(new Product('p2', Money.dollars(5)), 1)
expect(order.total).toEqual(Money.dollars(25))
})
it('prevents adding item with zero quantity', () => {
const order = new Order()
expect(() => order.addItem(product, 0)).toThrow(InvalidQuantityError)
})
})
// Use case tests - fast, test doubles for ports
describe('PlaceOrderUseCase', () => {
it('rejects order when inventory insufficient', () => {
const orders = new InMemoryOrderRepository()
const inventory = new FakeInventory({ 'p1': 5 }) // Only 5 in stock
const useCase = new PlaceOrderUseCase(orders, inventory)
const result = useCase.execute({
items: [{ productId: 'p1', quantity: 10 }] // Want 10
})
expect(result.isFailure).toBe(true)
expect(result.error).toBe('INSUFFICIENT_INVENTORY')
})
})
// Infrastructure tests - verify adapters work correctly
describe('PostgresOrderRepository', () => {
it('persists and retrieves order', async () => {
const repo = new PostgresOrderRepository(testDb)
const order = createTestOrder()
await repo.save(order)
const retrieved = await repo.findById(order.id)
expect(retrieved).toEqual(order)
})
})
// API tests - few, verify wiring only
describe('POST /orders', () => {
it('returns 201 when order placed successfully', async () => {
// Most logic tested above; this verifies HTTP wiring
const response = await request(app)
.post('/orders')
.send(validOrderPayload)
expect(response.status).toBe(201)
expect(response.body).toHaveProperty('orderId')
})
})
```
**Test pyramid:**
```text
/\
/ \ E2E: ~5 tests (slow, expensive)
/----\
/ \ Integration: ~20 tests (medium)
/--------\
/ \ Unit: ~200 tests (fast, cheap)
/____________\
```
Reference: [The Test Pyramid](https://martinfowler.com/articles/practical-test-pyramid.html)