|
| 1 | +/** |
| 2 | + * Test to verify toolRules.allowOnlyApiGroups filtering works correctly. |
| 3 | + */ |
| 4 | + |
| 5 | +import { createServer, type AgentToolProtocolServer } from '../../packages/server/src/index'; |
| 6 | +import { AgentToolProtocolClient } from '../../packages/client/src/index'; |
| 7 | +import { runInRequestScope } from '../../packages/server/src/core/request-scope'; |
| 8 | + |
| 9 | +describe('toolRules.allowOnlyApiGroups filtering', () => { |
| 10 | + let server: AgentToolProtocolServer; |
| 11 | + let client: AgentToolProtocolClient; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + server = createServer(); |
| 15 | + |
| 16 | + // Register "math" API group |
| 17 | + server.use({ |
| 18 | + name: 'math', |
| 19 | + type: 'custom', |
| 20 | + functions: [ |
| 21 | + { |
| 22 | + name: 'add', |
| 23 | + description: 'Add two numbers', |
| 24 | + inputSchema: { |
| 25 | + type: 'object', |
| 26 | + properties: { |
| 27 | + a: { type: 'number' }, |
| 28 | + b: { type: 'number' }, |
| 29 | + }, |
| 30 | + required: ['a', 'b'], |
| 31 | + }, |
| 32 | + handler: async (input: unknown) => { |
| 33 | + const { a, b } = input as { a: number; b: number }; |
| 34 | + return { result: a + b }; |
| 35 | + }, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + |
| 40 | + // Register "text" API group |
| 41 | + server.use({ |
| 42 | + name: 'text', |
| 43 | + type: 'custom', |
| 44 | + functions: [ |
| 45 | + { |
| 46 | + name: 'uppercase', |
| 47 | + description: 'Convert text to uppercase', |
| 48 | + inputSchema: { |
| 49 | + type: 'object', |
| 50 | + properties: { |
| 51 | + text: { type: 'string' }, |
| 52 | + }, |
| 53 | + required: ['text'], |
| 54 | + }, |
| 55 | + handler: async (input: unknown) => { |
| 56 | + const { text } = input as { text: string }; |
| 57 | + return { result: text.toUpperCase() }; |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + |
| 63 | + await server.start(); |
| 64 | + |
| 65 | + client = new AgentToolProtocolClient({ |
| 66 | + server: server as any, |
| 67 | + }); |
| 68 | + |
| 69 | + await client.init({ name: 'test-client', version: '1.0.0' }); |
| 70 | + await client.connect(); |
| 71 | + }); |
| 72 | + |
| 73 | + afterAll(async () => { |
| 74 | + // Cleanup if needed |
| 75 | + }); |
| 76 | + |
| 77 | + it('should allow math API when toolRules.allowOnlyApiGroups includes math', async () => { |
| 78 | + const result = await client.execute('return await api.math.add({ a: 2, b: 3 });', { |
| 79 | + toolRules: { |
| 80 | + allowOnlyApiGroups: ['math'], |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result.status).toBe('completed'); |
| 85 | + expect((result.result as any)?.result).toBe(5); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should block text API when toolRules.allowOnlyApiGroups only includes math', async () => { |
| 89 | + const result = await client.execute('return await api.text.uppercase({ text: "hello" });', { |
| 90 | + toolRules: { |
| 91 | + allowOnlyApiGroups: ['math'], |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + // When text API is blocked, the code should fail because api.text is undefined |
| 96 | + expect(result.status).toBe('failed'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should allow both APIs when no toolRules are specified', async () => { |
| 100 | + const mathResult = await client.execute('return await api.math.add({ a: 1, b: 2 });'); |
| 101 | + expect(mathResult.status).toBe('completed'); |
| 102 | + expect((mathResult.result as any)?.result).toBe(3); |
| 103 | + |
| 104 | + const textResult = await client.execute('return await api.text.uppercase({ text: "hello" });'); |
| 105 | + expect(textResult.status).toBe('completed'); |
| 106 | + expect((textResult.result as any)?.result).toBe('HELLO'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should allow text API when toolRules.allowOnlyApiGroups includes text', async () => { |
| 110 | + const result = await client.execute('return await api.text.uppercase({ text: "world" });', { |
| 111 | + toolRules: { |
| 112 | + allowOnlyApiGroups: ['text'], |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + expect(result.status).toBe('completed'); |
| 117 | + expect((result.result as any)?.result).toBe('WORLD'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should block math API when toolRules.allowOnlyApiGroups only includes text', async () => { |
| 121 | + const result = await client.execute('return await api.math.add({ a: 5, b: 5 });', { |
| 122 | + toolRules: { |
| 123 | + allowOnlyApiGroups: ['text'], |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + expect(result.status).toBe('failed'); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('explore_api filtering', () => { |
| 131 | + it('should show all API groups at root when no toolRules', async () => { |
| 132 | + const result = await client.exploreAPI('/'); |
| 133 | + |
| 134 | + // Should see 'custom' directory at root |
| 135 | + expect(result).toHaveProperty('items'); |
| 136 | + expect(Array.isArray((result as any).items)).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should show both math and text when exploring /custom without toolRules', async () => { |
| 140 | + const result = await client.exploreAPI('/custom'); |
| 141 | + |
| 142 | + const items = (result as any).items || []; |
| 143 | + const itemNames = items.map((i: any) => i.name); |
| 144 | + |
| 145 | + // Both APIs should be visible without filtering |
| 146 | + expect(itemNames).toContain('math'); |
| 147 | + expect(itemNames).toContain('text'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should filter explore results when runInRequestScope is used with toolRules', async () => { |
| 151 | + // Test that the filtering mechanism works when request scope is set |
| 152 | + const result = await runInRequestScope( |
| 153 | + { toolRules: { allowOnlyApiGroups: ['math'] } }, |
| 154 | + async () => { |
| 155 | + return await client.exploreAPI('/custom'); |
| 156 | + } |
| 157 | + ); |
| 158 | + |
| 159 | + const items = (result as any).items || []; |
| 160 | + const itemNames = items.map((i: any) => i.name); |
| 161 | + |
| 162 | + // Only math should be visible when toolRules filters to math only |
| 163 | + expect(itemNames).toContain('math'); |
| 164 | + expect(itemNames).not.toContain('text'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should filter explore results when toolRules passed directly to exploreAPI', async () => { |
| 168 | + const result = await client.exploreAPI('/custom', { |
| 169 | + toolRules: { allowOnlyApiGroups: ['math'] }, |
| 170 | + }); |
| 171 | + |
| 172 | + const items = (result as any).items || []; |
| 173 | + const itemNames = items.map((i: any) => i.name); |
| 174 | + |
| 175 | + // Only math should be visible when toolRules filters to math only |
| 176 | + expect(itemNames).toContain('math'); |
| 177 | + expect(itemNames).not.toContain('text'); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('search_api filtering', () => { |
| 182 | + it('should return results from all APIs when no options', async () => { |
| 183 | + const results = await client.searchAPI('add'); |
| 184 | + |
| 185 | + expect(Array.isArray(results)).toBe(true); |
| 186 | + expect(results.length).toBeGreaterThan(0); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should find text API uppercase when no options', async () => { |
| 190 | + const results = await client.searchAPI('uppercase'); |
| 191 | + |
| 192 | + expect(results.length).toBeGreaterThan(0); |
| 193 | + expect(results.some((r) => r.functionName === 'uppercase')).toBe(true); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should only return math results when apiGroups filter includes only math', async () => { |
| 197 | + // searchAPI accepts apiGroups option for filtering |
| 198 | + const results = await client.searchAPI('uppercase', { |
| 199 | + query: 'uppercase', |
| 200 | + apiGroups: ['math'], |
| 201 | + }); |
| 202 | + |
| 203 | + // When filtering by apiGroups, searching for "uppercase" with math-only filter |
| 204 | + // should return no results because uppercase belongs to text API |
| 205 | + expect(results.length).toBe(0); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should filter search results when toolRules passed directly to searchAPI', async () => { |
| 209 | + // searchAPI with toolRules.allowOnlyApiGroups |
| 210 | + const results = await client.searchAPI('uppercase', { |
| 211 | + query: 'uppercase', |
| 212 | + toolRules: { allowOnlyApiGroups: ['math'] }, |
| 213 | + }); |
| 214 | + |
| 215 | + // When filtering by toolRules, searching for "uppercase" with math-only filter |
| 216 | + // should return no results because uppercase belongs to text API |
| 217 | + expect(results.length).toBe(0); |
| 218 | + }); |
| 219 | + }); |
| 220 | +}); |
0 commit comments