Repository navigation
Expand file tree
/
Copy pathcompletion_test.go
More file actions
167 lines (163 loc) · 5.26 KB
/
Copy pathcompletion_test.go
File metadata and controls
167 lines (163 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"context"
"fmt"
"io"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"unicode/utf16"
)
func TestCompletionSourceUTF16(t *testing.T) {
n := Notebook{Version: 1, Imports: []string{"fmt"}, Cells: []Cell{{Code: `name := "علی😀"`}, {Code: "fmt.Pr"}}}
source, line, char, err := completionSource(n, 2, 6)
if err != nil || !strings.Contains(source, "name :=") || !strings.Contains(source, "fmt.Pr") || line < 4 || char != 6 {
t.Fatalf("%d %d %v %s", line, char, err, source)
}
if _, _, _, err = completionSource(n, 2, 99); err == nil {
t.Fatal("invalid offset")
}
}
func TestLocalCompletion(t *testing.T) {
n := Notebook{Version: 1, Imports: []string{"fmt"}, Cells: []Cell{{Code: `count := 1`}, {Code: "fmt.Pr"}}}
items := localCompletions(n, 2, 6)
found := false
for _, item := range items {
if item.Label == "Println" && strings.Contains(item.Detail, "func(") {
found = true
}
}
if !found {
t.Fatalf("missing fmt function completion: %+v", items)
}
n.Cells[1].Code = "cou"
items = localCompletions(n, 2, 3)
if len(items) != 1 || items[0].Label != "count" {
t.Fatalf("missing notebook symbol: %+v", items)
}
}
func TestGoplsIntegrationCompletion(t *testing.T) {
path := os.Getenv("GOBOOK_GOPLS")
if path == "" {
t.Skip("set GOBOOK_GOPLS to test semantic completion")
}
c := &CompletionManager{Path: path}
t.Cleanup(c.Close)
n := Notebook{Version: 1, Imports: []string{"fmt"}, Cells: []Cell{{Code: "name := \"Ada\""}, {Code: "fmt.Pr"}}}
items, err := c.Complete(context.Background(), n, 2, 6)
if err != nil {
t.Fatal(err)
}
canonical, e := filepath.EvalSymlinks(c.dir)
if e != nil || c.dir != canonical {
t.Fatalf("non-canonical language-server workspace: %q -> %q (%v)", c.dir, canonical, e)
}
found := false
for _, item := range items {
if item.Label == "Println" {
found = true
}
}
if !found {
t.Fatalf("missing semantic completion: %+v", items)
}
n.Cells[1].Code = "fmt.Println"
items, err = c.Complete(context.Background(), n, 2, 6)
if err != nil {
t.Fatal(err)
}
found = false
for _, item := range items {
if item.Label == "Println" && item.Start == 4 && item.End == 11 {
found = true
}
}
if !found {
t.Fatalf("mid-word replacement range missing: %+v", items)
}
n.Cells[1].Code = "na"
items, err = c.Complete(context.Background(), n, 2, 2)
if err != nil {
t.Fatal(err)
}
found = false
for _, item := range items {
if item.Label == "name" && strings.Contains(item.Detail, "string") {
found = true
}
}
if !found {
t.Fatalf("previous cell type completion missing: %+v", items)
}
}
func TestCompletionHTTPValidationAndOfflineFallback(t *testing.T) {
s := &Server{Host: "127.0.0.1:8080", Token: "test"}
request := func(body, auth string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest("POST", "http://"+s.Host+"/api/complete", strings.NewReader(body))
r.Header.Set("Authorization", auth)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
return w
}
body := `{"notebook":{"version":1,"imports":["fmt"],"cells":[{"code":"fmt.Pr"}]},"cell":1,"offset":6}`
if w := request(body, ""); w.Code != 401 {
t.Fatalf("unauthorized %d", w.Code)
}
w := request(body, "Bearer test")
if w.Code != 200 || !strings.Contains(w.Body.String(), "Println") || !strings.Contains(w.Body.String(), `"provider":"local"`) {
t.Fatal(w.Body.String())
}
if w := request(strings.Replace(body, `"offset":6`, `"offset":99`, 1), "Bearer test"); w.Code != 400 {
t.Fatal("invalid cursor accepted")
}
}
func TestLSPBoundedFraming(t *testing.T) {
message := `{"id":1,"result":{"ok":true}}`
framed := fmt.Sprintf("Content-Length: %d\r\n\r\n%s", len(message), message)
reply, err := readLSP(bufio.NewReader(strings.NewReader(framed)))
if err != nil || reply.ID != 1 {
t.Fatal(reply, err)
}
for _, input := range []string{"Content-Length: 999999999\r\n\r\n", "Content-Length: -1\r\n\r\n", "Content-Length: 10\r\n\r\n{}"} {
if _, err = readLSP(bufio.NewReader(strings.NewReader(input))); err == nil {
t.Fatal("bad frame accepted")
}
}
}
func TestCompletionCancellationUnblocksPendingWrite(t *testing.T) {
reader, writer := io.Pipe()
defer reader.Close()
p := &completionProcess{cmd: &exec.Cmd{Cancel: func() error { return nil }}, in: writer}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
stop := p.bindContext(ctx)
defer stop()
done := make(chan error, 1)
go func() { done <- p.send(map[string]any{"text": strings.Repeat("x", 200000)}) }()
select {
case err := <-done:
if err == nil {
t.Fatal("blocked write unexpectedly succeeded")
}
case <-time.After(time.Second):
t.Fatal("cancellation did not unblock language-server write")
}
}
func TestCompletionRangeMapping(t *testing.T) {
code := "// علی😀\nfmt.Println"
prefix := "// علی😀\nfmt.Pr"
offset := len(utf16.Encode([]rune(prefix)))
start, end, ok := mapCompletionRange(code, completionRange{Start: completionPosition{11, 4}, End: completionPosition{11, 11}}, 10, offset)
if !ok || end-start != 7 || start != offset-2 {
t.Fatalf("range %d %d %t", start, end, ok)
}
if _, _, ok = mapCompletionRange(code, completionRange{Start: completionPosition{0, 0}, End: completionPosition{0, 1}}, 10, offset); ok {
t.Fatal("edit outside cell accepted")
}
}