跳转到内容

深化模块

根据依赖关系,安全地深化一组浅模块。本文假定你已了解 SKILL.md 中的词汇:module、interface、seam、adapter。

评估某个候选模块是否适合深化时,先给它的依赖分类。类别决定了如何通过 seam 测试深化后的模块。

纯计算、内存状态、没有 I/O。始终可以深化:合并这些模块,并直接通过新 interface 测试。不需要 adapter。

有本地测试替身的依赖(例如,用 PGLite 替代 Postgres,用内存文件系统替代文件系统)。如果测试替身存在,就可以深化。测试套件运行测试替身,并用它测试深化后的模块。seam 是内部的;模块的外部 interface 不需要 port。

跨越网络边界的自有服务(微服务、内部 API)。在 seam 处定义一个 port(interface)。深模块负责核心逻辑;通过注入的 adapter 处理传输。测试使用内存 adapter。生产环境使用 HTTP/gRPC/队列 adapter。

建议写法:“在 seam 处定义一个 port,为生产环境实现 HTTP adapter,为测试实现内存 adapter。这样即使服务部署在网络两端,逻辑仍集中在一个深模块中。”

你无法控制的第三方服务(Stripe、Twilio 等)。深化后的模块接收注入的外部依赖 port;测试提供 mock adapter。

  • 一个 adapter 意味着 seam 只是设想;两个 adapter 才说明它确实存在。 除非至少有两个合理的 adapter(通常是生产环境和测试各一个),否则不要引入 port。只有一个 adapter 的 seam 只是间接层。
  • 内部 seam 与外部 seam。 深模块可以有内部 seam(仅供自身实现使用,也供自身测试使用),interface 处也可以有外部 seam。不要只因为测试要用内部 seam,就通过 interface 暴露它。
  • 一旦深化后的模块 interface 已有测试,浅模块上的旧单元测试就失去了价值;删除它们。
  • 在深化后的模块 interface 上编写新测试。interface 就是测试面。
  • 测试应通过 interface 检查可观察到的结果,而不是内部状态。
  • 测试应能经受内部重构,因为它们描述的是行为,而不是实现。如果实现一变,测试就必须跟着改,那它测试的内容已经越过了 interface。
英文 DEEPENING.md
# Deepening
How to deepen a cluster of shallow modules safely, given its dependencies. Assumes the vocabulary in [SKILL.md](SKILL.md): **module**, **interface**, **seam**, **adapter**.
## Dependency categories
When assessing a candidate for deepening, classify its dependencies. The category determines how the deepened module is tested across its seam.
### 1. In-process
Pure computation, in-memory state, no I/O. Always deepenable: merge the modules and test through the new interface directly. No adapter needed.
### 2. Local-substitutable
Dependencies that have local test stand-ins (PGLite for Postgres, in-memory filesystem). Deepenable if the stand-in exists. The deepened module is tested with the stand-in running in the test suite. The seam is internal; no port at the module's external interface.
### 3. Remote but owned (Ports & Adapters)
Your own services across a network boundary (microservices, internal APIs). Define a **port** (interface) at the seam. The deep module owns the logic; the transport is injected as an **adapter**. Tests use an in-memory adapter. Production uses an HTTP/gRPC/queue adapter.
Recommendation shape: *"Define a port at the seam, implement an HTTP adapter for production and an in-memory adapter for testing, so the logic sits in one deep module even though it's deployed across a network."*
### 4. True external (Mock)
Third-party services (Stripe, Twilio, etc.) you don't control. The deepened module takes the external dependency as an injected port; tests provide a mock adapter.
## Seam discipline
- **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a port unless at least two adapters are justified (typically production + test). A single-adapter seam is just indirection.
- **Internal seams vs external seams.** A deep module can have internal seams (private to its implementation, used by its own tests) as well as the external seam at its interface. Don't expose internal seams through the interface just because tests use them.
## Testing strategy: replace, don't layer
- Old unit tests on shallow modules become waste once tests at the deepened module's interface exist; delete them.
- Write new tests at the deepened module's interface. The **interface is the test surface**.
- Tests assert on observable outcomes through the interface, not internal state.
- Tests should survive internal refactors, since they describe behaviour, not implementation. If a test has to change when the implementation changes, it's testing past the interface.

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号