Skip to content

Commit

Permalink
Stub GCLOUD_PROJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDoyle committed Oct 23, 2024
1 parent e7070d4 commit 1cf8bda
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 49 deletions.
48 changes: 48 additions & 0 deletions js/plugins/google-cloud/jest.config.ts
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;
9 changes: 6 additions & 3 deletions js/plugins/google-cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:clean": "rimraf ./lib",
"build": "npm-run-all build:clean check compile",
"build:watch": "tsup-node --watch",
"test": "tsx --test ./tests/*_test.ts"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest --verbose"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -50,13 +50,16 @@
"winston": "^3.12.0"
},
"peerDependencies": {
"genkit": "workspace:*",
"@genkit-ai/core": "workspace:*"
"@genkit-ai/core": "workspace:*",
"genkit": "workspace:*"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/node": "^20.11.16",
"jest": "^29.7.0",
"npm-run-all": "^4.1.5",
"rimraf": "^6.0.1",
"ts-jest": "^29.1.2",
"tsup": "^8.0.2",
"tsx": "^4.7.0",
"typescript": "^4.9.0"
Expand Down
32 changes: 26 additions & 6 deletions js/plugins/google-cloud/tests/logs_no_io_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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({
Expand All @@ -56,7 +76,7 @@ describe('GoogleCloudLogs no I/O', () => {
logLines = '';
__getSpanExporterForTesting().reset();
});
after(async () => {
afterAll(async () => {
await ai.stopServers();
});

Expand Down Expand Up @@ -91,7 +111,7 @@ describe('GoogleCloudLogs no I/O', () => {
),
true
);
});
}, 10000); //timeout

it('writes generate logs', async () => {
const testModel = createModel(ai, 'testModel', async () => {
Expand Down
34 changes: 27 additions & 7 deletions js/plugins/google-cloud/tests/logs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -39,7 +59,7 @@ describe('GoogleCloudLogs', () => {

let ai: Genkit;

before(async () => {
beforeAll(async () => {
process.env.GENKIT_ENV = 'dev';
__addTransportStreamForTesting(logStream);
await enableGoogleCloudTelemetry({
Expand All @@ -58,7 +78,7 @@ describe('GoogleCloudLogs', () => {
logLines = '';
__getSpanExporterForTesting().reset();
});
after(async () => {
afterAll(async () => {
await ai.stopServers();
});

Expand Down Expand Up @@ -94,7 +114,7 @@ describe('GoogleCloudLogs', () => {
),
true
);
});
}, 10000); //timeout

it('writes generate logs', async () => {
const testModel = createModel(ai, 'testModel', async () => {
Expand Down
44 changes: 32 additions & 12 deletions js/plugins/google-cloud/tests/metrics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -61,7 +81,7 @@ describe('GoogleCloudMetrics', () => {
__getMetricExporterForTesting().reset();
__getSpanExporterForTesting().reset();
});
after(async () => {
afterAll(async () => {
await ai.stopServers();
});

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 1cf8bda

Please sign in to comment.