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 path from "node:path";
import { test } from "node:test";
import nativeText from "./native-text.cjs";
import { CONFIG_RELOAD_PATH, createSettingsFile, localPageShown } from "./settings-file.mjs";
const DATA = path.join(path.sep, "fake", "profile-data");
const FILE = path.join(DATA, "config.json");
const { t } = nativeText.englishText;
/** Every outside effect is a fake: no real file is opened and no server is asked. */
function harness({ here = true, exists = true, opened = "", answer = null, refuse = false } = {}) {
const calls = { openPath: [], shown: [], dialogs: [], requests: [], logs: [] };
const settingsFile = createSettingsFile({
here: () => (typeof here === "function" ? here() : here),
dataDir: () => DATA,
request: async (route, init) => {
calls.requests.push([route, init.method]);
if (refuse) throw new Error("not running");
return { json: async () => answer };
},
shell: {
openPath: async (file) => { calls.openPath.push(file); if (opened instanceof Error) throw opened; return opened; },
showItemInFolder: (file) => { calls.shown.push(file); },
},
dialog: { showMessageBox: async (options) => { calls.dialogs.push(options); return { response: 0 }; } },
fs: { existsSync: (file) => file === FILE && exists },
t,
log: (line) => calls.logs.push(line),
});
return { settingsFile, calls };
}
test("opens this computer's config.json with the system's app, and nothing else", async () => {
const { settingsFile, calls } = harness();
await settingsFile.open();
assert.deepEqual(calls.openPath, [FILE]);
assert.deepEqual(calls.shown, []);
assert.deepEqual(calls.dialogs, []);
});
test("shows the file in its folder when no app opens it", async () => {
for (const opened of ["No application knows how to open this file", new Error("spawn failed")]) {
const { settingsFile, calls } = harness({ opened });
await settingsFile.open();
assert.deepEqual(calls.openPath, [FILE]);
assert.deepEqual(calls.shown, [FILE]);
assert.deepEqual(calls.dialogs, []);
}
});
test("says there is no file yet rather than making one", async () => {
const { settingsFile, calls } = harness({ exists: false });
await settingsFile.open();
assert.deepEqual(calls.openPath, []);
assert.deepEqual(calls.shown, []);
assert.equal(calls.dialogs.length, 1);
assert.equal(calls.dialogs[0].message, "There's no settings file yet");
assert.match(calls.dialogs[0].detail, /config\.json/);
});
test("does nothing to a file while another place is on screen, and says why", async () => {
const { settingsFile, calls } = harness({ here: false });
await settingsFile.open();
await settingsFile.reload();
assert.deepEqual(calls.openPath, []);
assert.deepEqual(calls.requests, []);
assert.deepEqual(calls.dialogs.map((options) => options.message), [
"This place's settings file isn't on this computer",
"This place's settings file isn't on this computer",
]);
});
test("a reload that applied settings says which, and which wait for a restart", async () => {
const { settingsFile, calls } = harness({ answer: { ok: true, applied: ["features", "rooms"], restart: ["instances"] } });
await settingsFile.reload();
assert.deepEqual(calls.requests, [[CONFIG_RELOAD_PATH, "POST"]]);
assert.equal(calls.dialogs.length, 1);
assert.equal(calls.dialogs[0].type, "info");
assert.equal(calls.dialogs[0].message, "Settings reloaded");
assert.equal(calls.dialogs[0].detail, "Now in use: features, rooms\n\nQuit and reopen the app to use: instances");
});
test("a reload with nothing changed still says so in one dialog", async () => {
const { settingsFile, calls } = harness({ answer: { ok: true, applied: [], restart: [] } });
await settingsFile.reload();
assert.deepEqual(calls.dialogs.map((options) => [options.message, options.detail]), [["Settings reloaded", "Nothing in the settings file changed."]]);
});
test("a bad file is reported in plain words with its line, and the last good settings stay", async () => {
const error = "config.json line 12, column 5: unexpected } (a comma before it may be extra)";
const { settingsFile, calls } = harness({ answer: { ok: false, code: "json", error } });
await settingsFile.reload();
assert.equal(calls.dialogs.length, 1);
assert.equal(calls.dialogs[0].type, "warning");
assert.equal(calls.dialogs[0].message, "Settings not reloaded");
assert.ok(calls.dialogs[0].detail.startsWith(`${error}\n\n`));
assert.match(calls.dialogs[0].detail, /last settings that worked are still in use/);
});
test("a schema error, a missing file and a server that does not answer each get one dialog", async () => {
const schema = harness({ answer: { ok: false, code: "schema", error: "config.json: rooms.turnTimeoutMinutes Too big" } });
await schema.settingsFile.reload();
assert.match(schema.calls.dialogs[0].detail, /^config\.json: rooms\.turnTimeoutMinutes Too big/);
const missing = harness({ answer: { ok: false, code: "missing", error: "There is no config.json on this computer yet." } });
await missing.settingsFile.reload();
assert.deepEqual(missing.calls.dialogs.map((options) => options.message), ["There's no settings file yet"]);
const down = harness({ refuse: true });
await down.settingsFile.reload();
assert.equal(down.calls.dialogs.length, 1);
assert.match(down.calls.dialogs[0].detail, /isn't answering/);
assert.equal(down.calls.logs.length, 1);
});
test("only This computer's own page counts as here", () => {
const local = { remoteAccess: false, localOrigin: "http://127.0.0.1:8799", homePath: "/__home" };
assert.equal(localPageShown({ ...local, pageUrl: "http://127.0.0.1:8799/chat/abc" }), true);
assert.equal(localPageShown({ ...local, pageUrl: "http://127.0.0.1:8799/__home" }), false, "the screen shown while a Cloud wakes");
assert.equal(localPageShown({ ...local, pageUrl: "https://omb-u-1.fly.dev/" }), false, "My Cloud");
assert.equal(localPageShown({ ...local, pageUrl: "https://team.example.test/" }), false, "a team's Cloud");
assert.equal(localPageShown({ ...local, pageUrl: "" }), false, "nothing loaded yet");
assert.equal(localPageShown({ ...local, pageUrl: "http://127.0.0.1:8799/", remoteAccess: true }), false, "a remote client");
});
test("the menu is built again only when here changes", () => {
let here = true;
const { settingsFile } = harness({ here: () => here });
assert.equal(settingsFile.flipped(), true, "not built yet");
assert.equal(settingsFile.available(), true);
assert.equal(settingsFile.flipped(), false);
here = false;
assert.equal(settingsFile.flipped(), true);
assert.equal(settingsFile.available(), false);
assert.equal(settingsFile.flipped(), false);
});