Open beta. Rin Chat is under active development. Expect rough edges, and please tell us what breaks.

Skip to content
Extension guide contents

Testing

rinext test <folder> [test-file]

rinext test runs your extension folder in Rin Chat's own extension runtime: the same chat runtime that starts extensions and calls their hooks, the same broker that checks every rin.* call, and the same QuickJS sandbox, with the app's limits (1000 ms per turn hook or command, 100 ms of CPU per call, 32 MB of heap, 4 injections a turn, 16 commands, and every other number in Limits and errors). What is fake is only what the rest of the app would supply: the chat, the user's choices, storage, the model, and the network, each kept to the app's own rules.

The folder is read the way Rin Chat reads a developer folder: the manifest under every rule, then the entry. A folder the app would refuse is refused here with the same sentence.

Without a test file

rinext test extension

Opens a sample chat (a card named Rin and two messages), runs one turn, closes the chat, and prints the commands the extension registered, the text it injected, and its log. A load error or a failing hook is a failure.

A test file

A test file is an ES module outside the extension folder (a package carries one script, so a test inside it would refuse the package). Every exported function is one test. Each is called with a fresh harness:

// rinext test extension test/mood.test.mjs
import assert from "node:assert/strict";

export async function remindsTheModel({ open, storage }) {
  storage.saveSettings({ style: "short" });             // what the user saved in the settings form
  const chat = await open({ vars: { mood: "tired" } });   // a chat with the extension turned on
  const [line] = await chat.turn();                     // onBeforeAssemble, then what the prompt gets
  assert.match(line.text, /tired/);
  assert.equal(await chat.command("/mood happy"), true);
  assert.equal(chat.vars.mood, "happy");
}

A test fails when it throws, when it runs longer than 30 seconds, or when any hook failed (chat.errors) and the test did not empty that list. Every chat a test opened is closed after it (so onChatClose runs and is checked too). The output is one line per test and a count; the exit code is 1 when any failed.

To expect a hook failure, check it and clear the list:

assert.deepEqual(chat.errors, ["onCommand: threw: Error: no such mood"]);
chat.errors.length = 0;

The harness

MemberWhat it is
open(chat?)Opens a chat with the extension turned on for it: the script loads, then onChatOpen runs. chat may give messages ({ role, name, text }), cards ({ id, data }) and vars; each defaults to the sample chat's. Resolves a chat (below).
storageThe extension's storage and settings, on the app's rules (below). Lasts across open calls within one test.
modelThe fake model behind rin.model.run (below). One per test, so a budget spans every chat it opens.
netThe fake network (below).
manifestThe manifest as Rin Chat parsed it.
p6branges (the user's gen.params ranges, none by default), themes (every ui.theme offer, null for a withdrawal), generateLeft (swipes and continues left today, 20).

The chat

MemberWhat it does
turn({ continuation?, asUser? })One turn: onBeforeAssemble, then resolves what the prompt gets, [{ text, depth, role, name }]. text is wrapped inert ([code]...[/code]) as it is sent; name is the inspector's label.
command(text)The user sends text ("/roll 2d6"). Resolves false when no command of the extension matches.
panel(id, on?)A click on a panel button, or a toggle with its new state (onPanel).
widget(post, kind, value)A click on a widget in one of its posts (onWidget).
assembled(messages)A request's assembled prompt through onAssembled; resolves the messages as sent, or null when unchanged.
replyDone(text)A finished reply through onReplyDone; resolves { text, by }, the text saved and the extensions that changed it.
streaming(text)A streaming chunk through onReply (read only, throttled as in the app).
macros()Resolves every registered {{ext.<name>}} as for a turn: { "ext.name": "[code]value[/code]" }.
restyle(text, role?)How a message would be shown after onRender (or the text unchanged).
params()The gen.params request for the next turn as it would be sent, consumed like a turn, or null.
ui()What Rin Chat would draw now: { panels, hud, notes, choices }, or null when nothing. Notes are keyed by message index from the start. Read it after awaiting the call that changed it.
style(){ cls, css }: the ui.style CSS as given (not sanitized; that needs a browser).
messages, varsThe chat's messages and variables, live: hooks read them, and a test may change them between calls.
postsWhat rin.chat.send posted, { id, ext, name, text }, with the text made inert as the chat stores it.
noticesNotices the chat would show (ui.notify, the failure pause, a command that did not finish).
logs()The extension's log lines.
commands()The extension's slash commands, [{ cmd, desc }].
draftThe composer's text after composer.draft.
spoken, sounds, generatedWhat voice.speak read, which sounds audio.play started (checked like the app: in the folder, a real WAV, OGG or MP3, at most 1 MiB), and the chat.generate requests.
runningWhether the script loaded and onChatOpen ran.
errorsEvery hook or load that failed, as "<hook>: <code>[: message]".
close()Closes the chat (onChatClose).

Storage and settings

storage holds the extension's rows in memory with the app's rules: keys of 1 to 128 bytes, no rin: prefix, values of at most 256 KiB, a 1 MiB quota and 1024 keys.

MemberWhat it does
storage.saveSettings(values)What the user saved in the settings form. Strict like the app: a key the form does not have, or a value that does not fit its field, throws.
storage.settings()Every field's current value, as rin.settings.get() returns it.
storage.seed(values)Rows as if the extension had stored them earlier: seed({ count: 3 }).
storage.rowsA Map of key to stored JSON text.
// The template's Mood Tracker counts the chats it opened in storage.
export async function countsFromWhatWasStored({ open, storage }) {
  storage.seed({ opened: 41 });
  await open();
  assert.equal(JSON.parse(storage.rows.get("opened")), 42);
}

The model

model stands in for the provider behind rin.model.run, through Rin Chat's own budget code, so budget, no_model and model_failed happen exactly as in the app.

MemberWhat it does
model.reply(request) => string (or a promise): the model's answer. Throw to fail the call (model_failed). Default: "OK".
model.callsEvery request the provider received: { messages, maxTokens, temperature }.
model.budget{ requests, tokens } per day, as the user set them; default { requests: 20, tokens: 20000 }.
model.pickedfalse is "no model picked for this extension" (no_model). Default true.
model.used(id)Today's { requests, tokens } counted for the extension.
// The Story Summarizer example.
export async function stopsAtTheBudget({ open, model, manifest }) {
  model.budget = { requests: 1, tokens: 20000 };
  const chat = await open();
  await chat.command("/summarize");
  await chat.command("/summarize");
  assert.equal(model.calls.length, 1);
  assert.equal(model.used(manifest.id).requests, 1);
}

The network

net stands in for Rin Chat's network core and the server, on the app's rules: the levels, Block network for all extensions, net_needs_approval from a turn hook, declared hosts, HTTPS only, no IP literals, the methods, the forbidden headers, the body caps, local services, the rate limit and the three-denials block. The review dialog and the network log are the app's own code.

MemberWhat it does
net.level"blocked" (the default, as for a fresh install), "ask" or "allowed". Setting it clears the denial streak.
net.blockAllBlock network for all extensions.
net.allowedA Set of hosts the user allowed (for any place). "Always allow" adds to it.
net.localA Set of local services as "host:port" ("localhost:8123").
net.review(plan) => "deny" | "once" | "always": the user at the review dialog. Default "once".
net.respond(plan) => ({ status?, headers?, body? }): the server. Default 404 with an empty body.
net.sentEvery request that reached the server: { method, url, host, headers, body }.
net.log()The extension's network log, every attempt with its decision.
net.confirmOpen(url) => boolean: the user's answer to rin.open.url. Default true.
net.openedURLs that went to the browser.
// The Weather Report example.
export async function askEveryTimeShowsTheRequestFirst({ open, net }) {
  net.level = "ask";
  const seen = [];
  net.review = (plan) => (seen.push(plan), "once");
  net.respond = () => ({ status: 200, body: "Lisbon: sunny +18°C
" });
  const chat = await open();
  await chat.command("/weather Lisbon");
  assert.equal(seen[0].url, "https://wttr.in/Lisbon?format=3");
  assert.equal(chat.posts[0].text, "Weather: Lisbon: sunny +18°C");
  assert.equal(net.log().at(-1).decision, "allowed_once");
}

A request from a turn hook to a host not in net.allowed rejects with net_needs_approval and is logged with the decision "needs_approval", as in the app.

Differences from the app

  • No DNS, so the private-address refusals cannot happen in a test.
  • ui.style CSS is recorded as given; the sanitizer needs a browser. Preview it in the card editor's Chat style tab.
  • The extension is installed as unreviewed and turned on for the one chat each open makes; name labels carry "(Unreviewed)".
  • voice.speak treats the user's voice engine as a local one; sounds are recorded, not played.
  • In one process an interpreter wedged inside a single native call cannot be killed. The CPU budget still stops a busy loop, and a hook that never answers still times out.
  • Each test file runs on its own: pass one file per rinext test run.