Skip to content

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

6.0 KB
Raw Blame
import assert from "node:assert/strict";
import { EventEmitter } from "node:events";
import test from "node:test";
import { createDesktopActive, inView, installDesktopActive, localDay } from "./desktop-active.mjs";
import { createDesktopPrefs } from "./desktop-prefs.mjs";
import { createLaunchAnalytics } from "./launch-analytics.mjs";

/** One computer: its prefs file, what left it, and a calendar the test turns. Each `start()` is a launch of the app. */
function computer({ record = null, packaged = true, env = {}, answer = () => ({ ok: true }) } = {}) {
  let saved = record, date = "2026-11-29", shown = false;
  const sent = [];
  const start = () => {
    const prefs = createDesktopPrefs({ read: () => saved, write: value => { saved = structuredClone(value); } });
    const fetch = async (url, init) => { sent.push({ url, body: JSON.parse(init.body) }); return answer(); };
    const launch = createLaunchAnalytics({ fetch: async () => ({ ok: true }), choice: () => prefs.analytics(), installId: () => prefs.analyticsId(), lastVersion: null, version: "0.1.190", platform: "win32", packaged, env });
    const active = createDesktopActive({ fetch, installId: () => launch.installId(), visible: () => shown, day: { read: () => prefs.activeDay(), write: day => prefs.setActiveDay(day) },
      version: "0.1.190", platform: "win32", now: () => Date.UTC(2026, 10, 29, 12), today: () => date });
    return { prefs, check: async () => { active.check(); await new Promise(resolve => setImmediate(resolve)); } };
  };
  return { start, sent, saved: () => saved, show: (on = true) => { shown = on; }, day: next => { date = next; } };
}

test("a window in view sends one app_active under the install's id, with the version, the platform and the system only", async () => {
  const pc = computer();
  const app = pc.start();
  pc.show();
  await app.check();
  assert.equal(pc.sent.length, 1);
  const [{ url, body }] = pc.sent;
  assert.equal(url, "https://us.i.posthog.com/i/v0/e/");
  assert.equal(body.event, "app_active");
  assert.equal(body.distinct_id, app.prefs.analyticsId());
  assert.deepEqual(body.properties, { app_version: "0.1.190", platform: "desktop", surface: "main", $os: "Windows", $process_person_profile: false });
  assert.equal(body.timestamp, "2026-11-29T12:00:00.000Z");
});

test("one event per local day, however often it is checked and across restarts; a day that changes while the app runs sends the next day's", async () => {
  const pc = computer();
  pc.show();
  const first = pc.start();
  await first.check(); await first.check();
  const again = pc.start();
  await again.check();
  assert.equal(pc.sent.length, 1, "a restart on the same day sends nothing more");
  pc.day("2026-11-30");
  await again.check(); await again.check();
  assert.deepEqual(pc.sent.map(({ body }) => body.distinct_id), Array(2).fill(first.prefs.analyticsId()), "the same install both days");
  assert.equal(pc.saved().activeDay, "2026-11-30");
  await pc.start().check();
  assert.equal(pc.sent.length, 2);
});

test("a start at login out of sight sends nothing and makes no id until a window is shown", async () => {
  const pc = computer();
  const app = pc.start();
  await app.check();
  pc.day("2026-11-30");
  await app.check();
  assert.deepEqual([pc.sent.length, pc.saved()], [0, null]);
  pc.show();
  await app.check();
  assert.equal(pc.sent.length, 1);
  pc.show(false); pc.day("2026-12-01");
  await app.check();
  assert.equal(pc.sent.length, 1, "left running in the tray overnight is not a day of use");
});

test("with usage analytics off, in a development build or in CI nothing is sent and no id or day is stored", async () => {
  for (const options of [{ record: { analytics: false } }, { packaged: false }, { env: { CI: "true" } }, { env: { OMB_SMOKE_TEST: "1" } }]) {
    const pc = computer(options);
    pc.show();
    await pc.start().check();
    assert.equal(pc.sent.length, 0, JSON.stringify(options));
    assert.deepEqual(pc.saved(), options.record ?? null, JSON.stringify(options));
  }
  const pc = computer();
  pc.show();
  const app = pc.start();
  await app.check();
  app.prefs.setAnalytics(false);
  pc.day("2026-11-30");
  await app.check();
  assert.equal(pc.sent.length, 1, "switched off while running: nothing more");
  assert.deepEqual(pc.saved(), { seen: [], analytics: false });
});

test("a send that was not taken is tried again at the next check; one on its way is not sent twice", async () => {
  let ok = false;
  const pc = computer({ answer: () => { if (!ok) throw new Error("offline"); return { ok: true }; } });
  pc.show();
  const app = pc.start();
  await app.check();
  assert.equal(pc.saved().activeDay, undefined);
  ok = true;
  await app.check(); await app.check();
  assert.deepEqual([pc.sent.length, pc.saved().activeDay], [2, "2026-11-29"]);
});

test("the day is the computer's own calendar day; a window counts when shown and not minimized", () => {
  assert.equal(localDay(new Date(2026, 0, 5, 23, 59)), "2026-01-05");
  const win = (visible, minimized = false, destroyed = false) => ({ isVisible: () => visible, isMinimized: () => minimized, isDestroyed: () => destroyed });
  assert.equal(inView([]), false);
  assert.equal(inView([win(false), win(true, true), win(true, false, true)]), false);
  assert.equal(inView([win(false), win(true)]), true);
});

test("it is checked when a window is shown, restored or focused, and every ten minutes", () => {
  const app = Object.assign(new EventEmitter(), { getVersion: () => "0.1.190" });
  let checks = 0, every = 0, tick = null;
  const prefs = { activeDay: () => null, setActiveDay() {} };
  installDesktopActive({ app, session: { defaultSession: { fetch: async () => ({ ok: true }) } }, prefs, windows: () => { checks++; return []; },
    launch: { installId: () => "install-id" }, setInterval: (run, ms) => { tick = run; every = ms; return { unref() {} }; } });
  const win = new EventEmitter();
  app.emit("browser-window-created", {}, win);
  win.emit("show"); win.emit("restore"); app.emit("browser-window-focus"); tick();
  assert.deepEqual([checks, every], [4, 600_000]);
});