| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { promisify } |
| 6 | import type { INestApplication |
| 7 | import request from 'supertest |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { type Database, |
| 11 | import { and, eq |
| 12 | |
| 13 | import { DATABASE } |
| 14 | import { hasBackends, signUp |
| 15 | |
| 16 | type TimelineEvent |
| 17 | kind: string |
| 18 | event?: { |
| 19 | type: string |
| 20 | actorUsername: string |
| 21 | commitSha: string |
| 22 | beforeSha: string |
| 23 | commitMessage: string |
| 24 | commitAuthorName: string |
| 25 | }; |
| 26 | }; |
| 27 | |
| 28 | describe.skipIf(!hasBackends |
| 29 | let app: |
| 30 | let origin: |
| 31 | let owner: |
| 32 | let watcher: |
| 33 | let work: |
| 34 | const username = |
| 35 | const watcherName = |
| 36 | let repo: |
| 37 | |
| 38 | const api = |
| 39 | const remote = |
| 40 | `${origin.replace( |
| 41 | const git = |
| 42 | execFileSync( |
| 43 | 'git', |
| 44 | ['-c', 'user.name=Ada' |
| 45 | { cwd: work, |
| 46 | ).trim(); |
| 47 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 48 | const push = |
| 49 | promisify(execFile)('git |
| 50 | cwd: work, |
| 51 | env: { ...process |
| 52 | }); |
| 53 | const commit = |
| 54 | writeFileSync(path.join |
| 55 | git('add', |
| 56 | git('commit', |
| 57 | return git('rev-parse |
| 58 | }; |
| 59 | /** The commit and force-push events, oldest first. */ |
| 60 | const pushEvents = |
| 61 | const { body |
| 62 | .get(`/api/repositories/${username |
| 63 | .set('cookie', |
| 64 | .expect(200); |
| 65 | return (body.timeline |
| 66 | .filter( |
| 67 | (item) => |
| 68 | item.event?.type |
| 69 | item.event?.type |
| 70 | ) |
| 71 | .map(({ event |
| 72 | event!.type === |
| 73 | ? `${event!. |
| 74 | : `forced ${event! |
| 75 | ); |
| 76 | }; |
| 77 | |
| 78 | beforeAll(async () |
| 79 | work = mkdtempSync( |
| 80 | ({ app, origin |
| 81 | owner = await |
| 82 | watcher = await |
| 83 | repo = ( |
| 84 | await api() |
| 85 | .post('/api/repositories') |
| 86 | .set('cookie', |
| 87 | .send({ name: |
| 88 | .expect(201) |
| 89 | ).body.slug; |
| 90 | |
| 91 | git('init', |
| 92 | commit('main.txt', |
| 93 | await push('main |
| 94 | }, 60_000); |
| 95 | |
| 96 | afterAll(async () |
| 97 | await app?.close |
| 98 | if (work) rmSync |
| 99 | }); |
| 100 | |
| 101 | it('lists the commits a request opens with, then each push, leaving out base commits merged in, and marks a force push', |
| 102 | git('checkout', |
| 103 | const a = |
| 104 | await push('feature |
| 105 | await api() |
| 106 | .post(`/api/repositories/${username |
| 107 | .set('cookie', |
| 108 | .send({ title: |
| 109 | .expect(201); |
| 110 | expect(await pushEvents |
| 111 | // a participant, who would hear about a comment, must not hear about pushes |
| 112 | await api() |
| 113 | .post(`/api/repositories/${username |
| 114 | .set('cookie', |
| 115 | .send({ body: |
| 116 | .expect(201); |
| 117 | |
| 118 | const b = |
| 119 | await push('feature |
| 120 | await vi.waitFor |
| 121 | async () => |
| 122 | expect(await pushEvents |
| 123 | `add a ${a} by Ada` |
| 124 | `add b ${b} by Ada` |
| 125 | ]), |
| 126 | { timeout: 15_000, interval |
| 127 | ); |
| 128 | |
| 129 | git('checkout', |
| 130 | commit('main2.txt', |
| 131 | await push('main |
| 132 | git('checkout', |
| 133 | git('merge', |
| 134 | const merge = |
| 135 | await push('feature |
| 136 | await vi.waitFor |
| 137 | async () => |
| 138 | expect((await pushEvents |
| 139 | `Merge branch 'main' into feature ${merge} by Ada` |
| 140 | ]), |
| 141 | { timeout: 15_000, interval |
| 142 | ); |
| 143 | |
| 144 | git('reset', |
| 145 | git('commit', |
| 146 | const amended = |
| 147 | await push('--force |
| 148 | await vi.waitFor |
| 149 | async () => |
| 150 | expect((await pushEvents |
| 151 | `forced ${merge}..${ |
| 152 | `add b, reworded ${amended} by Ada` |
| 153 | ]), |
| 154 | { timeout: 15_000, interval |
| 155 | ); |
| 156 | |
| 157 | const db = |
| 158 | const { id: |
| 159 | await api() |
| 160 | .get(`/api/repositories/${username |
| 161 | .set('cookie', |
| 162 | .expect(200) |
| 163 | ).body; |
| 164 | // opening the request is `issue.opened`, not a synchronize |
| 165 | await vi.waitFor |
| 166 | async () => |
| 167 | const events = |
| 168 | .select({ |
| 169 | payload: schema.outboxEvent |
| 170 | processedAt: schema.outboxEvent |
| 171 | }) |
| 172 | .from(schema.outboxEvent |
| 173 | .where( |
| 174 | and( |
| 175 | eq(schema.outboxEvent |
| 176 | eq(schema.outboxEvent |
| 177 | ), |
| 178 | ) |
| 179 | .orderBy(schema.outboxEvent |
| 180 | expect( |
| 181 | events.map(({ |
| 182 | const { after, |
| 183 | after: string; |
| 184 | forced: boolean; |
| 185 | commits: { |
| 186 | }; |
| 187 | return [after, |
| 188 | }), |
| 189 | ).toEqual([ |
| 190 | [b, false, |
| 191 | [merge, false, |
| 192 | [amended, true, |
| 193 | ]); |
| 194 | expect(events.every |
| 195 | }, |
| 196 | { timeout: 15_000, interval |
| 197 | ); |
| 198 | |
| 199 | const [watcherRow] |
| 200 | .select({ id: |
| 201 | .from(schema.user |
| 202 | .where(eq(schema |
| 203 | expect( |
| 204 | await db |
| 205 | .select() |
| 206 | .from(schema.notification |
| 207 | .where(eq(schema |
| 208 | ).toEqual([]); |
| 209 | }, 90_000); |
| 210 | }); |