Skip to content

Commit ed16b0c

Browse files
authored
Merge pull request #1840 from ai16z-demirix/test/goals-memory-provider
test: adding more tests for goals,memory and provider. Fixing generation.test.ts
2 parents 9f4694f + 5e78b7a commit ed16b0c

File tree

4 files changed

+477
-5
lines changed

4 files changed

+477
-5
lines changed

packages/core/src/tests/generation.test.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ describe("Generation", () => {
129129
});
130130

131131
describe("trimTokens", () => {
132+
let mockRuntime: IAgentRuntime;
133+
134+
beforeEach(() => {
135+
mockRuntime = {
136+
getSetting: vi.fn().mockImplementation((key: string) => {
137+
switch (key) {
138+
case "TOKENIZER_MODEL":
139+
return "gpt-4";
140+
case "TOKENIZER_TYPE":
141+
return "tiktoken";
142+
default:
143+
return undefined;
144+
}
145+
}),
146+
} as unknown as IAgentRuntime;
147+
});
148+
132149
it("should return empty string for empty input", async () => {
133150
const result = await trimTokens("", 100, mockRuntime);
134151
expect(result).toBe("");
@@ -169,10 +186,10 @@ describe("Generation", () => {
169186

170187
it("should handle multiline text", async () => {
171188
const multilineText = `Line 1
172-
Line 2
173-
Line 3
174-
Line 4
175-
Line 5`;
189+
Line 2
190+
Line 3
191+
Line 4
192+
Line 5`;
176193
const result = await trimTokens(multilineText, 5, mockRuntime);
177194
expect(result.length).toBeGreaterThan(0);
178195
expect(result.length).toBeLessThan(multilineText.length);

packages/core/src/tests/goals.test.ts

+260-1
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,15 @@ const sampleGoal: Goal = {
247247
};
248248

249249
describe("getGoals", () => {
250+
let runtime: IAgentRuntime;
251+
250252
beforeEach(() => {
251-
vi.clearAllMocks();
253+
runtime = {
254+
agentId: "test-agent-id" as UUID,
255+
databaseAdapter: {
256+
getGoals: vi.fn().mockResolvedValue([]),
257+
} as any,
258+
} as IAgentRuntime;
252259
});
253260

254261
it("retrieves goals successfully", async () => {
@@ -274,6 +281,26 @@ describe("getGoals", () => {
274281
})
275282
).rejects.toThrow("Failed to retrieve goals");
276283
});
284+
285+
it("should handle empty goals list", async () => {
286+
const mockRuntime = {
287+
agentId: "test-agent-id" as UUID,
288+
databaseAdapter: {
289+
getGoals: vi.fn().mockResolvedValue([]),
290+
},
291+
} as unknown as IAgentRuntime;
292+
293+
const roomId = "test-room" as UUID;
294+
295+
await getGoals({ runtime: mockRuntime, roomId });
296+
297+
expect(mockRuntime.databaseAdapter.getGoals).toHaveBeenCalledWith({
298+
agentId: "test-agent-id",
299+
roomId,
300+
onlyInProgress: true,
301+
count: 5,
302+
});
303+
});
277304
});
278305

279306
describe("formatGoalsAsString", () => {
@@ -292,6 +319,53 @@ describe("formatGoalsAsString", () => {
292319
const formatted = formatGoalsAsString({ goals: [] });
293320
expect(formatted).toBe("");
294321
});
322+
323+
it("should format goals as string correctly", () => {
324+
const goals: Goal[] = [
325+
{
326+
id: "1" as UUID,
327+
name: "Goal 1",
328+
status: GoalStatus.IN_PROGRESS,
329+
objectives: [
330+
{
331+
id: "obj1" as UUID,
332+
description: "Objective 1",
333+
completed: true,
334+
},
335+
{
336+
id: "obj2" as UUID,
337+
description: "Objective 2",
338+
completed: false,
339+
},
340+
],
341+
roomId: "test-room" as UUID,
342+
userId: "test-user" as UUID,
343+
},
344+
{
345+
id: "2" as UUID,
346+
name: "Goal 2",
347+
status: GoalStatus.DONE,
348+
objectives: [
349+
{
350+
id: "obj3" as UUID,
351+
description: "Objective 3",
352+
completed: true,
353+
},
354+
],
355+
roomId: "test-room" as UUID,
356+
userId: "test-user" as UUID,
357+
},
358+
];
359+
360+
const formattedGoals = formatGoalsAsString({ goals });
361+
expect(formattedGoals).toContain("Goal: Goal 1");
362+
expect(formattedGoals).toContain("id: 1");
363+
expect(formattedGoals).toContain("- [x] Objective 1 (DONE)");
364+
expect(formattedGoals).toContain("- [ ] Objective 2 (IN PROGRESS)");
365+
expect(formattedGoals).toContain("Goal: Goal 2");
366+
expect(formattedGoals).toContain("id: 2");
367+
expect(formattedGoals).toContain("- [x] Objective 3 (DONE)");
368+
});
295369
});
296370

297371
describe("updateGoal", () => {
@@ -318,6 +392,138 @@ describe("updateGoal", () => {
318392
updateGoal({ runtime: mockRuntime, goal: sampleGoal })
319393
).rejects.toThrow("Failed to update goal");
320394
});
395+
396+
it("should update goal status correctly", async () => {
397+
const goalId = "test-goal" as UUID;
398+
const mockRuntime = {
399+
databaseAdapter: { updateGoal: vi.fn() },
400+
agentId: "test-agent-id" as UUID,
401+
} as unknown as IAgentRuntime;
402+
403+
const updatedGoal: Goal = {
404+
id: goalId,
405+
name: "Test Goal",
406+
objectives: [
407+
{
408+
description: "Objective 1",
409+
completed: false,
410+
},
411+
{
412+
description: "Objective 2",
413+
completed: true,
414+
},
415+
],
416+
roomId: "room-id" as UUID,
417+
userId: "user-id" as UUID,
418+
status: GoalStatus.DONE,
419+
};
420+
421+
await updateGoal({
422+
runtime: mockRuntime,
423+
goal: updatedGoal,
424+
});
425+
426+
expect(mockRuntime.databaseAdapter.updateGoal).toHaveBeenCalledWith(updatedGoal);
427+
});
428+
429+
it("should handle failed goal update", async () => {
430+
const goalId = "test-goal" as UUID;
431+
const mockRuntime = {
432+
databaseAdapter: { updateGoal: vi.fn() },
433+
agentId: "test-agent-id" as UUID,
434+
} as unknown as IAgentRuntime;
435+
436+
const updatedGoal: Goal = {
437+
id: goalId,
438+
name: "Test Goal",
439+
objectives: [
440+
{
441+
description: "Objective 1",
442+
completed: false,
443+
},
444+
{
445+
description: "Objective 2",
446+
completed: true,
447+
},
448+
],
449+
roomId: "room-id" as UUID,
450+
userId: "user-id" as UUID,
451+
status: GoalStatus.FAILED,
452+
};
453+
454+
await updateGoal({
455+
runtime: mockRuntime,
456+
goal: updatedGoal,
457+
});
458+
459+
expect(mockRuntime.databaseAdapter.updateGoal).toHaveBeenCalledWith(updatedGoal);
460+
});
461+
462+
it("should handle in-progress goal update", async () => {
463+
const goalId = "test-goal" as UUID;
464+
const mockRuntime = {
465+
databaseAdapter: { updateGoal: vi.fn() },
466+
agentId: "test-agent-id" as UUID,
467+
} as unknown as IAgentRuntime;
468+
469+
const updatedGoal: Goal = {
470+
id: goalId,
471+
name: "Test Goal",
472+
objectives: [
473+
{
474+
description: "Objective 1",
475+
completed: false,
476+
},
477+
{
478+
description: "Objective 2",
479+
completed: true,
480+
},
481+
],
482+
roomId: "room-id" as UUID,
483+
userId: "user-id" as UUID,
484+
status: GoalStatus.IN_PROGRESS,
485+
};
486+
487+
await updateGoal({
488+
runtime: mockRuntime,
489+
goal: updatedGoal,
490+
});
491+
492+
expect(mockRuntime.databaseAdapter.updateGoal).toHaveBeenCalledWith(updatedGoal);
493+
});
494+
495+
it("should handle goal priority updates", async () => {
496+
const goalId = "test-goal" as UUID;
497+
const mockRuntime = {
498+
databaseAdapter: { updateGoal: vi.fn() },
499+
agentId: "test-agent-id" as UUID,
500+
} as unknown as IAgentRuntime;
501+
502+
const updatedGoal: Goal = {
503+
id: goalId,
504+
name: "Test Goal",
505+
objectives: [
506+
{
507+
description: "Objective 1",
508+
completed: false,
509+
},
510+
{
511+
description: "Objective 2",
512+
completed: true,
513+
},
514+
],
515+
roomId: "room-id" as UUID,
516+
userId: "user-id" as UUID,
517+
status: GoalStatus.IN_PROGRESS,
518+
};
519+
520+
await updateGoal({
521+
runtime: mockRuntime,
522+
goal: updatedGoal,
523+
});
524+
525+
expect(mockRuntime.databaseAdapter.updateGoal).toHaveBeenCalledWith(updatedGoal);
526+
});
321527
});
322528

323529
describe("createGoal", () => {
@@ -344,4 +550,57 @@ describe("createGoal", () => {
344550
createGoal({ runtime: mockRuntime, goal: sampleGoal })
345551
).rejects.toThrow("Failed to create goal");
346552
});
553+
554+
it("should create new goal with correct properties", async () => {
555+
const newGoal: Goal = {
556+
name: "New Goal",
557+
roomId: "room-id" as UUID,
558+
userId: "user-id" as UUID,
559+
status: GoalStatus.IN_PROGRESS,
560+
objectives: []
561+
};
562+
563+
const mockRuntime = {
564+
databaseAdapter: { createGoal: vi.fn() },
565+
agentId: "test-agent-id" as UUID,
566+
} as unknown as IAgentRuntime;
567+
568+
await createGoal({
569+
runtime: mockRuntime,
570+
goal: newGoal,
571+
});
572+
573+
expect(mockRuntime.databaseAdapter.createGoal).toHaveBeenCalledWith(
574+
expect.objectContaining({
575+
name: "New Goal",
576+
roomId: "room-id",
577+
userId: "user-id",
578+
status: GoalStatus.IN_PROGRESS,
579+
objectives: []
580+
})
581+
);
582+
});
583+
584+
it("should create a new goal", async () => {
585+
const mockRuntime = {
586+
databaseAdapter: { createGoal: vi.fn() },
587+
agentId: "test-agent-id" as UUID,
588+
} as unknown as IAgentRuntime;
589+
590+
const newGoal = {
591+
id: "new-goal" as UUID,
592+
name: "New Goal",
593+
objectives: [],
594+
roomId: "test-room" as UUID,
595+
userId: "test-user" as UUID,
596+
status: GoalStatus.IN_PROGRESS,
597+
};
598+
599+
await createGoal({
600+
runtime: mockRuntime,
601+
goal: newGoal,
602+
});
603+
604+
expect(mockRuntime.databaseAdapter.createGoal).toHaveBeenCalledWith(newGoal);
605+
});
347606
});

0 commit comments

Comments
 (0)