@aj-archipelago/cortex 1.3.64 → 1.3.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ import test from 'ava';
2
+ import sinon from 'sinon';
3
+ import { getResolvers } from '../../../server/graphql.js';
4
+
5
+ // Mock logger to avoid actual logging during tests
6
+ const mockLogger = {
7
+ info: sinon.stub(),
8
+ debug: sinon.stub(),
9
+ error: sinon.stub(),
10
+ warn: sinon.stub()
11
+ };
12
+
13
+ // Mock config
14
+ const mockConfig = {
15
+ get: sinon.stub().returns('test-value')
16
+ };
17
+
18
+ test.beforeEach(t => {
19
+ // Reset stubs before each test
20
+ sinon.restore();
21
+ mockLogger.info.resetHistory();
22
+ mockLogger.debug.resetHistory();
23
+ mockLogger.error.resetHistory();
24
+ mockLogger.warn.resetHistory();
25
+ });
26
+
27
+ test('executeWorkspace throws error for legacy format with promptNames', async t => {
28
+ // Mock pathwayManager
29
+ const mockPathwayManager = {
30
+ getLatestPathways: sinon.stub().resolves({
31
+ 'test-user': {
32
+ 'test-pathway': {
33
+ prompt: ['legacy string prompt 1', 'legacy string prompt 2'], // Legacy format
34
+ systemPrompt: 'Test system prompt'
35
+ }
36
+ }
37
+ }),
38
+ isLegacyPromptFormat: sinon.stub().returns(true), // Mock returns true for legacy format
39
+ getResolvers: sinon.stub().returns({ Mutation: {} }) // Mock getResolvers method
40
+ };
41
+
42
+ // Get the resolvers function (need to mock the import first)
43
+ const resolvers = getResolvers(mockConfig, {}, mockPathwayManager);
44
+ const executeWorkspaceResolver = resolvers.Query.executeWorkspace;
45
+
46
+ // Mock GraphQL context and info
47
+ const mockContextValue = { config: mockConfig };
48
+ const mockInfo = {};
49
+
50
+ // Test arguments - userId, pathwayName, promptNames are the key ones
51
+ const args = {
52
+ userId: 'test-user',
53
+ pathwayName: 'test-pathway',
54
+ promptNames: ['specific-prompt'], // This triggers the check
55
+ text: 'test input'
56
+ };
57
+
58
+ // Execute the resolver and expect it to throw
59
+ const error = await t.throwsAsync(async () => {
60
+ await executeWorkspaceResolver(null, args, mockContextValue, mockInfo);
61
+ });
62
+
63
+ // Verify the error message
64
+ t.truthy(error);
65
+ t.true(error.message.includes('legacy prompt format'));
66
+ t.true(error.message.includes('unpublish and republish'));
67
+ t.true(error.message.includes('promptNames parameter'));
68
+ t.true(error.message.includes('test-pathway')); // Should include the pathway name
69
+
70
+ // Verify that the pathwayManager methods were called correctly
71
+ t.true(mockPathwayManager.getLatestPathways.calledOnce);
72
+ t.true(mockPathwayManager.isLegacyPromptFormat.calledOnce);
73
+ t.true(mockPathwayManager.isLegacyPromptFormat.calledWith('test-user', 'test-pathway'));
74
+ });
75
+
76
+ test('executeWorkspace does not throw for new format with promptNames', async t => {
77
+ // Mock pathwayManager with new format
78
+ const mockPathwayManager = {
79
+ getLatestPathways: sinon.stub().resolves({
80
+ 'test-user': {
81
+ 'test-pathway': {
82
+ prompt: [
83
+ { name: 'Prompt 1', prompt: 'New format prompt 1' },
84
+ { name: 'Prompt 2', prompt: 'New format prompt 2' }
85
+ ], // New format
86
+ systemPrompt: 'Test system prompt'
87
+ }
88
+ }
89
+ }),
90
+ isLegacyPromptFormat: sinon.stub().returns(false), // Mock returns false for new format
91
+ getPathways: sinon.stub().resolves([
92
+ {
93
+ name: 'specific-prompt',
94
+ prompt: [/* mock prompt object */],
95
+ rootResolver: sinon.stub().resolves({ result: 'test result' })
96
+ }
97
+ ]),
98
+ getResolvers: sinon.stub().returns({ Mutation: {} }) // Mock getResolvers method
99
+ };
100
+
101
+ const resolvers = getResolvers(mockConfig, {}, mockPathwayManager);
102
+ const executeWorkspaceResolver = resolvers.Query.executeWorkspace;
103
+
104
+ const mockContextValue = { config: mockConfig };
105
+ const mockInfo = {};
106
+
107
+ const args = {
108
+ userId: 'test-user',
109
+ pathwayName: 'test-pathway',
110
+ promptNames: ['specific-prompt'],
111
+ text: 'test input'
112
+ };
113
+
114
+ // This should not throw an error for new format
115
+ const result = await executeWorkspaceResolver(null, args, mockContextValue, mockInfo);
116
+
117
+ // Should return results without error
118
+ t.truthy(result);
119
+ t.is(typeof result, 'object');
120
+ t.false(Array.isArray(result));
121
+
122
+ // Verify that the pathwayManager methods were called correctly
123
+ t.true(mockPathwayManager.getLatestPathways.calledOnce);
124
+ t.true(mockPathwayManager.isLegacyPromptFormat.calledOnce);
125
+ t.true(mockPathwayManager.getPathways.calledOnce);
126
+ });
127
+
128
+ test('executeWorkspace does not check format when promptNames not provided', async t => {
129
+ // Mock pathwayManager with legacy format
130
+ const mockPathwayManager = {
131
+ getLatestPathways: sinon.stub().resolves({
132
+ 'test-user': {
133
+ 'test-pathway': {
134
+ prompt: ['legacy string prompt 1', 'legacy string prompt 2'], // Legacy format
135
+ systemPrompt: 'Test system prompt'
136
+ }
137
+ }
138
+ }),
139
+ isLegacyPromptFormat: sinon.stub(), // Should not be called
140
+ getPathway: sinon.stub().resolves({
141
+ rootResolver: sinon.stub().resolves({ result: 'test result' })
142
+ }),
143
+ getResolvers: sinon.stub().returns({ Mutation: {} }) // Mock getResolvers method
144
+ };
145
+
146
+ const resolvers = getResolvers(mockConfig, {}, mockPathwayManager);
147
+ const executeWorkspaceResolver = resolvers.Query.executeWorkspace;
148
+
149
+ const mockContextValue = { config: mockConfig };
150
+ const mockInfo = {};
151
+
152
+ const args = {
153
+ userId: 'test-user',
154
+ pathwayName: 'test-pathway',
155
+ // No promptNames provided - should use default behavior
156
+ text: 'test input'
157
+ };
158
+
159
+ // This should not throw an error even with legacy format when promptNames not provided
160
+ const result = await executeWorkspaceResolver(null, args, mockContextValue, mockInfo);
161
+
162
+ // Should return results without error
163
+ t.truthy(result);
164
+ t.is(typeof result, 'object');
165
+ t.false(Array.isArray(result));
166
+
167
+ // Verify that isLegacyPromptFormat was NOT called since promptNames wasn't provided
168
+ t.false(mockPathwayManager.isLegacyPromptFormat.called);
169
+ t.true(mockPathwayManager.getPathway.calledOnce);
170
+ });