festbench-web/tests/Feature/BenchmarkRunTest.php

72 lines
2.7 KiB
PHP

<?php
use App\Models\BenchmarkRun;
use App\Models\RunResult;
use App\Models\User;
test('guests see only public runs', function () {
$publicRun = BenchmarkRun::factory()->create(['visibility' => 'public']);
BenchmarkRun::factory()->create(['visibility' => 'private']);
$visible = BenchmarkRun::visibleTo(null)->get();
expect($visible->pluck('id'))->toContain($publicRun->id)
->and($visible)->toHaveCount(1);
});
test('authenticated users see public runs plus their own private runs', function () {
$me = User::factory()->create();
$other = User::factory()->create();
$publicRun = BenchmarkRun::factory()->create(['visibility' => 'public']);
$myPrivate = BenchmarkRun::factory()->create(['visibility' => 'private', 'user_id' => $me->id]);
BenchmarkRun::factory()->create(['visibility' => 'private', 'user_id' => $other->id]);
$visible = BenchmarkRun::visibleTo($me)->get();
expect($visible->pluck('id'))->toContain($publicRun->id)
->and($visible->pluck('id'))->toContain($myPrivate->id)
->and($visible)->toHaveCount(2);
});
test('submitted metrics are immutable through fillable updates', function () {
$run = BenchmarkRun::factory()->create(['model_id' => 'original/model', 'visibility' => 'public']);
// The only update path any user-facing code may use:
$run->fill(['notes' => 'my notes', 'visibility' => 'private', 'model_id' => 'hacked/model'])
->save();
expect($run->notes)->toBe('my notes')
->and($run->visibility)->toBe('private')
->and($run->fresh()->model_id)->toBe('original/model')
// fill() must have discarded the immutable field, not persisted it
->and($run->getOriginal('model_id'))->toBe('original/model');
});
test('overlay helper reports ownership correctly', function () {
$owner = User::factory()->create();
$run = BenchmarkRun::factory()->create(['user_id' => $owner->id]);
expect($run->isOwnedBy($owner))->toBeTrue()
->and($run->isOwnedBy(User::factory()->create()))->toBeFalse()
->and($run->isOwnedBy(null))->toBeFalse();
});
test('run results belong to a run and are deleted with it', function () {
$run = BenchmarkRun::factory()->create();
$result = RunResult::factory()->create(['benchmark_run_id' => $run->id]);
expect($result->run->id)->toBe($run->id);
$run->delete();
expect(RunResult::where('benchmark_run_id', $run->id)->exists())->toBeFalse();
});
test('run results cast their payload to array', function () {
$result = RunResult::factory()->create(['payload' => ['finish' => 'stop', 'ok' => true]]);
expect($result->fresh()->payload)->toBeArray()
->and($result->fresh()->payload['finish'])->toBe('stop')
->and($result->fresh()->ok)->toBeBool();
});