| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { afterEach, beforeEach |
| 6 | |
| 7 | import { NonFastForwardError } |
| 8 | import { ZERO_OID } |
| 9 | import { PullRefsService } |
| 10 | |
| 11 | describe('PullRefsService', () |
| 12 | let service: |
| 13 | let gitDir: |
| 14 | let base: |
| 15 | let head: |
| 16 | const where = |
| 17 | const db = |
| 18 | |
| 19 | const git = |
| 20 | execFileSync('git', |
| 21 | encoding: 'utf8', |
| 22 | input: '', |
| 23 | env: { |
| 24 | ...process.env, |
| 25 | GIT_AUTHOR_NAME: 'a', |
| 26 | GIT_AUTHOR_EMAIL: 'a@example.com', |
| 27 | GIT_COMMITTER_NAME: 'a', |
| 28 | GIT_COMMITTER_EMAIL: 'a@example.com', |
| 29 | }, |
| 30 | }).trim(); |
| 31 | |
| 32 | beforeEach(() => |
| 33 | vi.clearAllMocks(); |
| 34 | service = new |
| 35 | db as never, |
| 36 | {} as never, |
| 37 | {} as never, |
| 38 | {} as never, |
| 39 | {} as never, |
| 40 | ); |
| 41 | gitDir = mkdtempSync( |
| 42 | execFileSync('git', |
| 43 | const tree = |
| 44 | const commit = |
| 45 | git( |
| 46 | 'commit-tree', |
| 47 | tree, |
| 48 | ...parents.flatMap( |
| 49 | '-m', |
| 50 | message, |
| 51 | ); |
| 52 | base = commit( |
| 53 | head = commit( |
| 54 | git('update-ref', |
| 55 | git('update-ref', |
| 56 | }); |
| 57 | |
| 58 | afterEach(() => |
| 59 | |
| 60 | const reconcile = |
| 61 | service.reconcileInBackground({ |
| 62 | pullRequestId: 'pr_1', |
| 63 | number: 1, |
| 64 | gitDir, |
| 65 | baseSha, |
| 66 | headSha, |
| 67 | }); |
| 68 | |
| 69 | it('leaves refs alone when they match the tips a reader resolved', |
| 70 | const sync = |
| 71 | .spyOn(service, |
| 72 | .mockImplementation(() => |
| 73 | reconcile(base, head |
| 74 | await new Promise |
| 75 | expect(sync).not |
| 76 | }); |
| 77 | |
| 78 | it('reconciles pending accounting even when refs already match', |
| 79 | where.mockResolvedValueOnce([{ |
| 80 | const sync = |
| 81 | .spyOn(service, |
| 82 | .mockImplementation(() => |
| 83 | reconcile(base, head |
| 84 | await vi.waitFor |
| 85 | }); |
| 86 | |
| 87 | it('syncs when the base moved past the test merge', |
| 88 | const sync = |
| 89 | .spyOn(service, |
| 90 | .mockImplementation(() => |
| 91 | reconcile(head, head |
| 92 | await vi.waitFor |
| 93 | }); |
| 94 | |
| 95 | it('syncs when the merge ref is missing', |
| 96 | git('update-ref', |
| 97 | const sync = |
| 98 | .spyOn(service, |
| 99 | .mockImplementation(() => |
| 100 | reconcile(base, head |
| 101 | await vi.waitFor |
| 102 | }); |
| 103 | |
| 104 | it('retries a sync that lost the race to a push, then gives up', |
| 105 | const write = |
| 106 | .spyOn(service as |
| 107 | .mockRejectedValue(new NonFastForwardError |
| 108 | |
| 109 | await expect(service |
| 110 | NonFastForwardError, |
| 111 | ); |
| 112 | expect(write).toHaveBeenCalledTimes |
| 113 | }); |
| 114 | |
| 115 | it('reruns once for every sync asked for while one is running', |
| 116 | let finish!: |
| 117 | const sync = |
| 118 | .spyOn(service, |
| 119 | .mockImplementationOnce( |
| 120 | () => new |
| 121 | ) |
| 122 | .mockResolvedValue(undefined); |
| 123 | |
| 124 | service.syncInBackground('pr_1 |
| 125 | service.syncInBackground('pr_1 |
| 126 | service.syncInBackground('pr_1 |
| 127 | finish(); |
| 128 | |
| 129 | await vi.waitFor |
| 130 | await new Promise |
| 131 | expect(sync).toHaveBeenCalledTimes |
| 132 | }); |
| 133 | |
| 134 | it('swallows a failed lookup, since the push it follows already landed', |
| 135 | where.mockRejectedValueOnce(new |
| 136 | const sync = |
| 137 | |
| 138 | await expect( |
| 139 | service.syncAfterPush({ |
| 140 | repositoryId: 'repo_1', |
| 141 | transitions: [ |
| 142 | { ref: 'refs/heads/main', |
| 143 | ], |
| 144 | }), |
| 145 | ).resolves.toBeUndefined() |
| 146 | expect(sync).not |
| 147 | }); |
| 148 | |
| 149 | it('looks nothing up for a push that moved no branch', |
| 150 | await service.syncAfterPush |
| 151 | repositoryId: 'repo_1', |
| 152 | transitions: [ |
| 153 | { ref: 'refs/tags/v1', |
| 154 | ], |
| 155 | }); |
| 156 | expect(where).not |
| 157 | }); |
| 158 | }); |