Goal: tests that run with npm test on any machine, without the player, and check what a user would see: the menu items, the window's values, the on-screen messages and what happens when the player refuses a call.
The tests live in tests/*.test.ts, use node:test and run with vestra-plugin test (the test script). They run the plugin against TestHost, an in-memory player; what it simulates, every option and everything it records are in Reference: test host. Import @vestra-player/sdk/testing only in tests, never in the plugin itself.
Call startPlugin(activate, { directory }) with the plugin folder (from tests/, new URL("..", import.meta.url)). It reads the plugin's id from manifest.json and resolves once activation is done. To find the plugin playing something at activation, prepare a host first and pass it as host.
Run a command, press a key, fire a window's callback, or answer a dialog before the action that opens it:
await host.command("picture").run(); // choose the menu item
assert.equal(await host.press("Ctrl+B"), "ran"); // press its key
host.window("picture").emit("adjust", "gamma", 20); // move a control
host.dialogAnswers.push({ confirmed: true }); // answer the next confirm
Every way to act is in Reference: test host, with what press resolves with.
Decide what the player is doing: host.openElsewhere(path) when the user or another plugin opens a source (the plugin's calls against the old one fail with stale), host.setSource({ … }) when the state changes, preset data such as host.stats or host.chapters for what the player would return.
A user opens other files while your plugin runs. A test that opens one file before startPlugin and never changes it misses a whole kind of bug: a plugin that read the state once (await plugin.player.read()) and later pauses, seeks or sets the speed with that old copy. Every control is conditional on the copy it was made with, so after the user changed the source the player refuses it with stale. Between activation and the command, change the source the way the user does:
| The user | In the test | The player's source stamp |
|---|---|---|
| opens a file after activation | startPlugin with nothing open, then host.openElsewhere(path) |
a new generation |
| opens another file | host.openElsewhere("/other.mp4") |
a new generation |
| opens the same file again | host.openElsewhere(samePath) |
a new generation |
| pauses, seeks, changes speed | await host.coreAction("play-pause").run() (or seek-forward, …) |
a new intent |
Each advances the stamp exactly as the player does, so a control made with an older copy is refused as in the player. host.setSource({ … }) is different: it changes what the player reports (playback reached 30 s, the file finished loading) without a new intent, as the player's own observations do.
const host = new TestHost();
host.openElsewhere("/movie.mp4");
const { stop } = await startPlugin(activate, { directory, host });
host.openElsewhere("/other.mp4"); // the user opens another file
await host.settle();
await host.command("rate-2").run(); // rejects if the player refused the control
assert.equal(host.source.rate, 2);
await stop();
A host error that nothing in the plugin handles (no try/catch, no plugin.onError) only reaches the player's log, so the user sees a command that does nothing. In TestHost it fails the test: the next host.settle() (also host.command(id).run()) rejects with it, or else stop(), and the message names the error. When the test means it, say so first with host.expectError("stale"); an expected error that never comes fails stop(). host.loggedErrors lists every such error, and host.strictErrors = false only logs them, as before SDK 1.11.2. The tests of the menus, adjust-panel, three-ways and bookmark examples run their commands after each of these changes.
host.handle(method, fn) replaces the player's answer to one protocol method until host.reset(method). Throw a HostError for a refusal, a plain Error for a lost reply:
host.handle("processing.replace", () => {
throw new HostError("unsupported", "no video stream");
});
host.calls lists every request in order, which shows the method name an action uses.
await host.settle() after every action that makes the plugin talk to the player; host.settle(400) also lets debounces and timers due by then fire. Then assert on what the host recorded: host.window(id).props (kebab-case names), host.command(id).state, host.osd, host.source.
settle does not wait for the once-a-second full update pass: after host.setSource(...) only the bindings that read a changed field of plugin.player.state run again. A value your plugin changed outside a handler (a timer, a promise continuation, a child process) without plugin.update() shows in the next full pass, at most about a second later; call plugin.update() in the plugin after such a change, or wait await host.settle(1000) in the test.
settle waits only for calls to the player. Work the plugin does on its own (reading or parsing a file, a child process, a network request) can finish later on a loaded machine; poll the value you assert on instead:
importWindow.emit("load-file");
for (let i = 0; i < 100 && importWindow.props["status-text"] !== expected; i++)
await host.settle(20);
assert.equal(importWindow.props["status-text"], expected);
await stop() disposes the plugin as the player does (scopes released, stores flushed) and removes the host's temporary files (a removal Windows refuses for a moment is retried, and never fails the test). stop({ reason: "idle" }) gives the stop reason the player would send, and disconnect() stops the plugin as when the player is lost (host-lost, requests refused). Each test creates its own host.
Copy host.dataDirectory after the first run and pass it as dataDirectory to the second, to check what the plugin stored and restores (Open a tool window and remember its state is a plugin that needs it). A restart within one player session (a reload, an upgrade, a crash) keeps plugin.storage.session too: pass the first run's host.sessionStorage as sessionStorage; leave it out to test a fresh player session.
import assert from "node:assert/strict";
import { test } from "node:test";
import { startPlugin } from "@vestra-player/sdk/testing";
import activate from "../main.ts";
test("goes on with the queue after a restart", async () => {
const first = await startPlugin(activate);
await first.plugin.storage.session.set("queue", ["a.mkv", "b.mkv"]);
const kept = first.host.sessionStorage;
await first.stop();
const second = await startPlugin(activate, { sessionStorage: kept });
assert.deepEqual(await second.plugin.storage.session.get("queue"), [
"a.mkv",
"b.mkv",
]);
await second.stop();
});
The test of the Bookmark example: it prepares the source before the plugin starts, opens the window from its command, fires two Slint callbacks and checks the window and the playback position.
test("mark the position, then jump back to it", async () => {
const host = new TestHost();
host.openElsewhere("/movie.mp4");
host.setSource({ duration: 600, position: 42 });
const { stop } = await startPlugin(activate, { directory, host });
await host.command("bookmark.open").run();
const bookmarkWindow = host.window("bookmark");
bookmarkWindow.emit("mark");
await host.settle();
assert.equal(bookmarkWindow.props.label, "Bookmark: 42.0 s");
host.setSource({ position: 300 }); // playback moves on
bookmarkWindow.emit("jump");
await host.settle();
assert.equal(host.source.position, 42);
await stop();
});
A key that does nothing while no video is open, and a refused change that is shown and can be retried:
import assert from "node:assert/strict";
import test from "node:test";
import { HostError, startPlugin } from "@vestra-player/sdk/testing";
import activate from "../main.ts";
test("the key does nothing while no video is open", async () => {
const { host, stop } = await startPlugin(activate, {
directory: new URL("..", import.meta.url),
});
assert.equal(await host.press("Ctrl+B"), "reason");
assert.deepEqual(host.osd, ["Open a video first"]);
host.openElsewhere("/movie.mp4");
await host.settle();
assert.equal(await host.press("Ctrl+B"), "ran");
assert.deepEqual(host.conflicts, []);
await stop();
});
test("a refused change is shown and can be retried", async () => {
const { host, stop } = await startPlugin(activate, {
directory: new URL("..", import.meta.url),
});
host.openElsewhere("/movie.mp4");
await host.command("picture").run();
host.handle("processing.replace", () => {
throw new HostError("unsupported", "no video stream");
});
host.window("picture").emit("adjust", "gamma", 20);
await host.settle();
assert.match(String(host.window("picture").props["status"]), /^Picture settings failed/);
host.reset("processing.replace");
host.window("picture").emit("retry");
await host.settle();
assert.equal(host.window("picture").props["status"], "Picture settings applied");
await stop();
});
keepsRunning, standby, triggers and the clock.startPlugin options, every way to act and simulate, what the host records, every member.TestHost and startPlugin.