mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-12 20:27:30 +00:00
延续批次 1 的形态:只调模块**本来就有**的导出接口,输入 → 可观察输出。
4 个文件全部在 `<module>/test/`,源码目录仍零 `_test.go`。
共 24 条判据,每模块 1 个文件。
## 各模块的接口清单与判据
### internal/security —— NewCodec / Seal / Open / Domain / Enabled
TestCodecSealOpenRoundTrip Seal → Open 得回原文;Seal 不返回原文
TestCodecDomainSeparation 不同 domain 的密文互不通用
TestCodecWrongKeyCannotOpen 换钥后旧密文不可读
TestCodecEnabledAndDomain 元信息输出;nil Codec 不报 Enabled
TestCodecOldKeysStillDecrypt NewCodecWithOldKeys 轮换后旧密文可读
TestCodecOpenLegacyPlaintextPassthrough 旧明文原样透传
TestCodecOpenRejectsCorruptedCiphertext 带密文前缀但内容坏掉必须报错
**判据写错过一次并修正**:第一版把 `Open("not-a-cipher")` 也要求报错,
实测红。查实现(codec.go:138)确认那是**明确的兼容契约** —— 加密是后来加的,
库里存在未加密的历史值,判成错误会让整库读不出来。改成两条:
旧明文透传 / 损坏密文报错。把差异显式写下来。
### internal/events —— NewBus / Subscribe / Publish / SetGuard
TestPublishDeliversPayload 订阅后发布,handler 收到原 Payload
TestUnsubscribeStopsDelivery 退订后不再收到
TestOnlyMatchingTypeReceives 不同事件类型不串台
TestPublisherIdentity 空 Name = 内核,非空 = 插件
TestSetGuardRoundTrip SetGuard → Guard 拿回同一守卫
`Guard` stub 按真实接口签名实现(AllowPublish 返回 `(bool, string)`、
Scrub* 返回 map),不是猜的。
### internal/safeclose —— Safe / SafeFunc
TestSafeFuncIsIdempotent 连调 5 次不 panic,channel 确已关闭
TestSafeReturnsFiredOnce 同一把 Once 只有第一次返回 true
TestSafeRejectsNilInputs nil once / nil chan 安全返回 false
TestConcurrentCloseIsSafe 8 goroutine 并发关同一 channel
不用任何 `NewOnceForTest` 之类的测试钩子 —— `sync.Once` 由调用方自己造,
这正是模块本来的公开用法。
### internal/kstate —— NewSyncBus / Mount / Get / State
TestSyncBusReadyLifecycle MarkDeclared/MarkReady → IsReady/AllReady/Missing
TestSyncBusReadyInOrderIsStable 就绪顺序可复现,重复标记不重复记录
TestSyncBusKnownVsReady 声明过 ≠ 已就绪
TestSyncBusKernelComplete 内核完成标记可观察
TestSyncBusSnapshotIsCopy 外部改快照不影响总线
TestMountGet Mount → Get 拿到同一份状态
TestStateCloseAllInvokesClosers AddCloser 注册的清理被调用
不用 `MountForTest`(上一轮已删除):直接用 `Mount` + `Get` 断言输出。
## 变异验证(改回旧实现必须红)
security Seal 直接返回原文 → ★ Seal 返回了原文 —— 没有加密
events 退订函数变空操作 → ★ 退订后仍收到 1 次
kstate MarkReady 不记 order → ★ ReadyInOrder = [],期望 [a b]
safeclose Safe 不用 sync.Once → panic: close of closed channel
前两条第一版变异因文本形态不匹配**没有变红**,重做后确认判据有效 ——
空变异不算变异验证,这一点按纪律写清楚。
## 验证
go build ./... PASS
go vet ./... PASS
gofmt -l 干净
python3 .github/scripts/check-ci-gates.py PASS
go test ./... -count=1 13 个测试包 / 100 包无测试 / 0 FAIL
4 个新包 -race 全绿
源码目录 _test.go 0
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package test
|
||
|
||
// internal/events 的黑盒判据:NewBus → Subscribe → Publish → handler 收到的事件。
|
||
// 只调导出方法,不碰 b.subs、不读源码。
|
||
|
||
import (
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/JianFeeeee/HologramAgent/internal/events"
|
||
)
|
||
|
||
// TestPublishDeliversPayload:订阅后发布,handler 必须收到原事件内容。
|
||
func TestPublishDeliversPayload(t *testing.T) {
|
||
bus := events.NewBus()
|
||
got := make(chan *events.Event, 1)
|
||
unsub := bus.Subscribe(events.EventType("contract.test"), func(e *events.Event) {
|
||
got <- e
|
||
})
|
||
defer unsub()
|
||
|
||
bus.Publish(&events.Event{
|
||
Type: events.EventType("contract.test"),
|
||
Source: "contract",
|
||
Payload: map[string]interface{}{"value": 42},
|
||
})
|
||
select {
|
||
case e := <-got:
|
||
if e.Source != "contract" {
|
||
t.Fatalf("★ Source = %q", e.Source)
|
||
}
|
||
if e.Payload["value"] != 42 {
|
||
t.Fatalf("★ Payload = %v", e.Payload)
|
||
}
|
||
case <-time.After(2 * time.Second):
|
||
t.Fatal("★ 订阅者没收到事件")
|
||
}
|
||
}
|
||
|
||
// TestUnsubscribeStopsDelivery:退订后不得再收到。
|
||
func TestUnsubscribeStopsDelivery(t *testing.T) {
|
||
bus := events.NewBus()
|
||
n := 0
|
||
unsub := bus.Subscribe(events.EventType("contract.off"), func(e *events.Event) { n++ })
|
||
unsub()
|
||
bus.Publish(&events.Event{Type: events.EventType("contract.off")})
|
||
if n != 0 {
|
||
t.Fatalf("★ 退订后仍收到 %d 次", n)
|
||
}
|
||
}
|
||
|
||
// TestOnlyMatchingTypeReceives:不同事件类型之间不串台。
|
||
func TestOnlyMatchingTypeReceives(t *testing.T) {
|
||
bus := events.NewBus()
|
||
a, b := 0, 0
|
||
bus.Subscribe(events.EventType("type.a"), func(e *events.Event) { a++ })
|
||
bus.Subscribe(events.EventType("type.b"), func(e *events.Event) { b++ })
|
||
bus.Publish(&events.Event{Type: events.EventType("type.a")})
|
||
if a != 1 || b != 0 {
|
||
t.Fatalf("★ 串台:a=%d b=%d", a, b)
|
||
}
|
||
}
|
||
|
||
// TestPublisherIdentity:Name 为空即内核,非空即插件。
|
||
func TestPublisherIdentity(t *testing.T) {
|
||
if !((events.Publisher{}).IsKernel()) {
|
||
t.Fatal("★ 空 Name 应判为内核")
|
||
}
|
||
if (events.Publisher{Name: "qq"}).IsKernel() {
|
||
t.Fatal("★ 插件被判成内核")
|
||
}
|
||
}
|
||
|
||
// TestSetGuardRoundTrip:SetGuard → Guard 拿回同一个守卫。
|
||
type stubGuard struct{}
|
||
|
||
func (g *stubGuard) AllowPublish(p events.Publisher, et events.EventType) (bool, string) {
|
||
return true, ""
|
||
}
|
||
func (g *stubGuard) AllowSubscribe(p events.Publisher, et events.EventType) (bool, string) {
|
||
return true, ""
|
||
}
|
||
func (g *stubGuard) ScrubPayload(et events.EventType, m map[string]interface{}) map[string]interface{} {
|
||
return m
|
||
}
|
||
func (g *stubGuard) ScrubInbound(et events.EventType, m map[string]interface{}) map[string]interface{} {
|
||
return m
|
||
}
|
||
|
||
func TestSetGuardRoundTrip(t *testing.T) {
|
||
bus := events.NewBus()
|
||
g := &stubGuard{}
|
||
bus.SetGuard(g)
|
||
var got events.Guard = bus.Guard()
|
||
if got != g {
|
||
t.Fatal("★ Guard() 没拿回 SetGuard 设置的守卫")
|
||
}
|
||
}
|