-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7070d4
commit 1cf8bda
Showing
7 changed files
with
219 additions
and
49 deletions.
There are no files selected for viewing
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 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* For a detailed explanation regarding each configuration property, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
import type { Config } from 'jest'; | ||
|
||
const config: Config = { | ||
// Automatically clear mock calls, instances, contexts and results before every test | ||
clearMocks: true, | ||
|
||
// A preset that is used as a base for Jest's configuration | ||
preset: 'ts-jest', | ||
|
||
// The glob patterns Jest uses to detect test files | ||
testMatch: ['**/tests/**/*_test.ts'], | ||
|
||
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped | ||
testPathIgnorePatterns: ['/node_modules/'], | ||
|
||
// A map from regular expressions to paths to transformers | ||
transform: {}, // disabled for ESM | ||
|
||
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation | ||
transformIgnorePatterns: ['/node_modules/'], | ||
|
||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
}; | ||
|
||
export default config; |
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 |
---|---|---|
|
@@ -14,12 +14,19 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
afterAll, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
it, | ||
jest, | ||
} from '@jest/globals'; | ||
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; | ||
import { generate, Genkit, genkit, run, z } from 'genkit'; | ||
import { defineModel, GenerateResponseData } from 'genkit/model'; | ||
import { Genkit, generate, genkit, run, z } from 'genkit'; | ||
import { GenerateResponseData, defineModel } from 'genkit/model'; | ||
import { runWithRegistry } from 'genkit/registry'; | ||
import assert from 'node:assert'; | ||
import { after, before, beforeEach, describe, it } from 'node:test'; | ||
import { Writable } from 'stream'; | ||
import { | ||
__addTransportStreamForTesting, | ||
|
@@ -28,6 +35,19 @@ import { | |
enableGoogleCloudTelemetry, | ||
} from '../src/index.js'; | ||
|
||
jest.mock('../src/auth.js', () => { | ||
const original = jest.requireActual('../src/auth.js'); | ||
return { | ||
...(original || {}), | ||
functionToMock: jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
projectId: 'test', | ||
serviceAccountEmail: '[email protected]', | ||
}); | ||
}), | ||
}; | ||
}); | ||
|
||
describe('GoogleCloudLogs no I/O', () => { | ||
let logLines = ''; | ||
const logStream = new Writable(); | ||
|
@@ -38,7 +58,7 @@ describe('GoogleCloudLogs no I/O', () => { | |
|
||
let ai: Genkit; | ||
|
||
before(async () => { | ||
beforeAll(async () => { | ||
process.env.GENKIT_ENV = 'dev'; | ||
__addTransportStreamForTesting(logStream); | ||
await enableGoogleCloudTelemetry({ | ||
|
@@ -56,7 +76,7 @@ describe('GoogleCloudLogs no I/O', () => { | |
logLines = ''; | ||
__getSpanExporterForTesting().reset(); | ||
}); | ||
after(async () => { | ||
afterAll(async () => { | ||
await ai.stopServers(); | ||
}); | ||
|
||
|
@@ -91,7 +111,7 @@ describe('GoogleCloudLogs no I/O', () => { | |
), | ||
true | ||
); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes generate logs', async () => { | ||
const testModel = createModel(ai, 'testModel', async () => { | ||
|
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 |
---|---|---|
|
@@ -14,13 +14,20 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
afterAll, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
it, | ||
jest, | ||
} from '@jest/globals'; | ||
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; | ||
import { generate, Genkit, genkit, run, z } from 'genkit'; | ||
import { defineModel, GenerateResponseData } from 'genkit/model'; | ||
import { Genkit, generate, genkit, run, z } from 'genkit'; | ||
import { GenerateResponseData, defineModel } from 'genkit/model'; | ||
import { runWithRegistry } from 'genkit/registry'; | ||
import { appendSpan, SPAN_TYPE_ATTR } from 'genkit/tracing'; | ||
import { SPAN_TYPE_ATTR, appendSpan } from 'genkit/tracing'; | ||
import assert from 'node:assert'; | ||
import { after, before, beforeEach, describe, it } from 'node:test'; | ||
import { Writable } from 'stream'; | ||
import { | ||
__addTransportStreamForTesting, | ||
|
@@ -29,6 +36,19 @@ import { | |
enableGoogleCloudTelemetry, | ||
} from '../src/index.js'; | ||
|
||
jest.mock('../src/auth.js', () => { | ||
const original = jest.requireActual('../src/auth.js'); | ||
return { | ||
...(original || {}), | ||
functionToMock: jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
projectId: 'test', | ||
serviceAccountEmail: '[email protected]', | ||
}); | ||
}), | ||
}; | ||
}); | ||
|
||
describe('GoogleCloudLogs', () => { | ||
let logLines = ''; | ||
const logStream = new Writable(); | ||
|
@@ -39,7 +59,7 @@ describe('GoogleCloudLogs', () => { | |
|
||
let ai: Genkit; | ||
|
||
before(async () => { | ||
beforeAll(async () => { | ||
process.env.GENKIT_ENV = 'dev'; | ||
__addTransportStreamForTesting(logStream); | ||
await enableGoogleCloudTelemetry({ | ||
|
@@ -58,7 +78,7 @@ describe('GoogleCloudLogs', () => { | |
logLines = ''; | ||
__getSpanExporterForTesting().reset(); | ||
}); | ||
after(async () => { | ||
afterAll(async () => { | ||
await ai.stopServers(); | ||
}); | ||
|
||
|
@@ -94,7 +114,7 @@ describe('GoogleCloudLogs', () => { | |
), | ||
true | ||
); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes generate logs', async () => { | ||
const testModel = createModel(ai, 'testModel', async () => { | ||
|
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 |
---|---|---|
|
@@ -21,6 +21,14 @@ import { | |
__getSpanExporterForTesting, | ||
enableGoogleCloudTelemetry, | ||
} from '@genkit-ai/google-cloud'; | ||
import { | ||
afterAll, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
it, | ||
jest, | ||
} from '@jest/globals'; | ||
import { | ||
DataPoint, | ||
Histogram, | ||
|
@@ -42,12 +50,24 @@ import { defineModel } from 'genkit/model'; | |
import { runWithRegistry } from 'genkit/registry'; | ||
import { SPAN_TYPE_ATTR, appendSpan } from 'genkit/tracing'; | ||
import assert from 'node:assert'; | ||
import { after, before, beforeEach, describe, it } from 'node:test'; | ||
|
||
jest.mock('../src/auth.js', () => { | ||
const original = jest.requireActual('../src/auth.js'); | ||
return { | ||
...(original || {}), | ||
functionToMock: jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
projectId: 'test', | ||
serviceAccountEmail: '[email protected]', | ||
}); | ||
}), | ||
}; | ||
}); | ||
|
||
describe('GoogleCloudMetrics', () => { | ||
let ai: Genkit; | ||
|
||
before(async () => { | ||
beforeAll(async () => { | ||
process.env.GENKIT_ENV = 'dev'; | ||
await enableGoogleCloudTelemetry({ | ||
projectId: 'test', | ||
|
@@ -61,7 +81,7 @@ describe('GoogleCloudMetrics', () => { | |
__getMetricExporterForTesting().reset(); | ||
__getSpanExporterForTesting().reset(); | ||
}); | ||
after(async () => { | ||
afterAll(async () => { | ||
await ai.stopServers(); | ||
}); | ||
|
||
|
@@ -110,7 +130,7 @@ describe('GoogleCloudMetrics', () => { | |
assert.equal(requestCounter.attributes.source, 'ts'); | ||
assert.equal(requestCounter.attributes.error, 'TypeError'); | ||
assert.equal(requestCounter.attributes.status, 'failure'); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes feature metrics for a successful flow', async () => { | ||
const testFlow = createFlow(ai, 'testFlow'); | ||
|
@@ -153,7 +173,7 @@ describe('GoogleCloudMetrics', () => { | |
assert.equal(requestCounter.attributes.source, 'ts'); | ||
assert.equal(requestCounter.attributes.error, 'TypeError'); | ||
assert.equal(requestCounter.attributes.status, 'failure'); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes action metrics for an action inside a flow', async () => { | ||
const testAction = createAction(ai, 'testAction'); | ||
|
@@ -280,7 +300,7 @@ describe('GoogleCloudMetrics', () => { | |
assert.equal(requestCounter.attributes.source, 'ts'); | ||
assert.equal(requestCounter.attributes.status, 'failure'); | ||
assert.equal(requestCounter.attributes.error, 'TypeError'); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes generate metrics for a successful model action', async () => { | ||
const testModel = createTestModel(ai, 'testModel'); | ||
|
@@ -386,7 +406,7 @@ describe('GoogleCloudMetrics', () => { | |
assert.equal(requestCounter.attributes.status, 'failure'); | ||
assert.equal(requestCounter.attributes.error, 'TypeError'); | ||
assert.ok(requestCounter.attributes.sourceVersion); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes feature label to action metrics when running inside a flow', async () => { | ||
const testAction = createAction(ai, 'testAction'); | ||
|
@@ -437,7 +457,7 @@ describe('GoogleCloudMetrics', () => { | |
generateRequestCounter.attributes.featureName, | ||
'testGenerateAction' | ||
); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes feature label to generate metrics when running inside a flow', async () => { | ||
const testModel = createModel(ai, 'testModel', async () => { | ||
|
@@ -568,7 +588,7 @@ describe('GoogleCloudMetrics', () => { | |
['/{testFlow,t:flow}/{sub-action,t:flowStep}', 'success'], | ||
['/{testFlow,t:flow}', 'failure'], | ||
]); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes path metrics for a failing flow with exception in subaction', async () => { | ||
const flow = createFlow(ai, 'testFlow', async () => { | ||
|
@@ -613,7 +633,7 @@ describe('GoogleCloudMetrics', () => { | |
'failure', | ||
], | ||
]); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes path metrics for a flow with exception in action', async () => { | ||
const flow = createFlow(ai, 'testFlow', async () => { | ||
|
@@ -660,7 +680,7 @@ describe('GoogleCloudMetrics', () => { | |
], | ||
['/{testFlow,t:flow}/{sub-action-1,t:flowStep}', 'failure'], | ||
]); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes path metrics for a flow with an exception in a serial action', async () => { | ||
const flow = createFlow(ai, 'testFlow', async () => { | ||
|
@@ -701,7 +721,7 @@ describe('GoogleCloudMetrics', () => { | |
['/{testFlow,t:flow}/{sub-action-1,t:flowStep}', 'success'], | ||
['/{testFlow,t:flow}/{sub-action-2,t:flowStep}', 'failure'], | ||
]); | ||
}); | ||
}, 10000); //timeout | ||
|
||
it('writes user feedback metrics', async () => { | ||
appendSpan( | ||
|
Oops, something went wrong.