Skip to content

Commit 049d08a

Browse files
author
Guiners
committed
adding mock to live-ground-ragengine-with-txt
1 parent 69e4614 commit 049d08a

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

genai/test/live-ground-ragengine-with-txt.test.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,53 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const sinon = require('sinon');
19-
const {describe, it, beforeEach, afterEach} = require('mocha');
18+
const {describe, it} = require('mocha');
19+
const proxyquire = require('proxyquire');
2020

21-
const sample = require('../live/live-ground-ragengine-with-txt');
2221
const {delay} = require('./util');
2322

2423
describe('live-ground-ragengine-with-txt', () => {
25-
let mockClient, mockSession;
26-
27-
beforeEach(() => {
28-
mockSession = {
29-
async *receive() {
30-
yield {
31-
text: 'In December 2023, Google launched Gemini, their "most capable and general model". It\'s multimodal, meaning it understands and combines different types of information like text, code, audio, images, and video.',
32-
};
33-
},
34-
sendClientContent: sinon.stub().resolves(),
35-
close: sinon.stub().resolves(),
24+
it('should return text from mocked RAG session', async function () {
25+
const fakeSession = {
26+
sendClientContent: async () => {},
27+
close: async () => {},
3628
};
3729

38-
mockClient = {
39-
aio: {
40-
live: {
41-
connect: sinon.stub().resolves(mockSession),
30+
const mockClient = {
31+
live: {
32+
connect: async (opts = {}) => {
33+
setImmediate(() =>
34+
opts.callbacks.onmessage({
35+
text: 'In December 2023, Google launched Gemini...',
36+
serverContent: {turnComplete: false},
37+
})
38+
);
39+
setImmediate(() =>
40+
opts.callbacks.onmessage({
41+
text: 'Mock final message.',
42+
serverContent: {turnComplete: true},
43+
})
44+
);
45+
46+
return fakeSession;
4247
},
4348
},
4449
};
4550

46-
sinon.stub(require('@google/genai'), 'GoogleGenAI').returns(mockClient);
47-
});
48-
49-
afterEach(() => {
50-
sinon.restore();
51-
});
51+
const sample = proxyquire('../live/live-ground-ragengine-with-txt', {
52+
'@google/genai': {
53+
GoogleGenAI: function () {
54+
return mockClient;
55+
},
56+
Modality: {TEXT: 'TEXT'},
57+
},
58+
});
5259

53-
it('should return text from mocked RAG session', async function () {
54-
this.timeout(180000);
60+
this.timeout(10000);
5561
this.retries(4);
5662
await delay(this.test);
57-
5863
const output = await sample.generateLiveRagTextResponse();
59-
6064
console.log('Generated output:', output);
61-
6265
assert(output.length > 0);
6366
});
6467
});

genai/test/live-txt-with-audio.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('live-txt-with-audio', () => {
3232
const fakeClient = {
3333
live: {
3434
connect: async (opts = {}) => {
35-
console.log('Mock is called'); // <--- 👈
35+
console.log('Mock is called');
3636

3737
if (
3838
opts &&

0 commit comments

Comments
 (0)