From b8a4745d95ba5f8140c2a9fda7bda5056d6f29db Mon Sep 17 00:00:00 2001 From: Yule Date: Fri, 11 Sep 2026 18:51:22 +0800 Subject: [PATCH] =?UTF-8?q?test(api):=20=E7=8A=B6=E6=80=81=E8=81=9A?= =?UTF-8?q?=E5=90=88=E6=B5=8B=E8=AF=95=E6=94=B9=E7=94=A8=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=9F=BA=E5=87=86=EF=BC=8C=E6=B6=88=E9=99=A4?= =?UTF-8?q?=E6=97=B6=E9=92=9F=E6=BC=82=E7=A7=BB=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E5=81=B6=E5=8F=91=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 运行时长断言依赖「当前时间 - startedAt」,原先 startedAt 取真实时钟而 Now 取固定时刻,CI 上耗时稍长即算出负值(钳为 0)导致失败 --- go/internal/api/api_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/go/internal/api/api_test.go b/go/internal/api/api_test.go index 79f5a8d..793a53f 100644 --- a/go/internal/api/api_test.go +++ b/go/internal/api/api_test.go @@ -211,6 +211,10 @@ func TestReadModuleInfo(t *testing.T) { } } +// fixedNow 是状态聚合测试的基准时间:运行时长等字段依赖「当前时间」, +// 必须与写入 state 的 startedAt 用同一基准,否则测试会随执行时机漂移。 +var fixedNow = time.Date(2026, 9, 10, 12, 0, 0, 0, time.Local) + func TestBuildStatus(t *testing.T) { dir := t.TempDir() paths := DefaultPaths(dir) @@ -247,9 +251,9 @@ func TestBuildStatus(t *testing.T) { } if err := WriteState(paths.StateFile, DaemonState{ PID: os.Getpid(), - StartedAt: time.Now().Add(-90 * time.Second).Unix(), + StartedAt: fixedNow.Add(-90 * time.Second).Unix(), Mode: "event", - Ratio: &RatioInfo{Value: 1.08, At: time.Now().Unix()}, + Ratio: &RatioInfo{Value: 1.08, At: fixedNow.Unix()}, HdrSleep: true, }); err != nil { t.Fatal(err) @@ -265,7 +269,7 @@ func TestBuildStatus(t *testing.T) { status := BuildStatus(paths, cfg, StatusDeps{ AutoBrightness: system.AutoBrightness{Runner: runner}, HDR: system.HdrRatio{Runner: runner}, - Now: func() time.Time { return time.Date(2026, 9, 10, 12, 0, 0, 0, time.Local) }, + Now: func() time.Time { return fixedNow }, }, StatusOptions{IncludeDisplay: true}) if status.Module.Version != "" && status.Module.Channel != "stable" {