diff --git a/src/lib/gitea-lfs.test.ts b/src/lib/gitea-lfs.test.ts new file mode 100644 index 0000000..18147ad --- /dev/null +++ b/src/lib/gitea-lfs.test.ts @@ -0,0 +1,110 @@ +import { describe, test, expect, mock } from "bun:test"; +import type { Config } from "./db/schema"; + +describe("Git LFS Support", () => { + test("should include LFS flag when configured", () => { + const config: Partial = { + giteaConfig: { + url: "https://gitea.example.com", + token: "test-token", + defaultOwner: "testuser", + lfs: true, // LFS enabled + }, + mirrorOptions: { + mirrorLFS: true, // UI option enabled + }, + }; + + // Mock the payload that would be sent to Gitea API + const createMirrorPayload = (config: Partial, repoUrl: string) => { + const payload: any = { + clone_addr: repoUrl, + mirror: true, + private: false, + }; + + // Add LFS flag if configured + if (config.giteaConfig?.lfs || config.mirrorOptions?.mirrorLFS) { + payload.lfs = true; + } + + return payload; + }; + + const payload = createMirrorPayload(config, "https://github.com/user/repo.git"); + + expect(payload).toHaveProperty("lfs"); + expect(payload.lfs).toBe(true); + }); + + test("should not include LFS flag when not configured", () => { + const config: Partial = { + giteaConfig: { + url: "https://gitea.example.com", + token: "test-token", + defaultOwner: "testuser", + lfs: false, // LFS disabled + }, + mirrorOptions: { + mirrorLFS: false, // UI option disabled + }, + }; + + const createMirrorPayload = (config: Partial, repoUrl: string) => { + const payload: any = { + clone_addr: repoUrl, + mirror: true, + private: false, + }; + + if (config.giteaConfig?.lfs || config.mirrorOptions?.mirrorLFS) { + payload.lfs = true; + } + + return payload; + }; + + const payload = createMirrorPayload(config, "https://github.com/user/repo.git"); + + expect(payload).not.toHaveProperty("lfs"); + }); + + test("should handle LFS with either giteaConfig or mirrorOptions", () => { + // Test with only giteaConfig.lfs + const config1: Partial = { + giteaConfig: { + url: "https://gitea.example.com", + token: "test-token", + defaultOwner: "testuser", + lfs: true, + }, + }; + + // Test with only mirrorOptions.mirrorLFS + const config2: Partial = { + mirrorOptions: { + mirrorLFS: true, + }, + }; + + const createMirrorPayload = (config: Partial, repoUrl: string) => { + const payload: any = { + clone_addr: repoUrl, + mirror: true, + private: false, + }; + + if (config.giteaConfig?.lfs || config.mirrorOptions?.mirrorLFS) { + payload.lfs = true; + } + + return payload; + }; + + const payload1 = createMirrorPayload(config1, "https://github.com/user/repo.git"); + const payload2 = createMirrorPayload(config2, "https://github.com/user/repo.git"); + + expect(payload1.lfs).toBe(true); + expect(payload2.lfs).toBe(true); + }); +}); \ No newline at end of file diff --git a/src/lib/scheduler-service.test.ts b/src/lib/scheduler-service.test.ts new file mode 100644 index 0000000..8af4cef --- /dev/null +++ b/src/lib/scheduler-service.test.ts @@ -0,0 +1,82 @@ +import { describe, test, expect, mock } from "bun:test"; +import { repoStatusEnum } from "@/types/Repository"; +import type { Repository } from "./db/schema"; + +describe("Scheduler Service - Ignored Repository Handling", () => { + test("should skip repositories with 'ignored' status", async () => { + // Create a repository with ignored status + const ignoredRepo: Partial = { + id: "ignored-repo-id", + name: "ignored-repo", + fullName: "user/ignored-repo", + status: repoStatusEnum.parse("ignored"), + userId: "user-id", + }; + + // Mock the scheduler logic that checks repository status + const shouldMirrorRepository = (repo: Partial): boolean => { + // Skip ignored repositories + if (repo.status === "ignored") { + return false; + } + + // Skip recently mirrored repositories + if (repo.status === "synced" || repo.status === "mirrored") { + const lastUpdated = repo.updatedAt; + if (lastUpdated && Date.now() - lastUpdated.getTime() < 3600000) { + return false; // Skip if mirrored within last hour + } + } + + return true; + }; + + // Test that ignored repository is skipped + expect(shouldMirrorRepository(ignoredRepo)).toBe(false); + + // Test that non-ignored repository is not skipped + const activeRepo: Partial = { + ...ignoredRepo, + status: repoStatusEnum.parse("imported"), + }; + expect(shouldMirrorRepository(activeRepo)).toBe(true); + + // Test that recently synced repository is skipped + const recentlySyncedRepo: Partial = { + ...ignoredRepo, + status: repoStatusEnum.parse("synced"), + updatedAt: new Date(), + }; + expect(shouldMirrorRepository(recentlySyncedRepo)).toBe(false); + + // Test that old synced repository is not skipped + const oldSyncedRepo: Partial = { + ...ignoredRepo, + status: repoStatusEnum.parse("synced"), + updatedAt: new Date(Date.now() - 7200000), // 2 hours ago + }; + expect(shouldMirrorRepository(oldSyncedRepo)).toBe(true); + }); + + test("should validate all repository status enum values", () => { + const validStatuses = [ + "imported", + "mirroring", + "mirrored", + "syncing", + "synced", + "failed", + "skipped", + "ignored", + "deleting", + "deleted" + ]; + + validStatuses.forEach(status => { + expect(() => repoStatusEnum.parse(status)).not.toThrow(); + }); + + // Test invalid status + expect(() => repoStatusEnum.parse("invalid-status")).toThrow(); + }); +}); \ No newline at end of file diff --git a/src/tests/example.test.ts b/src/tests/example.test.ts deleted file mode 100644 index a191226..0000000 --- a/src/tests/example.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -// example.test.ts -import { describe, test, expect } from "bun:test"; - -describe("Example Test", () => { - test("should pass", () => { - expect(true).toBe(true); - }); -});