Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { afterEach, describe, expect, it } from "vitest";
import {
FOLLOWING,
forgetTranscriptPositions,
initialTranscriptFollow,
readTranscriptPosition,
rememberTranscriptPosition,
rowsInView,
transitionTranscriptFollow,
wantsJumpToLatest,
type TranscriptFollow,
type TranscriptFollowEvent,
} from "./transcript-follow";
const run = (from: TranscriptFollow, ...events: TranscriptFollowEvent["type"][]) =>
events.reduce((state, type) => transitionTranscriptFollow(state, { type } as TranscriptFollowEvent), from);
afterEach(() => forgetTranscriptPositions());
describe("following a transcript's end", () => {
it("opens following, or released when the reader left the conversation away from its end", () => {
expect(initialTranscriptFollow(true)).toBe(FOLLOWING);
expect(initialTranscriptFollow(false)).toEqual({ mode: "released", atEnd: false });
});
it("lets go when the reader navigates, and comes back only after leaving the end and returning", () => {
const released = run(FOLLOWING, "navigated");
expect(released).toEqual({ mode: "released", atEnd: true });
expect(run(released, "reachedEnd"), "a scroll key at the end cannot re-arm").toBe(released);
expect(run(released, "leftEnd")).toEqual({ mode: "released", atEnd: false });
expect(run(released, "leftEnd", "reachedEnd")).toBe(FOLLOWING);
});
it("ignores the end band flickering while it follows a growing reply", () => {
expect(run(FOLLOWING, "leftEnd", "reachedEnd", "leftEnd")).toBe(FOLLOWING);
});
it("follows again whenever the person asks for the end: a sent message or the latest button", () => {
expect(run(FOLLOWING, "navigated", "leftEnd", "followEnd")).toBe(FOLLOWING);
});
it("offers the way down only once the reader is released away from the end", () => {
expect(wantsJumpToLatest(FOLLOWING)).toBe(false);
expect(wantsJumpToLatest(run(FOLLOWING, "navigated"))).toBe(false);
expect(wantsJumpToLatest(run(FOLLOWING, "navigated", "leftEnd"))).toBe(true);
});
});
describe("remembered positions", () => {
it("keeps each conversation's own, the newest hundred", () => {
rememberTranscriptPosition("a", { atEnd: false, rowId: "m3", offsetWithinRow: 12 });
rememberTranscriptPosition("b", { atEnd: true });
expect(readTranscriptPosition("a")).toEqual({ atEnd: false, rowId: "m3", offsetWithinRow: 12 });
expect(readTranscriptPosition("b")).toEqual({ atEnd: true });
for (let index = 0; index < 100; index++) rememberTranscriptPosition(`k${index}`, { atEnd: true });
expect(readTranscriptPosition("a")).toBeUndefined();
expect(readTranscriptPosition("k99")).toEqual({ atEnd: true });
});
});
describe("rows held across a window step", () => {
const view = { getBoundingClientRect: () => ({ top: 100, bottom: 500 }) } as unknown as HTMLElement;
const list = (tops: [string, number][]) => ({
querySelectorAll: () => tops.map(([mid, top]) => ({ dataset: { mid }, getBoundingClientRect: () => ({ top, bottom: top + 150, height: 150 }) })),
}) as unknown as HTMLElement;
it("offers rows wholly on screen first and the window's first row last, skipping the pinned card and live rows", () => {
const rows = rowsInView(view, list([["first", 40], ["card", 60], ["a", 190], ["live:x", 250], ["b", 340], ["c", 490], ["below", 520]]), "card");
expect(rows.map((row) => row.rowId)).toEqual(["a", "b", "c", "first"]);
expect(rows[0]).toEqual({ rowId: "a", offsetWithinRow: -90 });
});
});