Premier commit déjà bien avancé
This commit is contained in:
116
internal/indexer/indexer_test.go
Normal file
116
internal/indexer/indexer_test.go
Normal file
@ -0,0 +1,116 @@
|
||||
package indexer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIndexerLoadAndSearchByTag(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
files := map[string]string{
|
||||
"note1.md": "---\ntags: [tag1, tag2]\n---\ncontenu 1\n",
|
||||
"note2.md": "---\ntags: tag2\n---\ncontenu 2\n",
|
||||
"note3.md": "---\n---\ncontenu 3\n",
|
||||
}
|
||||
|
||||
for name, content := range files {
|
||||
path := filepath.Join(tmp, name)
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("ecriture fichier %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
idx := New()
|
||||
if err := idx.Load(tmp); err != nil {
|
||||
t.Fatalf("chargement index: %v", err)
|
||||
}
|
||||
|
||||
cases := map[string][]string{
|
||||
"tag1": []string{"note1.md"},
|
||||
"TAG2": []string{"note1.md", "note2.md"},
|
||||
"tag3": nil,
|
||||
"": nil,
|
||||
}
|
||||
|
||||
for tag, expected := range cases {
|
||||
results := idx.SearchByTag(tag)
|
||||
if len(results) != len(expected) {
|
||||
t.Errorf("tag %q: taille %d attendue %d", tag, len(results), len(expected))
|
||||
continue
|
||||
}
|
||||
for i, file := range expected {
|
||||
if results[i] != file {
|
||||
t.Errorf("tag %q: resultat[%d]=%s attendu %s", tag, i, results[i], file)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexerSearchDocuments(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
files := map[string]string{
|
||||
"projet-alpha.md": `---
|
||||
title: Projet Alpha
|
||||
tags: [projet, alpha]
|
||||
---
|
||||
Le projet Alpha explore une nouvelle architecture.
|
||||
`,
|
||||
"journal.md": `---
|
||||
title: Journal Quotidien
|
||||
tags: [journal]
|
||||
---
|
||||
Aujourd'hui, nous avons discuté du projet Bêta et des priorités.
|
||||
`,
|
||||
"guide.md": `---
|
||||
title: Guide Pratique
|
||||
tags: [documentation, guide]
|
||||
---
|
||||
Ce guide explique comment démarrer rapidement.
|
||||
`,
|
||||
}
|
||||
|
||||
for name, content := range files {
|
||||
path := filepath.Join(tmp, name)
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("écriture fichier %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
idx := New()
|
||||
if err := idx.Load(tmp); err != nil {
|
||||
t.Fatalf("chargement index: %v", err)
|
||||
}
|
||||
|
||||
type expected struct {
|
||||
query string
|
||||
results []string
|
||||
}
|
||||
|
||||
tests := []expected{
|
||||
{query: "projet", results: []string{"journal.md", "projet-alpha.md"}},
|
||||
{query: "title:\"Projet Alpha\"", results: []string{"projet-alpha.md"}},
|
||||
{query: "tag:guide", results: []string{"guide.md"}},
|
||||
{query: "tag:projet projet", results: []string{"projet-alpha.md"}},
|
||||
{query: "tag:journal beta", results: []string{"journal.md"}},
|
||||
{query: "path:guide démarrer", results: []string{"guide.md"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
results := idx.SearchDocuments(tt.query)
|
||||
paths := make([]string, len(results))
|
||||
for i, res := range results {
|
||||
paths[i] = res.Path
|
||||
}
|
||||
if len(paths) != len(tt.results) {
|
||||
t.Fatalf("query %q: taille %d attendue %d (résultats: %v)", tt.query, len(paths), len(tt.results), paths)
|
||||
}
|
||||
for idxRes, path := range tt.results {
|
||||
if paths[idxRes] != path {
|
||||
t.Fatalf("query %q: résultat[%d]=%s attendu %s", tt.query, idxRes, paths[idxRes], path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user