package dataplane_test import ( "errors" "fmt" "io" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" "tangled.org/guid.foo/simmer/dataplane" ) // served serves a world over HTTP to a client that speaks as the token // in its token file, as a Job does. type served struct { world client *dataplane.Client tokenFile string } func serve(t *testing.T, token string) served { t.Helper() w := newWorld(t) server := httptest.NewServer(w.handler) t.Cleanup(server.Close) tokenFile := filepath.Join(t.TempDir(), "token") if err := os.WriteFile(tokenFile, []byte(token+"\n"), 0o600); err != nil { t.Fatal(err) } client, err := dataplane.ClientFromEnvironment(func(name string) string { return map[string]string{ "SIMMER_DATAPLANE_URL": server.URL, "SIMMER_SESSION": "telemetry/platform-7f3a2", "SIMMER_TOKEN_FILE": tokenFile, }[name] }) if err != nil { t.Fatal(err) } return served{world: w, client: client, tokenFile: tokenFile} } func TestTheClientFetchesTheTurn(t *testing.T) { s := serve(t, "own") s.control.turn = dataplane.Turn{Session: platform, Number: 1, Skill: "alert-triage"} deliver(t, s.store, "a") turn, err := s.client.Turn(t.Context()) if err != nil { t.Fatal(err) } if got := fmt.Sprintf("%s %d", turn.Skill, len(turn.Events)); got != "alert-triage 1" { t.Errorf("turn is %s", got) } } func TestTheClientSaysWhenThereIsNoTranscript(t *testing.T) { _, err := serve(t, "own").client.Transcript(t.Context()) if !errors.Is(err, dataplane.ErrNoTranscript) { t.Errorf("got %v", err) } } func TestTheClientRoundTripsATranscript(t *testing.T) { s := serve(t, "own") if err := s.client.PutTranscript(t.Context(), strings.NewReader("archive")); err != nil { t.Fatal(err) } archive, err := s.client.Transcript(t.Context()) if err != nil { t.Fatal(err) } defer archive.Close() if got, _ := io.ReadAll(archive); string(got) != "archive" { t.Errorf("transcript is %q", got) } } func TestTheClientFollowsAndReleasesKeys(t *testing.T) { s := serve(t, "own") if err := s.client.Follow(t.Context(), "issue/200"); err != nil { t.Fatal(err) } if err := s.client.Release(t.Context(), "alert/x"); err != nil { t.Fatal(err) } if got := fmt.Sprint(s.control.follows, s.control.releases); got != "[issue/200] [alert/x]" { t.Errorf("follows and releases are %s", got) } } func TestTheClientAsksForTheHoldersOfAKey(t *testing.T) { s := serve(t, "own") key := "https://forge.example/pulls/7?tab=files&x=1" s.control.holders = map[string][]dataplane.Holder{key: {reviewerHolder}} holders, err := s.client.Holders(t.Context(), key) if err != nil || len(holders) != 1 || holders[0] != reviewerHolder { t.Errorf("holders are %+v, %v; want the reviewer", holders, err) } } func TestTheClientReadsTheTokenFileOnEachRequest(t *testing.T) { s := serve(t, "forged") if err := s.client.Follow(t.Context(), "issue/200"); err == nil { t.Fatal("a forged token was accepted") } if err := os.WriteFile(s.tokenFile, []byte("own"), 0o600); err != nil { t.Fatal(err) } if err := s.client.Follow(t.Context(), "issue/200"); err != nil { t.Error(err) } } func TestTheClientNeedsItsSettings(t *testing.T) { _, err := dataplane.ClientFromEnvironment(func(string) string { return "" }) if err == nil { t.Error("made a client with no URL") } } func TestTheClientSetsAndCancelsAFollowUp(t *testing.T) { s := serve(t, "own") at, err := s.client.FollowUp(t.Context(), 2*time.Hour, "Read the p99.") if err != nil { t.Fatal(err) } if err := s.client.CancelFollowUp(t.Context()); err != nil { t.Fatal(err) } if !at.Equal(followUpAt) || fmt.Sprint(s.control.followUps) != "[2h0m0s Read the p99.]" || s.control.cancels != 1 { t.Errorf("due at %v, set %v, and %d cancels", at, s.control.followUps, s.control.cancels) } } func TestTheClientSaysWhenTheOperatorRefusesAFollowUp(t *testing.T) { s := serve(t, "own") s.control.refuse = fmt.Errorf("%w: it has had its 10", dataplane.ErrFollowUpRefused) _, err := s.client.FollowUp(t.Context(), 2*time.Hour, "Read the p99.") if !errors.Is(err, dataplane.ErrFollowUpRefused) || !strings.Contains(err.Error(), "it has had its 10") { t.Errorf("got %v", err) } }