github.com/anthropics/anthropic-sdk-go@v1.70.1/googlecloud/googlecloud_live_test.go (about)

     1  package googlecloud_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/anthropics/anthropic-sdk-go"
    10  	"github.com/anthropics/anthropic-sdk-go/googlecloud"
    11  )
    12  
    13  // Live integration test for the Google Cloud client. Skipped unless ANTHROPIC_LIVE=1.
    14  //
    15  // Authentication uses Application Default Credentials (cloud-platform scope), e.g.
    16  // after `gcloud auth application-default login`. Required env vars:
    17  //
    18  //	ANTHROPIC_GOOGLE_CLOUD_PROJECT       GCP consumer project (or inferred from ADC)
    19  //	ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID  Anthropic workspace ID for the org
    20  //	ANTHROPIC_GOOGLE_CLOUD_LOCATION      GCP location (test override; default us-central1)
    21  //
    22  // Run: ANTHROPIC_LIVE=1 go test ./googlecloud/... -run TestLiveGoogleCloud -v
    23  func TestLiveGoogleCloud(t *testing.T) {
    24  	if os.Getenv("ANTHROPIC_LIVE") != "1" {
    25  		t.Skip("set ANTHROPIC_LIVE=1 to run live integration tests")
    26  	}
    27  	for _, name := range []string{"ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"} {
    28  		if os.Getenv(name) == "" {
    29  			t.Fatalf("required env var %s is not set", name)
    30  		}
    31  	}
    32  
    33  	location := os.Getenv("ANTHROPIC_GOOGLE_CLOUD_LOCATION")
    34  	if location == "" {
    35  		location = "us-central1"
    36  	}
    37  	model := os.Getenv("ANTHROPIC_LIVE_MODEL")
    38  	if model == "" {
    39  		model = "claude-haiku-4-5"
    40  	}
    41  
    42  	ctx := context.Background()
    43  	client, err := googlecloud.NewClient(ctx, googlecloud.ClientConfig{
    44  		Location: location,
    45  	})
    46  	if err != nil {
    47  		t.Fatalf("NewClient failed: %v", err)
    48  	}
    49  
    50  	t.Run("messages", func(t *testing.T) {
    51  		msg, err := client.Messages.New(ctx, anthropic.MessageNewParams{
    52  			Model:     anthropic.Model(model),
    53  			MaxTokens: 64,
    54  			Messages: []anthropic.MessageParam{
    55  				anthropic.NewUserMessage(anthropic.NewTextBlock("Say hello in exactly three words.")),
    56  			},
    57  		})
    58  		if err != nil {
    59  			t.Fatalf("Messages.New failed: %v", err)
    60  		}
    61  		if len(msg.Content) == 0 {
    62  			t.Fatal("expected non-empty response content")
    63  		}
    64  		t.Logf("response: %s", msg.Content[0].Text)
    65  	})
    66  
    67  	t.Run("streaming", func(t *testing.T) {
    68  		stream := client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
    69  			Model:     anthropic.Model(model),
    70  			MaxTokens: 64,
    71  			Messages: []anthropic.MessageParam{
    72  				anthropic.NewUserMessage(anthropic.NewTextBlock("Count to three.")),
    73  			},
    74  		})
    75  		defer stream.Close()
    76  
    77  		var acc anthropic.Message
    78  		var sawStart, sawStop bool
    79  		for stream.Next() {
    80  			ev := stream.Current()
    81  			if err := acc.Accumulate(ev); err != nil {
    82  				t.Fatalf("Accumulate failed: %v", err)
    83  			}
    84  			switch ev.AsAny().(type) {
    85  			case anthropic.MessageStartEvent:
    86  				sawStart = true
    87  			case anthropic.MessageStopEvent:
    88  				sawStop = true
    89  			}
    90  		}
    91  		if err := stream.Err(); err != nil {
    92  			t.Fatalf("stream error: %v", err)
    93  		}
    94  		if !sawStart || !sawStop {
    95  			t.Fatalf("incomplete event sequence: start=%v stop=%v", sawStart, sawStop)
    96  		}
    97  		if len(acc.Content) == 0 || acc.Content[0].Text == "" {
    98  			t.Fatal("expected accumulated text content")
    99  		}
   100  		t.Logf("streamed: %s", acc.Content[0].Text)
   101  	})
   102  
   103  	t.Run("error", func(t *testing.T) {
   104  		_, err := client.Messages.New(ctx, anthropic.MessageNewParams{
   105  			Model:     "no-such-model",
   106  			MaxTokens: 1,
   107  			Messages: []anthropic.MessageParam{
   108  				anthropic.NewUserMessage(anthropic.NewTextBlock("hi")),
   109  			},
   110  		})
   111  		if err == nil {
   112  			t.Fatal("expected error for unknown model")
   113  		}
   114  		var apierr *anthropic.Error
   115  		if !errors.As(err, &apierr) {
   116  			t.Fatalf("error is not *anthropic.Error: %T %v", err, err)
   117  		}
   118  		if apierr.StatusCode < 400 || apierr.StatusCode >= 500 {
   119  			t.Errorf("status = %d, want 4xx", apierr.StatusCode)
   120  		}
   121  		t.Logf("error: status=%d %v", apierr.StatusCode, apierr)
   122  	})
   123  }