Skip to main content

Testing

A plugin can be tested in two ways:

  • Locally, as a regular .NET console application. This is the fastest way to develop and debug a plugin and does not require MoBro.
  • In MoBro, by installing the plugin. This verifies that everything works in the actual application.

There is no hot reload for plugins. Iterate on the plugin's logic locally, and install it to MoBro again whenever you want to verify the current state in the app.


Running Locally

The SDK provides the MoBroPluginBuilder class, which creates and runs the plugin locally, like MoBro would. The builder creates the plugin instance, injects all services and calls Init().

Program.cs
using MoBro.Plugin.SDK;

// Create and start the plugin to test it locally
using var plugin = MoBroPluginBuilder
.Create<Plugin.Example.Plugin>()
.WithSetting("update_frequency", "1")
.Build();

// Prevent the program from exiting immediately
Console.ReadLine();

Run it like any other .NET console application, or start it with the debugger from your IDE:

dotnet run

Builder Options

FunctionDescription
WithSetting(string key, string value)Sets the value of a single plugin setting. Values are always passed as strings.
WithSettings(IDictionary<string, string>)Sets the values of multiple plugin settings.
WithLogLevel(LogEventLevel)Sets the log level of the default console logger (default: Debug). LogEventLevel is in the Serilog.Events namespace.
WithLogger(ILogger)Replaces the default console logger. See Logging.
WithStorageDirectory(string path)Sets the directory used by the file and persistence managers (default: ./data).
Build()Creates the plugin and calls Init() and InitAsync(). Returns a MoBroPluginWrapper.

Interacting with the Plugin

The returned MoBroPluginWrapper allows interacting with the running plugin:

FunctionDescription
ApplySettings(IDictionary<string, string>)Applies new settings. Like in MoBro, this re-creates the plugin instance.
InvokeAction(string actionId, IDictionary<string, string>?)Invokes a registered action with the given action settings.
GetRegisteredItems() / GetRegisteredItems<T>()Returns all currently registered items (of a specific type).
GetPlugin()Returns the current plugin instance.
Dispose()Shuts down the plugin (calls Shutdown(), ShutdownAsync() and Dispose()) and stops the scheduler.

Example

Program.cs
using MoBro.Plugin.SDK;
using MoBro.Plugin.SDK.Models.Metrics;

using var plugin = MoBroPluginBuilder
.Create<Plugin.Example.Plugin>()
.WithSetting("update_frequency", "1")
.Build();

// Verify the registered metrics
foreach (var metric in plugin.GetRegisteredItems<Metric>())
{
Console.WriteLine($"Registered metric: {metric.Id}");
}

// Trigger an action
plugin.InvokeAction("set_metric_value", new Dictionary<string, string> { { "value", "42" } });

// Apply new settings after pressing enter
Console.ReadLine();
plugin.ApplySettings(new Dictionary<string, string> { { "update_frequency", "2" } });

// Prevent the program from exiting immediately
Console.ReadLine();

Differences to MoBro

Running locally closely mimics MoBro, with a few intentional differences:

  • Nothing is sent to MoBro. Registered items and metric values are only logged to the console.
  • Invalid items throw a MoBroItemValidationException, and value updates for unregistered metrics throw a MetricValueValidationException. MoBro skips or ignores those instead.
  • ApplySettings(...) only calls Dispose() on the old plugin instance, while MoBro also calls Shutdown() and ShutdownAsync(). See Plugin Lifecycle.
  • Action handlers are awaited by InvokeAction. In MoBro, asynchronous handlers run in the background.

Testing in MoBro

To test the plugin in MoBro, publish it and install it using the CLI or the local REST API.

The quickest way is to install the plugin straight from the project directory. The CLI builds and publishes the plugin and installs it in a single step:

mobro install .

Run the same command again to update the installed plugin after making changes.

For all options, refer to:

Debugging an Installed Plugin

Each installed plugin runs in its own process named MoBro.Plugin.[DisplayName].exe (all characters other than letters, digits, _ and - are removed from the display name). You can attach your IDE's debugger to this process. Plugins running in the default AdminBackground execution mode run with administrative privileges, so the IDE must be started as administrator as well.

The logs of an installed plugin are described under Logging.