package main import ( "os" "path/filepath" "testing" ) // The portable-folder contract: a database that lives inside the application // folder must be stored relative to it, so copying C:\OpsLog to D:\OpsLog (or to // a USB stick) keeps working. A path the operator deliberately put elsewhere // must be left exactly as it is. func TestPortablePath(t *testing.T) { base := appDir() if base == "" { t.Skip("no executable dir available") } inside := filepath.Join(base, "data", "logbook.db") if got := portablePath(inside); got != "data/logbook.db" { t.Errorf("inside the app folder: got %q, want %q", got, "data/logbook.db") } // Outside: an absolute path on another drive / a synced folder is a // deliberate choice and must survive untouched. outside := filepath.FromSlash("Z:/Sync/ham/logbook.db") if got := portablePath(outside); got != outside { t.Errorf("outside the app folder: got %q, want it unchanged", got) } if got := portablePath(""); got != "" { t.Errorf("empty path: got %q", got) } } func TestResolvePath(t *testing.T) { base := appDir() if base == "" { t.Skip("no executable dir available") } // A relative path is anchored to the CURRENT install, whatever drive it is on. want := filepath.Join(base, "data", "logbook.db") if got := resolvePath("", "data/logbook.db"); got != want { t.Errorf("relative: got %q, want %q", got, want) } // An absolute path that still exists is honoured as-is. dir := t.TempDir() real := filepath.Join(dir, "logbook.db") if err := os.WriteFile(real, []byte("x"), 0o644); err != nil { t.Fatal(err) } if got := resolvePath(dir, real); got != real { t.Errorf("existing absolute: got %q, want %q", got, real) } // The rescue: a path from ANOTHER machine, with the same file present in this // install's data folder — the copied-folder case. stale := filepath.FromSlash("C:/OldPC/OpsLog/data/logbook.db") if got := resolvePath(dir, stale); got != real { t.Errorf("stale absolute with a local twin: got %q, want %q", got, real) } // No local twin: leave it alone so the failure is REPORTED rather than // silently replaced by an unrelated database. missing := filepath.FromSlash("C:/OldPC/OpsLog/data/other.db") if got := resolvePath(dir, missing); got != missing { t.Errorf("stale absolute with no twin: got %q, want it unchanged", got) } }