Skip to content

Commit

Permalink
Merge pull request #2407 from ai16z-demirix/test/client-github
Browse files Browse the repository at this point in the history
feat: adding tests for github client
  • Loading branch information
shakkernerd authored Jan 16, 2025
2 parents dda5feb + cbbd6df commit 8ab8be5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/client-github/__tests__/environment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect, vi } from 'vitest';
import { validateGithubConfig, githubEnvSchema } from '../src/environment';
import { IAgentRuntime } from '@elizaos/core';

describe('GitHub Environment Configuration', () => {
const mockRuntime: IAgentRuntime = {
getSetting: vi.fn(),
} as unknown as IAgentRuntime;

it('validates correct GitHub configuration', async () => {
const validConfig = {
GITHUB_OWNER: 'testowner',
GITHUB_REPO: 'testrepo',
GITHUB_BRANCH: 'main',
GITHUB_PATH: 'src',
GITHUB_API_TOKEN: 'ghp_test123',
};

vi.mocked(mockRuntime.getSetting).mockImplementation((key: string) => validConfig[key as keyof typeof validConfig]);

const config = await validateGithubConfig(mockRuntime);
expect(config).toEqual(validConfig);
});

it('throws error for missing configuration', async () => {
const invalidConfig = {
GITHUB_OWNER: '',
GITHUB_REPO: '',
GITHUB_BRANCH: '',
GITHUB_PATH: '',
GITHUB_API_TOKEN: '',
};

vi.mocked(mockRuntime.getSetting).mockImplementation((key: string) => invalidConfig[key as keyof typeof invalidConfig]);

await expect(validateGithubConfig(mockRuntime)).rejects.toThrow();
});

it('throws error for partial configuration', async () => {
const partialConfig = {
GITHUB_OWNER: 'testowner',
GITHUB_REPO: 'testrepo',
// Missing other required fields
};

vi.mocked(mockRuntime.getSetting).mockImplementation((key: string) => partialConfig[key as keyof typeof partialConfig]);

await expect(validateGithubConfig(mockRuntime)).rejects.toThrow();
});
});
88 changes: 88 additions & 0 deletions packages/client-github/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GitHubClient, GitHubClientInterface } from '../src';
import { AgentRuntime, IAgentRuntime } from '@elizaos/core';
import { Octokit } from '@octokit/rest';
import simpleGit from 'simple-git';
import fs from 'fs';
import fsPromises from 'fs/promises';

// Mock external dependencies
vi.mock('@octokit/rest', () => ({
Octokit: vi.fn(),
}));

vi.mock('simple-git', () => ({
default: vi.fn(() => ({
clone: vi.fn(),
pull: vi.fn(),
checkout: vi.fn(),
})),
}));

vi.mock('fs/promises', async (importOriginal) => {
const actual = await importOriginal() as typeof fsPromises;
return {
...actual,
mkdir: vi.fn(),
lstat: vi.fn(),
readdir: vi.fn(),
readFile: vi.fn(),
writeFile: vi.fn(),
};
});

vi.mock('fs', async (importOriginal) => {
const actual = await importOriginal() as typeof fs;
return {
...actual,
existsSync: vi.fn(),
realpathSync: vi.fn(),
lstatSync: vi.fn(),
readdirSync: vi.fn(),
};
});

describe('GitHubClient', () => {
let mockRuntime: AgentRuntime;
const mockConfig = {
GITHUB_OWNER: 'testowner',
GITHUB_REPO: 'testrepo',
GITHUB_BRANCH: 'main',
GITHUB_PATH: 'src',
GITHUB_API_TOKEN: 'ghp_test123',
};

beforeEach(() => {
vi.clearAllMocks();
mockRuntime = {
getSetting: vi.fn((key: string) => mockConfig[key as keyof typeof mockConfig]),
} as unknown as AgentRuntime;
});

it('initializes with correct configuration', () => {
const client = new GitHubClient(mockRuntime);
expect(Octokit).toHaveBeenCalledWith({ auth: mockConfig.GITHUB_API_TOKEN });
});

describe('GitHubClientInterface', () => {
it('has start and stop methods', () => {
expect(GitHubClientInterface.start).toBeDefined();
expect(GitHubClientInterface.stop).toBeDefined();
});

it('start method initializes client', async () => {
const runtime = {
getSetting: vi.fn((key: string) => mockConfig[key as keyof typeof mockConfig]),
} as unknown as IAgentRuntime;

await GitHubClientInterface.start(runtime);
// Add more specific assertions based on what start should do
});

it('stop method cleans up resources', () => {
const runtime = {} as IAgentRuntime;
GitHubClientInterface.stop(runtime);
// Add assertions for cleanup if needed
});
});
});
7 changes: 5 additions & 2 deletions packages/client-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
},
"devDependencies": {
"@types/glob": "8.1.0",
"tsup": "8.3.5"
"tsup": "8.3.5",
"vitest": "^1.2.1"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
"lint": "eslint --fix --cache .",
"test": "vitest run",
"test:watch": "vitest"
}
}

0 comments on commit 8ab8be5

Please sign in to comment.