1package modes
2
3import (
4 "os"
5 "path/filepath"
6 "sort"
7 "testing"
8 "time"
9)
10
11func TestExpandFileChipsAtPickerShape(t *testing.T) {
12 cwd := filepath.Join(string(filepath.Separator), "repo")
13 in := "read [file:README.md] and [dir:docs/]"
14 want := "read " + filepath.Join(cwd, "README.md") + " and " + filepath.Join(cwd, "docs")
15 if got := expandFileChips(in, cwd); got != want {
16 t.Fatalf("expandFileChips() = %q, want %q", got, want)
17 }
18}
19
20func TestExpandFileChipsLeavesEditorPlaceholderShapeAlone(t *testing.T) {
21 cwd := filepath.Join(string(filepath.Separator), "repo")
22 // tui.Editor.SubmitValue should expand [file:N:name] using its
23 // private path map before modes see the text. If such a token leaks
24 // through, modes must not guess that "1:foo.txt" is a relative path.
25 in := "read [file:1:foo.txt]"
26 if got := expandFileChips(in, cwd); got != in {
27 t.Fatalf("editor placeholder was changed: %q", got)
28 }
29}
30
31// TestFileSuggesterPicksUpNewEntries pins the cache-invalidation bug
32// fix: creating a subdirectory after the picker has already scanned
33// the cwd must surface that subdirectory on the next scan, without
34// any explicit invalidation call. The cache is keyed on the dir's
35// mtime, which the OS bumps on every entry add/remove/rename.
36func TestFileSuggesterPicksUpNewEntries(t *testing.T) {
37 tmp := t.TempDir()
38 if err := os.WriteFile(filepath.Join(tmp, "existing.txt"), []byte("x"), 0o644); err != nil {
39 t.Fatal(err)
40 }
41
42 s := newFileSuggester()
43 s.SetCWD(tmp)
44
45 first := s.scan()
46 if !containsEntry(first, "existing.txt", false) {
47 t.Fatalf("first scan missing existing.txt: %#v", first)
48 }
49 if containsEntry(first, "test", true) {
50 t.Fatalf("first scan unexpectedly saw the not-yet-created test/: %#v", first)
51 }
52
53 // Sleep one filesystem tick: HFS+/APFS/ext4 all bump directory
54 // mtime with at-least 1s resolution depending on mount options.
55 // Without this sleep on coarse-resolution filesystems the mtime
56 // after Mkdir can equal the mtime captured during the first scan
57 // and the cache would (correctly) be retained.
58 time.Sleep(1100 * time.Millisecond)
59 if err := os.Mkdir(filepath.Join(tmp, "test"), 0o755); err != nil {
60 t.Fatal(err)
61 }
62
63 second := s.scan()
64 if !containsEntry(second, "test", true) {
65 t.Fatalf("second scan did not pick up the newly created test/: %#v", second)
66 }
67 if !containsEntry(second, "existing.txt", false) {
68 t.Fatalf("second scan dropped existing.txt: %#v", second)
69 }
70
71 // Directories sort before files.
72 sorted := make([]string, 0, len(second))
73 for _, e := range second {
74 sorted = append(sorted, e.name)
75 }
76 if !sort.IsSorted(byDirsFirst(second)) {
77 t.Fatalf("entries are not dirs-first / case-insensitive sorted: %v", sorted)
78 }
79}
80
81func containsEntry(entries []fileEntry, name string, isDir bool) bool {
82 for _, e := range entries {
83 if e.name == name && e.isDir == isDir {
84 return true
85 }
86 }
87 return false
88}
89
90type byDirsFirst []fileEntry
91
92func (b byDirsFirst) Len() int { return len(b) }
93func (b byDirsFirst) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
94func (b byDirsFirst) Less(i, j int) bool {
95 if b[i].isDir != b[j].isDir {
96 return b[i].isDir
97 }
98 return b[i].name < b[j].name
99}