Skip to content

Commit

Permalink
feat: Updated 6 files
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Apr 30, 2024
1 parent b217262 commit f3c0927
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 0 deletions.
52 changes: 52 additions & 0 deletions apps/nextjs/app/api/chat-with-vision/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { POST } from './route';
import { createMocks } from 'node-mocks-http';
import OpenAI from 'openai';

jest.mock('openai');

describe('POST /api/chat-with-vision', () => {
test('responds with streaming text response for valid request', async () => {
const { req, res } = createMocks({
method: 'POST',
body: {
messages: [/* valid messages array */],
data: { imageUrl: 'https://example.com/image.png' }
}
});

const mockResponse = {
// Mock OpenAI chat completion response
};
OpenAI.prototype.chat.completions.create.mockResolvedValue(mockResponse);

await POST(req, res);

expect(OpenAI.prototype.chat.completions.create).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-4-vision-preview',
stream: true,
// Other expected parameters
})
);
expect(res._getStatusCode()).toBe(200);
expect(res._getHeaders()['content-type']).toEqual(
'text/event-stream; charset=utf-8'
);
// Add more assertions for the response body
});

test('responds with error for invalid request', async () => {
const { req, res } = createMocks({
method: 'POST',
body: {
// Invalid or missing fields
}
});

await POST(req, res);

expect(OpenAI.prototype.chat.completions.create).not.toHaveBeenCalled();
expect(res._getStatusCode()).toBe(400);
// Add more assertions for the error response
});
});
52 changes: 52 additions & 0 deletions apps/nextjs/app/api/conversation2measurements/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { POST, GET } from './route';
import { createMocks } from 'node-mocks-http';
import { conversation2measurements } from '@/lib/conversation2measurements';
import { postMeasurements } from '@/lib/dfda';
import { getUserId } from '@/lib/getUserId';

jest.mock('@/lib/conversation2measurements');
jest.mock('@/lib/dfda');
jest.mock('@/lib/getUserId');

describe('POST /api/conversation2measurements', () => {
test('responds with measurements for valid request', async () => {
const { req, res } = createMocks({
method: 'POST',
body: {
statement: 'I ate an apple',
localDateTime: '2023-06-08T10:00:00.000Z',
previousStatements: 'What did you eat today?',
},
});

const mockMeasurements = [
// Mock measurements array
];
(conversation2measurements as jest.Mock).mockResolvedValue(mockMeasurements);
(getUserId as jest.Mock).mockResolvedValue('user123');

await POST(req, res);

expect(conversation2measurements).toHaveBeenCalledWith(
'I ate an apple',
'2023-06-08T10:00:00.000Z',
'What did you eat today?'
);
expect(getUserId).toHaveBeenCalled();
expect(postMeasurements).toHaveBeenCalledWith(
mockMeasurements,
'user123'
);
expect(res._getStatusCode()).toBe(200);
expect(res._getJSONData()).toEqual({
success: true,
measurements: mockMeasurements,
});
});

// Add more test cases for POST, handling errors etc.
});

describe('GET /api/conversation2measurements', () => {
// Add test cases for GET handler
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { GlobalVariableAddButton } from './global-variable-add-button';

const mockRouter = {
push: jest.fn(),
refresh: jest.fn(),
};
jest.mock('next/navigation', () => ({
useRouter: () => mockRouter,
}));

describe('GlobalVariableAddButton', () => {
test('opens confirmation dialog on click', () => {
render(<GlobalVariableAddButton />);

fireEvent.click(screen.getByText('New variable'));

expect(screen.getByText(/Are you sure you want to create a new variable?/i)).toBeInTheDocument();
});

test('creates new variable on confirmation', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({ id: 'var123' }),
});

render(<GlobalVariableAddButton />);

fireEvent.click(screen.getByText('New variable'));
fireEvent.click(screen.getByText('Add globalVariable'));

await waitFor(() => expect(global.fetch).toHaveBeenCalledWith(
'/api/globalVariables',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ name: 'New Variable' }),
})
));

expect(mockRouter.push).toHaveBeenCalledWith('/dashboard/globalVariables/var123/settings');
expect(mockRouter.refresh).toHaveBeenCalled();
});

// Add more test cases
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render, screen, waitFor } from '@testing-library/react';
import { GlobalVariableCharts } from './global-variable-charts';

describe('GlobalVariableCharts', () => {
test('renders loading spinner while fetching data', () => {
render(<GlobalVariableCharts variableId={123} />);

expect(screen.getByRole('status')).toBeInTheDocument();
});

test('renders charts with fetched data', async () => {
const mockGlobalVariable = {
name: 'Test Variable',
description: 'This is a test',
charts: {
// Mock chart data
},
};
global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue([mockGlobalVariable]),
});

render(<GlobalVariableCharts variableId={123} />);

await waitFor(() => expect(screen.getByText('Test Variable')).toBeInTheDocument());

// Assert that chart components are rendered with expected props
expect(screen.getAllByRole('img', { name: /highcharts/i })).toHaveLength(3);
});

test('renders error message on fetch failure', async () => {
global.fetch = jest.fn().mockRejectedValue(new Error('Fetch failed'));

render(<GlobalVariableCharts variableId={123} />);

await waitFor(() => expect(screen.getByText(/no data found/i)).toBeInTheDocument());
});
});
51 changes: 51 additions & 0 deletions apps/nextjs/lib/dfda.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
getOrCreateDfdaUser,
getOrCreateDfdaAccessToken,
dfdaGET,
dfdaPOST,
} from './dfda';
import { db } from '@/lib/db';

jest.mock('@/lib/db');

describe('getOrCreateDfdaUser', () => {
// Test cases
});

describe('getOrCreateDfdaAccessToken', () => {
// Test cases
});

describe('dfdaGET', () => {
test('makes GET request to DFDA API with correct parameters', async () => {
const mockResponse = {
// Mock API response
};
global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(mockResponse),
});

const response = await dfdaGET(
'measurements',
{ variableId: '123' },
'user456'
);

expect(global.fetch).toHaveBeenCalledWith(
'https://safe.dfda.earth/api/v3/measurements?variableId=123',
expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
Authorization: 'Bearer access_token',
}),
})
);
expect(response).toEqual(mockResponse);
});

// Add more test cases
});

describe('dfdaPOST', () => {
// Test cases, similar to dfdaGET
});
24 changes: 24 additions & 0 deletions apps/nextjs/lib/getUserId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getUserId } from './getUserId';
import { getServerSession } from 'next-auth/next';

jest.mock('next-auth/next');

describe('getUserId', () => {
test('returns user ID from session', async () => {
(getServerSession as jest.Mock).mockResolvedValue({
user: { id: 'user789' },
});

const userId = await getUserId();

expect(userId).toBe('user789');
});

test('returns anonymous ID if no session user', async () => {
(getServerSession as jest.Mock).mockResolvedValue({});

const userId = await getUserId();

expect(userId).toMatch(/^anon-/);
});
});

0 comments on commit f3c0927

Please sign in to comment.