package connectors import ( "context" "encoding/json/jsontext" "errors" "testing" "google.golang.org/grpc" pb "github.com/samcharles93/archie-core/internal/contracts/connector/v1" ) // fakeClient is a connector that declares one action and answers Invoke as // told, so the host's checks can be driven directly. type fakeClient struct { pb.ConnectorServiceClient readOnly bool idempotent bool // executed overrides the digest the connector reports; empty echoes the // host's digest. executed string unknown bool invoked int } func (f *fakeClient) Configure(context.Context, *pb.ConfigureRequest, ...grpc.CallOption) (*pb.ConfigureResponse, error) { return &pb.ConfigureResponse{}, nil } func (f *fakeClient) Describe(context.Context, *pb.DescribeRequest, ...grpc.CallOption) (*pb.DescribeResponse, error) { return &pb.DescribeResponse{Contract: contractVersion, Actions: []*pb.ActionType{{ Name: "delete", InputSchema: []byte(`{"type":"object","required":["path"],"properties":{"path":{"type":"string"}}}`), Effect: &pb.Effect{ReadOnly: f.readOnly, Reversibility: pb.Reversibility_REVERSIBILITY_RECOVERABLE}, Idempotent: f.idempotent, }}}, nil } func (f *fakeClient) Invoke(_ context.Context, req *pb.InvokeRequest, _ ...grpc.CallOption) (*pb.InvokeResponse, error) { f.invoked++ executed := req.GetDigest() if f.executed != "" { executed = f.executed } return &pb.InvokeResponse{Output: []byte(`{"deleted":3}`), ExecutedDigest: executed, OutcomeUnknown: f.unknown}, nil } // TestInvokeRunsOnlyTheJudgedAction guards R18 and 2.5's checks at the // connector boundary: input that breaks the declared schema never reaches the // connector, an action with external effects is refused unless the connector // honours idempotency keys, a connector that ran something other than the // judged action is caught by its digest, and an unconfirmed outcome is // reported as unknown rather than success. func TestInvokeRunsOnlyTheJudgedAction(t *testing.T) { good := jsontext.Value(`{"path":"/tmp/runner"}`) tests := []struct { name string client *fakeClient input jsontext.Value wantErr error wantAnyErr bool wantInvoked int }{ {name: "matching digest returns the output", client: &fakeClient{idempotent: true}, input: good, wantInvoked: 1}, {name: "a different executed digest is refused", client: &fakeClient{idempotent: true, executed: "other"}, input: good, wantErr: ErrDigestMismatch, wantInvoked: 1}, {name: "external effects without idempotency are refused before running", client: &fakeClient{}, input: good, wantErr: ErrNotIdempotent}, {name: "read-only actions need no idempotency", client: &fakeClient{readOnly: true}, input: good, wantInvoked: 1}, {name: "an unconfirmed outcome is unknown", client: &fakeClient{idempotent: true, unknown: true}, input: good, wantErr: ErrOutcomeUnknown, wantInvoked: 1}, {name: "input breaking the schema never reaches the connector", client: &fakeClient{idempotent: true}, input: jsontext.Value(`{"path":7}`), wantAnyErr: true, wantInvoked: 0}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c, err := open(t.Context(), "fake", tt.client, nil, nil) if err != nil { t.Fatalf("open: %v", err) } out, err := c.Invoke(t.Context(), Invocation{Action: "delete", Input: tt.input, IdempotencyKey: "run:1"}) wantOK := tt.wantErr == nil && !tt.wantAnyErr switch { case wantOK && err != nil: t.Fatalf("Invoke: %v", err) case !wantOK && err == nil: t.Fatal("Invoke succeeded, want an error") case tt.wantErr != nil && !errors.Is(err, tt.wantErr): t.Fatalf("Invoke error = %v, want %v", err, tt.wantErr) } if wantOK && out != `{"deleted":3}` { t.Fatalf("output = %q, want the connector's own %q", out, `{"deleted":3}`) } if tt.client.invoked != tt.wantInvoked { t.Fatalf("connector invoked %d times, want %d", tt.client.invoked, tt.wantInvoked) } }) } }