Phase3
This commit is contained in:
@@ -212,3 +212,168 @@ func TestNewEnvelope(t *testing.T) {
|
||||
t.Fatalf("protocol version %d", env.ProtocolVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPhase3Payloads_JSONRoundtrip(t *testing.T) {
|
||||
meta := OperationMeta{
|
||||
ServerID: "srv-01",
|
||||
Generation: 3,
|
||||
}
|
||||
|
||||
command := ServerCommandPayload{
|
||||
OperationMeta: meta,
|
||||
Command: "say hello",
|
||||
}
|
||||
commandRaw, err := json.Marshal(command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decodedCommand ServerCommandPayload
|
||||
if err := json.Unmarshal(commandRaw, &decodedCommand); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decodedCommand.Command != command.Command {
|
||||
t.Fatalf("command: got %q want %q", decodedCommand.Command, command.Command)
|
||||
}
|
||||
|
||||
logsSub := ServerLogsSubscribePayload{
|
||||
OperationMeta: meta,
|
||||
TailLines: 100,
|
||||
Follow: true,
|
||||
}
|
||||
logsSubRaw, err := json.Marshal(logsSub)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decodedLogsSub ServerLogsSubscribePayload
|
||||
if err := json.Unmarshal(logsSubRaw, &decodedLogsSub); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decodedLogsSub.TailLines != 100 || !decodedLogsSub.Follow {
|
||||
t.Fatalf("logs subscribe mismatch: %+v", decodedLogsSub)
|
||||
}
|
||||
|
||||
filesList := ServerFilesListPayload{
|
||||
OperationMeta: meta,
|
||||
Path: "world",
|
||||
}
|
||||
filesListRaw, err := json.Marshal(filesList)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := &Envelope{
|
||||
ProtocolVersion: CurrentProtocolVersion,
|
||||
MessageID: "msg-files-list",
|
||||
Type: TypeServerFilesList,
|
||||
Timestamp: time.Now().UTC(),
|
||||
Payload: filesListRaw,
|
||||
}
|
||||
if err := env.Validate(); err != nil {
|
||||
t.Fatalf("files list envelope: %v", err)
|
||||
}
|
||||
|
||||
filesRead := ServerFilesReadPayload{
|
||||
OperationMeta: meta,
|
||||
Path: "server.properties",
|
||||
MaxBytes: 4096,
|
||||
}
|
||||
filesReadRaw, err := json.Marshal(filesRead)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env = &Envelope{
|
||||
ProtocolVersion: CurrentProtocolVersion,
|
||||
MessageID: "msg-files-read",
|
||||
Type: TypeServerFilesRead,
|
||||
Timestamp: time.Now().UTC(),
|
||||
Payload: filesReadRaw,
|
||||
}
|
||||
if err := env.Validate(); err != nil {
|
||||
t.Fatalf("files read envelope: %v", err)
|
||||
}
|
||||
|
||||
filesWrite := ServerFilesWritePayload{
|
||||
OperationMeta: meta,
|
||||
Path: "ops.txt",
|
||||
Content: "test",
|
||||
Create: true,
|
||||
}
|
||||
filesWriteRaw, err := json.Marshal(filesWrite)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env = &Envelope{
|
||||
ProtocolVersion: CurrentProtocolVersion,
|
||||
MessageID: "msg-files-write",
|
||||
Type: TypeServerFilesWrite,
|
||||
Timestamp: time.Now().UTC(),
|
||||
Payload: filesWriteRaw,
|
||||
}
|
||||
if err := env.Validate(); err != nil {
|
||||
t.Fatalf("files write envelope: %v", err)
|
||||
}
|
||||
|
||||
logLine := ServerLogPayload{
|
||||
OperationMeta: meta,
|
||||
Line: "[Server] Done",
|
||||
Stream: "stdout",
|
||||
}
|
||||
logLineRaw, err := json.Marshal(logLine)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env = &Envelope{
|
||||
ProtocolVersion: CurrentProtocolVersion,
|
||||
MessageID: "msg-log",
|
||||
Type: TypeServerLog,
|
||||
Timestamp: time.Now().UTC(),
|
||||
Payload: logLineRaw,
|
||||
}
|
||||
if err := env.Validate(); err != nil {
|
||||
t.Fatalf("server log envelope: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerOperationResultPayload_WithResult(t *testing.T) {
|
||||
listResult, err := json.Marshal(ServerFilesListResultPayload{
|
||||
Entries: []FileEntry{{
|
||||
Name: "server.properties",
|
||||
Path: "server.properties",
|
||||
IsDir: false,
|
||||
Size: 128,
|
||||
ModifiedAt: "2026-06-26T12:00:00Z",
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
payload := ServerOperationResultPayload{
|
||||
OperationMeta: OperationMeta{ServerID: "srv-01", Generation: 1},
|
||||
ResultCode: "ok",
|
||||
Operation: "files.list",
|
||||
Result: listResult,
|
||||
}
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var decoded ServerOperationResultPayload
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Operation != "files.list" {
|
||||
t.Fatalf("operation: got %q", decoded.Operation)
|
||||
}
|
||||
if len(decoded.Result) == 0 {
|
||||
t.Fatal("expected result payload")
|
||||
}
|
||||
|
||||
var result ServerFilesListResultPayload
|
||||
if err := json.Unmarshal(decoded.Result, &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Entries) != 1 || result.Entries[0].Name != "server.properties" {
|
||||
t.Fatalf("unexpected list result: %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user