|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { GitHubClient, GitHubClientInterface } from '../src'; |
| 3 | +import { AgentRuntime, IAgentRuntime } from '@elizaos/core'; |
| 4 | +import { Octokit } from '@octokit/rest'; |
| 5 | +import simpleGit from 'simple-git'; |
| 6 | +import fs from 'fs'; |
| 7 | +import fsPromises from 'fs/promises'; |
| 8 | + |
| 9 | +// Mock external dependencies |
| 10 | +vi.mock('@octokit/rest', () => ({ |
| 11 | + Octokit: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('simple-git', () => ({ |
| 15 | + default: vi.fn(() => ({ |
| 16 | + clone: vi.fn(), |
| 17 | + pull: vi.fn(), |
| 18 | + checkout: vi.fn(), |
| 19 | + })), |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock('fs/promises', async (importOriginal) => { |
| 23 | + const actual = await importOriginal() as typeof fsPromises; |
| 24 | + return { |
| 25 | + ...actual, |
| 26 | + mkdir: vi.fn(), |
| 27 | + lstat: vi.fn(), |
| 28 | + readdir: vi.fn(), |
| 29 | + readFile: vi.fn(), |
| 30 | + writeFile: vi.fn(), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +vi.mock('fs', async (importOriginal) => { |
| 35 | + const actual = await importOriginal() as typeof fs; |
| 36 | + return { |
| 37 | + ...actual, |
| 38 | + existsSync: vi.fn(), |
| 39 | + realpathSync: vi.fn(), |
| 40 | + lstatSync: vi.fn(), |
| 41 | + readdirSync: vi.fn(), |
| 42 | + }; |
| 43 | +}); |
| 44 | + |
| 45 | +describe('GitHubClient', () => { |
| 46 | + let mockRuntime: AgentRuntime; |
| 47 | + const mockConfig = { |
| 48 | + GITHUB_OWNER: 'testowner', |
| 49 | + GITHUB_REPO: 'testrepo', |
| 50 | + GITHUB_BRANCH: 'main', |
| 51 | + GITHUB_PATH: 'src', |
| 52 | + GITHUB_API_TOKEN: 'ghp_test123', |
| 53 | + }; |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks(); |
| 57 | + mockRuntime = { |
| 58 | + getSetting: vi.fn((key: string) => mockConfig[key as keyof typeof mockConfig]), |
| 59 | + } as unknown as AgentRuntime; |
| 60 | + }); |
| 61 | + |
| 62 | + it('initializes with correct configuration', () => { |
| 63 | + const client = new GitHubClient(mockRuntime); |
| 64 | + expect(Octokit).toHaveBeenCalledWith({ auth: mockConfig.GITHUB_API_TOKEN }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('GitHubClientInterface', () => { |
| 68 | + it('has start and stop methods', () => { |
| 69 | + expect(GitHubClientInterface.start).toBeDefined(); |
| 70 | + expect(GitHubClientInterface.stop).toBeDefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('start method initializes client', async () => { |
| 74 | + const runtime = { |
| 75 | + getSetting: vi.fn((key: string) => mockConfig[key as keyof typeof mockConfig]), |
| 76 | + } as unknown as IAgentRuntime; |
| 77 | + |
| 78 | + await GitHubClientInterface.start(runtime); |
| 79 | + // Add more specific assertions based on what start should do |
| 80 | + }); |
| 81 | + |
| 82 | + it('stop method cleans up resources', () => { |
| 83 | + const runtime = {} as IAgentRuntime; |
| 84 | + GitHubClientInterface.stop(runtime); |
| 85 | + // Add assertions for cleanup if needed |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments