Skip to content

Commit bde1f7b

Browse files
committed
Fix tests
1 parent 39bfb1e commit bde1f7b

File tree

3 files changed

+117
-52
lines changed

3 files changed

+117
-52
lines changed

src/lib/db/index.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,68 @@ import fs from "fs";
44
import path from "path";
55
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
66

7-
// Define the database URL - for development we'll use a local SQLite file
8-
const dataDir = path.join(process.cwd(), "data");
9-
// Ensure data directory exists
10-
if (!fs.existsSync(dataDir)) {
11-
fs.mkdirSync(dataDir, { recursive: true });
12-
}
13-
14-
const dbPath = path.join(dataDir, "gitea-mirror.db");
7+
// Skip database initialization in test environment
8+
let db: ReturnType<typeof drizzle>;
159

16-
// Create an empty database file if it doesn't exist
17-
if (!fs.existsSync(dbPath)) {
18-
fs.writeFileSync(dbPath, "");
19-
}
10+
if (process.env.NODE_ENV !== "test") {
11+
// Define the database URL - for development we'll use a local SQLite file
12+
const dataDir = path.join(process.cwd(), "data");
13+
// Ensure data directory exists
14+
if (!fs.existsSync(dataDir)) {
15+
fs.mkdirSync(dataDir, { recursive: true });
16+
}
2017

21-
// Create SQLite database instance using Bun's native driver
22-
let sqlite: Database;
23-
try {
24-
sqlite = new Database(dbPath);
25-
console.log("Successfully connected to SQLite database using Bun's native driver");
26-
} catch (error) {
27-
console.error("Error opening database:", error);
28-
throw error;
29-
}
18+
const dbPath = path.join(dataDir, "gitea-mirror.db");
3019

31-
// Create drizzle instance with the SQLite client
32-
export const db = drizzle({ client: sqlite });
20+
// Create an empty database file if it doesn't exist
21+
if (!fs.existsSync(dbPath)) {
22+
fs.writeFileSync(dbPath, "");
23+
}
3324

34-
/**
35-
* Run Drizzle migrations
36-
*/
37-
function runDrizzleMigrations() {
25+
// Create SQLite database instance using Bun's native driver
26+
let sqlite: Database;
3827
try {
39-
console.log("🔄 Checking for pending migrations...");
40-
41-
// Check if migrations table exists
42-
const migrationsTableExists = sqlite
43-
.query("SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'")
44-
.get();
45-
46-
if (!migrationsTableExists) {
47-
console.log("📦 First time setup - running initial migrations...");
48-
}
49-
50-
// Run migrations using Drizzle migrate function
51-
migrate(db, { migrationsFolder: "./drizzle" });
52-
53-
console.log("✅ Database migrations completed successfully");
28+
sqlite = new Database(dbPath);
29+
console.log("Successfully connected to SQLite database using Bun's native driver");
5430
} catch (error) {
55-
console.error("Error running migrations:", error);
31+
console.error("Error opening database:", error);
5632
throw error;
5733
}
34+
35+
// Create drizzle instance with the SQLite client
36+
db = drizzle({ client: sqlite });
37+
38+
/**
39+
* Run Drizzle migrations
40+
*/
41+
function runDrizzleMigrations() {
42+
try {
43+
console.log("🔄 Checking for pending migrations...");
44+
45+
// Check if migrations table exists
46+
const migrationsTableExists = sqlite
47+
.query("SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'")
48+
.get();
49+
50+
if (!migrationsTableExists) {
51+
console.log("📦 First time setup - running initial migrations...");
52+
}
53+
54+
// Run migrations using Drizzle migrate function
55+
migrate(db, { migrationsFolder: "./drizzle" });
56+
57+
console.log("✅ Database migrations completed successfully");
58+
} catch (error) {
59+
console.error("❌ Error running migrations:", error);
60+
throw error;
61+
}
62+
}
63+
64+
// Run Drizzle migrations after db is initialized
65+
runDrizzleMigrations();
5866
}
5967

60-
// Run Drizzle migrations after db is initialized
61-
runDrizzleMigrations();
68+
export { db };
6269

6370
// Export all table definitions from schema
6471
export {

src/lib/gitea.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ export async function getOrCreateGiteaOrg({
480480
try {
481481
console.log(`Attempting to get or create Gitea organization: ${orgName}`);
482482

483+
// Decrypt config tokens for API usage
484+
const decryptedConfig = decryptConfigTokens(config as Config);
485+
483486
const orgRes = await fetch(
484487
`${config.giteaConfig.url}/api/v1/orgs/${orgName}`,
485488
{

src/tests/setup.bun.ts

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,71 @@
33
* This file is automatically loaded before running tests
44
*/
55

6-
import { afterEach, beforeEach } from "bun:test";
6+
import { mock } from "bun:test";
77

8-
// Clean up after each test
9-
afterEach(() => {
10-
// Add any cleanup logic here
8+
// Set NODE_ENV to test
9+
process.env.NODE_ENV = "test";
10+
11+
// Mock the database module to prevent real database access during tests
12+
mock.module("@/lib/db", () => {
13+
const mockDb = {
14+
select: () => ({
15+
from: () => ({
16+
where: () => ({
17+
limit: () => Promise.resolve([])
18+
})
19+
})
20+
}),
21+
insert: () => ({
22+
values: () => Promise.resolve()
23+
}),
24+
update: () => ({
25+
set: () => ({
26+
where: () => Promise.resolve()
27+
})
28+
}),
29+
delete: () => ({
30+
where: () => Promise.resolve()
31+
})
32+
};
33+
34+
return {
35+
db: mockDb,
36+
users: {},
37+
events: {},
38+
configs: {},
39+
repositories: {},
40+
mirrorJobs: {},
41+
organizations: {},
42+
sessions: {},
43+
accounts: {},
44+
verificationTokens: {},
45+
oauthApplications: {},
46+
oauthAccessTokens: {},
47+
oauthConsent: {},
48+
ssoProviders: {}
49+
};
50+
});
51+
52+
// Mock drizzle-orm to prevent database migrations from running
53+
mock.module("drizzle-orm/bun-sqlite/migrator", () => {
54+
return {
55+
migrate: () => {}
56+
};
1157
});
1258

13-
// Setup before each test
14-
beforeEach(() => {
15-
// Add any setup logic here
59+
// Mock config encryption utilities
60+
mock.module("@/lib/utils/config-encryption", () => {
61+
return {
62+
decryptConfigTokens: (config: any) => {
63+
// Return the config as-is for tests
64+
return config;
65+
},
66+
encryptConfigTokens: (config: any) => {
67+
// Return the config as-is for tests
68+
return config;
69+
}
70+
};
1671
});
1772

1873
// Add DOM testing support if needed

0 commit comments

Comments
 (0)