UNPKG

taskforce-aiagent

Version:

TaskForce is a modular, open-source, production-ready TypeScript agent framework for orchestrating AI agents, LLM-powered autonomous agents, task pipelines, dynamic toolchains, RAG workflows and memory/retrieval systems.

96 lines (95 loc) 3.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // @ts-nocheck const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const agentTraining_helper_js_1 = require("./agentTraining.helper.js"); const agent_js_1 = require("../agents/agent.js"); jest.mock("openai", () => { const mockOpenAI = jest.fn().mockImplementation(() => ({ chat: { completions: { create: jest .fn() .mockResolvedValue({ choices: [{ message: { content: "{}" } }] }), }, }, })); return { __esModule: true, default: mockOpenAI, OpenAI: mockOpenAI, }; }); jest.mock("fs"); describe("agentTraining.helper", () => { const mockAgent = new agent_js_1.Agent({ name: "Test Agent", role: "", goal: "", model: "gpt-4o-mini", backstory: "", }); beforeEach(() => { jest.resetAllMocks(); }); describe("defaultTrainingPath", () => { it("should return correct path with normalized agent name", () => { const expectedPath = path_1.default.join(process.cwd(), "trainings", "test_agent_trained.json"); const actualPath = (0, agentTraining_helper_js_1.defaultTrainingPath)("Test Agent"); expect(actualPath).toBe(expectedPath); }); }); describe("trainAgent", () => { it("should write training result to file with proper format", async () => { const trainingExamples = [ { initialOutput: "Output 1", humanFeedback: "Feedback 1", improvedOutput: "Improved 1", }, { initialOutput: "Output 2", humanFeedback: "Feedback 2", improvedOutput: "Improved 2", }, ]; const writeFileSyncMock = jest .spyOn(fs_1.default, "writeFileSync") .mockImplementation(() => { }); await (0, agentTraining_helper_js_1.trainAgent)(mockAgent, trainingExamples, "some/path.json"); expect(writeFileSyncMock).toHaveBeenCalledTimes(1); const [calledPath, content] = writeFileSyncMock.mock.calls[0]; expect(calledPath).toBe("some/path.json"); const parsedContent = JSON.parse(content); expect(parsedContent.suggestions).toEqual([ "Improve based on: Feedback 1", "Improve based on: Feedback 2", ]); expect(parsedContent.quality).toBe(8.5); expect(parsedContent.final_summary).toBe(`Trained on ${trainingExamples.length} feedback examples.`); }); }); describe("loadTraining", () => { it("should return null if file does not exist", () => { fs_1.default.existsSync.mockReturnValue(false); const result = (0, agentTraining_helper_js_1.loadTraining)("Nonexistent Agent"); expect(result).toBeNull(); }); it("should read and parse the training data from file", () => { const trainingData = { suggestions: ["Improve something"], quality: 9, final_summary: "Summary", }; fs_1.default.existsSync.mockReturnValue(true); fs_1.default.readFileSync.mockReturnValue(JSON.stringify(trainingData)); const result = (0, agentTraining_helper_js_1.loadTraining)("Test Agent"); expect(fs_1.default.readFileSync).toHaveBeenCalled(); expect(result).toEqual(trainingData); }); }); });