internal/lsp: make the ocagent test external

They now call a public encode function and have no knowledge of the private
conversion functions or any of the contents of the wire package.

Change-Id: I4364a4d9d1efe4bc872627556e537936b5880231
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207903
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
diff --git a/internal/telemetry/export/ocagent/metrics_test.go b/internal/telemetry/export/ocagent/metrics_test.go
index 033a60e..a12c66a 100644
--- a/internal/telemetry/export/ocagent/metrics_test.go
+++ b/internal/telemetry/export/ocagent/metrics_test.go
@@ -1,6 +1,7 @@
 package ocagent
 
 import (
+	"encoding/json"
 	"reflect"
 	"testing"
 	"time"
@@ -991,3 +992,8 @@
 		})
 	}
 }
+
+func marshaled(v interface{}) string {
+	blob, _ := json.MarshalIndent(v, "", "  ")
+	return string(blob)
+}
diff --git a/internal/telemetry/export/ocagent/ocagent.go b/internal/telemetry/export/ocagent/ocagent.go
index eb28d0e..b882500 100644
--- a/internal/telemetry/export/ocagent/ocagent.go
+++ b/internal/telemetry/export/ocagent/ocagent.go
@@ -144,6 +144,10 @@
 	}
 }
 
+func EncodeAnnotation(a telemetry.Event) ([]byte, error) {
+	return json.Marshal(convertAnnotation(a))
+}
+
 func (e *exporter) send(endpoint string, message interface{}) {
 	blob, err := json.Marshal(message)
 	if err != nil {
diff --git a/internal/telemetry/export/ocagent/ocagent_test.go b/internal/telemetry/export/ocagent/ocagent_test.go
index a5d0e96..534c54c 100644
--- a/internal/telemetry/export/ocagent/ocagent_test.go
+++ b/internal/telemetry/export/ocagent/ocagent_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package ocagent
+package ocagent_test
 
 import (
 	"context"
@@ -12,6 +12,7 @@
 	"testing"
 
 	"golang.org/x/tools/internal/telemetry"
+	"golang.org/x/tools/internal/telemetry/export/ocagent"
 	"golang.org/x/tools/internal/telemetry/tag"
 )
 
@@ -199,15 +200,25 @@
 	ctx := context.TODO()
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			got := marshaled(convertAnnotation(tt.event(ctx)))
-			if !reflect.DeepEqual(got, tt.want) {
-				t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want)
+			got, err := ocagent.EncodeAnnotation(tt.event(ctx))
+			if err != nil {
+				t.Fatal(err)
 			}
+			checkJSON(t, got, []byte(tt.want))
 		})
 	}
 }
 
-func marshaled(v interface{}) string {
-	blob, _ := json.MarshalIndent(v, "", "  ")
-	return string(blob)
+func checkJSON(t *testing.T, got, want []byte) {
+	// compare the decoded form, to allow for formatting differences
+	var g, w map[string]interface{}
+	if err := json.Unmarshal(got, &g); err != nil {
+		t.Fatal(err)
+	}
+	if err := json.Unmarshal(want, &w); err != nil {
+		t.Fatal(err)
+	}
+	if !reflect.DeepEqual(g, w) {
+		t.Fatalf("Got:\n%s\nWant:\n%s", got, want)
+	}
 }