Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { followBottomGrowth, haltBottomChase } from "./bottom-follow";
let now = 1_000;
let pending = new Map<number, FrameRequestCallback>();
let nextId = 1;
beforeEach(() => {
now = 1_000;
pending = new Map();
nextId = 1;
vi.spyOn(performance, "now").mockImplementation(() => now);
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
pending.set(nextId, callback);
return nextId++;
});
vi.stubGlobal("cancelAnimationFrame", (id: number) => void pending.delete(id));
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
function frame(ms: number) {
now += ms;
const batch = [...pending.values()];
pending.clear();
for (const callback of batch) callback(now);
}
/** A scroller that snaps every scrollTop write the way a browser does, and clamps it to its end. */
function scrollerWith(snap: (top: number) => number, { scrollHeight = 1_100, clientHeight = 500, scrollTop = 500 } = {}) {
let top = scrollTop;
const scroller = {
scrollHeight,
clientHeight,
calls: [] as ScrollToOptions[],
get scrollTop() {
return top;
},
set scrollTop(value: number) {
top = Math.max(0, Math.min(snap(value), scroller.scrollHeight - scroller.clientHeight));
},
scrollTo(options: ScrollToOptions = {}) {
scroller.calls.push(options);
if (typeof options.top === "number") scroller.scrollTop = options.top;
},
};
return scroller;
}
const SNAPS = {
"whole pixels": Math.round,
"half pixels": (value: number) => Math.round(value * 2) / 2,
"whole pixels, rounded down": Math.floor,
};
describe("followBottomGrowth", () => {
it("scrolls resized content to its new bottom while following", () => {
const calls: ScrollToOptions[] = [];
const scroller = {
scrollHeight: 1_240,
scrollTo: (options: ScrollToOptions) => calls.push(options),
};
expect(followBottomGrowth(scroller, true)).toBe(true);
expect(calls).toEqual([{ top: 1_240 }]);
});
it("falls back to a smooth scroll where the scroller has no layout to read", () => {
const calls: ScrollToOptions[] = [];
const scroller = {
scrollHeight: 1_240,
scrollTo: (options: ScrollToOptions) => calls.push(options),
};
expect(followBottomGrowth(scroller, true, true)).toBe(true);
expect(calls).toEqual([{ top: 1_240, behavior: "smooth" }]);
});
it("preserves scrollback when bottom-follow is detached", () => {
const calls: ScrollToOptions[] = [];
const scroller = {
scrollHeight: 1_240,
scrollTo: (options: ScrollToOptions) => calls.push(options),
};
expect(followBottomGrowth(scroller, false)).toBe(false);
expect(calls).toEqual([]);
});
for (const [name, snap] of Object.entries(SNAPS)) {
for (const [hz, ms] of [[60, 1000 / 60], [120, 1000 / 120]] as const) {
for (const dpr of [1, 2]) {
it(`glides all the way to the end on a scroller that snaps to ${name}, at ${hz} Hz and ${dpr}x, then stops asking for frames`, () => {
vi.stubGlobal("window", { devicePixelRatio: dpr });
const scroller = scrollerWith(snap);
scroller.scrollHeight += 37;
followBottomGrowth(scroller, true, true);
const tops: number[] = [];
for (let i = 0; i < 400 && pending.size; i++) {
frame(ms);
tops.push(scroller.scrollTop);
}
expect(pending.size, "no frame left asked for").toBe(0);
expect(scroller.scrollTop).toBe(637);
expect(tops.length, "a glide, not a jump").toBeGreaterThan(hz === 60 ? 4 : 8);
expect(tops.length, "and not a long one").toBeLessThan(hz === 60 ? 40 : 80);
expect(tops, "only ever down").toEqual([...tops].sort((a, b) => a - b));
});
}
}
}
it("takes growth during a glide into the same glide", () => {
const scroller = scrollerWith(Math.round);
scroller.scrollHeight += 40;
followBottomGrowth(scroller, true, true);
frame(16);
expect(scroller.scrollTop).toBeGreaterThan(500);
scroller.scrollHeight += 40;
followBottomGrowth(scroller, true, true);
expect(pending.size, "one glide").toBe(1);
for (let i = 0; i < 400 && pending.size; i++) frame(16);
expect(pending.size).toBe(0);
expect(scroller.scrollTop).toBe(680);
});
it("glides without a jump for as long as the stream grows, and ends after it ends", () => {
const scroller = scrollerWith((value) => Math.round(value * 2) / 2, { clientHeight: 5_000, scrollHeight: 5_500 });
for (let i = 0; i < 400; i++) {
scroller.scrollHeight += 6;
followBottomGrowth(scroller, true, true);
frame(1000 / 120);
}
expect(scroller.calls, "never landed by a jump while growing").toEqual([]);
for (let i = 0; i < 200 && pending.size; i++) frame(1000 / 60);
expect(pending.size).toBe(0);
expect(scroller.scrollTop).toBe(scroller.scrollHeight - scroller.clientHeight);
});
it("lands at once on a jump bigger than most of the view, with no glide", () => {
const scroller = scrollerWith(Math.round);
scroller.scrollHeight += 2_000;
followBottomGrowth(scroller, true, true);
expect(scroller.calls).toEqual([{ top: 3_100 }]);
expect(scroller.scrollTop).toBe(2_600);
expect(pending.size).toBe(0);
});
it("lands at once when a jump arrives during a glide", () => {
const scroller = scrollerWith(Math.round);
scroller.scrollHeight += 40;
followBottomGrowth(scroller, true, true);
frame(16);
scroller.scrollHeight += 2_000;
followBottomGrowth(scroller, true, true);
expect(pending.size).toBe(0);
expect(scroller.scrollTop).toBe(scroller.scrollHeight - scroller.clientHeight);
});
it("ends on the frame a write does not move the scroller, at the end", () => {
const scroller = scrollerWith(() => 500);
scroller.scrollHeight += 40;
followBottomGrowth(scroller, true, true);
frame(16);
expect(pending.size).toBe(0);
expect(scroller.calls).toEqual([{ top: 1_140 }]);
});
for (const [who, move] of [["the reader scrolls up", -20], ["something else scrolls it down", 15]] as const) {
it(`lets go at once when ${who}, and never writes again`, () => {
const scroller = scrollerWith(Math.round, { scrollHeight: 1_200 });
scroller.scrollHeight += 40;
followBottomGrowth(scroller, true, true);
frame(16);
const theirs = scroller.scrollTop + move;
scroller.scrollTop = theirs;
frame(16);
expect(scroller.scrollTop).toBe(theirs);
expect(pending.size).toBe(0);
expect(scroller.calls).toEqual([]);
});
}
it("stops a glide when halted, and when growth arrives while not following", () => {
const scroller = scrollerWith(Math.round);
scroller.scrollHeight += 40;
followBottomGrowth(scroller, true, true);
haltBottomChase(scroller);
expect(pending.size).toBe(0);
followBottomGrowth(scroller, true, true);
expect(followBottomGrowth(scroller, false, true)).toBe(false);
expect(pending.size).toBe(0);
expect(scroller.scrollTop).toBe(500);
});
it("asks for nothing when already at the end", () => {
const scroller = scrollerWith(Math.round, { scrollHeight: 1_000 });
followBottomGrowth(scroller, true, true);
expect(pending.size).toBe(0);
expect(scroller.calls).toEqual([]);
});
});