summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/hooks/useToolScheduler.test.ts
blob: 10ba4f284af3fc23a9eb39ef45f2685ad2fe442b (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import { formatLlmContentForFunctionResponse } from './useToolScheduler.js';
import { Part, PartListUnion } from '@google/genai';

describe('formatLlmContentForFunctionResponse', () => {
  it('should handle simple string llmContent', () => {
    const llmContent = 'Simple text output';
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({ output: 'Simple text output' });
    expect(additionalParts).toEqual([]);
  });

  it('should handle llmContent as a single Part with text', () => {
    const llmContent: Part = { text: 'Text from Part object' };
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({ output: 'Text from Part object' });
    expect(additionalParts).toEqual([]);
  });

  it('should handle llmContent as a PartListUnion array with a single text Part', () => {
    const llmContent: PartListUnion = [{ text: 'Text from array' }];
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({ output: 'Text from array' });
    expect(additionalParts).toEqual([]);
  });

  it('should handle llmContent with inlineData', () => {
    const llmContent: Part = {
      inlineData: { mimeType: 'image/png', data: 'base64...' },
    };
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({
      status: 'Binary content of type image/png was processed.',
    });
    expect(additionalParts).toEqual([llmContent]);
  });

  it('should handle llmContent with fileData', () => {
    const llmContent: Part = {
      fileData: { mimeType: 'application/pdf', fileUri: 'gs://...' },
    };
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({
      status: 'Binary content of type application/pdf was processed.',
    });
    expect(additionalParts).toEqual([llmContent]);
  });

  it('should handle llmContent as an array of multiple Parts (text and inlineData)', () => {
    const llmContent: PartListUnion = [
      { text: 'Some textual description' },
      { inlineData: { mimeType: 'image/jpeg', data: 'base64data...' } },
      { text: 'Another text part' },
    ];
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({
      status: 'Tool execution succeeded.',
    });
    expect(additionalParts).toEqual(llmContent);
  });

  it('should handle llmContent as an array with a single inlineData Part', () => {
    const llmContent: PartListUnion = [
      { inlineData: { mimeType: 'image/gif', data: 'gifdata...' } },
    ];
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    // When the array is a single Part and that part is inlineData
    expect(functionResponseJson).toEqual({
      status: 'Binary content of type image/gif was processed.',
    });
    expect(additionalParts).toEqual(llmContent);
  });

  it('should handle llmContent as a generic Part (not text, inlineData, or fileData)', () => {
    // This case might represent a malformed or unexpected Part type.
    // For example, a Part that is just an empty object or has other properties.
    const llmContent: Part = { functionCall: { name: 'test', args: {} } }; // Example of a non-standard part for this context
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({
      status: 'Tool execution succeeded.',
    });
    expect(additionalParts).toEqual([llmContent]);
  });

  it('should handle empty string llmContent', () => {
    const llmContent = '';
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({ output: '' });
    expect(additionalParts).toEqual([]);
  });

  it('should handle llmContent as an empty array', () => {
    const llmContent: PartListUnion = [];
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({
      status: 'Tool execution succeeded.',
    });
    expect(additionalParts).toEqual([]);
  });

  it('should handle llmContent as a Part with undefined inlineData/fileData/text', () => {
    const llmContent: Part = {}; // An empty part object
    const { functionResponseJson, additionalParts } =
      formatLlmContentForFunctionResponse(llmContent);
    expect(functionResponseJson).toEqual({
      status: 'Tool execution succeeded.',
    });
    expect(additionalParts).toEqual([llmContent]);
  });
});