Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

4.4 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as vm from 'node:vm';

function loadMathBridge({ failLoads = 0 } = {}) {
  const appended: any[] = [];
  const warn = vi.fn();
  const typesetPromise = vi.fn(async () => {});
  const context: any = {
    Map,
    Promise,
    Error,
    String,
    console: { info() {}, warn },
    createLogger: () => ({ info() {}, warn }),
    window: {
      MathJax: {
        startup: { typeset: false },
      },
    },
    document: {
      createElement: () => ({ dataset: {}, remove: vi.fn() }),
      head: {
        appendChild(script: any) {
          appended.push(script);
          if (appended.length <= failLoads) {
            script.onerror();
            return;
          }
          context.window.MathJax = {
            startup: { promise: Promise.resolve() },
            typesetPromise,
            typesetClear: () => {},
          };
          script.onload();
        },
      },
      documentElement: { appendChild() {} },
    },
  };
  vm.createContext(context);
  const source = fs.readFileSync(path.join(__dirname, '../../src/renderer/modules/math.js'), 'utf8');
  vm.runInContext(source, context, { filename: 'math.js' });
  return { context, appended, typesetPromise, warn };
}

describe('renderer MathJax lazy loading', () => {
  it('does not retain intermediate stream versions in the session HTML cache', async () => {
    const { context, warn } = loadMathBridge();
    context.document.createElement = () => ({ dataset: {}, style: {}, innerHTML: '', parentElement: null });
    context.window.MathJax.typesetPromise = async () => {};
    await context.typesetMathHtml('$stable$');
    for (let i = 0; i < 180; i++) await context.typesetMathHtml(`$x+${i}$`, { cache: false });
    expect(vm.runInContext('Array.from(_mathHtmlCache.keys())', context)).toEqual(['$stable$']);
    expect(warn).not.toHaveBeenCalled();
  });

  it.each([false, true])('releases temporary formula hosts after rendering (failure=%s)', async fail => {
    const { context, warn } = loadMathBridge();
    const retained = new Set();
    const mounted = new Set();
    context.document.createElement = () => ({ dataset: {}, style: {}, innerHTML: '', parentElement: null });
    context.document.body = {
      appendChild(host: any) { mounted.add(host); host.parentElement = this; },
      removeChild(host: any) { mounted.delete(host); host.parentElement = null; },
    };
    context.window.MathJax = {
      typesetClear: (hosts: any[]) => hosts.forEach(host => retained.delete(host)),
      typesetPromise: async (hosts: any[]) => {
        hosts.forEach(host => retained.add(host));
        if (fail) throw new Error('Fixture typesetting failure');
        hosts[0].innerHTML = '<math><mi>x</mi></math>';
      },
    };
    const result = await context.typesetMathHtml('$x$');
    expect(result).toBe(fail ? '$x$' : '<math><mi>x</mi></math>');
    expect(retained.size).toBe(0);
    expect(mounted.size).toBe(0);
    expect(warn.mock.calls).toEqual(fail ? [['typeset html failed']] : []);
  });

  it('does not load the runtime for ordinary chat content', async () => {
    const { context, appended } = loadMathBridge();
    await context.window.typesetMath({ textContent: 'plain message', querySelectorAll: () => [] });
    expect(appended).toEqual([]);
  });

  it('loads the runtime once when concurrent formula renders arrive', async () => {
    const { context, appended } = loadMathBridge();
    const root = { textContent: 'solve $x+1=2$', querySelectorAll: () => [] };
    await Promise.all([
      context.window.typesetMath(root),
      context.window.typesetMath(root),
    ]);
    expect(appended).toHaveLength(1);
    expect(appended[0].src).toBe('./vendor/mathjax/tex-chtml.js');
    expect(appended[0].async).toBe(true);
  });

  it('retries after a transient runtime load failure without exposing the raw error', async () => {
    const { context, appended, typesetPromise, warn } = loadMathBridge({ failLoads: 1 });
    const root = { textContent: 'solve $x+1=2$', querySelectorAll: () => [] };

    await context.window.typesetMath(root);
    await context.window.typesetMath(root);

    expect(appended).toHaveLength(2);
    expect(appended[0].remove).toHaveBeenCalledOnce();
    expect(typesetPromise).toHaveBeenCalledWith([root]);
    expect(warn).toHaveBeenCalledWith('typeset failed');
    expect(JSON.stringify(warn.mock.calls)).not.toContain('MathJax runtime');
  });
});