microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1e6c6424b397bc7a2e03de41e3d58c1434d9ba5e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps.Testing/Plugins/TestPlugin.cs

181lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Api;
5using Microsoft.Teams.Api.Activities;
6using Microsoft.Teams.Api.Auth;
7using Microsoft.Teams.Apps.Events;
8using Microsoft.Teams.Apps.Plugins;
9using Microsoft.Teams.Apps.Testing.Events;
10
11namespace Microsoft.Teams.Apps.Testing.Plugins;
12
13/// <summary>
14/// a plugin used to test any App implementation
15/// </summary>
16[Plugin(Name = "test")]
17public partial class TestPlugin : ISenderPlugin
18{
19 public event EventFunction Events;
20
21 protected Action<App>? OnInitHandler { get; set; }
22 protected Action<App>? OnStartHandler { get; set; }
23 protected Action<App, IPlugin, ErrorEvent>? OnErrorHandler { get; set; }
24 protected Action<App, ISenderPlugin, ActivityEvent>? OnActivityHandler { get; set; }
25 protected Action<App, ISenderPlugin, ActivityResponseEvent>? OnActivityResponseHandler { get; set; }
26 protected Action<App, ISenderPlugin, ActivitySentEvent>? OnActivitySentHandler { get; set; }
27
28 public TestPlugin WithInit(Action<App> handler)
29 {
30 OnInitHandler = handler;
31 return this;
32 }
33
34 public TestPlugin WithError(Action<App, IPlugin, ErrorEvent> handler)
35 {
36 OnErrorHandler = handler;
37 return this;
38 }
39
40 public TestPlugin WithActivity(Action<App, ISenderPlugin, ActivityEvent> handler)
41 {
42 OnActivityHandler = handler;
43 return this;
44 }
45
46 public TestPlugin WithActivityResponse(Action<App, ISenderPlugin, ActivityResponseEvent> handler)
47 {
48 OnActivityResponseHandler = handler;
49 return this;
50 }
51
52 public TestPlugin WithActivitySent(Action<App, ISenderPlugin, ActivitySentEvent> handler)
53 {
54 OnActivitySentHandler = handler;
55 return this;
56 }
57
58 public Task OnInit(App app, CancellationToken cancellationToken = default)
59 {
60 if (OnInitHandler is not null)
61 {
62 OnInitHandler(app);
63 }
64
65 return Task.CompletedTask;
66 }
67
68 public Task OnStart(App app, CancellationToken cancellationToken = default)
69 {
70 if (OnStartHandler is not null)
71 {
72 OnStartHandler(app);
73 }
74
75 return Task.CompletedTask;
76 }
77
78 public Task OnError(App app, IPlugin plugin, ErrorEvent @event, CancellationToken cancellationToken = default)
79 {
80 if (OnErrorHandler is not null)
81 {
82 OnErrorHandler(app, plugin, @event);
83 }
84
85 return Task.CompletedTask;
86 }
87
88 public Task OnActivity(App app, ISenderPlugin sender, ActivityEvent @event, CancellationToken cancellationToken = default)
89 {
90 if (OnActivityHandler is not null)
91 {
92 OnActivityHandler(app, sender, @event);
93 }
94
95 return Task.CompletedTask;
96 }
97
98 public Task OnActivityResponse(App app, ISenderPlugin sender, ActivityResponseEvent @event, CancellationToken cancellationToken = default)
99 {
100 if (OnActivityResponseHandler is not null)
101 {
102 OnActivityResponseHandler(app, sender, @event);
103 }
104
105 return Task.CompletedTask;
106 }
107
108 public Task OnActivitySent(App app, ISenderPlugin sender, ActivitySentEvent @event, CancellationToken cancellationToken = default)
109 {
110 if (OnActivitySentHandler is not null)
111 {
112 OnActivitySentHandler(app, sender, @event);
113 }
114
115 return Task.CompletedTask;
116 }
117
118 public Task<IActivity> Send(IActivity activity, ConversationReference reference, CancellationToken cancellationToken = default)
119 {
120 return Send(activity, reference, isTargeted: false, cancellationToken);
121 }
122
123 public Task<IActivity> Send(IActivity activity, ConversationReference reference, bool isTargeted, CancellationToken cancellationToken = default)
124 {
125 return Task.FromResult(activity);
126 }
127
128 public Task<TActivity> Send<TActivity>(TActivity activity, ConversationReference reference, CancellationToken cancellationToken = default) where TActivity : IActivity
129 {
130 return Send(activity, reference, isTargeted: false, cancellationToken);
131 }
132
133 public Task<TActivity> Send<TActivity>(TActivity activity, ConversationReference reference, bool isTargeted, CancellationToken cancellationToken = default) where TActivity : IActivity
134 {
135 return Task.FromResult(activity);
136 }
137
138 public IStreamer CreateStream(ConversationReference reference, CancellationToken cancellationToken = default)
139 {
140 return new Stream();
141 }
142
143 public async Task<Response> Do(IToken token, IActivity activity, IDictionary<string, object?>? extra = null, CancellationToken cancellationToken = default)
144 {
145 return await Do(new()
146 {
147 Token = token,
148 Activity = activity,
149 Extra = extra
150 }, cancellationToken);
151 }
152
153 public async Task<Response> Do(ActivityEvent @event, CancellationToken cancellationToken = default)
154 {
155 if (@event.Activity is MessageActivity message)
156 {
157 await Events(
158 this,
159 "message",
160 new TestMessageEvent() { Message = message.Text },
161 cancellationToken
162 );
163 }
164
165 var @out = await Events(
166 this,
167 "activity",
168 @event,
169 cancellationToken
170 );
171
172 var res = (Response?)@out;
173
174 if (res is null)
175 {
176 throw new Exception("expected activity response");
177 }
178
179 return res;
180 }
181}