-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_test.go
More file actions
263 lines (255 loc) · 9.21 KB
/
Copy pathmodel_test.go
File metadata and controls
263 lines (255 loc) · 9.21 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package codegraph
import (
"context"
"encoding/json"
"errors"
"reflect"
"strings"
"testing"
"github.com/odvcencio/gotreesitter/grammars"
)
// All tiers must survive publication, properties, relation values and paths.
func TestConfidencePublicationAndQuery(t *testing.T) {
ctx := context.Background()
g, _, err := Build(ctx, "tiers", []Document{
{Path: "a.go", Content: []byte("package a;type I interface { Run() };type Box struct{};func(Box) Run(){};func work(){};func entry(b Box){work();b.Run()}")},
{Path: "a.ts", Content: []byte("class Box { run() {} };function entry(x: any) { x.run(); }")},
}, Options{})
if err != nil {
t.Fatal(err)
}
found := map[Confidence]bool{}
originals := map[string]Relation{}
for _, r := range g.Relations() {
var derived Confidence
for _, e := range r.Evidence {
if !e.Confidence.Valid() {
t.Fatal(e)
}
derived = derived.Stronger(e.Confidence)
}
if !r.Confidence.Valid() || r.Confidence != derived {
t.Fatal(r)
}
found[r.Confidence] = true
originals[r.ID] = r
}
if len(found) != 4 {
t.Fatal(found)
}
rows, err := g.Query(ctx, "MATCH ()-[r]->() RETURN r, r.confidence AS confidence, r.evidenceData AS evidence", nil)
if err != nil {
t.Fatal(err)
}
for _, row := range rows {
r := row["r"].(Relation)
var proof []Evidence
if err := json.Unmarshal([]byte(row["evidence"].(string)), &proof); err != nil {
t.Fatal(err)
}
if row["confidence"] != string(r.Confidence) || !reflect.DeepEqual(proof, r.Evidence) || !reflect.DeepEqual(r, originals[r.ID]) {
t.Fatal(row)
}
}
rows, err = g.Query(ctx, "MATCH ()-[r]->() WHERE r.confidence IN ['exact', 'scoped'] RETURN r", nil)
if err != nil {
t.Fatal(err)
}
want := 0
for _, r := range originals {
if r.Confidence.AtLeast(Scoped) {
want++
}
}
if len(rows) != want {
t.Fatalf("threshold query: got %d want %d", len(rows), want)
}
for _, row := range rows {
if !row["r"].(Relation).Confidence.AtLeast(Scoped) {
t.Fatal(row)
}
}
rows, err = g.Query(ctx, "MATCH p=()-[:calls*1..1]->() RETURN p", nil)
if err != nil || len(rows) == 0 {
t.Fatal(rows, err)
}
for _, row := range rows {
for _, r := range row["p"].(Path).Relations {
if !reflect.DeepEqual(r, originals[r.ID]) {
t.Fatal(r)
}
}
}
}
// +case=`An omitted declaration preserves unrelated nodes and module facts from other documents`
func TestOutlineGapPreservesUsableFacts(t *testing.T) {
ctx := context.Background()
// Deliberately ambiguous query: diagnostics must remain testable when a
// real language's previously ambiguous declarations become supported.
entry := *grammars.DetectLanguageByName("python")
entry.Name, entry.Extensions = "codegraph-conflicting-outline", []string{".cggap"}
entry.TagsQuery = `(function_definition name: (identifier) @name) @definition.function
(function_definition parameters: (parameters (identifier) @name)) @definition.function`
grammars.Register(entry)
source := "def retry(text):\n pass\ndef healthy():\n pass\n"
g, err := NewBuilder("rev", Options{})
if err != nil {
t.Fatal(err)
}
document := Document{Path: "agent.cggap", Content: []byte(source)}
facts, err := g.AddDocument(ctx, document).Wait()
if err != nil {
t.Fatal(err)
}
module := Document{Path: "app.ts", Content: []byte("import { work } from './work'; export { work as task };")}
moduleFacts, err := g.AddDocument(ctx, module).Wait()
if err != nil {
t.Fatal(err)
}
if len(moduleFacts.Imports) == 0 || moduleFacts.Exports["task"] != "work" {
t.Fatalf("outline gap discarded module facts: %+v", moduleFacts)
}
if err := g.AddDocuments(ctx, Document{Path: "work.ts", Content: []byte("export function work() {}")}); err != nil {
t.Fatal(err)
}
report, err := g.Wait(ctx)
// The generic extension also reports unsupported resolution. This test
// isolates the declaration receipt from unrelated capability diagnostics.
declarations := func(items []Diagnostic) []Diagnostic {
var result []Diagnostic
for _, item := range items {
if item.Subject == DeclarationsSubject {
result = append(result, item)
}
}
return result
}
report.Diagnostics = declarations(report.Diagnostics)
facts.Issues = declarations(facts.Issues)
if err != nil || len(report.Diagnostics) != 1 {
t.Fatal(report, err)
}
d := report.Diagnostics[0]
if d.Code != "outline_incomplete" || d.Subject != DeclarationsSubject || d.Relation != "" ||
d.Outline == nil || d.Outline.OmittedNameConflict != 2 {
t.Fatalf("missing structured outline gap: %+v", d)
}
if d.Location.Path != document.Path || d.Location.StartByte != 0 || d.Location.EndByte != len(source) {
t.Fatalf("upstream counters must describe the document, not a guessed declaration: %+v", d.Location)
}
if len(g.Result().Find(document.Path, Function, "healthy")) != 1 {
t.Fatal("unrelated declaration disappeared")
}
edges := g.Result().RelationsFrom(module.ID(), Imports)
moduleEdge := false
for _, edge := range edges {
target, ok := g.Result().Node(edge.Target)
if ok && target.Kind == Module && target.QualifiedName == "work" && edge.Confidence == Exact {
moduleEdge = true
}
}
if !moduleEdge {
t.Fatalf("outline gap discarded or downgraded import: %+v", edges)
}
if !reflect.DeepEqual(facts.Issues, report.Diagnostics) {
t.Fatalf("early facts and published report disagree: %+v %+v", facts.Issues, report.Diagnostics)
}
// Detached early facts, Report and Wait must not leak nested coverage values
// back into the retained cache or another consumer's report.
facts.Issues[0].Outline.OmittedNameConflict = 99
report.Diagnostics[0].Outline.OmittedNameConflict = 99
again, err := g.Wait(ctx)
if err != nil || again.Diagnostics[0].Outline.OmittedNameConflict != 2 {
t.Fatal(again, err)
}
snapshot := g.Report()
snapshot.Diagnostics[0].Outline.OmittedNameConflict = 99
if g.Report().Diagnostics[0].Outline.OmittedNameConflict != 2 {
t.Fatal("Report aliases published coverage")
}
extracted, err := g.Extract(ctx, document)
if err != nil || extracted.Issues[0].Outline.OmittedNameConflict != 2 {
t.Fatal(extracted, err)
}
encoded, err := json.Marshal(again)
if err != nil || strings.Contains(string(encoded), `"complete"`) || !strings.Contains(string(encoded), `"omittedNameConflict":2`) {
t.Fatalf("report lost local coverage or introduced a global verdict: %s %v", encoded, err)
}
}
// +case=`Scoped edges coexist with exact edges and locally unresolved references`
func TestCandidateEvidenceAndUnresolvedLocations(t *testing.T) {
source := "import './lib';\nimport './missing';\nfunction target() {}\nfunction entry() { target(); unknown(); }\n"
g, report, err := Build(context.Background(), "rev", []Document{
{Path: "app.ts", Content: []byte(source)},
{Path: "lib.ts", Content: []byte("export function work() {}")},
{Path: "lib.js", Content: []byte("export function work() {}")},
}, Options{})
report.Diagnostics = withoutReferenceDiagnostics(report.Diagnostics)
if err != nil || len(report.Diagnostics) != 2 {
t.Fatal(report, err)
}
for _, d := range report.Diagnostics {
if d.Subject != RelationsSubject || d.Location.Path != "app.ts" || d.Location.EndByte <= d.Location.StartByte {
t.Fatalf("unlocalized relation gap: %+v", d)
}
switch d.Code {
case "unresolved_import":
if d.Relation != Imports || d.Location.Line != 2 {
t.Fatal(d)
}
case "unresolved_call":
if d.Relation != Calls || d.Location.Line != 4 || !strings.Contains(source[d.Location.StartByte:d.Location.EndByte], "unknown") {
t.Fatal(d)
}
default:
t.Fatalf("candidate edges must not also become coverage gaps: %+v", d)
}
}
imports := g.RelationsFrom(DocumentID("app.ts"), Imports)
if len(imports) != 2 {
t.Fatal(imports)
}
for _, edge := range imports {
if edge.Confidence != Scoped || edge.Evidence[0].Basis == "" {
t.Fatal(edge)
}
}
entry := g.Find("app.ts", Function, "entry")
if len(entry) != 1 {
t.Fatal(entry)
}
calls := g.RelationsFrom(entry[0].ID, Calls)
if len(calls) != 1 || calls[0].Confidence != Exact {
t.Fatalf("unresolved call contaminated exact call: %+v", calls)
}
}
func TestDuplicateOutlineCandidatesAreNotGaps(t *testing.T) {
entry := *grammars.DetectLanguageByName("python")
entry.Name, entry.Extensions = "codegraph-duplicate-outline", []string{".cgduplicates"}
pattern := "(function_definition name: (identifier) @name) @definition.function"
entry.TagsQuery = pattern + "\n" + pattern
grammars.Register(entry)
g, report, err := Build(context.Background(), "rev", []Document{{Path: "app.cgduplicates", Content: []byte("def work():\n pass\n")}}, Options{})
if err != nil || hasDiagnostic(report, "outline_incomplete") || len(g.Find("app.cgduplicates", Function, "work")) != 1 {
t.Fatal(report, err)
}
}
func withoutReferenceDiagnostics(diagnostics []Diagnostic) []Diagnostic {
out := make([]Diagnostic, 0, len(diagnostics))
for _, d := range diagnostics {
if d.Relation != References {
out = append(out, d)
}
}
return out
}
func TestEvidenceBudgetIsAtomic(t *testing.T) {
g, _ := NewBuilder("budget", Options{MaxEvidence: 1})
if err := g.AddDocuments(context.Background(), Document{Path: "a.go", Content: []byte("package a;func f(){}")}); err != nil {
t.Fatal(err)
}
_, err := g.Wait(context.Background())
if !errors.Is(err, ErrBuildBudget) || len(g.Result().Nodes()) != 0 {
t.Fatal(err, g.Result().Nodes())
}
}