返回 DeepSeek-Reasonix
state_test.go
根目录 / internal / remote / bootstrap / state_test.go
1 package bootstrap
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 func TestServeStateRoundTrip(t *testing.T) {
9 st := ServeState{
10 PID: 1234,
11 Addr: "127.0.0.1:38121",
12 Workspace: "/home/dev/app",
13 Version: "1.9.0",
14 ServeCaps: ServeCapsToken,
15 TokenFile: "/home/dev/.reasonix/remote/serve-x.token",
16 LogFile: "/home/dev/.reasonix/remote/serve-x.log",
17 StartedAt: 1_700_000_000,
18 }
19 data, err := MarshalState(st)
20 if err != nil {
21 t.Fatal(err)
22 }
23 got, err := UnmarshalState(data)
24 if err != nil {
25 t.Fatal(err)
26 }
27 if got != st {
28 t.Fatalf("round trip mismatch:\n got %+v\nwant %+v", got, st)
29 }
30 }
31
32 // TestServeStateBackwardCompat: an older record missing the newer optional
33 // fields must decode without error, leaving zero values.
34 func TestServeStateBackwardCompat(t *testing.T) {
35 old := `{"pid": 42, "addr": "127.0.0.1:9000", "workspace": "/app", "token_file": "/t"}`
36 st, err := UnmarshalState([]byte(old))
37 if err != nil {
38 t.Fatalf("old record failed to decode: %v", err)
39 }
40 if st.PID != 42 || st.Addr != "127.0.0.1:9000" || st.Version != "" || st.ServeCaps != "" || st.StartedAt != 0 {
41 t.Fatalf("backward-compat decode wrong: %+v", st)
42 }
43 }
44
45 func TestPathsFor(t *testing.T) {
46 paths := pathsFor("/home/dev", "/home/dev/projects/app")
47 if paths.Dir != "/home/dev/.reasonix/remote" {
48 t.Fatalf("Dir = %q", paths.Dir)
49 }
50 if !strings.HasPrefix(paths.StateJSON, paths.Dir+"/serve-") || !strings.HasSuffix(paths.StateJSON, ".json") {
51 t.Fatalf("StateJSON = %q", paths.StateJSON)
52 }
53 if !strings.HasSuffix(paths.TokenFile, ".token") || !strings.HasSuffix(paths.PortFile, ".port") {
54 t.Fatalf("token/port names wrong: %+v", paths)
55 }
56 }
57
57 lines GO