clerk/internal/lsp/textdocument_completion_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com lsp: add bechmarks, consisten golden tests naming, 1 month ago
olexsmir@gmail.com lsp: add bechmarks, consisten golden tests naming, 1 month ago
| 1 | package lsp |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "go.lsp.dev/protocol" |
| 10 | "go.lsp.dev/uri" |
| 11 | |
| 12 | "olexsmir.xyz/clerk/internal/analyzer" |
| 13 | "olexsmir.xyz/clerk/internal/lsp/lsputil" |
| 14 | "olexsmir.xyz/clerk/internal/testutil/golden" |
| 15 | "olexsmir.xyz/clerk/journal" |
| 16 | ) |
| 17 | |
| 18 | func TestDetectCompletion_NoJournal(t *testing.T) { |
| 19 | // detection must not depend on a parse |
| 20 | i := strings.Index(" no^te foo\n", "^") |
| 21 | content := " note foo\n" |
| 22 | ctx, _ := detectCompletionCtx(content, i) |
| 23 | if ctx != cmplAccount { |
| 24 | t.Errorf("ctx = %v, want account", ctx) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestGolden_Completion(t *testing.T) { |
| 29 | for _, tt := range []string{"completion-contexts", "completion-journal", "completion-no-transactions"} { |
| 30 | ar := golden.Read(t, tt) |
| 31 | |
| 32 | t.Run(tt, func(t *testing.T) { |
| 33 | h := newTxtarHarness(t, ar) |
| 34 | |
| 35 | var b strings.Builder |
| 36 | for i, c := range h.cursors { |
| 37 | tdp := h.textDocumentPosition(i) |
| 38 | res, err := h.srv.Completion(t.Context(), &protocol.CompletionParams{ |
| 39 | TextDocumentPositionParams: tdp, |
| 40 | }) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | ctx, start := detectCompletionCtx(h.content, c) |
| 45 | fmt.Fprintf(&b, "%d:%d %s %q\n", tdp.Position.Line, tdp.Position.Character, ctx, h.content[start:c]) |
| 46 | list, ok := res.(*protocol.CompletionList) |
| 47 | if !ok { |
| 48 | t.Fatalf("Completion returned %T, want *protocol.CompletionList", res) |
| 49 | } |
| 50 | for _, item := range list.Items { |
| 51 | fmt.Fprintf(&b, " %s\n", item.Label) |
| 52 | } |
| 53 | } |
| 54 | golden.Assert(t, ar, b.String()) |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func (c cmplCtx) String() string { |
| 60 | switch c { |
| 61 | case cmplNone: |
| 62 | return "none" |
| 63 | case cmplAccount: |
| 64 | return "account" |
| 65 | case cmplPayee: |
| 66 | return "payee" |
| 67 | case cmplCommodity: |
| 68 | return "commodity" |
| 69 | case cmplTagName: |
| 70 | return "tag" |
| 71 | case cmplTagValue: |
| 72 | return "tag-value" |
| 73 | case cmplDirective: |
| 74 | return "directive" |
| 75 | default: |
| 76 | return "?" |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func BenchmarkCompletion(b *testing.B) { |
| 81 | path := "../../journal/testdata/journals/actual-1ktxns-100accts.journal" |
| 82 | rj, err := journal.NewLoader().Resolve(path) |
| 83 | if err != nil { |
| 84 | b.Fatal(err) |
| 85 | } |
| 86 | a := analyzer.Build(rj) |
| 87 | content := string(rj.Occurrences[0].Src) |
| 88 | |
| 89 | srv := NewServer("test") |
| 90 | srv.server.openDoc(uri.URI("file:///test.journal"), content, 1, "journal") |
| 91 | srv.server.current = a |
| 92 | |
| 93 | for tname, tt := range map[string]int{ |
| 94 | "1k txns, account": strings.Index(content, "\n 1:2:3 ") + len("\n ") + 2, |
| 95 | "1k txns, empty payee": strings.Index(content, "transaction 1") + len("transaction "), |
| 96 | "1k txns, commodity": strings.Index(content, "2 B @@") + len("2 B"), |
| 97 | } { |
| 98 | b.Run(tname, func(b *testing.B) { |
| 99 | line, col := lsputil.LineCol(content, tt) |
| 100 | params := &protocol.CompletionParams{ |
| 101 | TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 102 | TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///test.journal")}, |
| 103 | Position: protocol.Position{Line: uint32(line), Character: uint32(col)}, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | // warm up |
| 108 | if _, err := srv.server.Completion(b.Context(), params); err != nil { |
| 109 | b.Fatal(err) |
| 110 | } |
| 111 | b.ReportAllocs() |
| 112 | b.ResetTimer() |
| 113 | for b.Loop() { |
| 114 | if _, err := srv.server.Completion(b.Context(), params); err != nil { |
| 115 | b.Fatal(err) |
| 116 | } |
| 117 | } |
| 118 | // guard: line-local lexing must stay far below the old whole-file |
| 119 | // relex (~3ms on this file); the measured target is sub-ms |
| 120 | if avg := b.Elapsed() / time.Duration(b.N); avg > 2*time.Millisecond { |
| 121 | b.Fatalf("completion %v/op: whole-file relex regression", avg) |
| 122 | } |
| 123 | }) |
| 124 | } |
| 125 | } |