Skip to content

Commit 7f88096

Browse files
committed
Format code with bun format
1 parent 54b3b83 commit 7f88096

File tree

8 files changed

+226
-214
lines changed

8 files changed

+226
-214
lines changed

jest.config.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import type { Config } from 'jest';
1+
import type { Config } from "jest";
22

33
const config: Config = {
4-
preset: 'ts-jest',
5-
testEnvironment: 'jsdom',
4+
preset: "ts-jest",
5+
testEnvironment: "jsdom",
66
transform: {
7-
'^.+\\.(ts|tsx)$': 'ts-jest',
7+
"^.+\\.(ts|tsx)$": "ts-jest",
88
},
9-
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
9+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
1010
moduleNameMapper: {
11-
'^@/(.*)$': '<rootDir>/$1', // Changed from src/$1 to match tsconfig
12-
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
13-
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js',
11+
"^@/(.*)$": "<rootDir>/$1", // Changed from src/$1 to match tsconfig
12+
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
13+
"^.+\\.(jpg|jpeg|png|gif|webp|svg)$": "<rootDir>/__mocks__/fileMock.js",
1414
},
15-
transformIgnorePatterns: ['<rootDir>/node_modules/'],
16-
modulePaths: ['<rootDir>'],
17-
roots: ['<rootDir>'],
15+
transformIgnorePatterns: ["<rootDir>/node_modules/"],
16+
modulePaths: ["<rootDir>"],
17+
roots: ["<rootDir>"],
1818
};
1919

20-
export default config;
20+
export default config;

jest.setup.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// jest.setup.js
2-
import '@testing-library/jest-dom';
3-
import { TextEncoder, TextDecoder } from 'util';
4-
import { fetch, Headers, Request, Response } from 'cross-fetch';
2+
import "@testing-library/jest-dom";
3+
import { TextEncoder, TextDecoder } from "util";
4+
import { fetch, Headers, Request, Response } from "cross-fetch";
55

66
// Define the global objects needed for Next.js
77
global.TextEncoder = TextEncoder;
88
global.TextDecoder = TextDecoder as any;
99
global.fetch = fetch;
1010
global.Headers = Headers;
1111
global.Request = Request;
12-
global.Response = Response;
12+
global.Response = Response;

tests/code.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import { POST } from '@/app/api/code/route'; // adjust the path if needed
2-
import { NextResponse } from 'next/server';
1+
import { POST } from "@/app/api/code/route"; // adjust the path if needed
2+
import { NextResponse } from "next/server";
33

44
// Mock global fetch
55
global.fetch = jest.fn();
66

7-
jest.mock('next/server', () => ({
7+
jest.mock("next/server", () => ({
88
NextResponse: {
99
json: jest.fn((data, init) => ({ data, status: init?.status || 200 })),
1010
},
1111
}));
1212

13-
describe('POST /api/execute', () => {
13+
describe("POST /api/execute", () => {
1414
beforeEach(() => {
1515
jest.clearAllMocks();
1616
});
1717

18-
it('should forward the request to Piston API and return the result', async () => {
18+
it("should forward the request to Piston API and return the result", async () => {
1919
const mockBody = {
20-
language: 'python',
21-
version: '3.10.0',
22-
files: [{ name: 'main.py', content: 'print("Hello")' }],
23-
stdin: '',
20+
language: "python",
21+
version: "3.10.0",
22+
files: [{ name: "main.py", content: 'print("Hello")' }],
23+
stdin: "",
2424
args: [],
2525
compile_timeout: 10000,
2626
run_timeout: 3000,
2727
};
2828

2929
const mockPistonResponse = {
30-
run: { stdout: 'Hello\n', stderr: '', code: 0 },
30+
run: { stdout: "Hello\n", stderr: "", code: 0 },
3131
};
3232

3333
(fetch as jest.Mock).mockResolvedValueOnce({
@@ -42,25 +42,25 @@ describe('POST /api/execute', () => {
4242

4343
expect(mockRequest.json).toHaveBeenCalled();
4444
expect(fetch).toHaveBeenCalledWith(
45-
'https://emkc.org/api/v2/piston/execute',
45+
"https://emkc.org/api/v2/piston/execute",
4646
expect.objectContaining({
47-
method: 'POST',
48-
headers: { 'Content-Type': 'application/json' },
47+
method: "POST",
48+
headers: { "Content-Type": "application/json" },
4949
body: JSON.stringify(mockBody),
5050
}),
5151
);
5252
expect(response.status).toBe(200);
5353
expect(response.data).toEqual(mockPistonResponse);
5454
});
5555

56-
it('should return 500 on unexpected error', async () => {
56+
it("should return 500 on unexpected error", async () => {
5757
const mockRequest = {
58-
json: jest.fn().mockRejectedValue(new Error('Invalid JSON')),
58+
json: jest.fn().mockRejectedValue(new Error("Invalid JSON")),
5959
} as unknown as Request;
6060

6161
const response = await POST(mockRequest);
6262

6363
expect(response.status).toBe(500);
64-
expect(response.data).toEqual({ error: 'Failed to execute code' });
64+
expect(response.data).toEqual({ error: "Failed to execute code" });
6565
});
6666
});

tests/health.test.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
1-
import { GET } from '@/app/api/health/route';
2-
import { sql } from 'drizzle-orm';
1+
import { GET } from "@/app/api/health/route";
2+
import { sql } from "drizzle-orm";
33

4-
jest.mock('@/db/drizzle', () => ({
4+
jest.mock("@/db/drizzle", () => ({
55
db: {
66
execute: jest.fn(),
77
},
88
}));
99

10-
jest.mock('next/server', () => ({
10+
jest.mock("next/server", () => ({
1111
NextResponse: {
1212
json: jest.fn((data, init) => ({ data, status: init?.status || 200 })),
1313
},
1414
}));
1515

16-
import { db } from '@/db/drizzle';
17-
import { NextResponse } from 'next/server';
16+
import { db } from "@/db/drizzle";
17+
import { NextResponse } from "next/server";
1818

19-
describe('GET /api/health', () => {
19+
describe("GET /api/health", () => {
2020
beforeEach(() => {
2121
jest.clearAllMocks();
2222
});
2323

24-
it('should return 200 if DB connection is healthy', async () => {
24+
it("should return 200 if DB connection is healthy", async () => {
2525
(db.execute as jest.Mock).mockResolvedValueOnce(undefined);
2626

2727
const res = await GET();
2828

2929
expect(db.execute).toHaveBeenCalledWith(sql`SELECT 1`);
3030
expect(res.status).toBe(200);
31-
expect(res.data).toHaveProperty('status', 'healthy');
32-
expect(res.data).toHaveProperty('timestamp');
31+
expect(res.data).toHaveProperty("status", "healthy");
32+
expect(res.data).toHaveProperty("timestamp");
3333
});
3434

35-
it('should return 503 if DB connection fails', async () => {
36-
(db.execute as jest.Mock).mockRejectedValueOnce(new Error('Connection error'));
35+
it("should return 503 if DB connection fails", async () => {
36+
(db.execute as jest.Mock).mockRejectedValueOnce(
37+
new Error("Connection error"),
38+
);
3739

3840
const res = await GET();
3941

4042
expect(db.execute).toHaveBeenCalled();
4143
expect(res.status).toBe(503);
4244
expect(res.data).toMatchObject({
43-
status: 'unhealthy',
44-
error: 'Connection error',
45+
status: "unhealthy",
46+
error: "Connection error",
4547
});
46-
expect(res.data).toHaveProperty('timestamp');
48+
expect(res.data).toHaveProperty("timestamp");
4749
});
4850
});

tests/login.test.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { jest } from '@jest/globals';
1+
import { jest } from "@jest/globals";
22

33
// Mock Next.js modules
4-
jest.mock('next/server', () => ({
4+
jest.mock("next/server", () => ({
55
NextResponse: {
66
json: jest.fn((data, options) => {
77
return {
@@ -15,7 +15,7 @@ jest.mock('next/server', () => ({
1515
},
1616
}));
1717

18-
jest.mock('@/db/drizzle', () => ({
18+
jest.mock("@/db/drizzle", () => ({
1919
db: {
2020
query: {
2121
users: {
@@ -25,41 +25,41 @@ jest.mock('@/db/drizzle', () => ({
2525
},
2626
}));
2727

28-
jest.mock('@/lib/password', () => ({
28+
jest.mock("@/lib/password", () => ({
2929
verifyPassword: jest.fn(),
3030
}));
3131

32-
jest.mock('@/lib/server/session', () => ({
33-
generateSessionToken: jest.fn().mockReturnValue('mock-session-token'),
34-
createSession: jest.fn().mockResolvedValue({
35-
token: 'mock-session-token',
36-
expiresAt: new Date(Date.now() + 86400000)
32+
jest.mock("@/lib/server/session", () => ({
33+
generateSessionToken: jest.fn().mockReturnValue("mock-session-token"),
34+
createSession: jest.fn().mockResolvedValue({
35+
token: "mock-session-token",
36+
expiresAt: new Date(Date.now() + 86400000),
3737
}),
3838
}));
3939

40-
jest.mock('@/lib/server/cookies', () => ({
40+
jest.mock("@/lib/server/cookies", () => ({
4141
setSessionTokenCookie: jest.fn(),
4242
}));
4343

44-
import { NextResponse } from 'next/server';
45-
import { db } from '@/db/drizzle';
46-
import { verifyPassword } from '@/lib/password';
47-
import { generateSessionToken, createSession } from '@/lib/server/session';
48-
import { setSessionTokenCookie } from '@/lib/server/cookies';
49-
import { POST } from '@/app/api/auth/login/route';
44+
import { NextResponse } from "next/server";
45+
import { db } from "@/db/drizzle";
46+
import { verifyPassword } from "@/lib/password";
47+
import { generateSessionToken, createSession } from "@/lib/server/session";
48+
import { setSessionTokenCookie } from "@/lib/server/cookies";
49+
import { POST } from "@/app/api/auth/login/route";
5050

51-
describe('POST /api/login', () => {
51+
describe("POST /api/login", () => {
5252
beforeEach(() => {
5353
jest.clearAllMocks();
5454
});
5555

56-
it('should return user data on valid credentials', async () => {
56+
it("should return user data on valid credentials", async () => {
5757
// Mock user data
5858
const mockUser = {
59-
id: '1',
60-
61-
name: 'Test User',
62-
hashedPassword: 'hashed_password',
59+
id: "1",
60+
61+
name: "Test User",
62+
hashedPassword: "hashed_password",
6363
};
6464

6565
// Set up mocks
@@ -69,8 +69,8 @@ describe('POST /api/login', () => {
6969
// Create mock request
7070
const request = {
7171
json: jest.fn().mockResolvedValue({
72-
73-
password: 'password123',
72+
73+
password: "password123",
7474
}),
7575
} as unknown as Request;
7676

@@ -79,27 +79,30 @@ describe('POST /api/login', () => {
7979

8080
// Assertions
8181
expect(db.query.users.findFirst).toHaveBeenCalled();
82-
expect(verifyPassword).toHaveBeenCalledWith('hashed_password', 'password123');
82+
expect(verifyPassword).toHaveBeenCalledWith(
83+
"hashed_password",
84+
"password123",
85+
);
8386
expect(generateSessionToken).toHaveBeenCalled();
8487
expect(createSession).toHaveBeenCalled();
8588
expect(setSessionTokenCookie).toHaveBeenCalled();
86-
89+
8790
expect(response.data).toEqual({
88-
_id: '1',
89-
90-
name: 'Test User',
91+
_id: "1",
92+
93+
name: "Test User",
9194
});
9295
});
9396

94-
it('should return 401 if user not found', async () => {
97+
it("should return 401 if user not found", async () => {
9598
// Set up mocks
9699
(db.query.users.findFirst as jest.Mock).mockResolvedValue(null);
97100

98101
// Create mock request
99102
const request = {
100103
json: jest.fn().mockResolvedValue({
101-
102-
password: 'password123',
104+
105+
password: "password123",
103106
}),
104107
} as unknown as Request;
105108

@@ -108,16 +111,16 @@ describe('POST /api/login', () => {
108111

109112
// Assertions
110113
expect(response.status).toBe(401);
111-
expect(response.data).toEqual({ error: 'Invalid credentials' });
114+
expect(response.data).toEqual({ error: "Invalid credentials" });
112115
});
113116

114-
it('should return 401 on invalid password', async () => {
117+
it("should return 401 on invalid password", async () => {
115118
// Mock user data
116119
const mockUser = {
117-
id: '1',
118-
119-
name: 'Test User',
120-
hashedPassword: 'hashed_password',
120+
id: "1",
121+
122+
name: "Test User",
123+
hashedPassword: "hashed_password",
121124
};
122125

123126
// Set up mocks
@@ -127,8 +130,8 @@ describe('POST /api/login', () => {
127130
// Create mock request
128131
const request = {
129132
json: jest.fn().mockResolvedValue({
130-
131-
password: 'wrong_password',
133+
134+
password: "wrong_password",
132135
}),
133136
} as unknown as Request;
134137

@@ -138,20 +141,20 @@ describe('POST /api/login', () => {
138141
// Assertions
139142
expect(verifyPassword).toHaveBeenCalled();
140143
expect(response.status).toBe(401);
141-
expect(response.data).toEqual({ error: 'Invalid credentials' });
144+
expect(response.data).toEqual({ error: "Invalid credentials" });
142145
});
143146

144-
it('should return 400 on invalid request', async () => {
147+
it("should return 400 on invalid request", async () => {
145148
// Create mock request that throws an error when trying to parse JSON
146149
const request = {
147-
json: jest.fn().mockRejectedValue(new Error('Invalid JSON')),
150+
json: jest.fn().mockRejectedValue(new Error("Invalid JSON")),
148151
} as unknown as Request;
149152

150153
// Call the handler
151154
const response = await POST(request);
152155

153156
// Assertions
154157
expect(response.status).toBe(400);
155-
expect(response.data).toEqual({ error: 'Invalid request' });
158+
expect(response.data).toEqual({ error: "Invalid request" });
156159
});
157-
});
160+
});

0 commit comments

Comments
 (0)