-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(firestore-multimodal-genai): added more tests
- Loading branch information
Showing
6 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
Binary file added
BIN
+6.99 MB
firestore-multimodal-genai/functions/__tests__/fixtures/large-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions
20
firestore-multimodal-genai/functions/src/__mocks__/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default { | ||
vertex: { | ||
model: 'gemini-pro', | ||
}, | ||
googleAi: { | ||
model: 'gemini-pro', | ||
apiKey: 'test-api-key', | ||
}, | ||
location: 'us-central1', | ||
projectId: 'text-project-id', | ||
instanceId: 'text-instance-id', | ||
collectionName: 'discussions', | ||
prompt: 'test prompt', | ||
responseField: 'output', | ||
candidateCount: 1, | ||
provider: 'vertex-ai', | ||
apiKey: process.env.API_KEY, | ||
bucketName: 'demo-gcp.appspot.com', | ||
imageField: 'image', | ||
}; |
48 changes: 48 additions & 0 deletions
48
firestore-multimodal-genai/functions/src/generative-client/image_utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import {compressImageBuffer, extractBucketName} from './image_utils'; | ||
describe('compressImageBuffer', () => { | ||
test('should compress large image', async () => { | ||
// get image fixture | ||
|
||
const imagePath = path.posix.join( | ||
__dirname, | ||
'../../__tests__/fixtures/large-image.jpg' | ||
); | ||
|
||
const imageBuffer = fs.readFileSync(imagePath); | ||
|
||
const compressed = await compressImageBuffer(imageBuffer, 'jpg'); | ||
|
||
expect(compressed).toBeDefined(); | ||
expect(compressed.length).toBeLessThan(imageBuffer.length); | ||
expect(compressed.length).toBeLessThan(900000); | ||
}); | ||
}); | ||
|
||
describe('extractBucketName', () => { | ||
test('extracts the bucket name from a valid URL', () => { | ||
const url = 'gs://my-bucket/path/to/object'; | ||
expect(extractBucketName(url)).toBe('my-bucket'); | ||
}); | ||
|
||
test('throws an error for URLs not containing gs://', () => { | ||
const url = 'http://my-bucket/path/to/object'; | ||
expect(() => extractBucketName(url)).toThrow('Invalid URL format'); | ||
}); | ||
|
||
test('throws an error for URLs with gs:// not at the start', () => { | ||
const url = 'http://my-bucket/gs://path/to/object'; | ||
expect(() => extractBucketName(url)).toThrow('Invalid URL format'); | ||
}); | ||
|
||
test('handles URLs with only the bucket name after gs://', () => { | ||
const url = 'gs://my-bucket'; | ||
expect(extractBucketName(url)).toBe('my-bucket'); | ||
}); | ||
|
||
test('throws an error for empty strings', () => { | ||
const url = ''; | ||
expect(() => extractBucketName(url)).toThrow('Invalid URL format'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as logFunctions from './logs'; // Adjust the path to where your functions are located | ||
import {logger} from 'firebase-functions'; | ||
import config from './__mocks__/config'; | ||
|
||
jest.mock('firebase-functions', () => ({ | ||
logger: { | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
log: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('Log Functions', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('init logs correct information', () => { | ||
logFunctions.init(config); | ||
expect(logger.info).toHaveBeenCalledWith( | ||
expect.stringContaining('Initialized with config') | ||
); | ||
|
||
const obfuscatedConfig = { | ||
...config, | ||
apiKey: '[REDACTED]', | ||
}; | ||
expect(logger.info).toHaveBeenCalledWith( | ||
expect.stringContaining(JSON.stringify(obfuscatedConfig)) | ||
); | ||
}); | ||
|
||
test('missingField logs correct information', () => { | ||
logFunctions.missingField('field', 'path'); | ||
expect(logger.info).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
"Missing ordering field 'field' on document 'path'" | ||
) | ||
); | ||
}); | ||
|
||
test('receivedAPIResponse logs correct information', () => { | ||
logFunctions.receivedAPIResponse('path', 100); | ||
expect(logger.info).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
"[firestore-multimodal-genai] Received API response for document 'path' in 100ms" | ||
) | ||
); | ||
}); | ||
|
||
test('errorCallingGLMAPI logs correct error message', () => { | ||
const error = new Error('Test Error'); | ||
logFunctions.errorCallingGLMAPI('path', error); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
expect.stringContaining("Error calling Gemini API for document 'path'") | ||
); | ||
}); | ||
|
||
test('usingADC logs correct message', () => { | ||
logFunctions.usingADC(); | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'no API key provided, using application default credentials.' | ||
) | ||
); | ||
}); | ||
|
||
test('usingAPIKey logs correct message', () => { | ||
logFunctions.usingAPIKey(); | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('using API key provided.') | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters