Skip to content

Commit cd977fc

Browse files
committed
Fix tests
1 parent 49b785d commit cd977fc

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

common/src/__tests__/agent-validation.test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { afterAll, beforeAll, beforeEach, describe, expect, it, test } from 'bun:test'
1+
import {
2+
afterAll,
3+
beforeAll,
4+
beforeEach,
5+
describe,
6+
expect,
7+
it,
8+
test,
9+
} from 'bun:test'
210

311
import { validateAgents } from '../templates/agent-validation'
412
import { DynamicAgentConfigSchema } from '../types/dynamic-agent-template'
@@ -216,7 +224,7 @@ describe('Agent Validation', () => {
216224
instructionsPrompt: 'Test user prompt',
217225
stepPrompt: 'Test step prompt',
218226
inputSchema: {
219-
prompt: null as any, // Invalid - null schema
227+
prompt: {} as any, // invalid prompt schema
220228
},
221229
outputMode: 'last_message',
222230
includeMessageHistory: true,
@@ -230,7 +238,7 @@ describe('Agent Validation', () => {
230238

231239
expect(result.validationErrors).toHaveLength(1)
232240
expect(result.validationErrors[0].message).toContain(
233-
'Failed to convert inputSchema.prompt to Zod',
241+
'Invalid inputSchema.prompt in invalid-schema-agent.ts',
234242
)
235243
expect(result.templates).not.toHaveProperty('invalid_schema_agent')
236244
})
@@ -538,7 +546,7 @@ describe('Agent Validation', () => {
538546
instructionsPrompt: 'Test user prompt',
539547
stepPrompt: 'Test step prompt',
540548
inputSchema: {
541-
prompt: null as any, // Invalid - null schema
549+
prompt: 10 as any, // Invalid - number schema
542550
},
543551
outputMode: 'last_message',
544552
includeMessageHistory: true,
@@ -813,8 +821,10 @@ describe('Agent Validation', () => {
813821
expect(result.templates['test-agent']).toBeDefined()
814822

815823
// Verify the loaded template's handleSteps field matches the original toString
816-
expect(result.templates['test-agent'].handleSteps).toBe(expectedStringified)
824+
expect(result.templates['test-agent'].handleSteps).toBe(
825+
expectedStringified,
826+
)
817827
expect(typeof result.templates['test-agent'].handleSteps).toBe('string')
818828
})
819829
})
820-
})
830+
})

common/src/templates/agent-validation.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,16 @@ function convertInputSchema(
283283
const fileContext = filePath ? ` in ${filePath}` : ''
284284

285285
// Handle prompt schema
286-
if (inputPromptSchema && Object.keys(inputPromptSchema).length > 0) {
286+
if (inputPromptSchema) {
287287
try {
288+
if (
289+
typeof inputPromptSchema !== 'object' ||
290+
Object.keys(inputPromptSchema).length === 0
291+
) {
292+
throw new Error(
293+
`Invalid inputSchema.prompt${fileContext}: Schema must be a valid non-empty JSON schema object. Found: ${typeof inputPromptSchema}`,
294+
)
295+
}
288296
const promptZodSchema = convertJsonSchemaToZod(inputPromptSchema)
289297
// Validate that the schema results in string or undefined
290298
const testResult = promptZodSchema.safeParse('test')
@@ -312,22 +320,30 @@ function convertInputSchema(
312320
error instanceof Error ? error.message : 'Unknown error'
313321
throw new Error(
314322
`Failed to convert inputSchema.prompt to Zod${fileContext}: ${errorMessage}. ` +
315-
`Please check that your inputSchema.prompt is a valid JSON schema object.`,
323+
`Please check that your inputSchema.prompt is a valid non-empty JSON schema object.`,
316324
)
317325
}
318326
}
319327

320328
// Handle params schema
321-
if (paramsSchema && Object.keys(paramsSchema).length > 0) {
329+
if (paramsSchema) {
322330
try {
331+
if (
332+
typeof paramsSchema !== 'object' ||
333+
Object.keys(paramsSchema).length === 0
334+
) {
335+
throw new Error(
336+
`Invalid inputSchema.params${fileContext}: Schema must be a valid non-empty JSON schema object. Found: ${typeof paramsSchema}`,
337+
)
338+
}
323339
const paramsZodSchema = convertJsonSchemaToZod(paramsSchema)
324340
result.params = paramsZodSchema
325341
} catch (error) {
326342
const errorMessage =
327343
error instanceof Error ? error.message : 'Unknown error'
328344
throw new Error(
329345
`Failed to convert inputSchema.params to Zod${fileContext}: ${errorMessage}. ` +
330-
`Please check that your inputSchema.params is a valid JSON schema object.`,
346+
`Please check that your inputSchema.params is a valid non-empty JSON schema object.`,
331347
)
332348
}
333349
}

0 commit comments

Comments
 (0)