File size: 2,085 Bytes
4135488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Botcierge, query_intent } from '../src/index';

// Mock global fetch
global.fetch = vi.fn();

describe('Botcierge SDK', () => {
  beforeEach(() => {
    vi.resetAllMocks();
  });

  describe('Botcierge Class', () => {
    it('should use the provided baseUrl', async () => {
      const customUrl = 'https://api.example.com';
      const client = new Botcierge({ baseUrl: customUrl });
      
      const mockResult = {
        domain: 'test-domain',
        intent: 'test-intent',
        confidence: 'high',
        scores: { 'test-intent': 0.9 }
      };

      (fetch as any).mockResolvedValue({
        ok: true,
        json: async () => mockResult,
      });

      await client.query_intent('hello');

      expect(fetch).toHaveBeenCalledWith(
        expect.stringContaining(customUrl),
        expect.any(Object)
      );
    });

    it('should throw an error if the API response is not ok', async () => {
      const client = new Botcierge();
      
      (fetch as any).mockResolvedValue({
        ok: false,
        statusText: 'Forbidden',
        json: async () => ({ detail: 'Invalid token' }),
      });

      await expect(client.query_intent('hello')).rejects.toThrow('Botcierge API error: Invalid token');
    });
  });

  describe('query_intent helper', () => {
    it('should correctly classify an utterance using the default client', async () => {
      const mockResult = {
        domain: 'moneyshare',
        intent: 'request_loan',
        confidence: 'high',
        scores: { 'request_loan': 0.95 }
      };

      (fetch as any).mockResolvedValue({
        ok: true,
        json: async () => mockResult,
      });

      const result = await query_intent('can I borrow some money');

      expect(result).toEqual(mockResult);
      expect(fetch).toHaveBeenCalledWith(
        'http://localhost:8000/classify',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({ utterance: 'can I borrow some money' })
        })
      );
    });
  });
});