|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/url" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// TestLSPNavigationE2E spawns the shared mdsmith binary, drives a |
| 18 | +// full symbol-navigation round-trip |
| 19 | +// (initialize → didOpen → documentSymbol → definition → references |
| 20 | +// → prepareCallHierarchy → incomingCalls → shutdown → exit) over |
| 21 | +// stdio, and asserts the headline plan-131 acceptance criteria |
| 22 | +// against a real workspace. |
| 23 | +func TestLSPNavigationE2E(t *testing.T) { |
| 24 | + if testing.Short() { |
| 25 | + t.Skip("skipping LSP navigation subprocess test in -short mode") |
| 26 | + } |
| 27 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 28 | + defer cancel() |
| 29 | + |
| 30 | + tmp, srcA, srcB := writeNavigationCorpus(t) |
| 31 | + pipe := startLSPSubprocess(t, ctx, binaryPath) |
| 32 | + rootURI := pathToFileURIE2E(t, tmp) |
| 33 | + uriA, uriB := rootURI+"/a.md", rootURI+"/b.md" |
| 34 | + |
| 35 | + initE2ENavigation(t, pipe, rootURI) |
| 36 | + pipe.openDocument(uriA, srcA) |
| 37 | + _ = pipe.awaitDiagnostics(t, uriA, time.Now().Add(15*time.Second)) |
| 38 | + pipe.openDocument(uriB, srcB) |
| 39 | + _ = pipe.awaitDiagnostics(t, uriB, time.Now().Add(15*time.Second)) |
| 40 | + |
| 41 | + assertDocumentSymbolOutline(t, pipe, uriA) |
| 42 | + assertDefinitionJumpsToHeading(t, pipe, uriA, uriB) |
| 43 | + assertReferencesFromB(t, pipe, uriA, uriB) |
| 44 | + assertCallHierarchyIncoming(t, pipe, uriA) |
| 45 | + |
| 46 | + pipe.shutdown(t) |
| 47 | +} |
| 48 | + |
| 49 | +// writeNavigationCorpus writes a two-file workspace where a.md is |
| 50 | +// the navigation target and b.md links into it. |
| 51 | +func writeNavigationCorpus(t *testing.T) (root, srcA, srcB string) { |
| 52 | + t.Helper() |
| 53 | + root = t.TempDir() |
| 54 | + srcA = "# Alpha\n\n## Inner\n\nbody\n" |
| 55 | + srcB = "# Beta\n\n[link](./a.md#inner)\n" |
| 56 | + require.NoError(t, os.WriteFile(filepath.Join(root, "a.md"), []byte(srcA), 0o644)) |
| 57 | + require.NoError(t, os.WriteFile(filepath.Join(root, "b.md"), []byte(srcB), 0o644)) |
| 58 | + return root, srcA, srcB |
| 59 | +} |
| 60 | + |
| 61 | +func initE2ENavigation(t *testing.T, pipe *lspPipe, rootURI string) { |
| 62 | + t.Helper() |
| 63 | + resp := pipe.request("initialize", 1, map[string]any{ |
| 64 | + "rootUri": rootURI, |
| 65 | + "capabilities": fullClientCapabilities(), |
| 66 | + }) |
| 67 | + require.Equal(t, float64(1), resp["id"]) |
| 68 | + res, ok := resp["result"].(map[string]any) |
| 69 | + require.True(t, ok) |
| 70 | + caps, ok := res["capabilities"].(map[string]any) |
| 71 | + require.True(t, ok) |
| 72 | + for _, want := range []string{ |
| 73 | + "documentSymbolProvider", |
| 74 | + "definitionProvider", |
| 75 | + "referencesProvider", |
| 76 | + "callHierarchyProvider", |
| 77 | + } { |
| 78 | + assert.Contains(t, caps, want) |
| 79 | + } |
| 80 | + pipe.notify("initialized", map[string]any{}) |
| 81 | +} |
| 82 | + |
| 83 | +func assertDocumentSymbolOutline(t *testing.T, pipe *lspPipe, uriA string) { |
| 84 | + t.Helper() |
| 85 | + syms := pipe.requestPickResult(t, "textDocument/documentSymbol", 100, map[string]any{ |
| 86 | + "textDocument": map[string]any{"uri": uriA}, |
| 87 | + }).([]any) |
| 88 | + require.NotEmpty(t, syms) |
| 89 | + root := syms[0].(map[string]any) |
| 90 | + assert.Equal(t, "Alpha", root["name"]) |
| 91 | + require.Contains(t, root, "children") |
| 92 | +} |
| 93 | + |
| 94 | +func assertDefinitionJumpsToHeading(t *testing.T, pipe *lspPipe, uriA, uriB string) { |
| 95 | + t.Helper() |
| 96 | + defLoc := pipe.requestPickResult(t, "textDocument/definition", 101, map[string]any{ |
| 97 | + "textDocument": map[string]any{"uri": uriB}, |
| 98 | + "position": map[string]any{"line": 2, "character": 12}, |
| 99 | + }) |
| 100 | + defObj, ok := defLoc.(map[string]any) |
| 101 | + require.True(t, ok, "definition result: %v", defLoc) |
| 102 | + assert.Equal(t, uriA, defObj["uri"]) |
| 103 | +} |
| 104 | + |
| 105 | +func assertReferencesFromB(t *testing.T, pipe *lspPipe, uriA, uriB string) { |
| 106 | + t.Helper() |
| 107 | + refsRaw := pipe.requestPickResult(t, "textDocument/references", 102, map[string]any{ |
| 108 | + "textDocument": map[string]any{"uri": uriA}, |
| 109 | + "position": map[string]any{"line": 2, "character": 3}, |
| 110 | + "context": map[string]any{"includeDeclaration": false}, |
| 111 | + }) |
| 112 | + refs, ok := refsRaw.([]any) |
| 113 | + require.True(t, ok, "references result: %v", refsRaw) |
| 114 | + require.Len(t, refs, 1) |
| 115 | + first := refs[0].(map[string]any) |
| 116 | + assert.Equal(t, uriB, first["uri"]) |
| 117 | +} |
| 118 | + |
| 119 | +func assertCallHierarchyIncoming(t *testing.T, pipe *lspPipe, uriA string) { |
| 120 | + t.Helper() |
| 121 | + preparedRaw := pipe.requestPickResult(t, "textDocument/prepareCallHierarchy", 103, map[string]any{ |
| 122 | + "textDocument": map[string]any{"uri": uriA}, |
| 123 | + "position": map[string]any{"line": 0, "character": 0}, |
| 124 | + }) |
| 125 | + prepared, ok := preparedRaw.([]any) |
| 126 | + require.True(t, ok) |
| 127 | + require.Len(t, prepared, 1) |
| 128 | + |
| 129 | + incomingRaw := pipe.requestPickResult(t, "callHierarchy/incomingCalls", 104, map[string]any{ |
| 130 | + "item": prepared[0], |
| 131 | + }) |
| 132 | + incoming, ok := incomingRaw.([]any) |
| 133 | + require.True(t, ok) |
| 134 | + require.Len(t, incoming, 1, "expected one incoming call from b.md") |
| 135 | +} |
| 136 | + |
| 137 | +// requestPickResult issues a request and returns the value at |
| 138 | +// `result`. It also handles server-initiated requests interleaved |
| 139 | +// with the response, so workspace/configuration replies don't stall |
| 140 | +// the dispatch loop on the other side. |
| 141 | +func (p *lspPipe) requestPickResult(t *testing.T, method string, id int, params any) any { |
| 142 | + t.Helper() |
| 143 | + p.writeFrame(map[string]any{ |
| 144 | + "jsonrpc": "2.0", "id": id, "method": method, "params": params, |
| 145 | + }) |
| 146 | + deadline := time.Now().Add(15 * time.Second) |
| 147 | + for time.Now().Before(deadline) { |
| 148 | + m := p.readFrame() |
| 149 | + if mid, ok := m["id"].(float64); ok && int(mid) == id { |
| 150 | + return m["result"] |
| 151 | + } |
| 152 | + // Server-initiated request? Auto-ack. |
| 153 | + if method, _ := m["method"].(string); method != "" { |
| 154 | + if mid, ok := m["id"]; ok && mid != nil { |
| 155 | + p.writeFrame(map[string]any{ |
| 156 | + "jsonrpc": "2.0", "id": mid, "result": nil, |
| 157 | + }) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + t.Fatalf("timed out waiting for response to %s", method) |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// pathToFileURIE2E mirrors the production server's pathToURI: it |
| 166 | +// emits RFC 8089-compliant file URIs so the helper produces the |
| 167 | +// same shape on every host OS (including Windows drive letters and |
| 168 | +// UNC paths). Without that the E2E test would send a non-standard |
| 169 | +// `file://C:/...` rootUri on Windows that the server's URI parser |
| 170 | +// would treat as a UNC host. |
| 171 | +func pathToFileURIE2E(t *testing.T, p string) string { |
| 172 | + t.Helper() |
| 173 | + abs, err := filepath.Abs(p) |
| 174 | + require.NoError(t, err) |
| 175 | + if isWindowsDrivePathE2E(abs) { |
| 176 | + u := url.URL{Scheme: "file", Path: "/" + filepath.ToSlash(abs)} |
| 177 | + return u.String() |
| 178 | + } |
| 179 | + if strings.HasPrefix(abs, `\\`) { |
| 180 | + rest := strings.TrimPrefix(filepath.ToSlash(abs), "//") |
| 181 | + host, tail, _ := strings.Cut(rest, "/") |
| 182 | + u := url.URL{Scheme: "file", Host: host, Path: "/" + tail} |
| 183 | + return u.String() |
| 184 | + } |
| 185 | + u := url.URL{Scheme: "file", Path: filepath.ToSlash(abs)} |
| 186 | + return u.String() |
| 187 | +} |
| 188 | + |
| 189 | +func isWindowsDrivePathE2E(p string) bool { |
| 190 | + if len(p) < 2 || p[1] != ':' { |
| 191 | + return false |
| 192 | + } |
| 193 | + c := p[0] |
| 194 | + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
| 195 | +} |
| 196 | + |
| 197 | +// silence unused import in some build paths |
| 198 | +var _ = json.Marshal |
0 commit comments