mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-09 13:06:45 +03:00
Fix tests
This commit is contained in:
@@ -4,61 +4,68 @@ import fs from "fs";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
||||||
|
|
||||||
// Define the database URL - for development we'll use a local SQLite file
|
// Skip database initialization in test environment
|
||||||
const dataDir = path.join(process.cwd(), "data");
|
let db: ReturnType<typeof drizzle>;
|
||||||
// Ensure data directory exists
|
|
||||||
if (!fs.existsSync(dataDir)) {
|
|
||||||
fs.mkdirSync(dataDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
const dbPath = path.join(dataDir, "gitea-mirror.db");
|
if (process.env.NODE_ENV !== "test") {
|
||||||
|
// Define the database URL - for development we'll use a local SQLite file
|
||||||
|
const dataDir = path.join(process.cwd(), "data");
|
||||||
|
// Ensure data directory exists
|
||||||
|
if (!fs.existsSync(dataDir)) {
|
||||||
|
fs.mkdirSync(dataDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
// Create an empty database file if it doesn't exist
|
const dbPath = path.join(dataDir, "gitea-mirror.db");
|
||||||
if (!fs.existsSync(dbPath)) {
|
|
||||||
fs.writeFileSync(dbPath, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create SQLite database instance using Bun's native driver
|
// Create an empty database file if it doesn't exist
|
||||||
let sqlite: Database;
|
if (!fs.existsSync(dbPath)) {
|
||||||
try {
|
fs.writeFileSync(dbPath, "");
|
||||||
sqlite = new Database(dbPath);
|
}
|
||||||
console.log("Successfully connected to SQLite database using Bun's native driver");
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error opening database:", error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create drizzle instance with the SQLite client
|
// Create SQLite database instance using Bun's native driver
|
||||||
export const db = drizzle({ client: sqlite });
|
let sqlite: Database;
|
||||||
|
|
||||||
/**
|
|
||||||
* Run Drizzle migrations
|
|
||||||
*/
|
|
||||||
function runDrizzleMigrations() {
|
|
||||||
try {
|
try {
|
||||||
console.log("🔄 Checking for pending migrations...");
|
sqlite = new Database(dbPath);
|
||||||
|
console.log("Successfully connected to SQLite database using Bun's native driver");
|
||||||
// Check if migrations table exists
|
|
||||||
const migrationsTableExists = sqlite
|
|
||||||
.query("SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'")
|
|
||||||
.get();
|
|
||||||
|
|
||||||
if (!migrationsTableExists) {
|
|
||||||
console.log("📦 First time setup - running initial migrations...");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run migrations using Drizzle migrate function
|
|
||||||
migrate(db, { migrationsFolder: "./drizzle" });
|
|
||||||
|
|
||||||
console.log("✅ Database migrations completed successfully");
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("❌ Error running migrations:", error);
|
console.error("Error opening database:", error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create drizzle instance with the SQLite client
|
||||||
|
db = drizzle({ client: sqlite });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run Drizzle migrations
|
||||||
|
*/
|
||||||
|
function runDrizzleMigrations() {
|
||||||
|
try {
|
||||||
|
console.log("🔄 Checking for pending migrations...");
|
||||||
|
|
||||||
|
// Check if migrations table exists
|
||||||
|
const migrationsTableExists = sqlite
|
||||||
|
.query("SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'")
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (!migrationsTableExists) {
|
||||||
|
console.log("📦 First time setup - running initial migrations...");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run migrations using Drizzle migrate function
|
||||||
|
migrate(db, { migrationsFolder: "./drizzle" });
|
||||||
|
|
||||||
|
console.log("✅ Database migrations completed successfully");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Error running migrations:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run Drizzle migrations after db is initialized
|
||||||
|
runDrizzleMigrations();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run Drizzle migrations after db is initialized
|
export { db };
|
||||||
runDrizzleMigrations();
|
|
||||||
|
|
||||||
// Export all table definitions from schema
|
// Export all table definitions from schema
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -480,6 +480,9 @@ export async function getOrCreateGiteaOrg({
|
|||||||
try {
|
try {
|
||||||
console.log(`Attempting to get or create Gitea organization: ${orgName}`);
|
console.log(`Attempting to get or create Gitea organization: ${orgName}`);
|
||||||
|
|
||||||
|
// Decrypt config tokens for API usage
|
||||||
|
const decryptedConfig = decryptConfigTokens(config as Config);
|
||||||
|
|
||||||
const orgRes = await fetch(
|
const orgRes = await fetch(
|
||||||
`${config.giteaConfig.url}/api/v1/orgs/${orgName}`,
|
`${config.giteaConfig.url}/api/v1/orgs/${orgName}`,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,16 +3,71 @@
|
|||||||
* This file is automatically loaded before running tests
|
* This file is automatically loaded before running tests
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { afterEach, beforeEach } from "bun:test";
|
import { mock } from "bun:test";
|
||||||
|
|
||||||
// Clean up after each test
|
// Set NODE_ENV to test
|
||||||
afterEach(() => {
|
process.env.NODE_ENV = "test";
|
||||||
// Add any cleanup logic here
|
|
||||||
|
// Mock the database module to prevent real database access during tests
|
||||||
|
mock.module("@/lib/db", () => {
|
||||||
|
const mockDb = {
|
||||||
|
select: () => ({
|
||||||
|
from: () => ({
|
||||||
|
where: () => ({
|
||||||
|
limit: () => Promise.resolve([])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
insert: () => ({
|
||||||
|
values: () => Promise.resolve()
|
||||||
|
}),
|
||||||
|
update: () => ({
|
||||||
|
set: () => ({
|
||||||
|
where: () => Promise.resolve()
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
delete: () => ({
|
||||||
|
where: () => Promise.resolve()
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
db: mockDb,
|
||||||
|
users: {},
|
||||||
|
events: {},
|
||||||
|
configs: {},
|
||||||
|
repositories: {},
|
||||||
|
mirrorJobs: {},
|
||||||
|
organizations: {},
|
||||||
|
sessions: {},
|
||||||
|
accounts: {},
|
||||||
|
verificationTokens: {},
|
||||||
|
oauthApplications: {},
|
||||||
|
oauthAccessTokens: {},
|
||||||
|
oauthConsent: {},
|
||||||
|
ssoProviders: {}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Setup before each test
|
// Mock drizzle-orm to prevent database migrations from running
|
||||||
beforeEach(() => {
|
mock.module("drizzle-orm/bun-sqlite/migrator", () => {
|
||||||
// Add any setup logic here
|
return {
|
||||||
|
migrate: () => {}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock config encryption utilities
|
||||||
|
mock.module("@/lib/utils/config-encryption", () => {
|
||||||
|
return {
|
||||||
|
decryptConfigTokens: (config: any) => {
|
||||||
|
// Return the config as-is for tests
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
encryptConfigTokens: (config: any) => {
|
||||||
|
// Return the config as-is for tests
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add DOM testing support if needed
|
// Add DOM testing support if needed
|
||||||
|
|||||||
Reference in New Issue
Block a user