Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test setup for 3p packages #591

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ jobs:
- name: Bootstrap
run: ./scripts/bootstrap

- name: Run tests
run: ./scripts/test
- name: Build
run: ./scripts/build-all

- name: Run tests
run: ./scripts/test-all
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config: JestConfigWithTsJest = {
'<rootDir>/dist/',
'<rootDir>/deno/',
'<rootDir>/deno_tests/',
'<rootDir>/packages/',
],
testPathIgnorePatterns: ['scripts'],
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"private": false,
"scripts": {
"test": "./scripts/test",
"test": "./scripts/test-all",
"build": "./scripts/build-all",
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
"format": "prettier --write --cache --cache-strategy metadata . !dist",
Expand Down
17 changes: 17 additions & 0 deletions packages/bedrock-sdk/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { JestConfigWithTsJest } from 'ts-jest';

const config: JestConfigWithTsJest = {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest', { sourceMaps: 'inline' }],
},
moduleNameMapper: {
'^@anthropic-ai/bedrock-sdk$': '<rootDir>/src/index.ts',
'^@anthropic-ai/bedrock-sdk/(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/deno/', '<rootDir>/deno_tests/'],
testPathIgnorePatterns: ['scripts'],
};

export default config;
2 changes: 1 addition & 1 deletion packages/bedrock-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"packageManager": "[email protected]",
"private": false,
"scripts": {
"test": "echo 'no tests defined yet' && exit 1",
"test": "jest",
"build": "bash ./build",
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
Expand Down
7 changes: 7 additions & 0 deletions packages/bedrock-sdk/scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

./node_modules/.bin/jest "$@"
6 changes: 6 additions & 0 deletions packages/bedrock-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ export class AnthropicBedrock extends Core.APIClient {
} {
options.__streamClass = Stream;

if (Core.isObj(options.body)) {
// create a shallow copy of the request body so that code that mutates it later
// doesn't mutate the original user-provided object
options.body = { ...options.body };
}

if (Core.isObj(options.body)) {
if (!options.body['anthropic_version']) {
options.body['anthropic_version'] = DEFAULT_VERSION;
Expand Down
29 changes: 29 additions & 0 deletions packages/bedrock-sdk/tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
import Anthropic from '@anthropic-ai/sdk';

test('body params are not mutated', async () => {
const client = new AnthropicBedrock({
baseURL: 'http://localhost:5000/',
awsRegion: 'us-east-1',
awsAccessKey: 'placeholder',
awsSecretKey: 'placeholder',
fetch: (url) => {
return Promise.resolve(
new Response(JSON.stringify({ url, custom: true }), {
headers: { 'Content-Type': 'application/json' },
}),
);
},
});

const params: Anthropic.MessageCreateParamsNonStreaming = {
model: 'claude-3-opus-latest',
messages: [],
max_tokens: 100,
};
const original = JSON.stringify(params);

await client.messages.create(params);

expect(JSON.stringify(params)).toEqual(original);
});
17 changes: 17 additions & 0 deletions packages/vertex-sdk/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { JestConfigWithTsJest } from 'ts-jest';

const config: JestConfigWithTsJest = {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest', { sourceMaps: 'inline' }],
},
moduleNameMapper: {
'^@anthropic-ai/vertex-sdk$': '<rootDir>/src/index.ts',
'^@anthropic-ai/vertex-sdk/(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/deno/', '<rootDir>/deno_tests/'],
testPathIgnorePatterns: ['scripts'],
};

export default config;
2 changes: 1 addition & 1 deletion packages/vertex-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"packageManager": "[email protected]",
"private": false,
"scripts": {
"test": "echo 'no tests defined yet' && exit 1",
"test": "jest",
"build": "bash ./build",
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
Expand Down
7 changes: 7 additions & 0 deletions packages/vertex-sdk/scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

./node_modules/.bin/jest "$@"
6 changes: 6 additions & 0 deletions packages/vertex-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ export class AnthropicVertex extends Core.APIClient {
url: string;
timeout: number;
} {
if (Core.isObj(options.body)) {
// create a shallow copy of the request body so that code that mutates it later
// doesn't mutate the original user-provided object
options.body = { ...options.body };
}

if (Core.isObj(options.body)) {
if (!options.body['anthropic_version']) {
options.body['anthropic_version'] = DEFAULT_VERSION;
Expand Down
29 changes: 29 additions & 0 deletions packages/vertex-sdk/tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
import Anthropic from '@anthropic-ai/sdk';

test('body params are not mutated', async () => {
const client = new AnthropicVertex({
baseURL: 'http://localhost:5000/',
accessToken: 'placeholder',
region: 'us-east-2',
projectId: 'placeholder',
fetch: (url) => {
return Promise.resolve(
new Response(JSON.stringify({ url, custom: true }), {
headers: { 'Content-Type': 'application/json' },
}),
);
},
});

const params: Anthropic.MessageCreateParamsNonStreaming = {
model: 'claude-3-opus-latest',
messages: [],
max_tokens: 100,
};
const original = JSON.stringify(params);

await client.messages.create(params);

expect(JSON.stringify(params)).toEqual(original);
});
12 changes: 12 additions & 0 deletions scripts/test-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -e

bash ./scripts/test

for dir in packages/*; do
if [ -f "$dir/scripts/test" ]; then
echo "==> Running tests in $dir"
(cd "$dir" && bash scripts/test)
fi
done
Loading