ananke/internal/state/heal_test.go

47 lines
1.8 KiB
Go

package state
import (
"os"
"path/filepath"
"testing"
)
// TestQuarantineCorruptFileWritesBackupAndReplacement runs one orchestration or CLI step.
// Signature: TestQuarantineCorruptFileWritesBackupAndReplacement(t *testing.T).
// Why: covers successful corruption quarantine flow.
func TestQuarantineCorruptFileWritesBackupAndReplacement(t *testing.T) {
path := filepath.Join(t.TempDir(), "intent.json")
if err := quarantineCorruptFile(path, []byte("{bad"), []byte("{}\n"), 0o640); err != nil {
t.Fatalf("quarantine failed: %v", err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read replacement: %v", err)
}
if string(b) != "{}\n" {
t.Fatalf("unexpected replacement payload: %q", string(b))
}
}
// TestQuarantineCorruptFileFailsOnEmptyPath runs one orchestration or CLI step.
// Signature: TestQuarantineCorruptFileFailsOnEmptyPath(t *testing.T).
// Why: covers mkdir failure branch for invalid destination path.
func TestQuarantineCorruptFileFailsOnEmptyPath(t *testing.T) {
if err := quarantineCorruptFile("", []byte("x"), []byte("y"), 0o640); err == nil {
t.Fatalf("expected failure for empty path")
}
}
// TestQuarantineCorruptFileFailsWhenReplacementIsDirectory runs one orchestration or CLI step.
// Signature: TestQuarantineCorruptFileFailsWhenReplacementIsDirectory(t *testing.T).
// Why: covers replacement-write error branch after backup succeeds.
func TestQuarantineCorruptFileFailsWhenReplacementIsDirectory(t *testing.T) {
path := filepath.Join(t.TempDir(), "intent-dir")
if err := os.MkdirAll(path, 0o755); err != nil {
t.Fatalf("mkdir replacement dir: %v", err)
}
if err := quarantineCorruptFile(path, []byte("{bad"), []byte("{}\n"), 0o640); err == nil {
t.Fatalf("expected write replacement failure when path is a directory")
}
}