Skip to content

Commit

Permalink
ci: Add unittests action
Browse files Browse the repository at this point in the history
  • Loading branch information
asafgardin committed Nov 12, 2024
1 parent 90aa7cb commit 0fedca4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Unittest

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v4

Check failure on line 14 in .github/workflows/unittests.yml

View workflow job for this annotation

GitHub Actions / quality-checks

[indentation] wrong indentation: expected 6 but found 4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm run test

12 changes: 4 additions & 8 deletions src/AI21.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { AI21EnvConfig } from './EnvConfig';
import { MissingAPIKeyError } from './errors';
import { Chat } from './resources/chat';
import { APIClient } from './APIClient';
import { DefaultQuery, Headers } from './types';
import { Headers } from './types';

export type ClientOptions = {
baseURL?: string;
apiKey?: string;
maxRetries?: number;
timeout?: number;
via?: string | null;
defaultQuery?: DefaultQuery;
defaultHeaders?: Headers;
dangerouslyAllowBrowser?: boolean;
};
Expand Down Expand Up @@ -53,6 +52,9 @@ export class AI21 extends APIClient {
this.options = options;
}

// Resources
chat: Chat = new Chat(this);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected override authHeaders(_: Types.FinalRequestOptions): Types.Headers {
return {
Expand All @@ -67,10 +69,6 @@ export class AI21 extends APIClient {
};
}

protected defaultQuery(): DefaultQuery | undefined {
return this.options.defaultQuery;
}

protected override getUserAgent(): string {
let userAgent = super.getUserAgent();

Expand All @@ -79,6 +77,4 @@ export class AI21 extends APIClient {
}
return userAgent;
}

chat: Chat = new Chat(this);
}
11 changes: 4 additions & 7 deletions src/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
FinalRequestOptions,
APIResponseProps,
HTTPMethod,
DefaultQuery,
Headers,
} from './types/index.js';
import { AI21EnvConfig } from './EnvConfig';
Expand Down Expand Up @@ -42,19 +41,19 @@ export abstract class APIClient {
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
this.timeout = validatePositiveInteger('timeout', timeout);
}
get<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
protected get<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
return this.makeRequest('get', path, opts);
}

post<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
protected post<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
return this.makeRequest('post', path, opts);
}

put<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
protected put<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
return this.makeRequest('put', path, opts);
}

delete<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
protected delete<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
return this.makeRequest('delete', path, opts);
}

Expand All @@ -80,8 +79,6 @@ export abstract class APIClient {
return {};
}

protected abstract defaultQuery(): DefaultQuery | undefined;

private makeRequest<Req, Rsp>(method: HTTPMethod, path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
const options = {
method,
Expand Down
16 changes: 0 additions & 16 deletions tests/AI21.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,4 @@ describe('AI21', () => {
expect(headers['User-Agent']).toContain('via custom-app');
});
});

describe('defaultQuery', () => {
it('should return undefined when no default query is set', () => {
const client = new AI21(defaultOptions);
expect(client['defaultQuery']()).toBeUndefined();
});

it('should return default query when set', () => {
const defaultQuery = { version: '1.0' };
const client = new AI21({
...defaultOptions,
defaultQuery,
});
expect(client['defaultQuery']()).toEqual(defaultQuery);
});
});
});
1 change: 0 additions & 1 deletion tests/resources/chat/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { AI21Error } from '../../../src/errors';

class MockAPIClient extends APIClient {
public post = jest.fn();
public defaultQuery = () => ({});
}


Expand Down

0 comments on commit 0fedca4

Please sign in to comment.