package client import ( "context" "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 TestCallRefusesNewerServerProtocol(t *testing.T) { server := buildinfo.Info{Version: "v9-server", Commit: "deadbeef"} endpoint := wstest.Server(t, func(protocol.Message) protocol.Message { return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion + 5} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c, err := Open(ctx, endpoint) if err != nil { t.Fatal(err) } defer c.Close() var warnings int c.Report(func(buildinfo.Relationship) { warnings++ }) _, err = c.Call(ctx, protocol.Message{Type: protocol.Ping}) if err == nil { t.Fatal("newer server protocol was not refused") } client := buildinfo.Current() for _, needle := range []string{client.Version, server.Version, fmt.Sprint(protocol.ProtocolVersion + 5), fmt.Sprint(protocol.ProtocolVersion)} { if !strings.Contains(err.Error(), needle) { t.Fatalf("refusal %q does not name %q", err.Error(), needle) } } if warnings != 0 { t.Fatalf("refusal reported %d warnings instead of failing fast", warnings) } } func TestCallWarnsOnceOnDifferingBuild(t *testing.T) { server := buildinfo.Info{Version: "v0-older-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() c, err := Open(ctx, endpoint) if err != nil { t.Fatal(err) } defer c.Close() var messages []string c.Report(func(relationship buildinfo.Relationship) { messages = append(messages, relationship.Message) }) for i := 0; i < 3; i++ { if _, err := c.Call(ctx, protocol.Message{Type: protocol.Ping}); err != nil { t.Fatal(err) } } if len(messages) != 1 { t.Fatalf("differing build reported %d notices, want exactly one: %v", len(messages), messages) } client := buildinfo.Current() for _, needle := range []string{client.Version, server.Version} { if !strings.Contains(messages[0], needle) { t.Fatalf("warning %q does not name %q", messages[0], needle) } } } func TestCallReportsUnmodeledFieldOnce(t *testing.T) { payload := map[string]any{"type": protocol.Pong, "future_field": true, "another": 2} client := buildinfo.Current() payload["build"] = client 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() c, err := Open(ctx, endpoint) if err != nil { t.Fatal(err) } defer c.Close() var notices []string c.Report(func(relationship buildinfo.Relationship) { notices = append(notices, relationship.Message) }) for i := 0; i < 2; i++ { response, err := c.Call(ctx, protocol.Message{Type: protocol.Ping}) if err != nil { t.Fatal(err) } if response.Type != protocol.Pong { t.Fatalf("unexpected response %+v", response) } } if len(notices) != 1 { t.Fatalf("unmodeled fields reported %d notices, want one: %v", len(notices), notices) } for _, needle := range []string{"another", "future_field", "unrecognized"} { if !strings.Contains(notices[0], needle) { t.Fatalf("unknown-field notice %q does not name %q", notices[0], needle) } } } func TestReceiveNoticeDoesNotFlagFullyModeledFrames(t *testing.T) { endpoint := wstest.Server(t, func(protocol.Message) protocol.Message { server := buildinfo.Current() return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c, err := Open(ctx, endpoint) if err != nil { t.Fatal(err) } defer c.Close() var notices int c.Report(func(buildinfo.Relationship) { notices++ }) if _, err := c.Call(ctx, protocol.Message{Type: protocol.Ping}); err != nil { t.Fatal(err) } if notices != 0 { t.Fatalf("an in-sync, fully modeled frame produced %d notices", notices) } }