File size: 3,136 Bytes
94193b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { describe, it, expect, vi } from 'vitest';
import type { ConversationNode } from '@/lib/llm/multi-agent-orchestrator';

const mockVfs = {
  init: vi.fn(),
  readFile: vi.fn(),
  fileExists: vi.fn(),
};

vi.mock('@/lib/vfs', () => ({
  vfs: mockVfs,
}));

function makeConversation(messages: Array<{
  role: string;
  content?: string;
  tool_calls?: Array<{ id: string; function: { name: string; arguments: string } }>;
  tool_call_id?: string;
}>): ConversationNode[] {
  return [{
    id: 'test-node',
    agent_type: 'orchestrator',
    messages: messages as any,
    metadata: { started_at: Date.now(), cost: 0, status: 'completed' },
  }];
}

describe('any_of assertion', () => {
  it('passes when first sub-assertion matches', async () => {
    const { runAssertions } = await import('../assertion-runner');
    const conversation = makeConversation([
      {
        role: 'assistant',
        tool_calls: [{
          id: 'tc1',
          function: { name: 'bash', arguments: '{"command": "propose-create"}' },
        }],
      },
      { role: 'tool', content: 'ok', tool_call_id: 'tc1' },
    ]);

    const results = await runAssertions('test', conversation, [{
      type: 'any_of',
      description: 'propose or ask',
      assertions: [
        { type: 'tool_args_match', toolName: 'bash', pattern: 'propose-create', description: 'propose' },
        { type: 'output_matches', pattern: '\\?', description: 'question' },
      ],
    }]);

    expect(results).toHaveLength(1);
    expect(results[0].passed).toBe(true);
    expect(results[0].actual).toContain('propose-create');
  });

  it('passes when second sub-assertion matches', async () => {
    const { runAssertions } = await import('../assertion-runner');
    const conversation = makeConversation([
      { role: 'assistant', content: 'What kind of site do you want?' },
    ]);

    const results = await runAssertions('test', conversation, [{
      type: 'any_of',
      description: 'propose or ask',
      assertions: [
        { type: 'tool_args_match', toolName: 'bash', pattern: 'propose-create', description: 'propose' },
        { type: 'output_matches', pattern: '\\?', description: 'question' },
      ],
    }]);

    expect(results).toHaveLength(1);
    expect(results[0].passed).toBe(true);
  });

  it('fails and joins all sub-assertion failures', async () => {
    const { runAssertions } = await import('../assertion-runner');
    const conversation = makeConversation([
      { role: 'assistant', content: 'I will build the site now.' },
    ]);

    const results = await runAssertions('test', conversation, [{
      type: 'any_of',
      description: 'propose or ask',
      assertions: [
        { type: 'tool_args_match', toolName: 'bash', pattern: 'propose-create', description: 'propose' },
        { type: 'output_matches', pattern: '\\?', description: 'question' },
      ],
    }]);

    expect(results).toHaveLength(1);
    expect(results[0].passed).toBe(false);
    expect(results[0].actual).toContain('|');
    expect(results[0].actual).toContain('propose');
    expect(results[0].actual).toContain('question');
  });

});