Files
mesh-host/internal/apply/remove_file_test.go
T

58 lines
1.9 KiB
Go

package apply
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/novox/mesh-host/internal/store"
)
// A file the host wrote whole is deleted only when it holds exactly what the host wrote; anything
// else is moved aside, never destroyed (novox/hq issue 241).
func TestAnUndeclaredFileIsDeletedOnlyWhenItIsWhatTheMeshWrote(t *testing.T) {
dir := t.TempDir()
now := time.Date(2026, 10, 5, 9, 30, 0, 0, time.UTC)
pure := filepath.Join(dir, "config.conf")
if err := os.WriteFile(pure, []byte("what the mesh wrote\n"), 0o644); err != nil {
t.Fatal(err)
}
action, _, err := removeFile(store.Applied{Type: "file", Target: pure, Wrote: digestOf("what the mesh wrote\n")}, now)
if err != nil || action != "removed" {
t.Fatalf("a file holding only the mesh's content was %q (%v)", action, err)
}
if _, err := os.Stat(pure); !os.IsNotExist(err) {
t.Fatal("the mesh's own file is still there")
}
for name, a := range map[string]store.Applied{
"filled since": {Type: "file", Wrote: digestOf("seed\n")},
"no record of it": {Type: "file"},
} {
path := filepath.Join(dir, strings.ReplaceAll(name, " ", "-")+".db")
if err := os.WriteFile(path, []byte("seed\nand a program's data\n"), 0o600); err != nil {
t.Fatal(err)
}
a.Target = path
action, detail, err := removeFile(a, now)
if err != nil || action != "kept" {
t.Fatalf("%s: was %q (%v)", name, action, err)
}
aside := path + ".removed-20261005-093000"
got, err := os.ReadFile(aside)
if err != nil || string(got) != "seed\nand a program's data\n" {
t.Fatalf("%s: not moved aside intact to %s: %v", name, aside, err)
}
if !strings.Contains(detail, aside) {
t.Errorf("%s: the report does not say where it went: %s", name, detail)
}
}
if action, _, _ := removeFile(store.Applied{Type: "file", Target: filepath.Join(dir, "gone")}, now); action != "forgotten" {
t.Errorf("a file already gone was %q", action)
}
}