|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use App\Jobs\DetectCommitLanguage; |
| 6 | +use App\Models\Commit; |
| 7 | +use App\Models\User; |
| 8 | +use App\Queries\CommitContinuationQuery; |
| 9 | +use Illuminate\Pagination\Cursor; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Illuminate\Support\Facades\Queue; |
| 12 | + |
| 13 | +beforeEach(function (): void { |
| 14 | + $this->user = User::factory()->create(); |
| 15 | + $this->keyPair = apiKeyPairFor($this->user); |
| 16 | +}); |
| 17 | + |
| 18 | +it('lists the next commits of the author thread as the API shows commits', function (): void { |
| 19 | + $author = User::factory()->create(); |
| 20 | + $commit = Commit::factory()->for($author)->create(['created_at' => now()->subMinutes(5)]); |
| 21 | + $first = Commit::factory()->for($author)->for($commit, 'parent')->create(['created_at' => now()->subMinutes(4)]); |
| 22 | + $second = Commit::factory()->for($author)->for($first, 'parent')->create(['created_at' => now()->subMinutes(3)]); |
| 23 | + Commit::factory()->for($author)->for($commit, 'parent')->create(['created_at' => now()->subMinutes(2)]); |
| 24 | + Commit::factory()->for($first, 'parent')->create(['created_at' => now()->subMinute()]); |
| 25 | + |
| 26 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit)) |
| 27 | + ->assertOk() |
| 28 | + ->assertJsonPath('data.*.id', [$first->id, $second->id]) |
| 29 | + ->assertJsonPath('data.0.author.handle', $author->handle) |
| 30 | + ->assertJsonPath('data.0.continuation_id', $second->id) |
| 31 | + ->assertJsonPath('data.0.replies_count', 1) |
| 32 | + ->assertJsonPath('data.1.continuation_id', null) |
| 33 | + ->assertJsonPath('meta.position', 1) |
| 34 | + ->assertJsonPath('meta.length', 2) |
| 35 | + ->assertJsonPath('meta.is_capped', false) |
| 36 | + ->assertJsonPath('meta.next_cursor', null) |
| 37 | + ->assertJsonPath('links.next', null); |
| 38 | +}); |
| 39 | + |
| 40 | +it('lists the thread 10 commits at a time with a cursor to the next page', function (): void { |
| 41 | + $author = User::factory()->create(); |
| 42 | + $commits = [Commit::factory()->for($author)->create()]; |
| 43 | + |
| 44 | + foreach (range(1, CommitContinuationQuery::PER_PAGE + 2) as $ignored) { |
| 45 | + $commits[] = Commit::factory()->for($author)->for(end($commits), 'parent')->create(); |
| 46 | + } |
| 47 | + |
| 48 | + $first = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commits[0])); |
| 49 | + |
| 50 | + $first->assertOk() |
| 51 | + ->assertJsonPath('data.*.id', array_map(fn (Commit $commit): string => $commit->id, array_slice($commits, 1, 10))) |
| 52 | + ->assertJsonPath('data.9.continuation_id', $commits[11]->id) |
| 53 | + ->assertJsonPath('meta.position', 1) |
| 54 | + ->assertJsonPath('meta.length', 12); |
| 55 | + |
| 56 | + $second = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commits[0], 'cursor' => $first->json('meta.next_cursor')])); |
| 57 | + |
| 58 | + $second->assertOk() |
| 59 | + ->assertJsonPath('data.*.id', [$commits[11]->id, $commits[12]->id]) |
| 60 | + ->assertJsonPath('meta.position', 11) |
| 61 | + ->assertJsonPath('meta.length', 2) |
| 62 | + ->assertJsonPath('meta.next_cursor', null); |
| 63 | + |
| 64 | + signedApiRequest($this->keyPair, 'GET', (string) $first->json('links.next')) |
| 65 | + ->assertOk() |
| 66 | + ->assertJsonPath('data.*.id', [$commits[11]->id, $commits[12]->id]); |
| 67 | +}); |
| 68 | + |
| 69 | +it('does not query each commit of the thread, its author or its stars', function (): void { |
| 70 | + Queue::fake([DetectCommitLanguage::class]); |
| 71 | + $author = User::factory()->create(); |
| 72 | + $commits = [Commit::factory()->for($author)->create()]; |
| 73 | + |
| 74 | + $queries = function () use ($commits): int { |
| 75 | + DB::flushQueryLog(); |
| 76 | + DB::enableQueryLog(); |
| 77 | + |
| 78 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commits[0]))->assertOk(); |
| 79 | + |
| 80 | + return count(DB::getQueryLog()); |
| 81 | + }; |
| 82 | + |
| 83 | + $create = function (int $count) use (&$commits, $author): void { |
| 84 | + foreach (range(1, $count) as $ignored) { |
| 85 | + $commits[] = $commit = Commit::factory()->for($author)->for(end($commits), 'parent')->create(); |
| 86 | + $commit->stars()->attach([$this->user->id, User::factory()->create()->id]); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + $create(2); |
| 91 | + |
| 92 | + // The first signed request also syncs the user's keys with GitHub, and the first cards load the features... |
| 93 | + $queries(); |
| 94 | + |
| 95 | + $few = $queries(); |
| 96 | + |
| 97 | + $create(8); |
| 98 | + |
| 99 | + expect($queries())->toBe($few); |
| 100 | +}); |
| 101 | + |
| 102 | +it('lists the first page for a cursor that is not one', function (mixed $cursor): void { |
| 103 | + $commit = Commit::factory()->create(); |
| 104 | + $next = Commit::factory()->for($commit->user)->for($commit, 'parent')->create(); |
| 105 | + |
| 106 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commit, 'cursor' => $cursor])) |
| 107 | + ->assertOk() |
| 108 | + ->assertJsonPath('data.*.id', [$next->id]); |
| 109 | +})->with([ |
| 110 | + 'malformed' => ['not-a-cursor'], |
| 111 | + 'array' => [['not-a-cursor']], |
| 112 | +]); |
| 113 | + |
| 114 | +it('returns not found for a cursor on something that is not a commit', function (): void { |
| 115 | + $commit = Commit::factory()->create(); |
| 116 | + |
| 117 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commit, 'cursor' => new Cursor(['id' => 'not-a-uuid'])->encode()])) |
| 118 | + ->assertNotFound(); |
| 119 | +}); |
| 120 | + |
| 121 | +it('stops the thread at the first commit the user may not see', function (): void { |
| 122 | + $author = User::factory()->create(); |
| 123 | + $commit = Commit::factory()->for($author)->create(); |
| 124 | + $visible = Commit::factory()->for($author)->for($commit, 'parent')->create(); |
| 125 | + $pending = Commit::factory()->for($author)->for($visible, 'parent')->pending()->create(); |
| 126 | + Commit::factory()->for($author)->for($pending, 'parent')->create(); |
| 127 | + |
| 128 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit)) |
| 129 | + ->assertOk() |
| 130 | + ->assertJsonPath('data.*.id', [$visible->id]) |
| 131 | + ->assertJsonPath('data.0.continuation_id', null) |
| 132 | + ->assertJsonPath('meta.length', 1); |
| 133 | +}); |
| 134 | + |
| 135 | +it('returns not found for the thread of a commit the user may not see', function (): void { |
| 136 | + $commit = Commit::factory()->pending()->create(); |
| 137 | + |
| 138 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))->assertNotFound(); |
| 139 | +}); |
| 140 | + |
| 141 | +it('returns not found for the thread of a commit across a block', function (): void { |
| 142 | + $commit = Commit::factory()->create(); |
| 143 | + Commit::factory()->for($commit->user)->for($commit, 'parent')->create(); |
| 144 | + $this->user->blocking()->attach($commit->user); |
| 145 | + |
| 146 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))->assertNotFound(); |
| 147 | +}); |
| 148 | + |
| 149 | +it('returns not found for a cursor on a commit the user may not see', function (): void { |
| 150 | + $commit = Commit::factory()->create(); |
| 151 | + $hidden = Commit::factory()->pending()->create(); |
| 152 | + |
| 153 | + signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commit, 'cursor' => new Cursor(['id' => $hidden->id])->encode()])) |
| 154 | + ->assertNotFound(); |
| 155 | +}); |
| 156 | + |
| 157 | +it('rejects requests that are not signed', function (): void { |
| 158 | + $this->getJson(route('api-commit-continuation.index', Commit::factory()->create()))->assertUnauthorized(); |
| 159 | +}); |
0 commit comments