返回 DeepSeek-Reasonix
startup_test.go
根目录 / internal / repair / startup_test.go
1 package repair
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7 "time"
8 )
9
10 func writeLegacyStartupState(t *testing.T, path, body string) {
11 t.Helper()
12 if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
13 t.Fatal(err)
14 }
15 }
16
17 func TestStartupTrackerObservesDeadLegacyOwner(t *testing.T) {
18 path := filepath.Join(t.TempDir(), "startup.json")
19 started := time.Now().UTC().Add(-time.Minute)
20 updated := started.Add(45 * time.Second)
21 writeLegacyStartupState(t, path, `{
22 "schemaVersion": 1,
23 "phase": "healthy",
24 "version": "v1.19.1",
25 "installProfile": "installer",
26 "updateFromVersion": "v1.19.0",
27 "updateToVersion": "v1.19.1",
28 "pid": 42,
29 "safeMode": true,
30 "startedAt": "`+started.Format(time.RFC3339Nano)+`",
31 "updatedAt": "`+updated.Format(time.RFC3339Nano)+`"
32 }`)
33
34 tracker := NewStartupTracker(path)
35 tracker.processAlive = func(int) bool { return false }
36 got := tracker.ObservePreviousRun()
37 if !got.Abnormal || got.Phase != "healthy" || got.Version != "v1.19.1" || got.InstallProfile != "installer" {
38 t.Fatalf("observation = %+v", got)
39 }
40 if got.UpdateFrom != "v1.19.0" || got.UpdateTo != "v1.19.1" || got.UptimeBucket != "m_0_2" {
41 t.Fatalf("observation metadata = %+v", got)
42 }
43 }
44
45 func TestStartupTrackerIgnoresLiveAndCleanLegacyRecords(t *testing.T) {
46 path := filepath.Join(t.TempDir(), "startup.json")
47 tracker := NewStartupTracker(path)
48 tracker.processAlive = func(pid int) bool { return pid == 42 }
49
50 writeLegacyStartupState(t, path, `{"phase":"ready","pid":42}`)
51 if got := tracker.ObservePreviousRun(); got.Abnormal {
52 t.Fatalf("live owner reported abnormal: %+v", got)
53 }
54 writeLegacyStartupState(t, path, `{"phase":"clean-exit","pid":42}`)
55 if got := tracker.ObservePreviousRun(); got.Abnormal {
56 t.Fatalf("clean exit reported abnormal: %+v", got)
57 }
58 }
59
60 func TestStartupTrackerInvalidOrMissingStateIsIgnored(t *testing.T) {
61 path := filepath.Join(t.TempDir(), "startup.json")
62 tracker := NewStartupTracker(path)
63 if got := tracker.ObservePreviousRun(); got.Abnormal {
64 t.Fatalf("missing state reported abnormal: %+v", got)
65 }
66 writeLegacyStartupState(t, path, `{broken`)
67 if got := tracker.ObservePreviousRun(); got.Abnormal {
68 t.Fatalf("invalid state reported abnormal: %+v", got)
69 }
70 }
71
71 lines GO