|
| 1 | +// apps/dashboard/server/lib/github-write-locks.test.ts |
| 2 | +// Integration tests — these hit the real Postgres instance. |
| 3 | +// Run with: bun test apps/dashboard/server/lib/github-write-locks.test.ts |
| 4 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 5 | +import { sql } from "drizzle-orm" |
| 6 | +import { db } from "../db" |
| 7 | +import { githubWriteLocks, projects, reports } from "../db/schema" |
| 8 | +import { |
| 9 | + cleanupExpiredLocks, |
| 10 | + consumeWriteLock, |
| 11 | + recordWriteLock, |
| 12 | + WRITE_LOCK_TTL_MS, |
| 13 | +} from "./github-write-locks" |
| 14 | + |
| 15 | +// We need a real report row to satisfy the FK |
| 16 | +let testProjectId: string |
| 17 | +let testReportId: string |
| 18 | + |
| 19 | +async function truncate() { |
| 20 | + await db.execute(sql`TRUNCATE github_write_locks RESTART IDENTITY CASCADE`) |
| 21 | +} |
| 22 | + |
| 23 | +beforeEach(async () => { |
| 24 | + await db.execute( |
| 25 | + sql`TRUNCATE project_invitations, project_members, projects, "account", "session", "verification", "user" RESTART IDENTITY CASCADE`, |
| 26 | + ) |
| 27 | + await db.execute(sql`TRUNCATE report_attachments, reports RESTART IDENTITY CASCADE`) |
| 28 | + await truncate() |
| 29 | + |
| 30 | + const [p] = await db |
| 31 | + .insert(projects) |
| 32 | + .values({ |
| 33 | + name: "test", |
| 34 | + createdBy: "user-test", |
| 35 | + publicKey: "rp_pk_writelocktest", |
| 36 | + allowedOrigins: [], |
| 37 | + }) |
| 38 | + .returning() |
| 39 | + testProjectId = p.id |
| 40 | + |
| 41 | + const [r] = await db |
| 42 | + .insert(reports) |
| 43 | + .values({ |
| 44 | + projectId: testProjectId, |
| 45 | + title: "test report", |
| 46 | + description: "test", |
| 47 | + context: { |
| 48 | + pageUrl: "http://example.com", |
| 49 | + userAgent: "UA", |
| 50 | + viewport: { w: 1, h: 1 }, |
| 51 | + timestamp: new Date().toISOString(), |
| 52 | + }, |
| 53 | + }) |
| 54 | + .returning() |
| 55 | + testReportId = r.id |
| 56 | +}) |
| 57 | + |
| 58 | +afterEach(async () => { |
| 59 | + await truncate() |
| 60 | + await db.execute(sql`TRUNCATE report_attachments, reports RESTART IDENTITY CASCADE`) |
| 61 | + await db.execute( |
| 62 | + sql`TRUNCATE project_invitations, project_members, projects, "account", "session", "verification", "user" RESTART IDENTITY CASCADE`, |
| 63 | + ) |
| 64 | +}) |
| 65 | + |
| 66 | +describe("recordWriteLock", () => { |
| 67 | + test("inserts a row with correct fields", async () => { |
| 68 | + await recordWriteLock(db, { |
| 69 | + reportId: testReportId, |
| 70 | + kind: "title", |
| 71 | + signature: "abc123", |
| 72 | + }) |
| 73 | + |
| 74 | + const rows = await db.select().from(githubWriteLocks) |
| 75 | + expect(rows.length).toBe(1) |
| 76 | + expect(rows[0]?.reportId).toBe(testReportId) |
| 77 | + expect(rows[0]?.kind).toBe("title") |
| 78 | + expect(rows[0]?.signature).toBe("abc123") |
| 79 | + expect(rows[0]?.expiresAt.getTime()).toBeGreaterThan(Date.now()) |
| 80 | + expect(rows[0]?.expiresAt.getTime()).toBeLessThanOrEqual(Date.now() + WRITE_LOCK_TTL_MS + 1000) |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +describe("consumeWriteLock", () => { |
| 85 | + test("returns true and deletes when matching live row exists", async () => { |
| 86 | + await recordWriteLock(db, { |
| 87 | + reportId: testReportId, |
| 88 | + kind: "state", |
| 89 | + signature: "sig-xyz", |
| 90 | + }) |
| 91 | + |
| 92 | + const result = await consumeWriteLock(db, { |
| 93 | + reportId: testReportId, |
| 94 | + kind: "state", |
| 95 | + signature: "sig-xyz", |
| 96 | + }) |
| 97 | + |
| 98 | + expect(result).toBe(true) |
| 99 | + const rows = await db.select().from(githubWriteLocks) |
| 100 | + expect(rows.length).toBe(0) |
| 101 | + }) |
| 102 | + |
| 103 | + test("returns false when no matching row", async () => { |
| 104 | + const result = await consumeWriteLock(db, { |
| 105 | + reportId: testReportId, |
| 106 | + kind: "labels", |
| 107 | + signature: "nonexistent", |
| 108 | + }) |
| 109 | + expect(result).toBe(false) |
| 110 | + }) |
| 111 | + |
| 112 | + test("returns false when signature differs", async () => { |
| 113 | + await recordWriteLock(db, { |
| 114 | + reportId: testReportId, |
| 115 | + kind: "labels", |
| 116 | + signature: "correct-sig", |
| 117 | + }) |
| 118 | + |
| 119 | + const result = await consumeWriteLock(db, { |
| 120 | + reportId: testReportId, |
| 121 | + kind: "labels", |
| 122 | + signature: "wrong-sig", |
| 123 | + }) |
| 124 | + expect(result).toBe(false) |
| 125 | + }) |
| 126 | + |
| 127 | + test("returns false for expired rows", async () => { |
| 128 | + // Insert an already-expired lock directly |
| 129 | + const expiresAt = new Date(Date.now() - 1000) |
| 130 | + await db.insert(githubWriteLocks).values({ |
| 131 | + reportId: testReportId, |
| 132 | + kind: "title", |
| 133 | + signature: "expired-sig", |
| 134 | + expiresAt, |
| 135 | + }) |
| 136 | + |
| 137 | + const result = await consumeWriteLock(db, { |
| 138 | + reportId: testReportId, |
| 139 | + kind: "title", |
| 140 | + signature: "expired-sig", |
| 141 | + }) |
| 142 | + expect(result).toBe(false) |
| 143 | + }) |
| 144 | +}) |
| 145 | + |
| 146 | +describe("cleanupExpiredLocks", () => { |
| 147 | + test("removes only expired rows, leaves live rows", async () => { |
| 148 | + // Insert one live and one expired lock |
| 149 | + const expired = new Date(Date.now() - 1000) |
| 150 | + await db.insert(githubWriteLocks).values([ |
| 151 | + { |
| 152 | + reportId: testReportId, |
| 153 | + kind: "labels", |
| 154 | + signature: "live-sig", |
| 155 | + expiresAt: new Date(Date.now() + WRITE_LOCK_TTL_MS), |
| 156 | + }, |
| 157 | + { |
| 158 | + reportId: testReportId, |
| 159 | + kind: "state", |
| 160 | + signature: "expired-sig", |
| 161 | + expiresAt: expired, |
| 162 | + }, |
| 163 | + ]) |
| 164 | + |
| 165 | + const count = await cleanupExpiredLocks(db) |
| 166 | + expect(count).toBe(1) |
| 167 | + |
| 168 | + const remaining = await db.select().from(githubWriteLocks) |
| 169 | + expect(remaining.length).toBe(1) |
| 170 | + expect(remaining[0]?.signature).toBe("live-sig") |
| 171 | + }) |
| 172 | + |
| 173 | + test("returns 0 when no expired rows", async () => { |
| 174 | + await recordWriteLock(db, { |
| 175 | + reportId: testReportId, |
| 176 | + kind: "title", |
| 177 | + signature: "live", |
| 178 | + }) |
| 179 | + |
| 180 | + const count = await cleanupExpiredLocks(db) |
| 181 | + expect(count).toBe(0) |
| 182 | + }) |
| 183 | +}) |
0 commit comments