| 1 | import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | import { beforeEach, describe |
| 3 | |
| 4 | import { PushTransactionService } |
| 5 | import { WalStoreService } |
| 6 | import { |
| 7 | NonFastForwardError, |
| 8 | RepositoryDeletedError, |
| 9 | } from '../../../lib/git/wal/wal.errors.js'; |
| 10 | import { |
| 11 | emptyIndex, |
| 12 | type RefTransition, |
| 13 | type WalIndex, |
| 14 | } from '../../../lib/git/wal/wal.types.js'; |
| 15 | import { Readable } |
| 16 | |
| 17 | import { |
| 18 | bufferBody, |
| 19 | type GitRequestBody, |
| 20 | } from '../../../lib/git/protocol/git-request-body.js'; |
| 21 | |
| 22 | const oid = ( |
| 23 | const PACK_OFFSET = 8 |
| 24 | const body = bufferBody |
| 25 | Buffer.concat([Buffer |
| 26 | ); |
| 27 | |
| 28 | function transition(oldByte: |
| 29 | return { ref: |
| 30 | } |
| 31 | |
| 32 | describe('PushTransactionService', () |
| 33 | let service: |
| 34 | const store = |
| 35 | putEntry: vi.fn |
| 36 | readIndex: vi.fn |
| 37 | casIndex: vi.fn |
| 38 | deleteEntry: vi.fn |
| 39 | }; |
| 40 | |
| 41 | beforeEach(async () |
| 42 | vi.clearAllMocks(); |
| 43 | const module: |
| 44 | providers: [ |
| 45 | PushTransactionService, |
| 46 | { provide: WalStoreService, |
| 47 | ], |
| 48 | }).compile(); |
| 49 | |
| 50 | service = module. |
| 51 | }); |
| 52 | |
| 53 | it('creates the index on a first push', |
| 54 | store.readIndex.mockResolvedValue |
| 55 | store.casIndex.mockResolvedValue |
| 56 | |
| 57 | const result = |
| 58 | repoId: 'phantomknight287/ghost', |
| 59 | transitions: [transition(0 |
| 60 | body, |
| 61 | packOffset: PACK_OFFSET, |
| 62 | }); |
| 63 | |
| 64 | expect(result.seq |
| 65 | expect(store.casIndex |
| 66 | 'phantomknight287/ghost', |
| 67 | expect.objectContaining({ |
| 68 | null, |
| 69 | ); |
| 70 | }); |
| 71 | |
| 72 | it('uploads the pack once and reuses it across CAS retries', |
| 73 | const first: |
| 74 | ...emptyIndex(), |
| 75 | seq: 5, |
| 76 | refs: new Map |
| 77 | }; |
| 78 | const second: |
| 79 | store.readIndex |
| 80 | .mockResolvedValueOnce({ index: |
| 81 | .mockResolvedValueOnce({ index: |
| 82 | store.casIndex.mockResolvedValueOnce |
| 83 | |
| 84 | const result = |
| 85 | repoId: 'phantomknight287/ghost', |
| 86 | transitions: [transition(0xaa |
| 87 | body, |
| 88 | packOffset: PACK_OFFSET, |
| 89 | }); |
| 90 | |
| 91 | expect(result.seq |
| 92 | expect(store.putEntry |
| 93 | }); |
| 94 | |
| 95 | it('hashes a packfile larger than the 2 GiB Buffer ceiling', |
| 96 | store.readIndex.mockResolvedValue |
| 97 | store.casIndex.mockResolvedValue |
| 98 | |
| 99 | const chunk = |
| 100 | const chunks = |
| 101 | const huge: |
| 102 | size: chunk.length |
| 103 | open: () => |
| 104 | Readable.from( |
| 105 | (function* () { |
| 106 | for (let i |
| 107 | })(), |
| 108 | ), |
| 109 | }; |
| 110 | |
| 111 | const result = |
| 112 | repoId: 'phantomknight287/ghost', |
| 113 | transitions: [transition(0 |
| 114 | body: huge, |
| 115 | packOffset: 0, |
| 116 | }); |
| 117 | |
| 118 | expect(result.seq |
| 119 | expect(store.casIndex |
| 120 | }, 120_000); |
| 121 | |
| 122 | it('rejects a stale ref instead of clobbering it', |
| 123 | store.readIndex.mockResolvedValue |
| 124 | index: { |
| 125 | ...emptyIndex(), |
| 126 | seq: 3, |
| 127 | refs: new Map |
| 128 | }, |
| 129 | etag: '"three"', |
| 130 | }); |
| 131 | |
| 132 | await expect( |
| 133 | service.commitPush({ |
| 134 | repoId: 'phantomknight287/ghost', |
| 135 | transitions: [transition(0xaa |
| 136 | body, |
| 137 | packOffset: PACK_OFFSET, |
| 138 | }), |
| 139 | ).rejects.toBeInstanceOf( |
| 140 | expect(store.casIndex |
| 141 | expect(store.deleteEntry |
| 142 | 'phantomknight287/ghost', |
| 143 | store.putEntry.mock |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | it('removes its entry when the repository is deleted under it', |
| 148 | store.readIndex.mockRejectedValue |
| 149 | |
| 150 | await expect( |
| 151 | service.commitPush({ |
| 152 | repoId: 'phantomknight287/ghost', |
| 153 | transitions: [transition(0 |
| 154 | body, |
| 155 | packOffset: PACK_OFFSET, |
| 156 | }), |
| 157 | ).rejects.toBeInstanceOf( |
| 158 | expect(store.deleteEntry |
| 159 | 'phantomknight287/ghost', |
| 160 | store.putEntry.mock |
| 161 | ); |
| 162 | }); |
| 163 | |
| 164 | it('keeps its entry when the CAS fails without a verdict', |
| 165 | store.readIndex.mockResolvedValue |
| 166 | store.casIndex.mockRejectedValue |
| 167 | |
| 168 | await expect( |
| 169 | service.commitPush({ |
| 170 | repoId: 'phantomknight287/ghost', |
| 171 | transitions: [transition(0 |
| 172 | body, |
| 173 | packOffset: PACK_OFFSET, |
| 174 | }), |
| 175 | ).rejects.toThrow( |
| 176 | expect(store.deleteEntry |
| 177 | }); |
| 178 | |
| 179 | it('treats an already-recorded entry as committed', |
| 180 | store.readIndex.mockImplementation |
| 181 | index: { |
| 182 | ...emptyIndex(), |
| 183 | seq: 9, |
| 184 | refs: new Map |
| 185 | layers: [ |
| 186 | { |
| 187 | ulid: store.putEntry |
| 188 | packSha: Buffer.alloc |
| 189 | size: body.size |
| 190 | }, |
| 191 | ], |
| 192 | }, |
| 193 | etag: '"nine"', |
| 194 | })); |
| 195 | |
| 196 | const result = |
| 197 | repoId: 'phantomknight287/ghost', |
| 198 | transitions: [transition(0xaa |
| 199 | body, |
| 200 | packOffset: PACK_OFFSET, |
| 201 | }); |
| 202 | |
| 203 | expect(result.seq |
| 204 | expect(store.casIndex |
| 205 | }); |
| 206 | }); |