package statedir
import (
"os"
"path/filepath"
"testing"
)
// mkdirWith makes dir under root with one file in it, so a test can tell a
// moved directory from a freshly created one.
func mkdirWith(t *testing.T, root, dir, name, body string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(root, dir), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, dir, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
func read(t *testing.T, path string) string {
t.Helper()
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return string(b)
}
// The event log is append-only and the sessions are what -resume reads, so a
// clone that predates the rename has its state carried over whole rather than
// started again under the new name.
func TestMigrateMovesTheLegacyDirectory(t *testing.T) {
root := t.TempDir()
mkdirWith(t, root, LegacyDir, "events.jsonl", "{\"event\":\"turn\"}\n")
moved, err := Migrate(root)
if err != nil {
t.Fatalf("Migrate: %v", err)
}
if !moved {
t.Fatal("Migrate did not report the move it made")
}
if got := read(t, filepath.Join(root, Dir, "events.jsonl")); got != "{\"event\":\"turn\"}\n" {
t.Errorf("the event log did not survive the move: %q", got)
}
if _, err := os.Stat(filepath.Join(root, LegacyDir)); !os.IsNotExist(err) {
t.Error("the old directory is still there after a move")
}
}
// Nothing to migrate is the common case and must not leave a directory behind:
// an empty .codey in every repo is litter.
func TestMigrateIsAQuietNoOpWithNothingToMove(t *testing.T) {
root := t.TempDir()
moved, err := Migrate(root)
if err != nil || moved {
t.Fatalf("Migrate on a clean tree: moved=%v err=%v", moved, err)
}
if _, err := os.Stat(filepath.Join(root, Dir)); !os.IsNotExist(err) {
t.Error("Migrate created a state directory with nothing to put in it")
}
}
// Both present means a build of each has run here. The new one is the live
// state, so it is never overwritten by the old one.
func TestMigrateLeavesBothAloneWhenBothExist(t *testing.T) {
root := t.TempDir()
mkdirWith(t, root, LegacyDir, "events.jsonl", "old\n")
mkdirWith(t, root, Dir, "events.jsonl", "new\n")
moved, err := Migrate(root)
if err != nil || moved {
t.Fatalf("Migrate with both present: moved=%v err=%v", moved, err)
}
if got := read(t, filepath.Join(root, Dir, "events.jsonl")); got != "new\n" {
t.Errorf("live state was overwritten by the old directory: %q", got)
}
if got := read(t, filepath.Join(root, LegacyDir, "events.jsonl")); got != "old\n" {
t.Errorf("the old directory was disturbed: %q", got)
}
}
// A repo that happens to have a file of that name is not a state directory,
// and renaming it would be taking something that is not ours.
func TestMigrateIgnoresALegacyFile(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, LegacyDir), []byte("not a directory"), 0o644); err != nil {
t.Fatal(err)
}
moved, err := Migrate(root)
if err != nil || moved {
t.Fatalf("Migrate over a file: moved=%v err=%v", moved, err)
}
if got := read(t, filepath.Join(root, LegacyDir)); got != "not a directory" {
t.Errorf("the file was touched: %q", got)
}
}