-
Notifications
You must be signed in to change notification settings - Fork 0
test: reach 80% coverage (4.4% → 81.11%) #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const mockGenerate = vi.fn(); | ||
| const mockGenerateStream = vi.fn(); | ||
| const mockDefineFlow = vi.fn((_config: unknown, handler: unknown) => handler); | ||
| const mockSetAttribute = vi.fn(); | ||
| const mockSpan = { setAttribute: mockSetAttribute }; | ||
|
|
||
| vi.mock('@/ai/genkit', () => ({ | ||
| ai: { | ||
| defineFlow: (...args: [unknown, unknown]) => mockDefineFlow(...args), | ||
| generate: (...args: [unknown]) => mockGenerate(...args), | ||
| generateStream: (...args: [unknown]) => mockGenerateStream(...args), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('@genkit-ai/google-genai', () => ({ | ||
| googleAI: { model: vi.fn(() => 'gemini-2.5-flash') }, | ||
| })); | ||
|
|
||
| vi.mock('@sentry/nextjs', () => ({ | ||
| startSpan: vi.fn(async (_config: unknown, fn: (span: typeof mockSpan) => Promise<unknown>) => | ||
| fn(mockSpan) | ||
| ), | ||
| })); | ||
|
|
||
| vi.mock('@/lib/constants', () => ({ | ||
| EXERCICIOS_POR_GRUPO: [ | ||
| { | ||
| grupo: 'Peito', | ||
| exercicios: [{ nomeExercicio: 'Supino Reto' }], | ||
| }, | ||
| ], | ||
| })); | ||
|
|
||
| vi.mock('@/ai/schemas', () => { | ||
| const WorkoutGeneratorInputSchema = z.object({ | ||
| objetivo: z.enum(['Hipertrofia', 'Perda de Peso', 'Força']), | ||
| nivelExperiencia: z.enum(['Iniciante', 'Intermediário', 'Avançado']), | ||
| diasPorSemana: z.number().min(1).max(7), | ||
| observacoesAdicionais: z.string().optional(), | ||
| }); | ||
|
|
||
| const WorkoutGeneratorAIOutputSchema = z.object({ | ||
| planName: z.string(), | ||
| workouts: z.array( | ||
| z.object({ | ||
| nome: z.string(), | ||
| objetivo: z.string(), | ||
| diaSugerido: z.number(), | ||
| exercicios: z.array( | ||
| z.object({ | ||
| nomeExercicio: z.string(), | ||
| grupoMuscular: z.string(), | ||
| series: z.number(), | ||
| repeticoes: z.string(), | ||
| observacoes: z.string(), | ||
| }) | ||
| ), | ||
| }) | ||
| ), | ||
| }); | ||
|
|
||
| return { | ||
| WorkoutGeneratorInputSchema, | ||
| WorkoutGeneratorAIOutputSchema, | ||
| WorkoutGeneratorInput: undefined, | ||
| WorkoutGeneratorAIOutput: undefined, | ||
| }; | ||
| }); | ||
|
|
||
| const validInput = { | ||
| objetivo: 'Hipertrofia' as const, | ||
| nivelExperiencia: 'Intermediário' as const, | ||
| diasPorSemana: 4, | ||
| }; | ||
|
|
||
| describe('streamWorkoutPlan', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('streams workout plan successfully', async () => { | ||
| const chunkOutput = { | ||
| planName: 'Plano Teste', | ||
| workouts: [], | ||
| }; | ||
| mockGenerateStream.mockReturnValue({ | ||
| stream: (async function* () { | ||
| yield { output: chunkOutput }; | ||
| })(), | ||
| response: Promise.resolve({ | ||
| output: { | ||
| planName: 'Plano Teste', | ||
| workouts: [ | ||
| { | ||
| nome: 'Treino A', | ||
| objetivo: 'Hipertrofia', | ||
| diaSugerido: 1, | ||
| exercicios: [ | ||
| { | ||
| nomeExercicio: 'Supino Reto', | ||
| grupoMuscular: 'Peito', | ||
| series: 3, | ||
| repeticoes: '10-12', | ||
| observacoes: '', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| usage: { inputTokens: 100, outputTokens: 200 }, | ||
| }), | ||
| }); | ||
|
|
||
| const { streamWorkoutPlan } = await import('@/ai/flows/workout-generator-flow'); | ||
| const sendChunk = vi.fn(); | ||
|
|
||
| const result = await ( | ||
| streamWorkoutPlan as unknown as ( | ||
| input: typeof validInput, | ||
| ctx: { sendChunk: (chunk: unknown) => void } | ||
| ) => Promise<unknown> | ||
| )(validInput, { sendChunk }); | ||
|
|
||
| expect(result).toBeDefined(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The Prompt for AI agents |
||
| expect(sendChunk).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('throws when AI returns no output', async () => { | ||
| mockGenerateStream.mockReturnValue({ | ||
| stream: (async function* () { | ||
| yield {}; | ||
| })(), | ||
| response: Promise.resolve({ | ||
| output: null, | ||
| usage: { inputTokens: 0, outputTokens: 0 }, | ||
| }), | ||
| }); | ||
|
|
||
| const { streamWorkoutPlan } = await import('@/ai/flows/workout-generator-flow'); | ||
| const sendChunk = vi.fn(); | ||
|
|
||
| await expect( | ||
| ( | ||
| streamWorkoutPlan as unknown as ( | ||
| input: typeof validInput, | ||
| ctx: { sendChunk: (chunk: unknown) => void } | ||
| ) => Promise<unknown> | ||
| )(validInput, { sendChunk }) | ||
| ).rejects.toThrow('A IA não retornou um plano de treino válido.'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateWorkoutPlan', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('generates workout plan successfully', async () => { | ||
| mockGenerate.mockResolvedValue({ | ||
| output: { | ||
| planName: 'Plano Força', | ||
| workouts: [ | ||
| { | ||
| nome: 'Treino A', | ||
| objetivo: 'Força', | ||
| diaSugerido: 2, | ||
| exercicios: [ | ||
| { | ||
| nomeExercicio: 'Supino Reto', | ||
| grupoMuscular: 'Peito', | ||
| series: 5, | ||
| repeticoes: '5-6', | ||
| observacoes: '', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| usage: { inputTokens: 50, outputTokens: 150 }, | ||
| }); | ||
|
|
||
| const { generateWorkoutPlan } = await import('@/ai/flows/workout-generator-flow'); | ||
| const result = await generateWorkoutPlan(validInput); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.planName).toBe('Plano Força'); | ||
| expect(mockGenerate).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('throws when output is null', async () => { | ||
| mockGenerate.mockResolvedValue({ | ||
| output: null, | ||
| usage: { inputTokens: 0, outputTokens: 0 }, | ||
| }); | ||
|
|
||
| const { generateWorkoutPlan } = await import('@/ai/flows/workout-generator-flow'); | ||
| await expect(generateWorkoutPlan(validInput)).rejects.toThrow( | ||
| 'A IA não retornou um plano de treino válido.' | ||
| ); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| const mockGenkit = vi.fn(() => ({ name: 'mocked-ai' })); | ||
|
|
||
| vi.mock('genkit', () => ({ | ||
| genkit: mockGenkit, | ||
| })); | ||
|
|
||
| vi.mock('@genkit-ai/google-genai', () => ({ | ||
| googleAI: vi.fn(() => 'google-ai-plugin'), | ||
| })); | ||
|
|
||
| describe('genkit initialization', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| it('initializes genkit with googleAI plugin', async () => { | ||
| const { ai } = await import('./genkit'); | ||
| expect(mockGenkit).toHaveBeenCalledTimes(1); | ||
| expect(mockGenkit).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| plugins: expect.arrayContaining(['google-ai-plugin']), | ||
| }) | ||
| ); | ||
| expect(ai).toEqual({ name: 'mocked-ai' }); | ||
| }); | ||
|
|
||
| it('passes GOOGLE_GENAI_API_KEY to googleAI plugin', async () => { | ||
| process.env.GOOGLE_GENAI_API_KEY = 'test-key'; | ||
| const { googleAI } = await import('@genkit-ai/google-genai'); | ||
| await import('./genkit'); | ||
|
|
||
| expect(googleAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: 'test-key' })); | ||
|
|
||
| delete process.env.GOOGLE_GENAI_API_KEY; | ||
| }); | ||
|
|
||
| it('falls back to GEMINI_API_KEY when GOOGLE_GENAI_API_KEY is not set', async () => { | ||
| delete process.env.GOOGLE_GENAI_API_KEY; | ||
| process.env.GEMINI_API_KEY = 'fallback-key'; | ||
| const { googleAI } = await import('@genkit-ai/google-genai'); | ||
| await import('./genkit'); | ||
|
|
||
| expect(googleAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: 'fallback-key' })); | ||
|
|
||
| delete process.env.GEMINI_API_KEY; | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.