Files
AutoZero/diag/diag_test.go
T
Sakurasan d5dac5a6fc feat: AutoZero 通用 Agent 框架(从 AutoGo 五阶段拆分完成)
llm(三协议 Provider)/ session(JSONL 持久化)/ tools(Registry、围栏、
builtin、computer)/ cdp(browser)/ skills / mcp / agent(循环、两级压缩、
编排、审批门控)+ 底座 jsonl/textx/execx/diag/scratch/spill/imagecache。

产品装配代码(config 桥接、schedule 工具、memory/plan/retro/prompt)
留在 AutoGo,宿主接入方式见 MIGRATION.md。
2026-10-26 00:54:46 +08:00

67 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package diag
import (
"bytes"
"strings"
"testing"
)
// TestIndirectResolvesAtWriteTime:**关键性质**——Indirect 在写入时才去向,
// 所以在它被交给子进程之后、再 SetWriter 重定向,后续写入仍会走新去向。
//
// 这正是 MCP 子进程 stderr 的场景:子进程在 buildRuntime 里启动(那时 TUI 还没建
// 日志文件),日志文件的绑定必须晚一步生效,否则就钉死在 os.Stderr 上、打穿 alt-screen。
func TestIndirectResolvesAtWriteTime(t *testing.T) {
before := Writer()
defer SetWriter(before) // 别污染其他测试
var early, late bytes.Buffer
if _, err := Indirect().Write([]byte("early\n")); err != nil {
t.Fatal(err)
}
// 默认去向是 os.Stderr,这里只验证"能写不报错";重点在下面。
SetWriter(&early)
w := Indirect()
if _, err := w.Write([]byte("a\n")); err != nil {
t.Fatal(err)
}
// 重定向之后:同一个 w 应当写到新去向
SetWriter(&late)
if _, err := w.Write([]byte("b\n")); err != nil {
t.Fatal(err)
}
if !strings.Contains(early.String(), "a") {
t.Errorf("重定向前应写进第一个 sink,实际 %q", early.String())
}
if !strings.Contains(late.String(), "b") {
t.Errorf("重定向后同一个 writer 应写进新 sink,实际 %q", late.String())
}
}
// TestSetWriterNilDiscards:传 nil 表示丢弃(TUI 想彻底静音时用)。
func TestSetWriterNilDiscards(t *testing.T) {
before := Writer()
defer SetWriter(before)
SetWriter(nil)
if Writer() == nil {
t.Fatal("nil 应被换成非 nil 的丢弃 writer")
}
Printf("这行不该 panic,也不该出现在任何地方\n")
}
// TestPrintfGoesToWriter:Printf 走当前去向。
func TestPrintfGoesToWriter(t *testing.T) {
before := Writer()
defer SetWriter(before)
var buf bytes.Buffer
SetWriter(&buf)
Printf("hello %s\n", "autogo")
if !strings.Contains(buf.String(), "hello autogo") {
t.Errorf("Printf 没写到当前去向:%q", buf.String())
}
}