zlh-agent/internal/handlers/backups_test.go
2026-04-16 18:53:08 +00:00

74 lines
2.2 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
agentbackup "zlh-agent/internal/backup"
"zlh-agent/internal/state"
)
func TestHandleGameBackupRestoreIncludesCheckpoint(t *testing.T) {
oldLoadBackupConfig := loadBackupConfig
oldRestoreBackup := restoreBackup
loadBackupConfig = func() (*state.Config, error) {
return &state.Config{
ContainerType: "game",
Game: "minecraft",
Variant: "vanilla",
}, nil
}
restoreBackup = func(_ *state.Config, id string) (agentbackup.RestoreResult, error) {
return agentbackup.RestoreResult{
Backup: agentbackup.Manifest{
ID: id,
Type: agentbackup.BackupTypeManual,
},
Checkpoint: agentbackup.Manifest{
ID: "checkpoint-1",
Type: agentbackup.BackupTypeCheckpoint,
Reason: agentbackup.BackupReasonPreRestore,
},
}, nil
}
t.Cleanup(func() {
loadBackupConfig = oldLoadBackupConfig
restoreBackup = oldRestoreBackup
})
req := httptest.NewRequest(http.MethodPost, "/game/backups/restore", strings.NewReader(`{"id":"target-1"}`))
rec := httptest.NewRecorder()
HandleGameBackupRestore(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}
var response struct {
Restored bool `json:"restored"`
Backup agentbackup.Manifest `json:"backup"`
Checkpoint agentbackup.Manifest `json:"checkpoint"`
}
if err := json.NewDecoder(rec.Body).Decode(&response); err != nil {
t.Fatalf("decode response: %v", err)
}
if !response.Restored {
t.Fatalf("restored = false, want true")
}
if response.Backup.ID != "target-1" {
t.Fatalf("backup id = %q, want target-1", response.Backup.ID)
}
if response.Checkpoint.ID != "checkpoint-1" {
t.Fatalf("checkpoint id = %q, want checkpoint-1", response.Checkpoint.ID)
}
if response.Checkpoint.Type != agentbackup.BackupTypeCheckpoint {
t.Fatalf("checkpoint type = %q, want %q", response.Checkpoint.Type, agentbackup.BackupTypeCheckpoint)
}
if response.Checkpoint.Reason != agentbackup.BackupReasonPreRestore {
t.Fatalf("checkpoint reason = %q, want %q", response.Checkpoint.Reason, agentbackup.BackupReasonPreRestore)
}
}