| 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 | TokenFile: "/home/dev/.reasonix/remote/serve-x.token", |
| 15 | LogFile: "/home/dev/.reasonix/remote/serve-x.log", |
| 16 | StartedAt: 1_700_000_000, |
| 17 | } |
| 18 | data, err := MarshalState(st) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | got, err := UnmarshalState(data) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if got != st { |
| 27 | t.Fatalf("round trip mismatch:\n got %+v\nwant %+v", got, st) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // TestServeStateBackwardCompat: an older record missing the newer optional |
| 32 | // fields must decode without error, leaving zero values. |
| 33 | func TestServeStateBackwardCompat(t *testing.T) { |
| 34 | old := `{"pid": 42, "addr": "127.0.0.1:9000", "workspace": "/app", "token_file": "/t"}` |
| 35 | st, err := UnmarshalState([]byte(old)) |
| 36 | if err != nil { |
| 37 | t.Fatalf("old record failed to decode: %v", err) |
| 38 | } |
| 39 | if st.PID != 42 || st.Addr != "127.0.0.1:9000" || st.Version != "" || st.StartedAt != 0 { |
| 40 | t.Fatalf("backward-compat decode wrong: %+v", st) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestPathsFor(t *testing.T) { |
| 45 | paths := pathsFor("/home/dev", "/home/dev/projects/app") |
| 46 | if paths.Dir != "/home/dev/.reasonix/remote" { |
| 47 | t.Fatalf("Dir = %q", paths.Dir) |
| 48 | } |
| 49 | if !strings.HasPrefix(paths.StateJSON, paths.Dir+"/serve-") || !strings.HasSuffix(paths.StateJSON, ".json") { |
| 50 | t.Fatalf("StateJSON = %q", paths.StateJSON) |
| 51 | } |
| 52 | if !strings.HasSuffix(paths.TokenFile, ".token") || !strings.HasSuffix(paths.PortFile, ".port") { |
| 53 | t.Fatalf("token/port names wrong: %+v", paths) |
| 54 | } |
| 55 | } |
| 56 |