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.

6.3 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';

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

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

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

  contains(cls: string) {
    return this.classes.has(cls);
  }

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

class FakeElement {
  dataset: Record<string, string>;
  classList: FakeClassList;
  hidden = false;
  tabIndex = -1;
  focused = false;
  attributes: Record<string, string> = {};
  listeners = new Map<string, Array<(event?: any) => void>>();

  constructor(dataset: Record<string, string>, classes: string[] = []) {
    this.dataset = dataset;
    this.classList = new FakeClassList(classes);
  }

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

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

  setAttribute(name: string, value: string) {
    this.attributes[name] = value;
  }

  focus() {
    this.focused = true;
  }

  keydown(key: string) {
    const event = { key, preventDefault: vi.fn() };
    for (const handler of this.listeners.get('keydown') || []) handler(event);
    return event;
  }
}

function loadSettingsTabsModule() {
  const tabs = [
    new FakeElement({ settingsTab: 'data' }, ['settings-tab', 'is-active']),
    new FakeElement({ settingsTab: 'credentials' }, ['settings-tab']),
    new FakeElement({ settingsTab: 'general' }, ['settings-tab']),
  ];
  const panes = [
    new FakeElement({ settingsPane: 'data' }, ['settings-tab-pane']),
    new FakeElement({ settingsPane: 'credentials' }, ['settings-tab-pane']),
    new FakeElement({ settingsPane: 'general' }, ['settings-tab-pane']),
  ];
  panes[1].hidden = true;
  panes[2].hidden = true;

  const document = {
    querySelectorAll(selector: string) {
      if (selector === '.settings-tab') return tabs;
      if (selector === '.settings-tab-pane') return panes;
      return [];
    },
    querySelector(selector: string) {
      if (selector === '.settings-tab.is-active') {
        return tabs.find((tab) => tab.classList.contains('is-active')) || null;
      }
      return null;
    },
  };
  const context: any = { document, window: {} };
  context.window.window = context.window;
  vm.createContext(context);
  const code = fs.readFileSync(path.join(root, 'src/renderer/modules/settings_tabs.js'), 'utf8');
  vm.runInContext(code, context, { filename: 'settings_tabs.js' });
  return { window: context.window, tabs, panes };
}

describe('settings tabs module', () => {
  it('refreshes local recycle data whenever the user returns to Data', () => {
    const { window, tabs } = loadSettingsTabsModule();
    window.initRecycleBin = vi.fn();
    tabs[1].click();
    expect(window.initRecycleBin).not.toHaveBeenCalled();
    tabs[0].click();
    expect(window.initRecycleBin).toHaveBeenCalledTimes(1);
    tabs[2].click();
    tabs[0].keydown('Home');
    expect(window.initRecycleBin).toHaveBeenCalledTimes(2);
  });

  it('initializes shared task cleanup on entering Data without requiring hosted data hooks', () => {
    const { window, tabs } = loadSettingsTabsModule();
    window.initTaskCleanup = vi.fn();
    tabs[2].click(); expect(window.initTaskCleanup).not.toHaveBeenCalled();
    tabs[0].click(); expect(window.initTaskCleanup).toHaveBeenCalledOnce();
  });

  it('loads tabs eagerly and the settings page on demand', () => {
    const indexHtml = fs.readFileSync(path.join(root, 'src/renderer/index.html'), 'utf8');
    const lazyFeatures = fs.readFileSync(path.join(root, 'src/renderer/modules/lazy-features.js'), 'utf8');
    const modulePath = path.join(root, 'src/renderer/modules/settings_tabs.js');
    const tabsScript = '<script src="./modules/settings_tabs.js"></script>';
    const settingsScript = '<script src="./modules/settings.js"></script>';

    expect(fs.existsSync(modulePath)).toBe(true);
    expect(indexHtml.indexOf(tabsScript)).toBeGreaterThanOrEqual(0);
    expect(indexHtml.indexOf(settingsScript)).toBe(-1);
    expect(lazyFeatures).toContain("{ src: './modules/settings.js' }");
  });

  it('binds the first click eagerly and preserves selection on repeated initialization', () => {
    const { window, tabs, panes } = loadSettingsTabsModule();

    tabs[1].click();
    window.initSettingsTabs();
    window.initSettingsTabs();

    expect(tabs[0].classList.contains('is-active')).toBe(false);
    expect(tabs[1].classList.contains('is-active')).toBe(true);
    expect(tabs[2].classList.contains('is-active')).toBe(false);
    expect(panes[0].hidden).toBe(true);
    expect(panes[1].hidden).toBe(false);
    expect(panes[2].hidden).toBe(true);
    expect(tabs[1].attributes['aria-selected']).toBe('true');
    expect(tabs[1].tabIndex).toBe(0);
    expect(tabs[0].attributes['aria-selected']).toBe('false');
    expect(tabs[0].tabIndex).toBe(-1);
  });

  it('supports the horizontal ARIA tab keyboard pattern', () => {
    const { window, tabs, panes } = loadSettingsTabsModule();

    window.initSettingsTabs();
    window.initSettingsTabs();
    window.initSettingsTabs();
    for (const tab of tabs) {
      expect(tab.listeners.get('click')).toHaveLength(1);
      expect(tab.listeners.get('keydown')).toHaveLength(1);
    }
    const event = tabs[0].keydown('ArrowLeft');

    expect(event.preventDefault).toHaveBeenCalledOnce();
    expect(tabs[2].focused).toBe(true);
    expect(tabs[2].attributes['aria-selected']).toBe('true');
    expect(panes[2].hidden).toBe(false);

    tabs[2].keydown('Home');
    expect(tabs[0].focused).toBe(true);
    expect(panes[0].hidden).toBe(false);
  });

  it('falls back to the first surviving tab when asked for a stripped tab', () => {
    const { window, tabs, panes } = loadSettingsTabsModule();

    window.activateSettingsTab('account');

    expect(tabs[0].classList.contains('is-active')).toBe(true);
    expect(panes[0].hidden).toBe(false);
    expect(panes[1].hidden).toBe(true);
    expect(panes[2].hidden).toBe(true);
  });
});