File size: 2,616 Bytes
3b6afc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
const fs = require('fs');
const { validateJson, loadSpecs, ManifestDefinition } = require('./loadSpecs');
const { createOpenAPIPlugin } = require('../dynamic/OpenAPIPlugin');

jest.mock('../dynamic/OpenAPIPlugin');

describe('ManifestDefinition', () => {
  it('should validate correct json', () => {
    const json = {
      name_for_human: 'Test',
      name_for_model: 'Test',
      description_for_human: 'Test',
      description_for_model: 'Test',
      api: {
        url: 'http://test.com',
      },
    };

    expect(() => ManifestDefinition.parse(json)).not.toThrow();
  });

  it('should not validate incorrect json', () => {
    const json = {
      name_for_human: 'Test',
      name_for_model: 'Test',
      description_for_human: 'Test',
      description_for_model: 'Test',
      api: {
        url: 123, // incorrect type
      },
    };

    expect(() => ManifestDefinition.parse(json)).toThrow();
  });
});

describe('validateJson', () => {
  it('should return parsed json if valid', () => {
    const json = {
      name_for_human: 'Test',
      name_for_model: 'Test',
      description_for_human: 'Test',
      description_for_model: 'Test',
      api: {
        url: 'http://test.com',
      },
    };

    expect(validateJson(json)).toEqual(json);
  });

  it('should return false if json is not valid', () => {
    const json = {
      name_for_human: 'Test',
      name_for_model: 'Test',
      description_for_human: 'Test',
      description_for_model: 'Test',
      api: {
        url: 123, // incorrect type
      },
    };

    expect(validateJson(json)).toEqual(false);
  });
});

describe('loadSpecs', () => {
  beforeEach(() => {
    jest.spyOn(fs.promises, 'readdir').mockResolvedValue(['test.json']);
    jest.spyOn(fs.promises, 'readFile').mockResolvedValue(
      JSON.stringify({
        name_for_human: 'Test',
        name_for_model: 'Test',
        description_for_human: 'Test',
        description_for_model: 'Test',
        api: {
          url: 'http://test.com',
        },
      }),
    );
    createOpenAPIPlugin.mockResolvedValue({});
  });

  afterEach(() => {
    jest.restoreAllMocks();
  });

  it('should return plugins', async () => {
    const plugins = await loadSpecs({ llm: true, verbose: false });

    expect(plugins).toHaveLength(1);
    expect(createOpenAPIPlugin).toHaveBeenCalledTimes(1);
  });

  it('should return constructorMap if map is true', async () => {
    const plugins = await loadSpecs({ llm: {}, map: true, verbose: false });

    expect(plugins).toHaveProperty('Test');
    expect(createOpenAPIPlugin).not.toHaveBeenCalled();
  });
});