[DEPRECATED] Go implementation of plcbundle
1package server_test
2
3import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "tangled.org/atscan.net/plcbundle-go/server"
9)
10
11func TestServerResponseTypes(t *testing.T) {
12 t.Run("StatusResponse_JSON", func(t *testing.T) {
13 response := server.StatusResponse{
14 Server: server.ServerStatus{
15 Version: "1.0.0",
16 UptimeSeconds: 3600,
17 SyncMode: true,
18 SyncIntervalSeconds: 60,
19 WebSocketEnabled: true,
20 Origin: "https://plc.directory",
21 },
22 Bundles: server.BundleStatus{
23 Count: 100,
24 FirstBundle: 1,
25 LastBundle: 100,
26 TotalSize: 1024000,
27 UncompressedSize: 5120000,
28 CompressionRatio: 5.0,
29 TotalOperations: 1000000,
30 AvgOpsPerHour: 10000,
31 UpdatedAt: time.Now(),
32 HeadAgeSeconds: 30,
33 RootHash: "root_hash",
34 HeadHash: "head_hash",
35 Gaps: 0,
36 HasGaps: false,
37 },
38 }
39
40 // Should marshal to JSON
41 data, err := json.Marshal(response)
42 if err != nil {
43 t.Fatalf("failed to marshal StatusResponse: %v", err)
44 }
45
46 // Should unmarshal back
47 var decoded server.StatusResponse
48 if err := json.Unmarshal(data, &decoded); err != nil {
49 t.Fatalf("failed to unmarshal StatusResponse: %v", err)
50 }
51
52 // Verify round-trip
53 if decoded.Server.Version != "1.0.0" {
54 t.Error("version not preserved")
55 }
56
57 if decoded.Bundles.Count != 100 {
58 t.Error("bundle count not preserved")
59 }
60 })
61
62 t.Run("MempoolStatus_JSON", func(t *testing.T) {
63 status := server.MempoolStatus{
64 Count: 500,
65 TargetBundle: 42,
66 CanCreateBundle: false,
67 MinTimestamp: time.Now(),
68 Validated: true,
69 ProgressPercent: 5.0,
70 BundleSize: 10000,
71 OperationsNeeded: 9500,
72 FirstTime: time.Now().Add(-time.Hour),
73 LastTime: time.Now(),
74 TimespanSeconds: 3600,
75 LastOpAgeSeconds: 10,
76 EtaNextBundleSeconds: 1800,
77 }
78
79 data, err := json.Marshal(status)
80 if err != nil {
81 t.Fatalf("failed to marshal MempoolStatus: %v", err)
82 }
83
84 var decoded server.MempoolStatus
85 if err := json.Unmarshal(data, &decoded); err != nil {
86 t.Fatalf("failed to unmarshal MempoolStatus: %v", err)
87 }
88
89 if decoded.Count != 500 {
90 t.Error("count not preserved")
91 }
92
93 if decoded.ProgressPercent != 5.0 {
94 t.Error("progress not preserved")
95 }
96 })
97
98 t.Run("BundleStatus_WithGaps", func(t *testing.T) {
99 status := server.BundleStatus{
100 Count: 100,
101 Gaps: 3,
102 HasGaps: true,
103 GapNumbers: []int{5, 23, 67},
104 }
105
106 data, err := json.Marshal(status)
107 if err != nil {
108 t.Fatalf("marshal failed: %v", err)
109 }
110
111 var decoded server.BundleStatus
112 json.Unmarshal(data, &decoded)
113
114 if !decoded.HasGaps {
115 t.Error("HasGaps flag not preserved")
116 }
117
118 if len(decoded.GapNumbers) != 3 {
119 t.Error("gap numbers not preserved")
120 }
121 })
122}