Skip to content

Commit 4e62ac9

Browse files
chore(add-tests): plugin anyone: test config and test coverage (elizaOS#2854)
* plugin-anyone: vitest config * plugin-anyone: vitest config * plugin-anyone: vitest config - fixing coverage * plugin-anyone: start anyone tests * plugin-anyone: stop anyone tests --------- Co-authored-by: Sayo <hi@sayo.wtf>
1 parent e30481e commit 4e62ac9

File tree

4 files changed

+225
-1
lines changed

4 files changed

+225
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { startAnyone } from '../../src/actions/startAnyone';
3+
import { AnyoneClientService } from '../../src/services/AnyoneClientService';
4+
import { AnyoneProxyService } from '../../src/services/AnyoneProxyService';
5+
6+
vi.mock('../../src/services/AnyoneClientService', () => ({
7+
AnyoneClientService: {
8+
initialize: vi.fn(),
9+
getInstance: vi.fn(),
10+
stop: vi.fn(),
11+
}
12+
}));
13+
14+
vi.mock('../../src/services/AnyoneProxyService', () => ({
15+
AnyoneProxyService: {
16+
getInstance: vi.fn(() => ({
17+
initialize: vi.fn(),
18+
cleanup: vi.fn()
19+
}))
20+
}
21+
}));
22+
23+
describe('startAnyone Action', () => {
24+
const mockRuntime = {
25+
getSetting: vi.fn(),
26+
getState: vi.fn(),
27+
setState: vi.fn(),
28+
};
29+
30+
const mockMessage = {
31+
content: {
32+
text: 'Start Anyone',
33+
type: 'START_ANYONE'
34+
}
35+
};
36+
37+
const mockState = {};
38+
const mockCallback = vi.fn();
39+
40+
beforeEach(() => {
41+
vi.clearAllMocks();
42+
});
43+
44+
describe('validate', () => {
45+
it('should validate successfully', async () => {
46+
const result = await startAnyone.validate(mockRuntime, mockMessage);
47+
expect(result).toBe(true);
48+
});
49+
});
50+
51+
describe('handler', () => {
52+
it('should initialize AnyoneClientService and AnyoneProxyService', async () => {
53+
const mockProxyInstance = {
54+
initialize: vi.fn(),
55+
cleanup: vi.fn()
56+
};
57+
vi.mocked(AnyoneProxyService.getInstance).mockReturnValue(mockProxyInstance);
58+
59+
const result = await startAnyone.handler(
60+
mockRuntime,
61+
mockMessage,
62+
mockState,
63+
{},
64+
mockCallback
65+
);
66+
67+
expect(AnyoneClientService.initialize).toHaveBeenCalled();
68+
expect(AnyoneProxyService.getInstance).toHaveBeenCalled();
69+
expect(mockProxyInstance.initialize).toHaveBeenCalled();
70+
expect(mockCallback).toHaveBeenCalledWith({
71+
text: 'Started Anyone'
72+
});
73+
expect(result).toBe(true);
74+
});
75+
76+
it('should handle initialization errors gracefully', async () => {
77+
const error = new Error('Initialization failed');
78+
vi.mocked(AnyoneClientService.initialize).mockRejectedValue(error);
79+
80+
await expect(
81+
startAnyone.handler(mockRuntime, mockMessage, mockState, {}, mockCallback)
82+
).rejects.toThrow('Initialization failed');
83+
});
84+
});
85+
86+
describe('metadata', () => {
87+
it('should have correct name and similes', () => {
88+
expect(startAnyone.name).toBe('START_ANYONE');
89+
expect(startAnyone.similes).toEqual(['ANYONE']);
90+
});
91+
92+
it('should have valid examples', () => {
93+
expect(Array.isArray(startAnyone.examples)).toBe(true);
94+
expect(startAnyone.examples.length).toBeGreaterThan(0);
95+
96+
startAnyone.examples.forEach(example => {
97+
expect(Array.isArray(example)).toBe(true);
98+
expect(example.length).toBe(2);
99+
expect(example[1].content.action).toBe('START_ANYONE');
100+
});
101+
});
102+
});
103+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { stopAnyone } from '../../src/actions/stopAnyone';
3+
import { AnyoneClientService } from '../../src/services/AnyoneClientService';
4+
import { AnyoneProxyService } from '../../src/services/AnyoneProxyService';
5+
6+
vi.mock('../../src/services/AnyoneClientService', () => ({
7+
AnyoneClientService: {
8+
initialize: vi.fn(),
9+
getInstance: vi.fn(),
10+
stop: vi.fn(),
11+
}
12+
}));
13+
14+
vi.mock('../../src/services/AnyoneProxyService', () => ({
15+
AnyoneProxyService: {
16+
getInstance: vi.fn(() => ({
17+
initialize: vi.fn(),
18+
cleanup: vi.fn()
19+
}))
20+
}
21+
}));
22+
23+
describe('stopAnyone Action', () => {
24+
const mockRuntime = {
25+
getSetting: vi.fn(),
26+
getState: vi.fn(),
27+
setState: vi.fn(),
28+
};
29+
30+
const mockMessage = {
31+
content: {
32+
text: 'Stop Anyone',
33+
type: 'STOP_ANYONE'
34+
}
35+
};
36+
37+
const mockState = {};
38+
const mockCallback = vi.fn();
39+
40+
beforeEach(() => {
41+
vi.clearAllMocks();
42+
});
43+
44+
describe('validate', () => {
45+
it('should validate successfully', async () => {
46+
const result = await stopAnyone.validate(mockRuntime, mockMessage);
47+
expect(result).toBe(true);
48+
});
49+
});
50+
51+
describe('handler', () => {
52+
it('should stop AnyoneClientService and cleanup AnyoneProxyService', async () => {
53+
const mockProxyInstance = {
54+
initialize: vi.fn(),
55+
cleanup: vi.fn()
56+
};
57+
vi.mocked(AnyoneProxyService.getInstance).mockReturnValue(mockProxyInstance);
58+
59+
const result = await stopAnyone.handler(
60+
mockRuntime,
61+
mockMessage,
62+
mockState,
63+
{},
64+
mockCallback
65+
);
66+
67+
expect(mockProxyInstance.cleanup).toHaveBeenCalled();
68+
expect(AnyoneClientService.stop).toHaveBeenCalled();
69+
expect(mockCallback).toHaveBeenCalledWith({
70+
text: 'Stopped Anyone and cleaned up proxy'
71+
});
72+
expect(result).toBe(true);
73+
});
74+
75+
it('should handle cleanup errors gracefully', async () => {
76+
const error = new Error('Cleanup failed');
77+
vi.mocked(AnyoneClientService.stop).mockRejectedValue(error);
78+
79+
await expect(
80+
stopAnyone.handler(mockRuntime, mockMessage, mockState, {}, mockCallback)
81+
).rejects.toThrow('Cleanup failed');
82+
});
83+
});
84+
85+
describe('metadata', () => {
86+
it('should have correct name and similes', () => {
87+
expect(stopAnyone.name).toBe('STOP_ANYONE');
88+
expect(stopAnyone.similes).toEqual(['STOP_PROXY']);
89+
});
90+
91+
it('should have valid examples', () => {
92+
expect(Array.isArray(stopAnyone.examples)).toBe(true);
93+
expect(stopAnyone.examples.length).toBeGreaterThan(0);
94+
95+
stopAnyone.examples.forEach(example => {
96+
expect(Array.isArray(example)).toBe(true);
97+
expect(example.length).toBe(2);
98+
expect(example[1].content.action).toBe('STOP_ANYONE');
99+
});
100+
});
101+
});
102+
});

packages/plugin-anyone/package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
"axios": "^1.7.9",
1111
"tsup": "8.3.5"
1212
},
13+
"devDependencies": {
14+
"vitest": "^1.2.1",
15+
"@vitest/coverage-v8": "^1.2.1"
16+
},
1317
"scripts": {
1418
"build": "tsup --format esm --dts",
15-
"dev": "tsup --format esm --dts --watch"
19+
"dev": "tsup --format esm --dts --watch",
20+
"test": "vitest run",
21+
"test:watch": "vitest",
22+
"test:coverage": "vitest run --coverage"
1623
},
1724
"peerDependencies": {
1825
"whatwg-url": "7.1.0"
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
environment: 'node',
7+
include: ['__tests__/**/*.test.ts'],
8+
mockReset: true,
9+
clearMocks: true,
10+
restoreMocks: true,
11+
},
12+
});

0 commit comments

Comments
 (0)