|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use App\Enums\ModerationReason; |
| 6 | +use App\Models\Commit; |
| 7 | +use App\Models\ScheduledCommit; |
| 8 | +use App\Models\User; |
| 9 | +use App\Models\UserTimeout; |
| 10 | +use App\Services\CommitBodies; |
| 11 | + |
| 12 | +beforeEach(function (): void { |
| 13 | + $this->travelTo(now()->setDate(2026, 10, 15)->setTime(12, 0)); |
| 14 | + $this->user = User::factory()->create(); |
| 15 | + $this->keyPair = apiKeyPairFor($this->user); |
| 16 | +}); |
| 17 | + |
| 18 | +it('schedules a commit in UTC, without committing it, and answers with it', function (): void { |
| 19 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 20 | + 'body' => ' Shipped the parser benchmark. ', |
| 21 | + 'scheduled_at' => '2026-10-16T10:30:00+05:30', |
| 22 | + ]); |
| 23 | + |
| 24 | + $scheduled = ScheduledCommit::query()->sole(); |
| 25 | + |
| 26 | + $response->assertCreated() |
| 27 | + ->assertExactJson([ |
| 28 | + 'data' => [ |
| 29 | + 'id' => $scheduled->id, |
| 30 | + 'body' => 'Shipped the parser benchmark.', |
| 31 | + ...resolve(CommitBodies::class)->segments('Shipped the parser benchmark.'), |
| 32 | + 'poll_options' => null, |
| 33 | + 'image_count' => 0, |
| 34 | + 'scheduled_at' => '2026-10-16T05:00:00Z', |
| 35 | + 'submitted_at' => null, |
| 36 | + 'status' => 'scheduled', |
| 37 | + 'commit_id' => null, |
| 38 | + ], |
| 39 | + ]); |
| 40 | + |
| 41 | + expect($scheduled->user_id)->toBe($this->user->id) |
| 42 | + ->and($scheduled->scheduled_at->toIso8601String())->toBe('2026-10-16T05:00:00+00:00') |
| 43 | + ->and(Commit::query()->count())->toBe(0); |
| 44 | +}); |
| 45 | + |
| 46 | +it('submits it for review at the due time', function (): void { |
| 47 | + signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 48 | + 'body' => 'Shipped the parser benchmark.', |
| 49 | + 'scheduled_at' => '2026-10-15T13:00:00+00:00', |
| 50 | + ])->assertCreated(); |
| 51 | + |
| 52 | + $this->travelTo(now()->setTime(13, 0)); |
| 53 | + $this->artisan('commits:submit-scheduled')->assertSuccessful(); |
| 54 | + |
| 55 | + expect(Commit::query()->sole()) |
| 56 | + ->user_id->toBe($this->user->id) |
| 57 | + ->body->toBe('Shipped the parser benchmark.') |
| 58 | + ->and(ScheduledCommit::query()->sole()->submitted_at)->not->toBeNull(); |
| 59 | +}); |
| 60 | + |
| 61 | +it('splits a long body like a commit, with the excerpt a list shows', function (): void { |
| 62 | + $body = str_repeat('Shipped a long changelog entry. ', 40); |
| 63 | + |
| 64 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 65 | + 'body' => $body, |
| 66 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 67 | + ]); |
| 68 | + |
| 69 | + $segments = resolve(CommitBodies::class)->segments(mb_trim($body)); |
| 70 | + |
| 71 | + $response->assertCreated() |
| 72 | + ->assertJsonPath('data.body_segments', $segments['body_segments']) |
| 73 | + ->assertJsonPath('data.preview_segments', $segments['preview_segments']); |
| 74 | + |
| 75 | + expect($segments['preview_segments'])->not->toBeNull(); |
| 76 | +}); |
| 77 | + |
| 78 | +it('rejects invalid times and bodies, as the web does', function (array $payload, string $field): void { |
| 79 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 80 | + 'body' => 'Shipped a scheduling feature.', |
| 81 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 82 | + ...$payload, |
| 83 | + ]); |
| 84 | + |
| 85 | + $response->assertUnprocessable() |
| 86 | + ->assertJsonValidationErrors($field); |
| 87 | + |
| 88 | + expect(ScheduledCommit::query()->count())->toBe(0); |
| 89 | +})->with([ |
| 90 | + 'missing time' => [['scheduled_at' => null], 'scheduled_at'], |
| 91 | + 'missing offset' => [['scheduled_at' => '2026-10-16T12:00:00'], 'scheduled_at'], |
| 92 | + 'invalid date' => [['scheduled_at' => '2026-10-32T12:00:00+00:00'], 'scheduled_at'], |
| 93 | + 'now' => [['scheduled_at' => '2026-10-15T12:00:00+00:00'], 'scheduled_at'], |
| 94 | + 'past' => [['scheduled_at' => '2026-10-15T11:59:00+00:00'], 'scheduled_at'], |
| 95 | + 'beyond thirty days' => [['scheduled_at' => '2026-11-14T12:00:01+00:00'], 'scheduled_at'], |
| 96 | + 'missing body' => [['body' => null], 'body'], |
| 97 | + 'whitespace' => [['body' => ' '], 'body'], |
| 98 | + 'too long' => [['body' => str_repeat('a', 4001)], 'body'], |
| 99 | +]); |
| 100 | + |
| 101 | +it('accepts the end of the window and a unicode body', function (): void { |
| 102 | + signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 103 | + 'body' => str_repeat('é', 4000), |
| 104 | + 'scheduled_at' => '2026-11-14T12:00:00+00:00', |
| 105 | + ])->assertCreated(); |
| 106 | + |
| 107 | + expect(ScheduledCommit::query()->count())->toBe(1); |
| 108 | +}); |
| 109 | + |
| 110 | +it('does not schedule replies or quotes', function (string $field): void { |
| 111 | + $commit = Commit::factory()->create(); |
| 112 | + |
| 113 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 114 | + 'body' => 'This must not become a reply or a quote.', |
| 115 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 116 | + $field => $commit->id, |
| 117 | + ]); |
| 118 | + |
| 119 | + $response->assertUnprocessable() |
| 120 | + ->assertJsonValidationErrors($field); |
| 121 | + |
| 122 | + expect(ScheduledCommit::query()->count())->toBe(0); |
| 123 | +})->with(['parent_id', 'quoted_commit_id']); |
| 124 | + |
| 125 | +it('keeps to 10 scheduled commits waiting at once', function (): void { |
| 126 | + ScheduledCommit::factory()->for($this->user)->count(10)->create(); |
| 127 | + |
| 128 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 129 | + 'body' => 'One more scheduled commit.', |
| 130 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 131 | + ]); |
| 132 | + |
| 133 | + $response->assertUnprocessable() |
| 134 | + ->assertJsonValidationErrors(['scheduled_at' => 'You can have up to 10 scheduled commits. Cancel one before scheduling another.']); |
| 135 | + |
| 136 | + expect(ScheduledCommit::query()->count())->toBe(10); |
| 137 | +}); |
| 138 | + |
| 139 | +it('shares the limit of 10 commits a minute', function (): void { |
| 140 | + for ($i = 0; $i < 10; $i++) { |
| 141 | + signedApiRequest($this->keyPair, 'POST', route('api-commit.store'), ['body' => 'An ordinary commit.'])->assertCreated(); |
| 142 | + } |
| 143 | + |
| 144 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 145 | + 'body' => 'Must be throttled.', |
| 146 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 147 | + ]); |
| 148 | + |
| 149 | + $response->assertTooManyRequests() |
| 150 | + ->assertJsonPath('code', 'rate_limited') |
| 151 | + ->assertHeader('Retry-After'); |
| 152 | + |
| 153 | + expect(ScheduledCommit::query()->count())->toBe(0); |
| 154 | +}); |
| 155 | + |
| 156 | +it('does not schedule for a user in timeout', function (): void { |
| 157 | + UserTimeout::factory()->for($this->user)->create(['reason' => ModerationReason::Spam, 'ends_at' => now()->addDays(2)]); |
| 158 | + |
| 159 | + signedApiRequest($this->keyPair, 'POST', route('api-scheduled-commit.store'), [ |
| 160 | + 'body' => 'It was DNS.', |
| 161 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 162 | + ])->assertForbidden() |
| 163 | + ->assertJsonPath('code', 'timed_out'); |
| 164 | + |
| 165 | + expect(ScheduledCommit::query()->count())->toBe(0); |
| 166 | +}); |
| 167 | + |
| 168 | +it('leaves the first commit to the web, where the commit rules are read', function (): void { |
| 169 | + $user = User::factory()->firstTimePoster()->create(); |
| 170 | + |
| 171 | + signedApiRequest(apiKeyPairFor($user), 'POST', route('api-scheduled-commit.store'), [ |
| 172 | + 'body' => 'Hello, world.', |
| 173 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 174 | + ])->assertForbidden() |
| 175 | + ->assertJsonPath('code', 'commit_rules_required'); |
| 176 | + |
| 177 | + expect(ScheduledCommit::query()->count())->toBe(0); |
| 178 | +}); |
| 179 | + |
| 180 | +it('requires a signed request', function (): void { |
| 181 | + $this->postJson(route('api-scheduled-commit.store'), [ |
| 182 | + 'body' => 'Shipped the parser benchmark.', |
| 183 | + 'scheduled_at' => '2026-10-16T12:00:00+00:00', |
| 184 | + ])->assertUnauthorized(); |
| 185 | + |
| 186 | + expect(ScheduledCommit::query()->count())->toBe(0); |
| 187 | +}); |
0 commit comments