|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | 5 | use App\Actions\UpdateCommitHotScores; |
| 6 | +use App\Enums\Feature; |
6 | 7 | use App\Enums\LivePlatform; |
7 | 8 | use App\Enums\Log; |
8 | 9 | use App\Jobs\DetectCommitLanguage; |
9 | 10 | use App\Models\Commit; |
10 | 11 | use App\Models\CommitImage; |
| 12 | +use App\Models\Guild; |
11 | 13 | use App\Models\LinkPreview; |
12 | 14 | use App\Models\User; |
13 | 15 | use App\Services\CommitBodies; |
| 16 | +use App\Services\Features; |
14 | 17 | use Illuminate\Database\Eloquent\Factories\Sequence; |
15 | 18 | use Illuminate\Support\Facades\DB; |
16 | 19 | use Illuminate\Support\Facades\Queue; |
|
409 | 412 | it('rejects requests that are not signed', function (): void { |
410 | 413 | $this->getJson(route('api-log.show', 'latest'))->assertUnauthorized(); |
411 | 414 | }); |
| 415 | + |
| 416 | +it('says which joined guild brought a commit into the following log, as the web does', function (): void { |
| 417 | + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); |
| 418 | + $rust = Guild::factory()->create(['handle' => 'rust']); |
| 419 | + $rust->members()->attach($this->user, ['joined_at' => now()]); |
| 420 | + $this->user->following()->attach($followed = User::factory()->create()); |
| 421 | + |
| 422 | + $board = Commit::factory()->for($rust)->create(['created_at' => now()->subMinutes(3)]); |
| 423 | + $hashtag = Commit::factory()->create(['body' => 'Learning #RuSt', 'created_at' => now()->subMinutes(2)]); |
| 424 | + $theirs = Commit::factory()->for($followed)->create(['body' => 'Shipping #rust', 'created_at' => now()->subMinute()]); |
| 425 | + $mine = Commit::factory()->for($this->user)->create(['body' => 'My #rust']); |
| 426 | + |
| 427 | + $data = collect(signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk()->json('data'))->keyBy('id'); |
| 428 | + |
| 429 | + expect($data->keys()->all())->toBe([$mine->id, $theirs->id, $hashtag->id, $board->id]) |
| 430 | + ->and($data[$board->id]['joined_guild'])->toBe('rust') |
| 431 | + ->and($data[$hashtag->id]['joined_guild'])->toBe('rust') |
| 432 | + ->and($data[$theirs->id])->toHaveKey('joined_guild', null) |
| 433 | + ->and($data[$mine->id])->toHaveKey('joined_guild', null); |
| 434 | +}); |
| 435 | + |
| 436 | +it('leaves a muted guild out of the following log, naming no guild on what is left', function (): void { |
| 437 | + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); |
| 438 | + $rust = Guild::factory()->create(['handle' => 'rust']); |
| 439 | + $rust->members()->attach($this->user, ['joined_at' => now(), 'muted_at' => now()]); |
| 440 | + Commit::factory()->for($rust)->create(); |
| 441 | + Commit::factory()->create(['body' => 'Learning #rust']); |
| 442 | + $mine = Commit::factory()->for($this->user)->create(['created_at' => now()->subMinute()]); |
| 443 | + |
| 444 | + $response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following')) |
| 445 | + ->assertOk() |
| 446 | + ->assertJsonPath('data.*.id', [$mine->id]); |
| 447 | + |
| 448 | + expect($response->json('data.0'))->toHaveKey('joined_guild', null); |
| 449 | +}); |
| 450 | + |
| 451 | +it('names no joined guild on the trending and latest logs', function (string $log): void { |
| 452 | + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); |
| 453 | + Guild::factory()->create(['handle' => 'rust'])->members()->attach($this->user, ['joined_at' => now()]); |
| 454 | + Commit::factory()->create(['body' => 'Learning #rust']); |
| 455 | + |
| 456 | + $response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', $log))->assertOk()->assertJsonCount(1, 'data'); |
| 457 | + |
| 458 | + expect($response->json('data.0'))->not->toHaveKey('joined_guild'); |
| 459 | +})->with(['trending', 'latest']); |
| 460 | + |
| 461 | +it('names no joined guild once guilds are off', function (): void { |
| 462 | + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); |
| 463 | + $rust = Guild::factory()->create(['handle' => 'rust']); |
| 464 | + $rust->members()->attach($this->user, ['joined_at' => now()]); |
| 465 | + $this->user->following()->attach($followed = User::factory()->create()); |
| 466 | + $theirs = Commit::factory()->for($followed)->create(['body' => 'Shipping #rust', 'created_at' => now()->subMinute()]); |
| 467 | + $hashtag = Commit::factory()->create(['body' => 'Learning #rust']); |
| 468 | + |
| 469 | + signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following')) |
| 470 | + ->assertOk() |
| 471 | + ->assertJsonPath('data.*.id', [$hashtag->id, $theirs->id]) |
| 472 | + ->assertJsonPath('data.0.joined_guild', 'rust'); |
| 473 | + |
| 474 | + resolve(Features::class)->update(Feature::Guilds, false); |
| 475 | + |
| 476 | + $response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following')) |
| 477 | + ->assertOk() |
| 478 | + ->assertJsonPath('data.*.id', [$theirs->id]); |
| 479 | + |
| 480 | + expect($response->json('data.0'))->toHaveKey('joined_guild', null); |
| 481 | +}); |
| 482 | + |
| 483 | +it('does not query each commit or guild on the following log', function (): void { |
| 484 | + Queue::fake([DetectCommitLanguage::class]); |
| 485 | + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); |
| 486 | + $rust = Guild::factory()->create(['handle' => 'rust']); |
| 487 | + $rust->members()->attach($this->user, ['joined_at' => now()]); |
| 488 | + signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk(); |
| 489 | + |
| 490 | + $queries = function (): int { |
| 491 | + DB::flushQueryLog(); |
| 492 | + DB::enableQueryLog(); |
| 493 | + |
| 494 | + signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk(); |
| 495 | + |
| 496 | + return count(DB::getQueryLog()); |
| 497 | + }; |
| 498 | + |
| 499 | + Commit::factory()->for($rust)->create(); |
| 500 | + Commit::factory()->create(['body' => 'Learning #rust']); |
| 501 | + $few = $queries(); |
| 502 | + |
| 503 | + Commit::factory()->for($rust)->count(4)->create(); |
| 504 | + Commit::factory()->count(4)->create(['body' => 'Learning #rust']); |
| 505 | + |
| 506 | + expect($queries())->toBe($few); |
| 507 | +}); |
0 commit comments