154 lines
4.4 KiB
Go
154 lines
4.4 KiB
Go
package datamigrate
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
)
|
|
|
|
func TestClipboardSettingsUpgradeFromV3Fixture(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := writeStamp(root, Stamp{DataVersion: 3, WrittenBy: "0.2.13"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := plugin.NewStateStore(plugin.NewPluginsLayout(root).Root)
|
|
if err := store.Save(&plugin.StateFile{
|
|
Plugins: map[string]*plugin.PluginRecord{
|
|
builtin.IDClipboard: {
|
|
ID: builtin.IDClipboard,
|
|
Enabled: true,
|
|
AppSettings: map[string]any{
|
|
"sync_files": true,
|
|
"max_file_mb": 128,
|
|
},
|
|
VMSettings: map[string]map[string]any{
|
|
"reims-macos:mac-a": {
|
|
"enabled": false,
|
|
"direction": "host-to-guest",
|
|
"sync_images": false,
|
|
"max_image_mb": 4,
|
|
"max_file_mb": 64,
|
|
},
|
|
"reims-macos:mac-b": {
|
|
"enabled": true,
|
|
"direction": "bidirectional",
|
|
"sync_images": true,
|
|
"max_image_mb": 12,
|
|
"sync_files": false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original, err := os.ReadFile(store.Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
res, err := Run(root, "dev")
|
|
if err != nil || res.From != 3 || res.To != CurrentVersion {
|
|
t.Fatalf("Run = %+v, err=%v", res, err)
|
|
}
|
|
backup, err := os.ReadFile(store.Path() + ".bak")
|
|
if err != nil || string(backup) != string(original) {
|
|
t.Fatalf("backup did not preserve pre-upgrade state.json: err=%v", err)
|
|
}
|
|
|
|
st, err := store.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec := st.Plugins[builtin.IDClipboard]
|
|
if rec == nil {
|
|
t.Fatal("clipboard plugin record missing")
|
|
}
|
|
if len(rec.VMSettings) != 0 {
|
|
t.Fatalf("legacy VMSettings were not cleared: %+v", rec.VMSettings)
|
|
}
|
|
if rec.AppSettings["enabled"] != false {
|
|
t.Fatalf("enabled = %v, want false", rec.AppSettings["enabled"])
|
|
}
|
|
if rec.AppSettings["direction"] != "host-to-guest" {
|
|
t.Fatalf("direction = %v, want host-to-guest", rec.AppSettings["direction"])
|
|
}
|
|
if rec.AppSettings["sync_images"] != false {
|
|
t.Fatalf("sync_images = %v, want false", rec.AppSettings["sync_images"])
|
|
}
|
|
if rec.AppSettings["max_image_mb"] != float64(4) {
|
|
t.Fatalf("max_image_mb = %v, want 4", rec.AppSettings["max_image_mb"])
|
|
}
|
|
if rec.AppSettings["sync_files"] != false {
|
|
t.Fatalf("sync_files = %v, want false", rec.AppSettings["sync_files"])
|
|
}
|
|
if rec.AppSettings["max_file_mb"] != float64(64) {
|
|
t.Fatalf("max_file_mb = %v, want 64", rec.AppSettings["max_file_mb"])
|
|
}
|
|
|
|
// Idempotent on second Run.
|
|
before, _ := os.ReadFile(store.Path())
|
|
res2, err := Run(root, "dev")
|
|
if err != nil || len(res2.Applied) != 0 {
|
|
t.Fatalf("second Run = %+v, err=%v", res2, err)
|
|
}
|
|
after, _ := os.ReadFile(store.Path())
|
|
if string(before) != string(after) {
|
|
t.Fatal("second Run modified state.json")
|
|
}
|
|
}
|
|
|
|
func TestClipboardSettingsMigrationDisjointDirectionsDisableSync(t *testing.T) {
|
|
st := &plugin.StateFile{
|
|
Plugins: map[string]*plugin.PluginRecord{
|
|
builtin.IDClipboard: {
|
|
ID: builtin.IDClipboard,
|
|
Enabled: true,
|
|
VMSettings: map[string]map[string]any{
|
|
"reims-macos:mac-a": {"direction": "host-to-guest"},
|
|
"reims-macos:mac-b": {"direction": "guest-to-host"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if !migrateClipboardSettingsState(st) {
|
|
t.Fatal("expected state change")
|
|
}
|
|
rec := st.Plugins[builtin.IDClipboard]
|
|
if rec.AppSettings["enabled"] != false {
|
|
t.Fatalf("disjoint directions should disable sync, got enabled=%v", rec.AppSettings["enabled"])
|
|
}
|
|
if len(rec.VMSettings) != 0 {
|
|
t.Fatalf("VMSettings not cleared: %+v", rec.VMSettings)
|
|
}
|
|
}
|
|
|
|
func TestClipboardSettingsMigrationPreservesDisabledPluginAndNoOp(t *testing.T) {
|
|
st := &plugin.StateFile{
|
|
Plugins: map[string]*plugin.PluginRecord{
|
|
builtin.IDClipboard: {
|
|
ID: builtin.IDClipboard,
|
|
Enabled: false,
|
|
VMSettings: map[string]map[string]any{
|
|
"reims-macos:mac": {"direction": "guest-to-host", "max_image_mb": float64(8)},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if !migrateClipboardSettingsState(st) {
|
|
t.Fatal("expected state change")
|
|
}
|
|
rec := st.Plugins[builtin.IDClipboard]
|
|
if rec.Enabled {
|
|
t.Fatal("disabled plugin was silently enabled")
|
|
}
|
|
if rec.AppSettings["direction"] != "guest-to-host" || rec.AppSettings["max_image_mb"] != 8 {
|
|
t.Fatalf("unexpected migrated settings: %+v", rec.AppSettings)
|
|
}
|
|
if migrateClipboardSettingsState(st) {
|
|
t.Fatal("second migration pass should be a no-op")
|
|
}
|
|
}
|