| 1 | import { describe, expect, it } from 'vitest'; |
| 2 | |
| 3 | import { InvalidReceivePackRequestError } |
| 4 | import { bufferBody } |
| 5 | import { |
| 6 | isProbeRequest, |
| 7 | parseReceivePackRequest, |
| 8 | readReceivePackHeader, |
| 9 | } from './receive-pack-request.js'; |
| 10 | |
| 11 | const ZERO = ' |
| 12 | const NEW = ' |
| 13 | |
| 14 | function pkt(payload: |
| 15 | const length = |
| 16 | return length + |
| 17 | } |
| 18 | |
| 19 | function body(lines: |
| 20 | return Buffer.concat |
| 21 | } |
| 22 | |
| 23 | describe('parseReceivePackRequest', () |
| 24 | it('parses a single ref update with capabilities', |
| 25 | const request = |
| 26 | body( |
| 27 | [pkt(`${ZERO |
| 28 | Buffer.from('PACKPAYLOAD |
| 29 | ), |
| 30 | ); |
| 31 | |
| 32 | expect(request.transitions |
| 33 | expect(request.transitions |
| 34 | expect(request.transitions |
| 35 | expect(request.transitions |
| 36 | expect(request.capabilities |
| 37 | expect(request.pack |
| 38 | }); |
| 39 | |
| 40 | it('parses a multi-ref atomic push', |
| 41 | const request = |
| 42 | body([ |
| 43 | pkt(`${ZERO} |
| 44 | pkt(`${ZERO} |
| 45 | ]), |
| 46 | ); |
| 47 | |
| 48 | expect(request.transitions |
| 49 | 'refs/heads/main', |
| 50 | 'refs/heads/dev', |
| 51 | ]); |
| 52 | }); |
| 53 | |
| 54 | it('accepts a delete-only push with no packfile', |
| 55 | const request = |
| 56 | body([pkt(`${ |
| 57 | ); |
| 58 | |
| 59 | expect(request.pack |
| 60 | expect(request.transitions |
| 61 | }); |
| 62 | |
| 63 | it('recognises the flush-only probe git sends before a chunked push', |
| 64 | expect(await isProbeRequest |
| 65 | }); |
| 66 | |
| 67 | it('does not mistake a real push for a probe', |
| 68 | const push = |
| 69 | expect(await isProbeRequest |
| 70 | expect(await isProbeRequest |
| 71 | expect(await isProbeRequest |
| 72 | false, |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | it('locates the packfile without reading it', |
| 77 | const raw = |
| 78 | [pkt(`${ZERO |
| 79 | Buffer.from('PACKPAYLOAD |
| 80 | ); |
| 81 | |
| 82 | const header = |
| 83 | |
| 84 | expect(header.transitions |
| 85 | expect(header.packSize |
| 86 | expect(raw.subarray |
| 87 | }); |
| 88 | |
| 89 | it('reads a command section larger than the first window', |
| 90 | const lines = |
| 91 | pkt(`${ZERO} |
| 92 | ); |
| 93 | const raw = |
| 94 | |
| 95 | const header = |
| 96 | |
| 97 | expect(header.transitions |
| 98 | expect(raw.subarray |
| 99 | }); |
| 100 | |
| 101 | it('rejects a body with no commands', |
| 102 | expect(() => |
| 103 | InvalidReceivePackRequestError, |
| 104 | ); |
| 105 | }); |
| 106 | |
| 107 | it('rejects a truncated pkt-line', |
| 108 | expect(() => |
| 109 | parseReceivePackRequest( |
| 110 | Buffer.from(`00ff |
| 111 | ), |
| 112 | ).toThrow(InvalidReceivePackRequestError) |
| 113 | }); |
| 114 | |
| 115 | it('rejects a payload that is not a packfile', |
| 116 | expect(() => |
| 117 | parseReceivePackRequest( |
| 118 | body( |
| 119 | [pkt(`${ZERO |
| 120 | Buffer.from('NOTAPACK |
| 121 | ), |
| 122 | ), |
| 123 | ).toThrow(InvalidReceivePackRequestError) |
| 124 | }); |
| 125 | }); |