Skip to content

Commit

Permalink
chore: add unit test automation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 592614153
  • Loading branch information
sararob authored and copybara-github committed Dec 27, 2023
1 parent 3ec92e7 commit 71e53a3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/presubmit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
pull_request:
name: presubmit
jobs:
units:
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, 20]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: ^6.24.1
- run: node --version
- run: npm install
- run: npm run test
name: Run unit tests
env:
BUILD_TYPE: presubmit
TEST_TYPE: units
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"type": "commonjs",
"scripts": {
"clean": "gts clean",
"compile": "tsc",
"compile": "tsc -p .",
"docs": "jsdoc -c .jsdoc.js",
"predocs-test": "npm run docs",
"docs-test": "linkinator docs",
"compile:oss": "tsc -p tsconfig.json.oss",
"fix": "gts fix",
"test": "TODO",
"test": "jasmine build/test/*.js",
"system-test": "jasmine build/system_test/*.js",
"lint": "gts lint",
"clean-js-files": "find . -type f -name \"*.js\" -exec rm -f {} +",
Expand All @@ -36,9 +36,9 @@
},
"devDependencies": {
"@types/jasmine": "^5.1.2",
"jasmine": "^5.1.0",
"@types/node": "^20.9.0",
"gts": "^5.2.0",
"jasmine": "^5.1.0",
"typescript": "~5.2.0",
"jsdoc": "^4.0.0",
"jsdoc-fresh": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion system_test/end_to_end_sample_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// @ts-ignore
import * as assert from 'assert';

import {VertexAI, TextPart} from '../src';
import {TextPart, VertexAI} from '../src/index';

// TODO: this env var isn't getting populated correctly
const PROJECT = process.env.GCLOUD_PROJECT;
Expand Down
51 changes: 34 additions & 17 deletions src/index_test.ts → test/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,10 @@
/* tslint:disable */
import 'jasmine';

import {ChatSession, GenerativeModel, StartChatParams, VertexAI} from './index';
import * as StreamFunctions from './process_stream';
import {
CountTokensRequest,
FinishReason,
GenerateContentRequest,
GenerateContentResponse,
GenerateContentResult,
HarmBlockThreshold,
HarmCategory,
HarmProbability,
SafetyRating,
SafetySetting,
StreamGenerateContentResult,
} from './types/content';
import {constants} from './util';
import {ChatSession, GenerativeModel, StartChatParams, VertexAI} from '../src/index';
import * as StreamFunctions from '../src/process_stream';
import {CountTokensRequest, FinishReason, GenerateContentRequest, GenerateContentResponse, GenerateContentResult, HarmBlockThreshold, HarmCategory, HarmProbability, SafetyRating, SafetySetting, StreamGenerateContentResult,} from '../src/types/content';
import {constants} from '../src/util';

const PROJECT = 'test_project';
const LOCATION = 'test_location';
Expand Down Expand Up @@ -247,7 +235,8 @@ describe('VertexAI', () => {
location: LOCATION,
apiEndpoint: TEST_ENDPOINT_BASE_PATH,
});
spyOnProperty(vertexaiWithBasePath.preview, 'token', 'get').and.resolveTo(TEST_TOKEN);
spyOnProperty(vertexaiWithBasePath.preview, 'token', 'get')
.and.resolveTo(TEST_TOKEN);
model = vertexaiWithBasePath.preview.getGenerativeModel({
model: 'gemini-pro',
});
Expand Down Expand Up @@ -483,6 +472,13 @@ describe('ChatSession', () => {
response: Promise.resolve(TEST_MODEL_RESPONSE),
stream: testGenerator(),
};
const fetchResult =
Promise.resolve(new Response(JSON.stringify(expectedStreamResult), {
status: 200,
statusText: 'OK',
headers: {'Content-Type': 'application/json'}
}));
spyOn(global, 'fetch').and.returnValue(fetchResult);
spyOn(StreamFunctions, 'processStream').and.returnValue(
expectedStreamResult
);
Expand All @@ -501,6 +497,13 @@ describe('ChatSession', () => {
response: Promise.resolve(TEST_MODEL_RESPONSE),
stream: testGenerator(),
};
const fetchResult = Promise.resolve(
new Response(JSON.stringify(expectedStreamResult), {
status: 200,
statusText: 'OK',
headers: {'Content-Type': 'application/json'}
}));
spyOn(global, 'fetch').and.returnValue(fetchResult);
spyOn(StreamFunctions, 'processStream')
.and.returnValue(expectedStreamResult);
const resp = await chatSessionWithNoArgs.sendMessage(req);
Expand All @@ -523,6 +526,13 @@ describe('ChatSession', () => {
response: Promise.resolve(TEST_MODEL_RESPONSE),
stream: testGenerator(),
};
const fetchResult =
Promise.resolve(new Response(JSON.stringify(expectedStreamResult), {
status: 200,
statusText: 'OK',
headers: {'Content-Type': 'application/json'}
}));
spyOn(global, 'fetch').and.returnValue(fetchResult);
spyOn(StreamFunctions, 'processStream').and.returnValue(
expectedStreamResult
);
Expand Down Expand Up @@ -550,6 +560,13 @@ describe('ChatSession', () => {
},
],
});
const fetchResult =
Promise.resolve(new Response(JSON.stringify(expectedResult), {
status: 200,
statusText: 'OK',
headers: {'Content-Type': 'application/json'}
}));
spyOn(global, 'fetch').and.returnValue(fetchResult);
spyOn(StreamFunctions, 'processStream').and.returnValue(expectedResult);
expect(chatSession.history.length).toEqual(1);
expect(chatSession.history[0].role).toEqual(constants.USER_ROLE);
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"include": [
"src/*.ts",
"src/**/*.ts",
"src/**/*.js",
"test/*.ts",
"test/*.js",
"test/**/*.ts",
"system_test/*.ts"
]
Expand Down

0 comments on commit 71e53a3

Please sign in to comment.