89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package scanner
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestScanner_FindMarkdownFiles(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
files := map[string]string{
|
|
"note1.md": "# Note 1",
|
|
"note2.markdown": "# Note 2",
|
|
"folder/note3.md": "# Note 3",
|
|
"folder/nested/note4.md": "# Note 4",
|
|
"readme.txt": "not markdown",
|
|
}
|
|
|
|
for path, content := range files {
|
|
fullPath := filepath.Join(tmpDir, path)
|
|
os.MkdirAll(filepath.Dir(fullPath), 0755)
|
|
os.WriteFile(fullPath, []byte(content), 0644)
|
|
}
|
|
|
|
scanner := NewScanner(tmpDir)
|
|
found, err := scanner.FindAll()
|
|
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
// Should only return markdown files
|
|
if len(found) != 3 {
|
|
t.Errorf("expected 4 files, got %d", len(found))
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|