diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 6e2c1c2aa..0e6bd1936 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/prymitive/unsee/internal/uri" + "github.com/pmezard/go-difflib/difflib" log "github.com/sirupsen/logrus" @@ -207,3 +208,9 @@ func TestUrlSecretTest(t *testing.T) { } } } + +// FIXME check logged values +func TestLogValues(t *testing.T) { + Config.Read() + Config.LogValues() +} diff --git a/internal/transform/autocomplete_test.go b/internal/transform/autocomplete_test.go new file mode 100644 index 000000000..db1ce3f7a --- /dev/null +++ b/internal/transform/autocomplete_test.go @@ -0,0 +1,82 @@ +package transform_test + +import ( + "encoding/json" + "sort" + "testing" + + "github.com/prymitive/unsee/internal/models" + "github.com/prymitive/unsee/internal/transform" + + "github.com/pmezard/go-difflib/difflib" +) + +type acTest struct { + Alerts []models.Alert + Expected []string +} + +var acTests = []acTest{ + { + Alerts: []models.Alert{}, + Expected: []string{ + "@age\u003e1h", + "@age\u003c10m", + "@age\u003c1h", + "@age\u003e10m", + "@limit=10", + "@limit=50", + }, + }, + { + Alerts: []models.Alert{ + models.Alert{ + Labels: map[string]string{ + "foo": "bar", + }, + }, + }, + Expected: []string{ + "@age\u003e1h", + "@age\u003c10m", + "@age\u003c1h", + "@age\u003e10m", + "@limit=10", + "@limit=50", + "@state!=", + "@state=", + "foo!=bar", + "foo=bar", + }, + }, +} + +func TestBuildAutocomplete(t *testing.T) { + for _, acTest := range acTests { + result := []string{} + for _, hint := range transform.BuildAutocomplete(acTest.Alerts) { + result = append(result, hint.Value) + } + + sort.Strings(result) + sort.Strings(acTest.Expected) + + resultJSON, _ := json.Marshal(result) + expectedJSON, _ := json.Marshal(acTest.Expected) + + if string(resultJSON) != string(expectedJSON) { + diff := difflib.UnifiedDiff{ + A: difflib.SplitLines(string(expectedJSON)), + B: difflib.SplitLines(string(resultJSON)), + FromFile: "Expected", + ToFile: "Returned", + Context: 3, + } + text, err := difflib.GetUnifiedDiffString(diff) + if err != nil { + t.Error(err) + } + t.Errorf("Autocomplete mismatch:\n%s", text) + } + } +}