Files
Toiminnanohjaus/tests/Feature/Modules/Machines/ConsoleTest.php
T
2026-11-26 16:57:56 +02:00

133 lines
6.1 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\Machines\Application\UseCases\MachineWorkList;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now()->setTimezone(config('app.business_timezone'))->setTime(10, 0));
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
$this->actingAs($this->employee);
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
$print->update(['uses_stock' => true]);
$this->machine = MachineModel::factory()->inUnits([$print->id])->create(['name' => 'Suurkuvatulostin']);
$this->job = CustomerJobModel::factory()->create(['name' => 'Messut', 'status' => 'order', 'instructions' => 'Kiiltävä laminointi']);
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $print->id]);
$this->task = TaskModel::factory()->create([
'customer_job_id' => $this->job->id,
'work_type_id' => WorkTypeModel::query()->where('name', 'painotyö')->value('id'),
'title' => 'Banderolli',
]);
app(MachineWorkList::class)->planTask($this->task->id, $this->machine->id, null);
$papers = StockItemGroupModel::factory()->create(['name' => 'Paperit']);
$this->paper = StockItemModel::factory()->create(['stock_item_group_id' => $papers->id, 'name' => 'Bannerikangas', 'unit' => 'square_metre']);
});
function console(object $test): mixed
{
return Livewire::test('machines::console', ['machine' => $test->machine->id]);
}
it('opens without the main menu and shows today\'s work list', function () {
$this->get(route('machines.console', $this->machine->id))
->assertOk()
->assertSee('Suurkuvatulostin')
->assertSee('Banderolli')
->assertSee(__('machines.console.refresh'))
->assertDontSee(__('layout.main_menu'));
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('machines.console', $this->machine->id))->assertForbidden();
});
it('records material on the selected task, undoes it, and records hours', function () {
console($this)
->call('selectRow', MachineQueueEntryModel::query()->sole()->id)
->assertSee('Kiiltävä laminointi')
->call('chooseGroup', StockItemGroupModel::query()->value('id'))
->assertSee('Bannerikangas')
->call('selectItem', $this->paper->id)
->call('changeQuantity', 1)
->assertSet('quantity', '2')
->call('recordMaterial')
->assertHasNoErrors()
->assertSet('message', "Kirjattu Bannerikangas 2\u{00A0}m². Saldo nyt -2\u{00A0}m².")
->call('undoLast')
->assertSet('message', __('machines.console.material_undone'))
->set('hours', '1,5')
->call('saveHours')
->assertHasNoErrors();
$use = StockMovementModel::query()->where('kind', 'consumption')->sole();
expect($use->only(['task_id', 'machine_id', 'quantity_milli']))->toBe(['task_id' => $this->task->id, 'machine_id' => $this->machine->id, 'quantity_milli' => -2_000])
->and((float) TimeEntryModel::query()->sole()->hours)->toBe(1.5);
// Frequent items come first without a search.
console($this)->assertSee('Bannerikangas');
});
it('records material without a task on the machine, with an optional reason', function () {
console($this)
->set('search', 'banneri')
->call('selectItem', $this->paper->id)
->set('reason', 'Koeajo')
->call('recordMaterial')
->assertHasNoErrors();
expect(StockMovementModel::query()->sole()->only(['task_id', 'machine_id', 'reason']))
->toBe(['task_id' => null, 'machine_id' => $this->machine->id, 'reason' => 'Koeajo']);
});
it('finishes a task from the console, which marks it done', function () {
$entry = MachineQueueEntryModel::query()->sole();
console($this)
->call('setStatus', $entry->id, 'in_progress')
->assertSet('message', 'Banderolli: tila nyt Työn alla.')
->call('finish', $entry->id)
->assertSet('message', __('machines.console.finished', ['title' => 'Banderolli']))
->assertDontSee(__('machines.console.finish_named', ['title' => 'Banderolli']));
expect($this->task->fresh()->status->value)->toBe('done');
});
it('announces new rows when refreshed', function () {
$component = console($this);
$other = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id, 'title' => 'Juliste']);
app(MachineWorkList::class)->planTask($other->id, $this->machine->id, null);
$component->call('refresh')
->assertSet('refreshMessage', fn (string $message) => str_contains($message, '1 uusi rivi työlistalla.'))
->assertSee('Juliste')
->call('showDay', true)
->assertDontSee('Juliste');
});
it('returns to the console after changing user', function () {
$this->post(route('logout'), ['return_to' => route('machines.console', $this->machine->id)])->assertRedirect(route('login'));
expect(session('url.intended'))->toBe(route('machines.console', $this->machine->id));
$this->actingAs($this->employee);
$this->post(route('logout'), ['return_to' => 'https://example.com/phish']);
expect(session('url.intended'))->toBeNull();
});