mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-09 21:16:48 +03:00
test update
This commit is contained in:
@@ -44,6 +44,8 @@ class MockHttpError extends Error {
|
||||
// Track call counts for org tests
|
||||
let orgCheckCount = 0;
|
||||
let orgTestContext = "";
|
||||
let getOrgCalled = false;
|
||||
let createOrgCalled = false;
|
||||
|
||||
const mockHttpGet = mock(async (url: string, headers?: any) => {
|
||||
// Return different responses based on URL patterns
|
||||
@@ -76,6 +78,35 @@ const mockHttpGet = mock(async (url: string, headers?: any) => {
|
||||
headers: new Headers()
|
||||
};
|
||||
}
|
||||
if (url.includes("/api/v1/repos/starred/non-mirror-repo")) {
|
||||
return {
|
||||
data: {
|
||||
id: 456,
|
||||
name: "non-mirror-repo",
|
||||
mirror: false,
|
||||
owner: { login: "starred" },
|
||||
private: false
|
||||
},
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: new Headers()
|
||||
};
|
||||
}
|
||||
if (url.includes("/api/v1/repos/starred/mirror-repo")) {
|
||||
return {
|
||||
data: {
|
||||
id: 789,
|
||||
name: "mirror-repo",
|
||||
mirror: true,
|
||||
owner: { login: "starred" },
|
||||
mirror_interval: "8h",
|
||||
private: false
|
||||
},
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: new Headers()
|
||||
};
|
||||
}
|
||||
if (url.includes("/api/v1/repos/")) {
|
||||
throw new MockHttpError("Not Found", 404, "Not Found");
|
||||
}
|
||||
@@ -97,6 +128,7 @@ const mockHttpGet = mock(async (url: string, headers?: any) => {
|
||||
}
|
||||
|
||||
if (url.includes("/api/v1/orgs/neworg")) {
|
||||
getOrgCalled = true;
|
||||
// Org doesn't exist
|
||||
throw new MockHttpError("Not Found", 404, "Not Found");
|
||||
}
|
||||
@@ -115,6 +147,7 @@ const mockHttpPost = mock(async (url: string, body?: any, headers?: any) => {
|
||||
);
|
||||
}
|
||||
if (url.includes("/api/v1/orgs") && body?.username === "neworg") {
|
||||
createOrgCalled = true;
|
||||
return {
|
||||
data: { id: 777, username: "neworg" },
|
||||
status: 201,
|
||||
@@ -122,10 +155,23 @@ const mockHttpPost = mock(async (url: string, body?: any, headers?: any) => {
|
||||
headers: new Headers()
|
||||
};
|
||||
}
|
||||
if (url.includes("/mirror-sync")) {
|
||||
return {
|
||||
data: { success: true },
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: new Headers()
|
||||
};
|
||||
}
|
||||
return { data: {}, status: 200, statusText: "OK", headers: new Headers() };
|
||||
});
|
||||
|
||||
const mockHttpDelete = mock(async () => ({ data: {}, status: 200, statusText: "OK", headers: new Headers() }));
|
||||
const mockHttpDelete = mock(async (url: string, headers?: any) => {
|
||||
if (url.includes("/api/v1/repos/starred/test-repo")) {
|
||||
return { data: {}, status: 204, statusText: "No Content", headers: new Headers() };
|
||||
}
|
||||
return { data: {}, status: 200, statusText: "OK", headers: new Headers() };
|
||||
});
|
||||
|
||||
mock.module("@/lib/http-client", () => ({
|
||||
httpGet: mockHttpGet,
|
||||
@@ -156,6 +202,11 @@ describe("Enhanced Gitea Operations", () => {
|
||||
mockCreateMirrorJob.mockClear();
|
||||
mockDb.insert.mockClear();
|
||||
mockDb.update.mockClear();
|
||||
// Reset tracking variables
|
||||
orgCheckCount = 0;
|
||||
orgTestContext = "";
|
||||
getOrgCalled = false;
|
||||
createOrgCalled = false;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -251,44 +302,10 @@ describe("Enhanced Gitea Operations", () => {
|
||||
});
|
||||
|
||||
describe("getOrCreateGiteaOrgEnhanced", () => {
|
||||
beforeEach(() => {
|
||||
orgCheckCount = 0;
|
||||
orgTestContext = "";
|
||||
});
|
||||
|
||||
test("should handle duplicate organization constraint error with retry", async () => {
|
||||
orgTestContext = "duplicate-retry";
|
||||
let attemptCount = 0;
|
||||
orgCheckCount = 0; // Reset the count
|
||||
|
||||
global.fetch = mockFetch(async (url: string, options?: RequestInit) => {
|
||||
attemptCount++;
|
||||
|
||||
if (url.includes("/api/v1/orgs/starred") && options?.method !== "POST") {
|
||||
// First two attempts: org doesn't exist
|
||||
if (attemptCount <= 2) {
|
||||
return createMockResponse(
|
||||
"Not Found",
|
||||
{ ok: false, status: 404, statusText: "Not Found" }
|
||||
);
|
||||
}
|
||||
// Third attempt: org now exists (created by another process)
|
||||
return createMockResponse({ id: 999, username: "starred" });
|
||||
}
|
||||
|
||||
if (url.includes("/api/v1/orgs") && options?.method === "POST") {
|
||||
// Simulate duplicate constraint error
|
||||
return createMockResponse(
|
||||
{ message: "pq: duplicate key value violates unique constraint \"UQE_user_lower_name\"" },
|
||||
{ ok: false, status: 422, statusText: "Unprocessable Entity" }
|
||||
);
|
||||
}
|
||||
|
||||
return createMockResponse(
|
||||
"Internal Server Error",
|
||||
{ ok: false, status: 500 }
|
||||
);
|
||||
});
|
||||
|
||||
const config: Partial<Config> = {
|
||||
userId: "user123",
|
||||
giteaConfig: {
|
||||
@@ -307,35 +324,13 @@ describe("Enhanced Gitea Operations", () => {
|
||||
});
|
||||
|
||||
expect(orgId).toBe(999);
|
||||
expect(attemptCount).toBeGreaterThanOrEqual(3);
|
||||
expect(orgCheckCount).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
|
||||
test("should create organization on first attempt", async () => {
|
||||
let getOrgCalled = false;
|
||||
let createOrgCalled = false;
|
||||
|
||||
global.fetch = mockFetch(async (url: string, options?: RequestInit) => {
|
||||
if (url.includes("/api/v1/orgs/neworg") && options?.method !== "POST") {
|
||||
getOrgCalled = true;
|
||||
return createMockResponse(
|
||||
"Not Found",
|
||||
{ ok: false, status: 404, statusText: "Not Found" }
|
||||
);
|
||||
}
|
||||
|
||||
if (url.includes("/api/v1/orgs") && options?.method === "POST") {
|
||||
createOrgCalled = true;
|
||||
return createMockResponse(
|
||||
{ id: 777, username: "neworg" },
|
||||
{ ok: true, status: 201 }
|
||||
);
|
||||
}
|
||||
|
||||
return createMockResponse(
|
||||
"Internal Server Error",
|
||||
{ ok: false, status: 500 }
|
||||
);
|
||||
});
|
||||
// Reset tracking variables
|
||||
getOrgCalled = false;
|
||||
createOrgCalled = false;
|
||||
|
||||
const config: Partial<Config> = {
|
||||
userId: "user123",
|
||||
@@ -366,22 +361,6 @@ describe("Enhanced Gitea Operations", () => {
|
||||
|
||||
describe("syncGiteaRepoEnhanced", () => {
|
||||
test("should fail gracefully when repository is not a mirror", async () => {
|
||||
global.fetch = mockFetch(async (url: string) => {
|
||||
if (url.includes("/api/v1/repos/starred/non-mirror-repo") && !url.includes("mirror-sync")) {
|
||||
return createMockResponse({
|
||||
id: 456,
|
||||
name: "non-mirror-repo",
|
||||
owner: "starred",
|
||||
mirror: false, // Not a mirror
|
||||
private: false,
|
||||
});
|
||||
}
|
||||
return createMockResponse(
|
||||
"Not Found",
|
||||
{ ok: false, status: 404 }
|
||||
);
|
||||
});
|
||||
|
||||
const config: Partial<Config> = {
|
||||
userId: "user123",
|
||||
githubConfig: {
|
||||
@@ -427,31 +406,6 @@ describe("Enhanced Gitea Operations", () => {
|
||||
});
|
||||
|
||||
test("should successfully sync a mirror repository", async () => {
|
||||
let syncCalled = false;
|
||||
|
||||
global.fetch = mockFetch(async (url: string, options?: RequestInit) => {
|
||||
if (url.includes("/api/v1/repos/starred/mirror-repo") && !url.includes("mirror-sync")) {
|
||||
return createMockResponse({
|
||||
id: 789,
|
||||
name: "mirror-repo",
|
||||
owner: "starred",
|
||||
mirror: true,
|
||||
mirror_interval: "8h",
|
||||
private: false,
|
||||
});
|
||||
}
|
||||
|
||||
if (url.includes("/mirror-sync") && options?.method === "POST") {
|
||||
syncCalled = true;
|
||||
return createMockResponse({ success: true });
|
||||
}
|
||||
|
||||
return createMockResponse(
|
||||
"Not Found",
|
||||
{ ok: false, status: 404 }
|
||||
);
|
||||
});
|
||||
|
||||
const config: Partial<Config> = {
|
||||
userId: "user123",
|
||||
githubConfig: {
|
||||
@@ -494,7 +448,6 @@ describe("Enhanced Gitea Operations", () => {
|
||||
const result = await syncGiteaRepoEnhanced({ config, repository });
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(syncCalled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -543,19 +496,7 @@ describe("Enhanced Gitea Operations", () => {
|
||||
});
|
||||
|
||||
test("should delete non-mirror repository with delete strategy", async () => {
|
||||
let deleteCalled = false;
|
||||
|
||||
global.fetch = mockFetch(async (url: string, options?: RequestInit) => {
|
||||
if (url.includes("/api/v1/repos/starred/test-repo") && options?.method === "DELETE") {
|
||||
deleteCalled = true;
|
||||
return {
|
||||
ok: true,
|
||||
status: 204,
|
||||
};
|
||||
}
|
||||
return { ok: false, status: 404 };
|
||||
});
|
||||
|
||||
// Mock deleteGiteaRepo which uses httpDelete via the http-client mock
|
||||
const repoInfo = {
|
||||
id: 124,
|
||||
name: "test-repo",
|
||||
@@ -587,6 +528,17 @@ describe("Enhanced Gitea Operations", () => {
|
||||
},
|
||||
};
|
||||
|
||||
// deleteGiteaRepo in the actual code uses fetch directly, not httpDelete
|
||||
// We need to mock fetch for this test
|
||||
let deleteCalled = false;
|
||||
global.fetch = mockFetch(async (url: string, options?: RequestInit) => {
|
||||
if (url.includes("/api/v1/repos/starred/test-repo") && options?.method === "DELETE") {
|
||||
deleteCalled = true;
|
||||
return createMockResponse(null, { ok: true, status: 204 });
|
||||
}
|
||||
return createMockResponse(null, { ok: false, status: 404 });
|
||||
});
|
||||
|
||||
await handleExistingNonMirrorRepo({
|
||||
config,
|
||||
repository,
|
||||
|
||||
Reference in New Issue
Block a user