Skip to content

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

1.3 KB
Raw Blame
// Loaded with --import in isolated fixtures when OMB_TEST_LIMIT_CLOCK=1: a limit-reset automation starts a second
// after the reset instead of a minute, and a routine still due is retried every 250 ms instead of every 10 s, so a fixture can
// watch one run without waiting. No authorization or dispatch decision changes.
import { registerHooks } from 'node:module';

const dataDir = process.env.OMB_DATA_DIR;
if (!dataDir || dataDir !== process.env.HOME || process.env.OMB_TEST_LIMIT_CLOCK !== '1') {
  throw new Error('The limit clock requires an explicitly isolated fixture');
}
const seams = new Map([
  [new URL('../../packages/phone-protocol/src/limit-reset.ts', import.meta.url).href, ['export const AFTER_RESET_MS = 60_000;', 'export const AFTER_RESET_MS = 1_000;']],
  [new URL('../routines.ts', import.meta.url).href, ['startDueTimer({ run: () => this.tick(),', 'startDueTimer({ retryMs: 250, run: () => this.tick(),']],
]);
registerHooks({
  load(url, context, nextLoad) {
    const result = nextLoad(url, context);
    const seam = seams.get(url);
    if (!seam) return result;
    const source = String(result.source);
    if (source.split(seam[0]).length !== 2) throw new Error(`Limit clock seam changed in ${url}`);
    return { ...result, source: source.replace(seam[0], seam[1]) };
  },
});