Open History to see the last commit
1/**
2 * Test utilities for the snippets CLI tests
3 */
4
5import { toolDefinition } from '@tanstack/ai'
6import { z } from 'zod'
7import { createMemorySnippetStorage } from '../src/storage/memory-storage'
8import { createAlwaysTrustedStrategy } from '../src/trust-strategies'
9import type { SnippetStorage } from '../src/types'
10
11/**
12 * A simple add_numbers tool for testing
13 */
14export const addNumbersTool = toolDefinition({
15 name: 'add_numbers',
16 description: 'Add two numbers together and return the result',
17 inputSchema: z.object({
18 a: z.number().describe('First number to add'),
19 b: z.number().describe('Second number to add'),
20 }),
21 outputSchema: z.object({
22 result: z.number().describe('The sum of a and b'),
23 }),
24}).server(async ({ a, b }) => {
25 console.log(`[add_numbers] Adding ${a} + ${b}`)
26 return { result: a + b }
27})
28
29/**
30 * Create a fresh memory storage for testing
31 */
32export function createTestStorage(): SnippetStorage {
33 return createMemorySnippetStorage({
34 initialSnippets: [],
35 trustStrategy: createAlwaysTrustedStrategy(),
36 })
37}
38
39/**
40 * Result from a test phase
41 */
42export interface TestPhaseResult {
43 success: boolean
44 error?: string
45 details?: Record<string, unknown>
46}
47
48/**
49 * Result from the full test
50 */
51export interface TestResult {
52 passed: boolean
53 phases: {
54 phase1: TestPhaseResult
55 phase2: TestPhaseResult
56 }
57 snippetCreated: boolean
58 snippetUsed: boolean
59}
60
61/**
62 * Console colors for output
63 */
64export const colors = {
65 reset: '\x1b[0m',
66 bright: '\x1b[1m',
67 red: '\x1b[31m',
68 green: '\x1b[32m',
69 yellow: '\x1b[33m',
70 blue: '\x1b[34m',
71 magenta: '\x1b[35m',
72 cyan: '\x1b[36m',
73}
74
75/**
76 * Log a section header
77 */
78export function logSection(title: string) {
79 console.log(`\n${colors.cyan}${'='.repeat(60)}${colors.reset}`)
80 console.log(`${colors.bright}${colors.cyan}${title}${colors.reset}`)
81 console.log(`${colors.cyan}${'='.repeat(60)}${colors.reset}\n`)
82}
83
84/**
85 * Log a success message
86 */
87export function logSuccess(message: string) {
88 console.log(`${colors.green}✅ ${message}${colors.reset}`)
89}
90
91/**
92 * Log an error message
93 */
94export function logError(message: string) {
95 console.log(`${colors.red}❌ ${message}${colors.reset}`)
96}
97
98/**
99 * Log an info message
100 */
101export function logInfo(message: string) {
102 console.log(`${colors.blue}ℹ️ ${message}${colors.reset}`)
103}
104
105/**
106 * Log a warning message
107 */
108export function logWarning(message: string) {
109 console.log(`${colors.yellow}⚠️ ${message}${colors.reset}`)
110}
111
112/**
113 * Log a step in the test
114 */
115export function logStep(step: number, message: string) {
116 console.log(`${colors.magenta}[Step ${step}]${colors.reset} ${message}`)
117}
118
119/**
120 * Expected code for the snippet that wraps add_numbers
121 */
122export const EXPECTED_SNIPPET_CODE = `
123const { a, b } = input;
124const result = await external_add_numbers({ a, b });
125return result;
126`.trim()
127
128/**
129 * Expected input schema for the add_two_numbers snippet
130 */
131export const EXPECTED_SNIPPET_INPUT_SCHEMA = {
132 type: 'object',
133 properties: {
134 a: { type: 'number', description: 'First number to add' },
135 b: { type: 'number', description: 'Second number to add' },
136 },
137 required: ['a', 'b'],
138}
139
140/**
141 * Expected output schema for the add_two_numbers snippet
142 */
143export const EXPECTED_SNIPPET_OUTPUT_SCHEMA = {
144 type: 'object',
145 properties: {
146 result: { type: 'number', description: 'The sum of a and b' },
147 },
148 required: ['result'],
149}