package cli import ( "bytes" "context" "encoding/json" "fmt" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/buildinfo" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/wstest" ) func TestCLIWarnsOnceOnDifferingBuildAndKeepsJSONParseable(t *testing.T) { server := buildinfo.Info{Version: "v0-stale-server", Commit: "deadbeef"} endpoint := wstest.Server(t, func(protocol.Message) protocol.Message { return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var out, errOut bytes.Buffer app := App{Out: &out, Err: &errOut, In: strings.NewReader("")} if err := app.Run(ctx, []string{"--url", endpoint, "ping", "--json"}); err != nil { t.Fatal(err) } // stdout must stay valid JSON. var decoded protocol.Message if err := json.Unmarshal(out.Bytes(), &decoded); err != nil { t.Fatalf("stdout is not parseable JSON: %v\n%s", err, out.String()) } if decoded.Build == nil || decoded.Build.Version != server.Version { t.Fatalf("stdout lost the server build: %+v", decoded.Build) } warnings := 0 for _, line := range strings.Split(strings.TrimSpace(errOut.String()), "\n") { if strings.HasPrefix(line, "warning:") { warnings++ client := buildinfo.Current() for _, needle := range []string{client.Version, server.Version} { if !strings.Contains(line, needle) { t.Fatalf("warning %q does not name %q", line, needle) } } } } if warnings != 1 { t.Fatalf("differing build produced %d stderr warnings, want exactly one:\n%s", warnings, errOut.String()) } t.Logf("stdout:\n%s", out.String()) t.Logf("stderr:\n%s", strings.TrimSpace(errOut.String())) // Human ping prints the relationship too, without hiding the server build. var humanOut, humanErr bytes.Buffer human := App{Out: &humanOut, Err: &humanErr, In: strings.NewReader("")} if err := human.Run(ctx, []string{"--url", endpoint, "ping"}); err != nil { t.Fatal(err) } if !strings.Contains(humanOut.String(), "Note:") || !strings.Contains(humanOut.String(), server.Version) { t.Fatalf("human ping omitted the client/server relationship:\n%s", humanOut.String()) } } func TestCLINoticesUnmodeledFieldInsteadOfDroppingIt(t *testing.T) { payload := map[string]any{"type": protocol.Pong, "future_field": true} payload["build"] = buildinfo.Current() payload["protocol"] = protocol.ProtocolVersion endpoint := wstest.Raw(t, func(protocol.Message) map[string]any { return payload }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var out, errOut bytes.Buffer app := App{Out: &out, Err: &errOut, In: strings.NewReader("")} if err := app.Run(ctx, []string{"--url", endpoint, "ping"}); err != nil { t.Fatal(err) } if !strings.Contains(errOut.String(), "future_field") || !strings.Contains(errOut.String(), "unrecognized") { t.Fatalf("unmodeled field was silent: stderr=%q stdout=%q", errOut.String(), out.String()) } t.Logf("stderr:\n%s", strings.TrimSpace(errOut.String())) } func TestCLIRefusesNewerServerProtocol(t *testing.T) { server := buildinfo.Info{Version: "v9-future-server", Commit: "deadbeef"} endpoint := wstest.Server(t, func(protocol.Message) protocol.Message { return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion + 7} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var out, errOut bytes.Buffer app := App{Out: &out, Err: &errOut, In: strings.NewReader("")} err := app.Run(ctx, []string{"--url", endpoint, "ping", "--json"}) if err == nil { t.Fatal("a newer server protocol did not fail the command") } client := buildinfo.Current() for _, needle := range []string{client.Version, server.Version, fmt.Sprint(protocol.ProtocolVersion + 7), fmt.Sprint(protocol.ProtocolVersion)} { if !strings.Contains(err.Error(), needle) { t.Fatalf("refusal %q does not name %q", err.Error(), needle) } } t.Logf("exit error: %v", err) }