|
| 1 | +// Set required env vars before requiring modules |
| 2 | +process.env.STORAGE_DIR = __dirname; |
| 3 | +process.env.NODE_ENV = "test"; |
| 4 | + |
| 5 | +const { SystemPromptVariables } = require("../../../models/systemPromptVariables"); |
| 6 | +const Provider = require("../../../utils/agents/aibitat/providers/ai-provider"); |
| 7 | + |
| 8 | +jest.mock("../../../models/systemPromptVariables"); |
| 9 | +jest.mock("../../../models/systemSettings"); |
| 10 | +jest.mock("../../../utils/agents/imported", () => ({ |
| 11 | + activeImportedPlugins: jest.fn().mockReturnValue([]), |
| 12 | +})); |
| 13 | +jest.mock("../../../utils/agentFlows", () => ({ |
| 14 | + AgentFlows: { |
| 15 | + activeFlowPlugins: jest.fn().mockReturnValue([]), |
| 16 | + }, |
| 17 | +})); |
| 18 | +jest.mock("../../../utils/MCP", () => { |
| 19 | + return jest.fn().mockImplementation(() => ({ |
| 20 | + activeMCPServers: jest.fn().mockResolvedValue([]), |
| 21 | + })); |
| 22 | +}); |
| 23 | + |
| 24 | +const { WORKSPACE_AGENT } = require("../../../utils/agents/defaults"); |
| 25 | + |
| 26 | +describe("WORKSPACE_AGENT.getDefinition", () => { |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + // Mock SystemSettings to return empty arrays for agent skills |
| 30 | + const { SystemSettings } = require("../../../models/systemSettings"); |
| 31 | + SystemSettings.getValueOrFallback = jest.fn().mockResolvedValue("[]"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should use provider default system prompt when workspace has no openAiPrompt", async () => { |
| 35 | + const workspace = { |
| 36 | + id: 1, |
| 37 | + name: "Test Workspace", |
| 38 | + openAiPrompt: null, |
| 39 | + }; |
| 40 | + const user = { id: 1 }; |
| 41 | + const provider = "openai"; |
| 42 | + const expectedPrompt = await Provider.systemPrompt({ provider, workspace, user }); |
| 43 | + const definition = await WORKSPACE_AGENT.getDefinition( |
| 44 | + provider, |
| 45 | + workspace, |
| 46 | + user |
| 47 | + ); |
| 48 | + expect(definition.role).toBe(expectedPrompt); |
| 49 | + expect(SystemPromptVariables.expandSystemPromptVariables).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should use workspace system prompt with variable expansion when openAiPrompt exists", async () => { |
| 53 | + const workspace = { |
| 54 | + id: 1, |
| 55 | + name: "Test Workspace", |
| 56 | + openAiPrompt: "You are a helpful assistant for {workspace.name}. The current user is {user.name}.", |
| 57 | + }; |
| 58 | + const user = { id: 1 }; |
| 59 | + const provider = "openai"; |
| 60 | + |
| 61 | + const expandedPrompt = "You are a helpful assistant for Test Workspace. The current user is John Doe."; |
| 62 | + SystemPromptVariables.expandSystemPromptVariables.mockResolvedValue(expandedPrompt); |
| 63 | + |
| 64 | + const definition = await WORKSPACE_AGENT.getDefinition( |
| 65 | + provider, |
| 66 | + workspace, |
| 67 | + user |
| 68 | + ); |
| 69 | + |
| 70 | + expect(SystemPromptVariables.expandSystemPromptVariables).toHaveBeenCalledWith( |
| 71 | + workspace.openAiPrompt, |
| 72 | + user.id, |
| 73 | + workspace.id |
| 74 | + ); |
| 75 | + expect(definition.role).toBe(expandedPrompt); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should handle workspace system prompt without user context", async () => { |
| 79 | + const workspace = { |
| 80 | + id: 1, |
| 81 | + name: "Test Workspace", |
| 82 | + openAiPrompt: "You are a helpful assistant. Today is {date}.", |
| 83 | + }; |
| 84 | + const user = null; |
| 85 | + const provider = "lmstudio"; |
| 86 | + const expandedPrompt = "You are a helpful assistant. Today is January 1, 2024."; |
| 87 | + SystemPromptVariables.expandSystemPromptVariables.mockResolvedValue(expandedPrompt); |
| 88 | + |
| 89 | + const definition = await WORKSPACE_AGENT.getDefinition( |
| 90 | + provider, |
| 91 | + workspace, |
| 92 | + user |
| 93 | + ); |
| 94 | + |
| 95 | + expect(SystemPromptVariables.expandSystemPromptVariables).toHaveBeenCalledWith( |
| 96 | + workspace.openAiPrompt, |
| 97 | + null, |
| 98 | + workspace.id |
| 99 | + ); |
| 100 | + expect(definition.role).toBe(expandedPrompt); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should return functions array in definition", async () => { |
| 104 | + const workspace = { id: 1, openAiPrompt: null }; |
| 105 | + const provider = "openai"; |
| 106 | + |
| 107 | + const definition = await WORKSPACE_AGENT.getDefinition( |
| 108 | + provider, |
| 109 | + workspace, |
| 110 | + null |
| 111 | + ); |
| 112 | + |
| 113 | + expect(definition).toHaveProperty("functions"); |
| 114 | + expect(Array.isArray(definition.functions)).toBe(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should use LMStudio specific prompt when workspace has no openAiPrompt", async () => { |
| 118 | + const workspace = { id: 1, openAiPrompt: null }; |
| 119 | + const user = null; |
| 120 | + const provider = "lmstudio"; |
| 121 | + const definition = await WORKSPACE_AGENT.getDefinition( |
| 122 | + provider, |
| 123 | + workspace, |
| 124 | + null |
| 125 | + ); |
| 126 | + |
| 127 | + expect(definition.role).toBe(await Provider.systemPrompt({ provider, workspace, user })); |
| 128 | + expect(definition.role).toContain("helpful ai assistant"); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
0 commit comments