Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import assert from "node:assert/strict";
import test from "node:test";
import { BOAT_SIGN_IN_CREDENTIAL, boatSignInEnv, createBoatSignInStore, decodeBoatSignInStoreRequest } from "./boat-sign-in-store.mjs";
const REQUEST = "openmausbot:boat-sign-in-store";
const settled = () => new Promise((done) => setImmediate(done));
test("only a packaged app hands the connection to its server, and says so even when there is none", () => {
assert.deepEqual(boatSignInEnv({ [BOAT_SIGN_IN_CREDENTIAL]: "{\"v\":1}" }, true), { OMB_BOAT_SIGN_IN: "{\"v\":1}" });
assert.deepEqual(boatSignInEnv({}, true), { OMB_BOAT_SIGN_IN: "" });
assert.deepEqual(boatSignInEnv({ [BOAT_SIGN_IN_CREDENTIAL]: "{\"v\":1}" }, false), {});
});
test("a request is one read or one write of a bounded string, and nothing else", () => {
assert.deepEqual(decodeBoatSignInStoreRequest({ data: { type: REQUEST, requestId: "a1", op: "read" } }), { requestId: "a1", op: "read" });
assert.deepEqual(decodeBoatSignInStoreRequest({ type: REQUEST, requestId: "a1", op: "write", value: null }), { requestId: "a1", op: "write", value: null });
for (const bad of [null, {}, { type: "other", requestId: "a", op: "read" }, { type: REQUEST, requestId: "bad id", op: "read" }, { type: REQUEST, requestId: "a", op: "erase" },
{ type: REQUEST, requestId: "a", op: "write" }, { type: REQUEST, requestId: "a", op: "write", value: "" }, { type: REQUEST, requestId: "a", op: "write", value: { v: 1 } },
{ type: REQUEST, requestId: "a", op: "write", value: "x".repeat(16_385) }]) {
assert.equal(decodeBoatSignInStoreRequest(bad), null);
}
});
test("writes go through the app's one encrypted writer and are answered only after it; nothing secret is logged", async () => {
let document = { boxToken: "boat_key" };
const logged = [];
const posted = [];
const store = createBoatSignInStore({
packaged: true, read: () => document, log: (line) => logged.push(line),
update: async (derive) => { document = derive({ ...document }); },
});
const proc = { postMessage: (message) => posted.push(message) };
assert.equal(store.receive(proc, { type: "openmausbot:phone-secret-save" }), false);
assert.equal(store.receive(proc, { data: { type: REQUEST, requestId: "w1", op: "write", value: "{\"refresh\":\"boatrt_secret\"}" } }), true);
await settled();
assert.deepEqual(document, { boxToken: "boat_key", [BOAT_SIGN_IN_CREDENTIAL]: "{\"refresh\":\"boatrt_secret\"}" });
assert.deepEqual(posted.pop(), { type: "openmausbot:boat-sign-in-store-result", requestId: "w1", ok: true });
store.receive(proc, { type: REQUEST, requestId: "r1", op: "read" });
await settled();
assert.deepEqual(posted.pop(), { type: "openmausbot:boat-sign-in-store-result", requestId: "r1", ok: true, value: "{\"refresh\":\"boatrt_secret\"}" });
store.receive(proc, { type: REQUEST, requestId: "w2", op: "write", value: null });
await settled();
assert.deepEqual(document, { boxToken: "boat_key" });
assert.equal(posted.pop().ok, true);
const failing = createBoatSignInStore({ packaged: true, read: () => document, log: (line) => logged.push(line), update: async () => { throw new Error("The operating-system credential store is unavailable"); } });
failing.receive(proc, { type: REQUEST, requestId: "w3", op: "write", value: "{\"refresh\":\"boatrt_other\"}" });
await settled();
assert.deepEqual(posted.pop(), { type: "openmausbot:boat-sign-in-store-result", requestId: "w3", ok: false });
assert.equal(logged.length, 1);
assert.ok(!logged.join("\n").includes("boatrt_"));
});
test("an app run from source keeps nothing here: its server uses its own owner-only file", async () => {
const posted = [];
const store = createBoatSignInStore({ packaged: false, read: () => ({}), update: async () => { throw new Error("never"); }, log: () => {} });
store.receive({ postMessage: (message) => posted.push(message) }, { type: REQUEST, requestId: "w1", op: "write", value: "{}" });
await settled();
assert.deepEqual(posted, [{ type: "openmausbot:boat-sign-in-store-result", requestId: "w1", ok: false }]);
});