| 1 | import { createHash, randomBytes } from 'node:crypto'; |
| 2 | import path from 'node:path |
| 3 | import { PassThrough } |
| 4 | import { createDatabase, type |
| 5 | import { ConfigService } |
| 6 | import { and, eq |
| 7 | import { migrate } |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { lfsObjectKey } |
| 11 | import { LfsObjectMismatchError } |
| 12 | import { InMemoryS3 } |
| 13 | import type { S3Service |
| 14 | import { StorageQuotaService } |
| 15 | import { LfsService } |
| 16 | |
| 17 | const CONNECTION = process |
| 18 | const MIGRATIONS = path |
| 19 | import.meta.dirname, |
| 20 | '../../../../../../packages/db/drizzle', |
| 21 | ); |
| 22 | const OWNER = ' |
| 23 | |
| 24 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 25 | describe.skipIf(!CONNECTION |
| 26 | let db: |
| 27 | let pool: |
| 28 | let s3: |
| 29 | let lfs: |
| 30 | let repository: |
| 31 | |
| 32 | beforeAll(async () |
| 33 | ({ db, pool |
| 34 | await migrate(db |
| 35 | await db.delete |
| 36 | await db.insert |
| 37 | id: OWNER, |
| 38 | name: 'Owner', |
| 39 | email: 'lfs-owner@example.com', |
| 40 | username: 'lfs-owner', |
| 41 | }); |
| 42 | [repository] = await |
| 43 | .insert(schema.repository |
| 44 | .values({ |
| 45 | name: 'lfs', |
| 46 | slug: 'lfs', |
| 47 | ownerId: OWNER, |
| 48 | visibility: 'public', |
| 49 | }) |
| 50 | .returning(); |
| 51 | const config = |
| 52 | BETTER_AUTH_URL: 'http://ghost.test', |
| 53 | BETTER_AUTH_SECRET: 'secret', |
| 54 | }); |
| 55 | s3 = new |
| 56 | lfs = new |
| 57 | db, |
| 58 | s3 as unknown |
| 59 | new StorageQuotaService(db |
| 60 | config, |
| 61 | ); |
| 62 | }); |
| 63 | |
| 64 | afterAll(async () |
| 65 | await db.delete |
| 66 | await pool?.end |
| 67 | }); |
| 68 | |
| 69 | it('leaves the upload that took over a lapsed reservation alone when the lapsed one fails', |
| 70 | const file = |
| 71 | const oid = |
| 72 | const upload = |
| 73 | lfs.upload({ |
| 74 | |
| 75 | const stalled = |
| 76 | const first = |
| 77 | // Wait for its reservation, then age it past the TTL as a day-long stall would. |
| 78 | await expect |
| 79 | .poll(() => |
| 80 | .toBe(1); |
| 81 | await db |
| 82 | .update(schema.lfsObject |
| 83 | .set({ createdAt: |
| 84 | .where(eq(schema |
| 85 | |
| 86 | const second = |
| 87 | second.end(file |
| 88 | await upload(second |
| 89 | |
| 90 | stalled.end(randomBytes |
| 91 | await expect(first |
| 92 | |
| 93 | expect(await lfs |
| 94 | expect(s3.objects |
| 95 | // Neither upload's staging object is left behind. |
| 96 | expect([...s3. |
| 97 | const [row] |
| 98 | .select({ uploadedAt: |
| 99 | .from(schema.lfsObject |
| 100 | .where( |
| 101 | and( |
| 102 | eq(schema.lfsObject |
| 103 | eq(schema.lfsObject |
| 104 | ), |
| 105 | ); |
| 106 | expect(row.uploadedAt |
| 107 | }); |
| 108 | }); |