54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package connector
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
"errors"
|
|
"testing"
|
|
|
|
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
|
|
)
|
|
|
|
type stubActor struct {
|
|
unknown bool
|
|
err error
|
|
}
|
|
|
|
func (stubActor) Describe() Description { return Description{} }
|
|
|
|
func (stubActor) Resolve(context.Context, string, jsontext.Value) (*connectorv1.ResolveResponse, error) {
|
|
return &connectorv1.ResolveResponse{}, nil
|
|
}
|
|
|
|
func (a stubActor) Invoke(context.Context, string, jsontext.Value, string) (jsontext.Value, bool, error) {
|
|
return nil, a.unknown, a.err
|
|
}
|
|
|
|
// TestAnActionThatMayHaveRunIsNotAFailure guards the unknown outcome: an
|
|
// action that fails after it may have taken effect must reach the host as
|
|
// outcome_unknown, not as an error, or the host treats it as not having run
|
|
// and may run it again.
|
|
func TestAnActionThatMayHaveRunIsNotAFailure(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
actor stubActor
|
|
wantErr bool
|
|
wantUnknown bool
|
|
}{
|
|
{name: "a failure before any effect is an error", actor: stubActor{err: errors.New("bad input")}, wantErr: true},
|
|
{name: "a failure after a possible effect is unknown", actor: stubActor{unknown: true, err: errors.New("lost track of task 9")}, wantUnknown: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := &server{build: func(Config) (Connector, error) { return tt.actor, nil }}
|
|
if _, err := s.Configure(t.Context(), &connectorv1.ConfigureRequest{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp, err := s.Invoke(t.Context(), &connectorv1.InvokeRequest{Action: "a", Input: []byte(`{}`)})
|
|
if (err != nil) != tt.wantErr || resp.GetOutcomeUnknown() != tt.wantUnknown {
|
|
t.Fatalf("err = %v, unknown = %v; want error %v, unknown %v", err, resp.GetOutcomeUnknown(), tt.wantErr, tt.wantUnknown)
|
|
}
|
|
})
|
|
}
|
|
}
|