Files
Reims-Go/internal/app/extras_snapshot_test.go
T

68 lines
2.4 KiB
Go

package app
import (
"os"
"path/filepath"
"testing"
"github.com/Nwflower/reims-go/internal/driver/reims"
)
func TestParseDiskSnapshotsJSON(t *testing.T) {
raw := []byte(`{
"format": "qcow2",
"snapshots": [
{"id": "1", "name": "before-ota", "vm-state-size": 0, "date-sec": 1764400000},
{"id": "2", "name": "clean-xcode", "vm-state-size": 0, "date-sec": 1764403600}
]
}`)
fmtName, snaps, err := reims.ParseDiskSnapshotsJSON(raw)
if err != nil {
t.Fatal(err)
}
if fmtName != "qcow2" || len(snaps) != 2 || snaps[0].Name != "before-ota" || snaps[1].CreatedAt != 1764403600 {
t.Fatalf("unexpected snapshots: format=%q snaps=%+v", fmtName, snaps)
}
}
func TestValidateSnapshotName(t *testing.T) {
if got, err := reims.ValidateSnapshotName(" before-ota-26.2 "); err != nil || got != "before-ota-26.2" {
t.Fatalf("valid name failed: %q %v", got, err)
}
if _, err := reims.ValidateSnapshotName(" "); err == nil {
t.Fatal("expected error for empty snapshot name")
}
if _, err := reims.ValidateSnapshotName(`bad"quote`); err == nil {
t.Fatal("expected error for quote in snapshot name")
}
}
func TestSanitizeVMNoteAndStorageBreakdown(t *testing.T) {
if got := sanitizeVMNote(" Xcode 26\nBuilder "); got != "Xcode 26 Builder" {
t.Fatalf("sanitizeVMNote = %q", got)
}
root := t.TempDir()
vms := filepath.Join(root, "vms")
inst := filepath.Join(root, "installers")
rtDir := filepath.Join(root, "runtimes")
_ = os.MkdirAll(filepath.Join(vms, "mac-a"), 0o755)
_ = os.MkdirAll(filepath.Join(vms, "mac-b"), 0o755)
_ = os.MkdirAll(inst, 0o755)
_ = os.MkdirAll(rtDir, 0o755)
_ = os.MkdirAll(filepath.Join(root, "cache"), 0o755)
_ = os.WriteFile(filepath.Join(vms, "mac-a", "disk.qcow2"), make([]byte, 200), 0o644)
_ = os.WriteFile(filepath.Join(vms, "mac-b", "disk.qcow2"), make([]byte, 500), 0o644)
_ = os.WriteFile(filepath.Join(inst, "tahoe.img"), make([]byte, 1000), 0o644)
_ = os.WriteFile(filepath.Join(rtDir, "qemu.exe"), make([]byte, 300), 0o644)
_ = os.WriteFile(filepath.Join(root, "cache", "download.tmp"), make([]byte, 75), 0o644)
_ = os.WriteFile(filepath.Join(root, "reims-go.json"), make([]byte, 25), 0o644)
bd := computeStorageBreakdown(root, vms, inst, "")
if bd.VMsBytes != 700 || bd.InstallersBytes != 1000 || bd.RuntimesBytes != 400 || bd.TotalBytes != 2100 {
t.Fatalf("breakdown = %+v", bd)
}
if len(bd.VMs) != 2 || bd.VMs[0].Name != "mac-b" || bd.VMs[0].Bytes != 500 {
t.Fatalf("VMs order = %+v", bd.VMs)
}
}