Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

5.3 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';
import { registerBotSkillTools } from '../xdt-helper/bot_skills.js';

function parse(result: { content: Array<{ type: string; text?: string }> }) {
  const block = result.content[0];
  if (block?.type !== 'text' || typeof block.text !== 'string') throw new Error('text expected');
  return JSON.parse(block.text) as Record<string, unknown>;
}

const SKILL = {
  slug: 'weekly-report',
  name: 'weekly-report',
  description: 'How I put the weekly report together',
  updatedAt: '2026-08-19T00:00:00.000Z',
};

function registry(overrides: Partial<Parameters<typeof registerBotSkillTools>[1]> = {}) {
  const reg = new XdtHelperToolRegistry();
  registerBotSkillTools(reg, {
    getSessionContext: () => ({ sessionId: 'bot-session', agentKind: 'pi', workingDir: '/w' }),
    callbacks: {
      save: vi.fn(async () => ({
        ok: true as const,
        skill: SKILL,
        created: true,
        effective: 'next-session' as const,
      })),
      list: vi.fn(async () => ({ ok: true as const, skills: [SKILL] })),
    },
    ...overrides,
  });
  return reg;
}

describe('Bot skill tools', () => {
  it('saves through the host-owned caller Session and never accepts a botId', async () => {
    const save = vi.fn(async () => ({
      ok: true as const,
      skill: SKILL,
      created: true,
      effective: 'next-session' as const,
    }));
    const reg = registry({
      callbacks: { save, list: vi.fn() },
    });

    const result = await reg.call('save_teammate_skill', {
      name: 'weekly-report',
      description: 'How I put the weekly report together',
      body: '1. Pull merged PRs\n2. Group by author',
    });

    expect(parse(result)).toMatchObject({ ok: true, created: true, effective: 'next-session' });
    expect(save).toHaveBeenCalledWith({
      callerSessionId: 'bot-session',
      name: 'weekly-report',
      description: 'How I put the weekly report together',
      body: '1. Pull merged PRs\n2. Group by author',
    });
  });

  it('reports an update rather than a new skill when the host says so', async () => {
    const reg = registry({
      callbacks: {
        save: vi.fn(async () => ({
          ok: true as const,
          skill: SKILL,
          created: false,
          effective: 'next-session' as const,
        })),
        list: vi.fn(),
      },
    });

    const result = await reg.call('save_teammate_skill', {
      name: 'weekly-report',
      description: 'sharper now',
      body: 'step 1',
    });
    expect(parse(result)).toMatchObject({ ok: true, created: false });
  });

  it('lists what the Bot already learned so it does not learn it twice', async () => {
    const result = await registry().call('list_teammate_skills', {});
    expect(parse(result)).toMatchObject({ ok: true, skills: [SKILL] });
  });

  it('forwards search and pagination to the owner and preserves the next page and source location', async () => {
    const list = vi.fn(async () => ({ ok: true as const, skills: [{ ...SKILL, filePath: '/bot/skills/weekly-report/SKILL.md', bodyStartLine: 5 }], total: 3000, nextOffset: 41 }));
    const reg = registry({ callbacks: { save: vi.fn(), list } });
    const result = await reg.call('list_teammate_skills', { query: 'weekly report', offset: 40, limit: 1 });
    expect(list).toHaveBeenCalledWith({ callerSessionId: 'bot-session', query: 'weekly report', offset: 40, limit: 1 });
    expect(parse(result)).toMatchObject({ total: 3000, nextOffset: 41,
      skills: [{ filePath: '/bot/skills/weekly-report/SKILL.md', bodyStartLine: 5 }] });
  });

  it('fails closed without a bound Session', async () => {
    const reg = new XdtHelperToolRegistry();
    registerBotSkillTools(reg, {
      getSessionContext: () => ({ agentKind: 'pi', workingDir: '/w' }),
      callbacks: { save: vi.fn(), list: vi.fn() },
    });

    for (const tool of ['save_teammate_skill', 'list_teammate_skills']) {
      const result = await reg.call(
        tool,
        tool === 'save_teammate_skill' ? { name: 'a', description: 'b', body: 'c' } : {},
      );
      expect(result.isError).toBe(true);
      expect(parse(result)).toMatchObject({ ok: false, errorCode: 'NOT_A_BOT_SESSION' });
    }
  });

  it('passes the host error code straight through instead of pretending it worked', async () => {
    const reg = registry({
      callbacks: {
        save: vi.fn(async () => ({
          ok: false as const,
          errorCode: 'SKILL_NAME_UNUSABLE',
          message: 'name could not be turned into a slug',
        })),
        list: vi.fn(),
      },
    });

    const result = await reg.call('save_teammate_skill', {
      name: '周报怎么写',
      description: 'b',
      body: 'c',
    });
    expect(result.isError).toBe(true);
    expect(parse(result)).toMatchObject({ ok: false, errorCode: 'SKILL_NAME_UNUSABLE' });
  });

  it('rejects a stray field instead of silently dropping it', async () => {
    // registry 用 strictObject 校验:模型把 botId 塞进来必须被打回,不能静默剥掉后
    // 当成「写进自己名下」成功返回。
    const result = await registry().call('save_teammate_skill', {
      name: 'a',
      description: 'b',
      body: 'c',
      botId: 'someone-else',
    });
    expect(result.isError).toBe(true);
    expect(parse(result)).toMatchObject({ ok: false, errorCode: 'INVALID_ARGS' });
  });
});