Files
donniemarko/internal/scanner/scanner_test.go

59 lines
1.3 KiB
Go

package scanner
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestScanner_DetectsNewFile(t *testing.T) {
tmpDir := t.TempDir()
scanner := NewScanner(tmpDir)
scanner.Scan() // Initial scan
os.WriteFile(filepath.Join(tmpDir, "new.md"), []byte("# New"), 0644)
changes, _ := scanner.Scan()
if len(changes) != 1 || changes[0].Type != Created {
t.Error("should detect new file")
}
}
func TestScanner_DetectChanges(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "test.md")
// Initial state
os.WriteFile(filePath, []byte("# Original"), 0644)
scanner := NewScanner(tmpDir)
changes, _ := scanner.Scan()
originalModTime := changes[0].ModTime
// Wait and modify
time.Sleep(10 * time.Millisecond)
os.WriteFile(filePath, []byte("# Modified"), 0644)
changes, _ = scanner.Scan()
newModTime := changes[0].ModTime
if !newModTime.After(originalModTime) {
t.Error("should detect file modification")
}
if changes[0].Type != Modified {
t.Errorf("Last state should be modified, got '%v'\n", changes[0].Type)
}
newPath := filepath.Join(tmpDir, "test_renamed.md")
os.Rename(filePath, newPath)
changes, _ = scanner.Scan()
if changes[0].Path != newPath {
t.Errorf("Should find renamed file '%s'. Got '%s'\n", newPath, changes[0].Path)
}
}