Skip to content

Commit bbaf161

Browse files
committed
test(agents): Provide basic test coverage
1 parent 25dfa07 commit bbaf161

3 files changed

Lines changed: 66 additions & 10 deletions

File tree

packages/agents/src/llm.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,28 @@ import { consoleTransport, Logger } from '@metamask/logger';
33
import { kunser } from '@metamask/ocap-kernel';
44
import type { Kernel } from '@metamask/ocap-kernel';
55
import { makeKernel } from '@ocap/nodejs';
6-
import { expect, describe, it, beforeEach } from 'vitest';
6+
import { expect, describe, it, beforeEach, vi } from 'vitest';
77

8+
import { makeLlm } from './llm.ts';
89
import { getBundleSpec } from './vats/index.ts';
910

1011
const logger = new Logger({
1112
tags: ['test'],
1213
transports: [consoleTransport],
1314
});
1415

16+
const mocks = vi.hoisted(() => ({
17+
Ollama: vi.fn(() => ({
18+
generate: vi.fn().mockResolvedValue({
19+
[Symbol.asyncIterator]: vi.fn(),
20+
}),
21+
chat: vi.fn().mockResolvedValue({
22+
[Symbol.asyncIterator]: vi.fn(),
23+
}),
24+
})),
25+
makeFarGenerator: vi.fn(),
26+
}));
27+
1528
describe('llm', () => {
1629
let kernel: Kernel;
1730
beforeEach(async () => {
@@ -23,6 +36,41 @@ describe('llm', () => {
2336
});
2437
});
2538

39+
vi.mock('ollama/browser', () => ({
40+
Ollama: mocks.Ollama,
41+
}));
42+
43+
vi.mock('@metamask/streams/vat', () => ({
44+
makeFarGenerator: mocks.makeFarGenerator,
45+
}));
46+
47+
describe('makeLlm', () => {
48+
it('should return an object with generate and chat methods', async () => {
49+
const llm = await makeLlm();
50+
expect(llm).toHaveProperty('generate');
51+
expect(llm).toHaveProperty('chat');
52+
});
53+
54+
it('should pass config to Ollama', async () => {
55+
const config = { host: 'http://test' };
56+
await makeLlm(config);
57+
// check that the Ollama constructor was called with the correct config
58+
expect(mocks.Ollama).toHaveBeenCalledOnce();
59+
expect(mocks.Ollama.mock.calls?.[0]).toMatchObject([config]);
60+
});
61+
62+
it.each(['generate', 'chat'])(
63+
'should promise a FarGenerator from its %s method',
64+
async (method) => {
65+
const llm = await makeLlm();
66+
// @ts-expect-error The underlying ollama library is mocked in these tests
67+
const result = await llm[method as keyof typeof llm]();
68+
expect(mocks.makeFarGenerator).toHaveBeenCalledOnce();
69+
expect(mocks.makeFarGenerator.mock.calls?.[0]).toMatchObject([result]);
70+
},
71+
);
72+
});
73+
2674
// Only run in development mode
2775
// eslint-disable-next-line n/no-process-env
2876
describe.runIf(process.env.NODE_ENV === 'development')('integration', () => {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { getBundleSpec } from './index.ts';
4+
5+
describe('getBundleSpec', () => {
6+
it.each(['ollama', 'user'])(
7+
'should return a valid bundle spec for %s',
8+
(bundleName) => {
9+
const bundleSpec = getBundleSpec(bundleName);
10+
expect(typeof bundleSpec).toBe('string');
11+
expect(bundleSpec).toMatch(/^file:\/\//u);
12+
expect(bundleSpec).toMatch(new RegExp(`${bundleName}\\.bundle$`, 'u'));
13+
},
14+
);
15+
});

packages/agents/src/vats/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,5 @@
44
* @param bundleName - The name of the bundle.
55
* @returns The bundle spec.
66
*/
7-
export const getBundleSpec = (bundleName: string): string => {
8-
try {
9-
return `file://${new URL(`./${bundleName}.bundle`, import.meta.url).pathname}`;
10-
} catch (error) {
11-
throw new Error(
12-
`Failed to getBundleSpec for ${bundleName}: ${error instanceof Error ? error.message : 'Unknown error'}`,
13-
);
14-
}
15-
};
7+
export const getBundleSpec = (bundleName: string): string =>
8+
`file://${new URL(`./${bundleName}.bundle`, import.meta.url).pathname}`;

0 commit comments

Comments
 (0)