Blame · Line-by-line history
import-verify.test.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 14c3cc8 | 1 | /** |
| 2 | * Unit tests for src/lib/import-verify.ts. | |
| 3 | * | |
| 4 | * We stub the `../db` module with `mock.module` so we never touch Neon. | |
| 5 | * The fake `db.select(...)` chain returns whatever the per-test closure | |
| 6 | * decides — either undefined (repo not found) or a plausible row whose | |
| 7 | * on-disk path points somewhere that doesn't exist. | |
| 8 | */ | |
| 9 | ||
| 10 | import { describe, it, expect, mock } from "bun:test"; | |
| 11 | ||
| 12 | // Per-test mutable row — each test assigns its own value before calling | |
| 13 | // verifyMigration. The chained Drizzle-style select builder ends in | |
| 14 | // `.limit(1)` which we return as an array containing (or omitting) this row. | |
| 15 | let _nextRow: { repoName: string; ownerName: string | null } | undefined; | |
| 16 | ||
| 17 | // Minimal Drizzle `db.select(...).from(...).leftJoin(...).where(...).limit(1)` | |
| 18 | // chain. Every step returns `this` except `.limit()` which resolves the | |
| 19 | // fake row as a 1-element (or 0-element) array. | |
| 20 | const _chain: any = { | |
| 21 | from: () => _chain, | |
| 22 | leftJoin: () => _chain, | |
| 23 | where: () => _chain, | |
| 24 | limit: async () => (_nextRow ? [_nextRow] : []), | |
| 25 | }; | |
| 26 | ||
| 27 | // Mock `../db` at module scope so the dynamic import of ../lib/import-verify | |
| 28 | // below picks up the stub instead of the real Neon-backed proxy. | |
| 29 | const _fakeDb = { | |
| 30 | db: { select: () => _chain }, | |
| 31 | getDb: () => ({ select: () => _chain }), | |
| 32 | }; | |
| 33 | mock.module("../db", () => _fakeDb); | |
| 34 | ||
| 35 | // Point GIT_REPOS_PATH at a directory that definitely doesn't contain | |
| 36 | // any of the fake repos we'll reference, so `clonable` checks fail. | |
| 37 | process.env.GIT_REPOS_PATH = "/tmp/gluecron-import-verify-does-not-exist"; | |
| 38 | ||
| 39 | describe("verifyMigration", () => { | |
| 40 | it("returns clonable:false and issue when repo not found in DB", async () => { | |
| 41 | _nextRow = undefined; | |
| 42 | const { verifyMigration } = await import("../lib/import-verify"); | |
| 43 | const r = await verifyMigration(999); | |
| 44 | expect(r.repoId).toBe(999); | |
| 45 | expect(r.clonable).toBe(false); | |
| 46 | expect(r.hasDefaultBranch).toBe(false); | |
| 47 | expect(r.commitCount).toBe(0); | |
| 48 | expect(r.issues).toContain("repo not found"); | |
| 49 | }); | |
| 50 | ||
| 51 | it("returns clonable:false with issue when git dir is missing", async () => { | |
| 52 | _nextRow = { repoName: "ghost", ownerName: "nobody" }; | |
| 53 | const { verifyMigration } = await import("../lib/import-verify"); | |
| 54 | const r = await verifyMigration(42); | |
| 55 | expect(r.repoId).toBe(42); | |
| 56 | expect(r.clonable).toBe(false); | |
| 57 | // At least one of the sentinel-file issues should be present. | |
| 58 | const hasMissing = r.issues.some( | |
| 59 | (s) => | |
| 60 | s.includes("missing HEAD") || | |
| 61 | s.includes("missing config") || | |
| 62 | s.includes("missing objects") | |
| 63 | ); | |
| 64 | expect(hasMissing).toBe(true); | |
| 65 | // The git shell-outs against a non-existent path should also fail | |
| 66 | // and contribute to issues, but we don't assert the exact wording | |
| 67 | // (it varies across git versions). | |
| 68 | expect(r.hasDefaultBranch).toBe(false); | |
| 69 | expect(r.commitCount).toBe(0); | |
| 70 | }); | |
| 71 | }); |