Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { describe, expect, it } from "vitest";
import { hostTimeZone, takesTurnClock, turnClockLine, withTrailingTurnClock, withTurnClock } from "./turn-clock.ts";
const at = (iso: string) => Date.parse(iso);
describe("turnClockLine", () => {
it("names the weekday, local date and time, IANA timezone and UTC offset", () => {
expect(turnClockLine(at("2026-10-08T16:48:00Z"), "Asia/Baghdad"))
.toBe("Current time when this message was sent: Thursday, 2026-10-08 19:48 Asia/Baghdad (Iraq time, UTC+03:00).");
expect(turnClockLine(at("2026-10-08T16:48:00Z"), "Asia/Kolkata"))
.toBe("Current time when this message was sent: Thursday, 2026-10-08 22:18 Asia/Kolkata (India time, UTC+05:30).");
expect(turnClockLine(at("2026-10-08T16:48:00Z"), "UTC"))
.toBe("Current time when this message was sent: Thursday, 2026-10-08 16:48 UTC (UTC+00:00).");
});
it("follows daylight saving and writes a western offset with an ASCII minus", () => {
expect(turnClockLine(at("2026-01-15T17:05:00Z"), "America/New_York"))
.toBe("Current time when this message was sent: Thursday, 2026-01-15 12:05 America/New_York (Eastern time, UTC-05:00).");
expect(turnClockLine(at("2026-07-15T16:05:00Z"), "America/New_York"))
.toBe("Current time when this message was sent: Wednesday, 2026-07-15 12:05 America/New_York (Eastern time, UTC-04:00).");
});
it("uses the local calendar day, not the UTC one, near midnight", () => {
// 22:30 UTC on the 8th is already Friday the 9th in Baghdad
expect(turnClockLine(at("2026-10-08T22:30:00Z"), "Asia/Baghdad")).toContain("Friday, 2026-10-09 01:30 Asia/Baghdad");
// 03:00 UTC on the 9th is still Thursday the 8th in Los Angeles
expect(turnClockLine(at("2026-10-09T03:00:00Z"), "America/Los_Angeles")).toContain("Thursday, 2026-10-08 20:00 America/Los_Angeles");
expect(turnClockLine(at("2026-10-08T21:00:00Z"), "Asia/Baghdad")).toContain("Friday, 2026-10-09 00:00");
});
it("falls back to UTC for a timezone the runtime does not know, instead of failing the turn", () => {
expect(turnClockLine(at("2026-10-08T16:48:00Z"), "Nowhere/Atlantis"))
.toBe("Current time when this message was sent: Thursday, 2026-10-08 16:48 UTC (UTC+00:00).");
});
it("reads the host timezone the routine scheduler uses", () => {
expect(hostTimeZone()).toBe(Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC");
});
it("costs a few dozen tokens at most", () => {
const longest = turnClockLine(at("2026-09-30T16:48:00Z"), "America/Argentina/ComodRivadavia");
expect(Math.ceil(Buffer.byteLength(longest) / 4)).toBeLessThanOrEqual(36);
});
});
describe("withTurnClock", () => {
const clock = turnClockLine(at("2026-10-08T16:48:00Z"), "Asia/Baghdad");
it("puts the line in front of the turn, ahead of any recalled block", () => {
expect(withTurnClock(clock, "what is on my calendar today?")).toBe(`${clock}\n\nwhat is on my calendar today?`);
const recalled = "Recalled for this message …\n[end of recalled passages — the message follows]\n\nhi";
expect(withTurnClock(clock, recalled).startsWith(`${clock}\n\nRecalled for this message`)).toBe(true);
});
it("leaves an image-only turn and an engine slash command untouched", () => {
expect(withTurnClock(clock, "")).toBe("");
expect(withTurnClock(clock, " \n")).toBe(" \n");
expect(withTurnClock(clock, "/compact")).toBe("/compact");
expect(withTurnClock(clock, " /review the diff")).toBe(" /review the diff");
});
it("decides on the person's own text, not on the context put in front of it", () => {
expect(takesTurnClock("what day is it?")).toBe(true);
expect(takesTurnClock("")).toBe(false);
expect(takesTurnClock("/compact")).toBe(false);
const replayed = "Earlier in this conversation:\nuser: hi\n\n/compact";
expect(withTurnClock(takesTurnClock("/compact") ? clock : "", replayed)).toBe(replayed);
expect(withTurnClock(takesTurnClock("") ? clock : "", "Earlier in this conversation:\nuser: hi")).toBe("Earlier in this conversation:\nuser: hi");
});
it("stamps each turn of a long conversation with its own time", () => {
const first = withTurnClock(turnClockLine(at("2026-10-08T20:59:00Z"), "Asia/Baghdad"), "remind me tomorrow");
const second = withTurnClock(turnClockLine(at("2026-10-08T21:01:00Z"), "Asia/Baghdad"), "and what day is it now?");
expect(first).toContain("Thursday, 2026-10-08 23:59 Asia/Baghdad");
expect(second).toContain("Friday, 2026-10-09 00:01 Asia/Baghdad");
expect(second).not.toContain("2026-10-08");
});
});
describe("withTrailingTurnClock", () => {
const clock = turnClockLine(at("2026-10-08T16:48:00Z"), "Asia/Baghdad");
it("moves a leading clock line to the end, so the first line is the person's own words", () => {
expect(withTrailingTurnClock(`${clock}\n\ncut the middle`)).toBe(`cut the middle\n\n${clock}`);
});
it("carries a note riding on the clock line along with it", () => {
const noted = `${clock}\nnear its limit note\n\ncut the middle`;
expect(withTrailingTurnClock(noted)).toBe(`cut the middle\n\n${clock}\nnear its limit note`);
});
it("leaves a text that does not lead with the clock untouched", () => {
expect(withTrailingTurnClock("cut the middle")).toBe("cut the middle");
expect(withTrailingTurnClock(`${clock}\n\n`)).toBe(`${clock}\n\n`);
});
});