mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-12 04:07:34 +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
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package test
|
||
|
||
// internal/safeclose 的黑盒判据:只用导出 API,输入 → 可观察输出。
|
||
// 不构造内部状态、不用任何测试专用钩子。
|
||
|
||
import (
|
||
"sync"
|
||
"testing"
|
||
|
||
"github.com/JianFeeeee/HologramAgent/internal/safeclose"
|
||
)
|
||
|
||
// TestSafeFuncIsIdempotent:同一个返回函数连调多次不得 panic,channel 确已关闭。
|
||
func TestSafeFuncIsIdempotent(t *testing.T) {
|
||
ch := make(chan struct{})
|
||
stop := safeclose.SafeFunc(ch)
|
||
for i := 0; i < 5; i++ {
|
||
stop()
|
||
}
|
||
select {
|
||
case <-ch:
|
||
default:
|
||
t.Fatal("★ channel 未关闭 —— SafeFunc 的返回函数没生效")
|
||
}
|
||
}
|
||
|
||
// TestSafeReturnsFiredOnce:Safe 的返回值报告「本次是否真关闭」,
|
||
// 同一把 Once 只有第一次返回 true。
|
||
func TestSafeReturnsFiredOnce(t *testing.T) {
|
||
ch := make(chan struct{})
|
||
var once sync.Once
|
||
if !safeclose.Safe(&once, ch) {
|
||
t.Fatal("★ 第一次 Safe 应返回 true")
|
||
}
|
||
if safeclose.Safe(&once, ch) {
|
||
t.Fatal("★ 第二次 Safe 仍返回 true —— 幂等语义坏了")
|
||
}
|
||
}
|
||
|
||
// TestSafeRejectsNilInputs:nil once / nil chan 必须安全返回 false。
|
||
func TestSafeRejectsNilInputs(t *testing.T) {
|
||
var once sync.Once
|
||
if safeclose.Safe[struct{}](&once, nil) {
|
||
t.Fatal("★ nil chan 被接受")
|
||
}
|
||
ch := make(chan struct{})
|
||
if safeclose.Safe[struct{}](nil, ch) {
|
||
t.Fatal("★ nil once 被接受")
|
||
}
|
||
select {
|
||
case <-ch:
|
||
t.Fatal("★ nil once 却关闭了 channel")
|
||
default:
|
||
}
|
||
}
|
||
|
||
// TestConcurrentCloseIsSafe:8 个 goroutine 同时关同一个 channel 不得 panic/死锁。
|
||
func TestConcurrentCloseIsSafe(t *testing.T) {
|
||
for round := 0; round < 100; round++ {
|
||
ch := make(chan struct{})
|
||
var once sync.Once
|
||
done := make(chan struct{}, 8)
|
||
for i := 0; i < 8; i++ {
|
||
go func() {
|
||
safeclose.Safe(&once, ch)
|
||
done <- struct{}{}
|
||
}()
|
||
}
|
||
for i := 0; i < 8; i++ {
|
||
<-done
|
||
}
|
||
select {
|
||
case <-ch:
|
||
default:
|
||
t.Fatal("★ 并发结束后 channel 未关闭")
|
||
}
|
||
}
|
||
}
|