mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-06 11:36:44 +03:00
fix: resolve CI test failures and timeouts
- Update Bun version in CI to match local version (1.2.16) - Add bunfig.toml with 5s test timeout to prevent hanging tests - Mock setTimeout globally in test setup to avoid timing issues - Add NODE_ENV check to skip delays during tests - Fix missing exports in config-encryption mock - Remove retryDelay in tests to ensure immediate execution These changes ensure tests run consistently between local and CI environments
This commit is contained in:
2
.github/workflows/astro-build-test.yml
vendored
2
.github/workflows/astro-build-test.yml
vendored
@@ -28,7 +28,7 @@ jobs:
|
|||||||
- name: Setup Bun
|
- name: Setup Bun
|
||||||
uses: oven-sh/setup-bun@v1
|
uses: oven-sh/setup-bun@v1
|
||||||
with:
|
with:
|
||||||
bun-version: '1.2.9'
|
bun-version: '1.2.16'
|
||||||
|
|
||||||
- name: Check lockfile and install dependencies
|
- name: Check lockfile and install dependencies
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
6
bunfig.toml
Normal file
6
bunfig.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[test]
|
||||||
|
# Set test timeout to 5 seconds (5000ms) to prevent hanging tests
|
||||||
|
timeout = 5000
|
||||||
|
|
||||||
|
# Preload the setup file
|
||||||
|
preload = ["./src/tests/setup.bun.ts"]
|
||||||
@@ -38,27 +38,17 @@ import { repoStatusEnum } from "@/types/Repository";
|
|||||||
|
|
||||||
describe("Enhanced Gitea Operations", () => {
|
describe("Enhanced Gitea Operations", () => {
|
||||||
let originalFetch: typeof global.fetch;
|
let originalFetch: typeof global.fetch;
|
||||||
let originalTimeout: typeof global.setTimeout;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
originalFetch = global.fetch;
|
originalFetch = global.fetch;
|
||||||
originalTimeout = global.setTimeout;
|
|
||||||
// Clear mocks
|
// Clear mocks
|
||||||
mockCreateMirrorJob.mockClear();
|
mockCreateMirrorJob.mockClear();
|
||||||
mockDb.insert.mockClear();
|
mockDb.insert.mockClear();
|
||||||
mockDb.update.mockClear();
|
mockDb.update.mockClear();
|
||||||
|
|
||||||
// Mock setTimeout for consistent timing in tests
|
|
||||||
global.setTimeout = ((fn: Function, delay: number) => {
|
|
||||||
// Execute immediately in tests to avoid timing issues
|
|
||||||
fn();
|
|
||||||
return 0;
|
|
||||||
}) as any;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
global.fetch = originalFetch;
|
global.fetch = originalFetch;
|
||||||
global.setTimeout = originalTimeout;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getGiteaRepoInfo", () => {
|
describe("getGiteaRepoInfo", () => {
|
||||||
@@ -196,7 +186,7 @@ describe("Enhanced Gitea Operations", () => {
|
|||||||
orgName: "starred",
|
orgName: "starred",
|
||||||
config,
|
config,
|
||||||
maxRetries: 3,
|
maxRetries: 3,
|
||||||
retryDelay: 10,
|
retryDelay: 0, // No delay in tests
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(orgId).toBe(999);
|
expect(orgId).toBe(999);
|
||||||
@@ -248,6 +238,7 @@ describe("Enhanced Gitea Operations", () => {
|
|||||||
const orgId = await getOrCreateGiteaOrgEnhanced({
|
const orgId = await getOrCreateGiteaOrgEnhanced({
|
||||||
orgName: "neworg",
|
orgName: "neworg",
|
||||||
config,
|
config,
|
||||||
|
retryDelay: 0, // No delay in tests
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(orgId).toBe(777);
|
expect(orgId).toBe(777);
|
||||||
|
|||||||
@@ -157,9 +157,11 @@ export async function getOrCreateGiteaOrgEnhanced({
|
|||||||
console.log(`[Org Creation] Organization creation failed due to duplicate. Will retry check.`);
|
console.log(`[Org Creation] Organization creation failed due to duplicate. Will retry check.`);
|
||||||
|
|
||||||
// Wait before retry with exponential backoff
|
// Wait before retry with exponential backoff
|
||||||
const delay = retryDelay * Math.pow(2, attempt);
|
const delay = process.env.NODE_ENV === 'test' ? 0 : retryDelay * Math.pow(2, attempt);
|
||||||
console.log(`[Org Creation] Waiting ${delay}ms before retry...`);
|
console.log(`[Org Creation] Waiting ${delay}ms before retry...`);
|
||||||
await new Promise(resolve => setTimeout(resolve, delay));
|
if (delay > 0) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, delay));
|
||||||
|
}
|
||||||
continue; // Retry the loop
|
continue; // Retry the loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,18 @@ import type { Config, Repository } from "./db/schema";
|
|||||||
|
|
||||||
describe("Mirror Sync Error Handling", () => {
|
describe("Mirror Sync Error Handling", () => {
|
||||||
let originalFetch: typeof global.fetch;
|
let originalFetch: typeof global.fetch;
|
||||||
|
let originalSetTimeout: typeof global.setTimeout;
|
||||||
let mockDbUpdate: any;
|
let mockDbUpdate: any;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
originalFetch = global.fetch;
|
originalFetch = global.fetch;
|
||||||
|
originalSetTimeout = global.setTimeout;
|
||||||
|
|
||||||
|
// Mock setTimeout to avoid delays in tests
|
||||||
|
global.setTimeout = ((fn: Function) => {
|
||||||
|
Promise.resolve().then(() => fn());
|
||||||
|
return 0;
|
||||||
|
}) as any;
|
||||||
|
|
||||||
// Mock database update operations
|
// Mock database update operations
|
||||||
mockDbUpdate = mock(() => ({
|
mockDbUpdate = mock(() => ({
|
||||||
@@ -24,6 +32,7 @@ describe("Mirror Sync Error Handling", () => {
|
|||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
global.fetch = originalFetch;
|
global.fetch = originalFetch;
|
||||||
|
global.setTimeout = originalSetTimeout;
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Mirror sync API errors", () => {
|
describe("Mirror sync API errors", () => {
|
||||||
|
|||||||
@@ -8,6 +8,23 @@ import { mock } from "bun:test";
|
|||||||
// Set NODE_ENV to test
|
// Set NODE_ENV to test
|
||||||
process.env.NODE_ENV = "test";
|
process.env.NODE_ENV = "test";
|
||||||
|
|
||||||
|
// Mock setTimeout globally to prevent hanging tests
|
||||||
|
const originalSetTimeout = global.setTimeout;
|
||||||
|
global.setTimeout = ((fn: Function, delay?: number) => {
|
||||||
|
// In tests, execute immediately or with minimal delay
|
||||||
|
if (delay && delay > 100) {
|
||||||
|
// For long delays, execute immediately
|
||||||
|
Promise.resolve().then(() => fn());
|
||||||
|
} else {
|
||||||
|
// For short delays, use setImmediate-like behavior
|
||||||
|
Promise.resolve().then(() => fn());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}) as any;
|
||||||
|
|
||||||
|
// Restore setTimeout for any code that needs real timing
|
||||||
|
(global as any).__originalSetTimeout = originalSetTimeout;
|
||||||
|
|
||||||
// Mock the database module to prevent real database access during tests
|
// Mock the database module to prevent real database access during tests
|
||||||
mock.module("@/lib/db", () => {
|
mock.module("@/lib/db", () => {
|
||||||
const mockDb = {
|
const mockDb = {
|
||||||
@@ -66,6 +83,14 @@ mock.module("@/lib/utils/config-encryption", () => {
|
|||||||
encryptConfigTokens: (config: any) => {
|
encryptConfigTokens: (config: any) => {
|
||||||
// Return the config as-is for tests
|
// Return the config as-is for tests
|
||||||
return config;
|
return config;
|
||||||
|
},
|
||||||
|
getDecryptedGitHubToken: (config: any) => {
|
||||||
|
// Return the token as-is for tests
|
||||||
|
return config.githubConfig?.token || "";
|
||||||
|
},
|
||||||
|
getDecryptedGiteaToken: (config: any) => {
|
||||||
|
// Return the token as-is for tests
|
||||||
|
return config.giteaConfig?.token || "";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user