Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

4.5 KB
Raw Blame
// The app's own window on its own server, read as signed out (local-sign-in-repair.mjs): its ended session cookie goes and
// the page loads again, at most once a minute, and only while the server is the app's and ready.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";

import { installHomeWindowIpc } from "./home-window.mjs";
import { createLocalSignInRepair, LOCAL_REPAIR_EVERY_MS } from "./local-sign-in-repair.mjs";
import { CLOUD, LOCAL, world as homeWorld } from "./fixtures/home-window-world.mjs";

const ORIGIN = "http://127.0.0.1:8799";

function world({ ready = true, jar = ["omb_session_8799_3f2aabcdef01", "omb_session_8800_3f2aabcdef01", "theme"], open = true } = {}) {
  const w = { ready, clock: 1_000, jar: [...jar], removed: [], reloads: 0, open };
  const win = { isDestroyed: () => !w.open, webContents: { reload: () => { w.reloads++; } } };
  w.repair = createLocalSignInRepair({
    ready: () => w.ready,
    origin: () => ORIGIN,
    port: () => 8799,
    cookies: () => ({
      get: async ({ url }) => { assert.equal(url, ORIGIN); return w.jar.map((name) => ({ name })); },
      remove: async (url, name) => { assert.equal(url, ORIGIN); w.removed.push(name); w.jar = w.jar.filter((other) => other !== name); },
    }),
    window: () => win,
    now: () => w.clock,
  });
  return w;
}

test("drops this server's ended session cookie, keeps another port's and other cookies, and loads the page again", async () => {
  const w = world();
  await w.repair();
  assert.deepEqual(w.removed, ["omb_session_8799_3f2aabcdef01"]);
  assert.deepEqual(w.jar, ["omb_session_8800_3f2aabcdef01", "theme"]);
  assert.equal(w.reloads, 1);
});

test("with no cookie (the page loaded before the server had the window's token), it only loads the page again", async () => {
  const w = world({ jar: [] });
  await w.repair();
  assert.deepEqual(w.removed, []);
  assert.equal(w.reloads, 1);
});

test("never loops: a second ask within the minute is refused, so the pairing page shows", async () => {
  const w = world();
  await w.repair();
  w.clock += LOCAL_REPAIR_EVERY_MS - 1;
  await assert.rejects(w.repair(), /already tried/);
  assert.equal(w.reloads, 1);
  w.clock += 1;
  await w.repair();
  assert.equal(w.reloads, 2);
});

test("refused, touching nothing, while the server is not the app's own or not ready", async () => {
  const w = world({ ready: false });
  await assert.rejects(w.repair(), /not ready/);
  assert.deepEqual(w.removed, []);
  assert.equal(w.reloads, 0);
  w.ready = true;
  await w.repair();
  assert.equal(w.reloads, 1, "a refusal does not use up the minute");
});

test("a closed window is not reloaded", async () => {
  const w = world({ open: false });
  await w.repair();
  assert.equal(w.reloads, 0);
});

test("the window's IPC: this computer's own page, read as signed out, asks the app to sign it in again", async () => {
  const w = homeWorld();
  await w.home.show();
  const handlers = new Map(), repaired = [];
  const install = (repairLocal) => installHomeWindowIpc({ ipcMain: { handle: (channel, handler) => handlers.set(channel, handler), on: () => {} },
    homeWindow: w.home, cloudPage: () => null, repairLocal });
  install(async () => { repaired.push("local"); });
  const frame = w.contents.mainFrame, page = { sender: w.contents, senderFrame: frame };
  w.url = `${LOCAL}/`;
  await handlers.get("home:repair")(page);
  assert.deepEqual(repaired, ["local"]);
  assert.throws(() => handlers.get("home:repair")({ sender: {}, senderFrame: frame }), /only available/, "another window");
  w.url = "https://other.example.test/";
  assert.throws(() => handlers.get("home:repair")(page), /only available/, "a page from elsewhere");
  w.url = `${CLOUD}/`;
  await handlers.get("home:repair")(page);
  assert.deepEqual(repaired, ["local"], "My Cloud's page still renews its own session");
  install(undefined);
  w.url = `${LOCAL}/`;
  assert.throws(() => handlers.get("home:repair")(page), /only available/, "without a local repair, as before");
});

test("main.mjs hands the window's IPC this repair, only for the app's own packaged server once it is ready", () => {
  const main = readFileSync(new URL("./main.mjs", import.meta.url), "utf8");
  assert.match(main, /installHomeWindowIpc\(\{[^\n]*?repairLocal: createLocalSignInRepair\(\{ ready: \(\) => app\.isPackaged && !desktopRemoteAccess && serverReady, origin: rendererOrigin, port: \(\) => SERVER_PORT, cookies: \(\) => session\.defaultSession\.cookies, window: \(\) => mainWindow,/);
});