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.

8.7 KB
Raw Blame
import { composerAccessorSource } from './composer-test-source';
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';

const root = path.join(__dirname, '../..');

class FakeClassList {
  private classes = new Set<string>();

  constructor(initial: string[] = []) {
    initial.forEach((name) => this.classes.add(name));
  }

  add(name: string) { this.classes.add(name); }
  remove(name: string) { this.classes.delete(name); }
  contains(name: string) { return this.classes.has(name); }
  toggle(name: string, force?: boolean) {
    const enabled = force === undefined ? !this.classes.has(name) : force;
    if (enabled) this.classes.add(name);
    else this.classes.delete(name);
    return enabled;
  }
}

class FakeElement {
  classList: FakeClassList;
  dataset: Record<string, string> = {};
  style: Record<string, string> = {};
  value = '';
  scrollHeight = 0;
  private listeners = new Map<string, Array<(event?: any) => void>>();

  constructor(initialClasses: string[] = []) {
    this.classList = new FakeClassList(initialClasses);
  }

  addEventListener(type: string, handler: (event?: any) => void) {
    const handlers = this.listeners.get(type) || [];
    handlers.push(handler);
    this.listeners.set(type, handlers);
  }

  querySelector() { return null; }

  click() {
    for (const handler of this.listeners.get('click') || []) {
      handler({ currentTarget: this, target: this });
    }
  }
}

function loadRendererNavigation() {
  const elements = new Map<string, FakeElement>();
  const panelIds = [
    'panel-new-chat', 'panel-auto', 'panel-agents', 'panel-skills',
    'panel-connectors', 'panel-contexts', 'panel-apps', 'panel-settings',
    'panel-memory', 'panel-devtools', 'panel-project', 'panel-marketplace',
    'panel-conversation',
  ];
  for (const id of panelIds) {
    elements.set(id, new FakeElement(id === 'panel-new-chat' ? ['panel', 'active'] : ['panel']));
  }

  const documentListeners = new Map<string, Array<(event?: any) => void>>();
  const document = {
    documentElement: { classList: new FakeClassList() },
    getElementById(id: string) {
      if (!elements.has(id)) elements.set(id, new FakeElement());
      return elements.get(id)!;
    },
    querySelectorAll(selector: string) {
      if (selector === '.panel') return panelIds.map((id) => elements.get(id)!);
      return [];
    },
    addEventListener(type: string, handler: (event?: any) => void) {
      const handlers = documentListeners.get(type) || [];
      handlers.push(handler);
      documentListeners.set(type, handlers);
    },
  };

  const loadRendererFeature = vi.fn(async () => undefined);
  const loadSettings = vi.fn(async () => undefined);
  const noop = () => undefined;
  const window = {
    addEventListener: noop,
    orkas: { onPushEvent: noop },
    loadRendererFeature,
  } as any;
  window.window = window;

  const context: any = {
    window,
    document,
    navigator: { userAgent: 'test' },
    localStorage: { getItem: () => null, setItem: noop, removeItem: noop, length: 0, key: () => null },
    performance: { now: () => 0 },
    requestAnimationFrame: (handler: () => void) => { handler(); return 1; },
    setTimeout,
    clearTimeout,
    createLogger: () => ({ info: noop, warn: noop, error: noop, debug: noop }),
    loadRendererFeature,
    loadSettings,
    _bindGlobalSearch: noop,
    handleNewChatSubmit: noop,
    handleChatSubmit: noop,
    toggleAgentEditMode: noop,
    deleteSelectedAgent: noop,
    clearAgentChat: noop,
    bindAgentPickers: noop,
  };

  vm.createContext(context);
  vm.runInContext(composerAccessorSource, context);
  for (const file of ['state.js', 'boot.js']) {
    const source = fs.readFileSync(path.join(root, 'src/renderer/modules', file), 'utf8');
    vm.runInContext(source, context, { filename: file });
  }
  return { context, elements, loadRendererFeature, loadSettings };
}

describe('settings sidebar navigation', () => {
  it('opens the settings panel and starts its lazy feature from a real click binding', async () => {
    const { context, elements, loadRendererFeature, loadSettings } = loadRendererNavigation();

    context.bindStaticHandlers();
    elements.get('settings-btn')!.click();
    await new Promise((resolve) => setTimeout(resolve, 10));

    expect(elements.get('panel-settings')!.classList.contains('active')).toBe(true);
    expect(elements.get('panel-new-chat')!.classList.contains('active')).toBe(false);
    expect(elements.get('settings-btn')!.classList.contains('active')).toBe(true);
    expect(loadRendererFeature).toHaveBeenCalledWith('settings');
    expect(loadSettings).toHaveBeenCalledOnce();
  });

  it('ships the complete local Settings shell and the five-purpose add dialog', () => {
    const html = fs.readFileSync(path.join(root, 'src/renderer/index.html'), 'utf8');
    const settings = html.slice(
      html.indexOf('<!-- Settings -->'),
      html.indexOf('<!-- Memory detail page'),
    );

    expect(settings).toContain('class="settings-tabs" role="tablist" aria-orientation="horizontal"');
    expect(settings).toContain('data-ui-icon="database" data-ui-icon-class="settings-tab-icon"');
    expect(settings).toContain('data-ui-icon="sparkles" data-ui-icon-class="settings-tab-icon"');
    expect(settings).toContain('data-ui-icon="settings" data-ui-icon-class="settings-tab-icon"');
    expect(settings).toContain('class="settings-page"');
    expect(settings).toContain('data-settings-model-tab="chat"');
    expect(settings).toContain('data-settings-model-panel="tts"');
    expect(settings).toContain('data-settings-add="chat"');
    expect(settings).not.toContain('data-settings-pane="account"');

    expect(html).toContain('id="settings-add-modal"');
    for (const kind of ['chat', 'search', 'image', 'video', 'tts']) {
      expect(html).toContain(`data-settings-add-form="${kind}"`);
    }
  });

  it('keeps all three named AI Team entries and the external-only CLI route', () => {
    const html = fs.readFileSync(path.join(root, 'src/renderer/index.html'), 'utf8');
    const state = fs.readFileSync(path.join(root, 'src/renderer/modules/state.js'), 'utf8');
    const css = fs.readFileSync(path.join(root, 'src/renderer/style.css'), 'utf8');
    const header = html.slice(
      html.indexOf('<div class="agents-grid-header">'),
      html.indexOf('<div class="marketplace-reconcile-banner" data-reconcile-banner data-reconcile-kind="agent"'),
    );
    const handlerStart = state.indexOf("document.getElementById('agents-connect-cli-btn')");
    const handler = state.slice(handlerStart, state.indexOf("document.getElementById('agents-back-btn')", handlerStart));

    expect(header).toContain('id="create-agent-btn"');
    expect(header).toContain('id="agents-connect-cli-btn"');
    expect(header).toContain('data-i18n="agents.connect_cli_sub"');
    expect(header).toContain('id="agents-more-btn"');
    expect(handlerStart).toBeGreaterThan(-1);
    expect(handler).toContain("initialTab: 'external'");
    expect(handler).toContain('externalOnly: true');
    expect(handler).toContain("entryPoint: 'agents_connect_cli_button'");
    expect(css).toMatch(/\.header-entry-actions\s*{[\s\S]*?gap:\s*6px;[\s\S]*?align-items:\s*stretch;/);
    expect(css).toMatch(/\.header-entry-btn\s*{[\s\S]*?min-height:\s*36px;[\s\S]*?padding:\s*4px 10px;[\s\S]*?font-size:\s*12px;/);
    expect(css).toMatch(/\.header-entry-btn\s*{[\s\S]*?background:\s*var\(--surface\);/);
    expect(css).not.toMatch(/\.header-entry-btn-create\s*{[^}]*background:/);
    expect(css).toMatch(/\.header-entry-icon\s*{[^}]*width:\s*14px;[^}]*height:\s*14px;/);
  });
});

it('restores unread state after the real user initialization boundary', async () => {
  const { context } = loadRendererNavigation();
  context.apiFetch = vi.fn(async () => ({ json: async () => ({ ok: true, user_id: 'local-owner' }) }));
  const owners: string[] = [];
  context._restoreUnreadTaskState = () => { owners.push(vm.runInContext('currentUserId', context)); };
  await context.initUser();
  expect(owners).toEqual(['local-owner']);
  expect(context.currentUserId).toBe('local-owner');
});

it('binds the requested task board while conversation history is still loading', () => {
  const { context } = loadRendererNavigation();
  const history = Promise.withResolvers<void>();
  context.loadConversationHistory = vi.fn(() => history.promise);
  context._restoreDraft = vi.fn();
  context.isConvPending = () => false;
  context._updateConvSendUI = vi.fn();
  context.focusChatComposerIfIdle = vi.fn();
  context.window.TaskBoard = { sync: vi.fn() };
  context.setView('conversation', 'task-board-session');
  expect(context.loadConversationHistory).toHaveBeenCalledWith('task-board-session', undefined);
  expect(context.window.TaskBoard.sync).toHaveBeenCalledExactlyOnceWith('task-board-session');
  history.resolve();
});