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.

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

function loadSkillRendererHelpers() {
  const context: any = {
    console,
    createLogger: () => ({ warn: () => {}, info: () => {}, error: () => {} }),
    t: (key: string) => ({
      'skills.import_seed_display': '整理已导入的技能',
    } as Record<string, string>)[key] || key,
    window: { addEventListener: () => {} },
    normalizeDisplayText: (value: unknown) => String(value || '')
      .replace(/\\"/g, '"')
      .replace(/\\'/g, "'")
      .replace(/\\{2,}/g, '\\')
      .replace(/\s+/g, ' ')
      .trim(),
  };
  context.global = context;
  context.globalThis = context;
  vm.createContext(context);
  const code = fs.readFileSync(path.join(process.cwd(), 'src', 'renderer', 'modules', 'skills.js'), 'utf8');
  vm.runInContext(code, context, { filename: 'skills.js' });
  return context;
}

describe('skills renderer frontmatter parsing', () => {
  it('normalizes escaped quotes before showing skill descriptions', () => {
    const context = loadSkillRendererHelpers();
    const pairs = context._parseSkillFrontmatterPairs([
      '---',
      'name: "growth"',
      'description: "适合\\"创建 skill\\" 和 \\"编辑 skill\\""',
      '---',
      '',
    ].join('\n'));

    expect(pairs).toContainEqual(['description', '适合"创建 skill" 和 "编辑 skill"']);
  });

  it('keeps import seed instructions in model text instead of visible text', () => {
    const context = loadSkillRendererHelpers();
    const seed = context._skillImportAutoSeedFromResponse({
      seedModelText: '已按源文件直接安装这些技能:growth。请只读取现有 SKILL.md。',
    });

    expect(seed).toEqual({
      displayText: '整理已导入的技能',
      modelText: '已按源文件直接安装这些技能:growth。请只读取现有 SKILL.md。',
      force: true,
    });
    expect(seed.displayText).not.toContain('SKILL.md');
  });

  it('opens import edit chat after the file view without waiting for source tree expansion', async () => {
    const context = loadSkillRendererHelpers();
    const calls: string[] = [];
    context.__calls = calls;
    vm.runInContext(`
      closeSkillModal = () => { __calls.push('close'); };
      loadSkills = async () => { __calls.push('load'); };
      setView = (view) => { __calls.push('set:' + view); };
      _ensureSkillsSourceExpanded = async () => { __calls.push('source:expand'); };
      _showSkillsDetailView = async (source, id, opts) => {
        __calls.push('detail:start:' + source + ':' + id + ':' + (opts && opts.expandSource === false ? 'no-tree' : 'tree'));
        await new Promise((resolve) => { globalThis.__releaseDetail = resolve; });
        _selectedSkill = { source, id, filepath: 'SKILL.md', name: 'Imported' };
        __calls.push('detail:end');
      };
      toggleSkillEditMode = async (opts) => {
        __calls.push('toggle:' + (opts.autoSeed && opts.autoSeed.modelText) + ':' + opts.autoSeed.force);
      };
    `, context);

    const pending = context._afterSkillCreated('imported', true, {
      displayText: '整理已导入的技能',
      modelText: '已直接安装这些技能:imported。',
    });
    await Promise.resolve();
    await Promise.resolve();

    expect(calls).toEqual(['close', 'load', 'set:skills', 'detail:start:custom:imported:no-tree']);
    context.__releaseDetail();
    await pending;
    await Promise.resolve();

    expect(calls).toEqual([
      'close',
      'load',
      'set:skills',
      'detail:start:custom:imported:no-tree',
      'detail:end',
      'toggle:已直接安装这些技能:imported。:undefined',
      'source:expand',
    ]);
  });

  it('routes folder import confirmation into the edit-chat create tail', async () => {
    const context = loadSkillRendererHelpers();
    const calls: string[] = [];
    const msgEl = { textContent: '', className: '' };
    context.__calls = calls;
    context.apiFetch = async (url: string, opts: any) => {
      calls.push(`api:${url}:${opts?.method || 'GET'}:${JSON.parse(opts?.body || '{}').srcDir}`);
      return {
        json: async () => ({
          ok: true,
          skill: { id: 'imported' },
          seedModelText: '已直接安装这些技能:imported。',
        }),
      };
    };
    vm.runInContext(`
      _setSkillModalBusy = (busy) => { __calls.push('busy:' + busy); };
      _waitForSkillModalBusyPaint = async () => { __calls.push('paint'); };
      _afterSkillCreated = async (sid, isNew, autoSeed) => {
        __calls.push('after:' + sid + ':' + isNew + ':' + autoSeed.modelText + ':' + autoSeed.force);
      };
    `, context);

    await context._saveSkillFromDirWithQuality({ msgEl, srcDir: '/tmp/imported', force: false });

    expect(calls).toEqual([
      'busy:true',
      'paint',
      'api:/api/skills/create-from-dir:POST:/tmp/imported',
      'after:imported:true:已直接安装这些技能:imported。:true',
      'busy:false',
    ]);
    expect(msgEl.textContent).toBe('skills.saving');
  });

  it('opens the created URL skill after persistence', async () => {
    const context = loadSkillRendererHelpers();
    const monitorCalls: any[] = [];
    const calls: string[] = [];
    let now = 100;
    const msgEl = { textContent: '', className: '' };
    context.__calls = calls;
    context.performance = { now: () => { now += 25; return now; } };
    context.apiFetch = async (url: string, opts: any) => {
      calls.push(`api:${url}:${opts?.method || 'GET'}`);
      return {
        json: async () => ({
          ok: true,
          skill: { id: 'url-skill', name: 'URL Skill' },
        }),
      };
    };
    vm.runInContext(`
      document = {
        getElementById: () => ({ value: 'https://example.com/skill', focus() {} }),
      };
      _setSkillModalBusy = (busy) => { __calls.push('busy:' + busy); };
      _waitForSkillModalBusyPaint = async () => { __calls.push('paint'); };
      _afterSkillCreated = async (sid, isNew) => { __calls.push('after:' + sid + ':' + isNew); };
    `, context);

    await context._saveSkillFromUrl({ msgEl });
    expect(calls).toEqual([
      'busy:true',
      'paint',
      'api:/api/skills/create-from-url:POST',
      'after:url-skill:true',
      'busy:false',
    ]);
  });

  it('shows recoverable validation errors without creating invalid skills', async () => {
    const context = loadSkillRendererHelpers();
    const monitorCalls: any[] = [];
    let now = 100;
    context.performance = { now: () => { now += 25; return now; } };
    vm.runInContext(`
      document = {
        getElementById: () => ({ value: '', textContent: '', className: '', focus() {} }),
      };
    `, context);
    const msgEl = { textContent: '', className: '' };

    context.apiFetch = () => { throw new Error('Invalid input must not create a skill'); };
    await context._saveSkillManual({ editId: '', msgEl });
    expect(msgEl.textContent).toBe('skills.input_name_needed');
    await context._saveSkillFromUrl({ msgEl });
    expect(msgEl.textContent).toBe('skill_modal.err_url_invalid');
    await context._saveSkillFromDir({ msgEl });
    expect(msgEl.textContent).toBe('skill_modal.err_dir_missing');
    expect(msgEl.className).toBe('form-msg err');
  });

  it('shows a failed import and releases the busy state', async () => {
    const context = loadSkillRendererHelpers();
    const monitorCalls: any[] = [];
    let now = 100;
    context.performance = { now: () => { now += 25; return now; } };
    context.apiFetch = async () => ({
      json: async () => ({ ok: false, error: 'Import failed' }),
    });
    vm.runInContext(`
      document = { getElementById: () => ({ value: 'https://example.com/private', focus() {} }) };
      _setSkillModalBusy = () => {};
      _waitForSkillModalBusyPaint = async () => {};
    `, context);

    const msgEl = { textContent: '', className: '' };
    const busy: boolean[] = [];
    context._setSkillModalBusy = (value: boolean) => busy.push(value);
    await context._saveSkillFromUrl({ msgEl });
    expect(msgEl.className).toBe('form-msg err');
    expect(msgEl.textContent).toBe('Import failed');
    expect(busy[busy.length - 1]).toBe(false);
  });

  it('keeps Skill toggle and package mutation success authoritative across refresh failures', async () => {
    const context = loadSkillRendererHelpers();
    const monitorCalls: any[] = [];
    context.Monitor = context.window.Monitor;
    context.window.orkas = { invoke: async () => ({ ok: true }) };
    context.uiAlert = async () => {};
    vm.runInContext(`
      _skillsCache = [];
      _selectedSkill = null;
      loadSkills = async () => { throw new Error('refresh failed'); };
    `, context);

    await expect(context._flipSkillEnabled('skill-one', false)).resolves.toBe(true);
    await context._runOpenPackageAction('update', 'pkg-private', null, 'Private package');

    const skillResults = monitorCalls.filter(
      ([kind, name]) => kind === 'event' && name === 'skill_manage_result',
    );
    const packageResults = monitorCalls.filter(
      ([kind, name]) => kind === 'event' && name === 'package_action_result',
    );
    expect(skillResults).toHaveLength(0);
    expect(packageResults).toHaveLength(0);
  });

  it('keeps a successful Skill delete authoritative when refresh fails', async () => {
    const context = loadSkillRendererHelpers();
    const monitorCalls: any[] = [];
    context.Monitor = context.window.Monitor;
    context.uiConfirm = async () => true;
    context.uiAlert = async () => {};
    context.apiFetch = async () => ({ json: async () => ({ ok: true }) });
    vm.runInContext(`
      _selectedSkill = { id: 'skill-one', source: 'custom' };
      _skillsCache = [{ id: 'skill-one', source: 'custom', name: 'Private Skill' }];
      loadSkills = async () => { throw new Error('refresh failed'); };
    `, context);

    await context.deleteSelectedSkill();

    const results = monitorCalls.filter(
      ([kind, name]) => kind === 'event' && name === 'skill_manage_result',
    );
    expect(results).toHaveLength(0);
    expect(vm.runInContext('_selectedSkill', context)).toBeNull();
  });

  it('sends forced import auto-seed even when edit chat history is not empty', async () => {
    const context = loadSkillRendererHelpers();
    const calls: string[] = [];
    context.__calls = calls;
    vm.runInContext(`
      document = {
        getElementById: (id) => ({ style: {}, dataset: {}, textContent: '', classList: { add(){}, remove(){}, contains(){ return false; } } }),
        querySelectorAll: (selector) => selector === '#skills-chat-messages .chat-message' ? [{}] : [],
      };
      _selectedSkill = { source: 'custom', id: 'imported', filepath: 'SKILL.md' };
      _updateEditButtonLabel = () => {};
      selectSkillFile = async () => { __calls.push('select'); };
      _chatAttachRefreshFromServer = async () => { __calls.push('attachments'); };
      _skillChatCtrl = {
        loadHistory: async () => { __calls.push('history'); },
        send: async (content, extra) => { __calls.push('send:' + content + ':' + extra.model_text); },
      };
      _ensureSkillChatController = () => _skillChatCtrl;
    `, context);

    await context.toggleSkillEditMode({
      autoSeed: {
        displayText: '整理已导入的技能',
        modelText: '已直接安装这些技能:imported。',
        force: true,
      },
    });

    expect(calls).toEqual([
      'select',
      'history',
      'attachments',
      'send:整理已导入的技能:skills.help_finish_seed_model\n\n已直接安装这些技能:imported。',
    ]);
  });
});