Skip to content

Commit

Permalink
test: write first test file
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Jan 16, 2025
1 parent 30b673d commit 7d385b0
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 1 deletion.
153 changes: 153 additions & 0 deletions packages/vertexai/__tests__/chat-session-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* @license
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, expect, it } from '@jest/globals';
import { validateChatHistory } from '../lib/methods/chat-session-helpers';
import { Content } from '../lib/types';
import { FirebaseError } from '@firebase/util';

describe('chat-session-helpers', () => {
describe('validateChatHistory', () => {
const TCS: Array<{ history: Content[]; isValid: boolean }> = [
{
history: [{ role: 'user', parts: [{ text: 'hi' }] }],
isValid: true,
},
{
history: [
{
role: 'user',
parts: [{ text: 'hi' }, { inlineData: { mimeType: 'image/jpeg', data: 'base64==' } }],
},
],
isValid: true,
},
{
history: [
{ role: 'user', parts: [{ text: 'hi' }] },
{ role: 'model', parts: [{ text: 'hi' }, { text: 'hi' }] },
],
isValid: true,
},
{
history: [
{ role: 'user', parts: [{ text: 'hi' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'greet', args: { name: 'user' } } }],
},
],
isValid: true,
},
{
history: [
{ role: 'user', parts: [{ text: 'hi' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'greet', args: { name: 'user' } } }],
},
{
role: 'function',
parts: [
{
functionResponse: { name: 'greet', response: { name: 'user' } },
},
],
},
],
isValid: true,
},
{
history: [
{ role: 'user', parts: [{ text: 'hi' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'greet', args: { name: 'user' } } }],
},
{
role: 'function',
parts: [
{
functionResponse: { name: 'greet', response: { name: 'user' } },
},
],
},
{
role: 'model',
parts: [{ text: 'hi name' }],
},
],
isValid: true,
},
{
//@ts-expect-error
history: [{ role: 'user', parts: '' }],
isValid: false,
},
{
//@ts-expect-error
history: [{ role: 'user' }],
isValid: false,
},
{
history: [{ role: 'user', parts: [] }],
isValid: false,
},
{
history: [{ role: 'model', parts: [{ text: 'hi' }] }],
isValid: false,
},
{
history: [
{
role: 'function',
parts: [
{
functionResponse: { name: 'greet', response: { name: 'user' } },
},
],
},
],
isValid: false,
},
{
history: [
{ role: 'user', parts: [{ text: 'hi' }] },
{ role: 'user', parts: [{ text: 'hi' }] },
],
isValid: false,
},
{
history: [
{ role: 'user', parts: [{ text: 'hi' }] },
{ role: 'model', parts: [{ text: 'hi' }] },
{ role: 'model', parts: [{ text: 'hi' }] },
],
isValid: false,
},
];
TCS.forEach((tc, index) => {
it(`case ${index}`, () => {
const fn = (): void => validateChatHistory(tc.history);
if (tc.isValid) {
expect(fn).not.toThrow();
} else {
expect(fn).toThrow(FirebaseError);
}
});
});
});
});
2 changes: 1 addition & 1 deletion packages/vertexai/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ESNext"
],
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "Bundler",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
Expand All @@ -21,6 +22,5 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext"
}
}

0 comments on commit 7d385b0

Please sign in to comment.