Files
Toiminnanohjaus/tests/Feature/Modules/Machines/ConsoleTest.php
petriandClaude Sonnet 5.5 6692396a63 Fix console hours, sign-in landing and counter usage races
- Hours the counter reading cannot parse (",5") no longer fail after
  they are saved; they just get no hour counter suggestion.
- The console's sign-in falls back to a console machine list
  (machines::console-picker at /console/machines) instead of /machines,
  so it stays in the console's own session.
- Recording counter usage locks the counter first, so usage saved at
  the same time from two devices is not lost.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 10:05:02 +02:00

219 lines
9.9 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\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterReadingModel;
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 Illuminate\Support\Facades\Hash;
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->actingAs($this->employee, 'console');
$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(), 'console');
$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 signing out of the console to sign in with a password', function () {
$this->actingAs($this->employee, 'console');
$this->post(route('console.logout'), ['return_to' => route('machines.console', $this->machine->id)])->assertRedirect(route('console.login'));
expect(session('url.intended'))->toBe(route('machines.console', $this->machine->id));
$this->actingAs($this->employee, 'console');
$this->post(route('console.logout'), ['return_to' => 'https://example.com/phish']);
expect(session('url.intended'))->toBeNull();
});
it('suggests the hours booked as the usage of the machine\'s hour counter and leaves out counters and maintenance when there are none', function () {
console($this)->assertDontSee(__('machines.console.counters_and_maintenance'));
$hours = MachineCounterModel::query()->create(['machine_id' => $this->machine->id, 'name' => 'Käyttötunnit', 'unit' => 'hour', 'sort_order' => 1]);
console($this)
->assertSee(__('machines.console.counters_and_maintenance'))
->call('selectRow', MachineQueueEntryModel::query()->sole()->id)
->set('hours', '1,5')
->call('saveHours')
->assertSet('usage.'.$hours->id, '1,5')
->assertSet('message', fn (string $message) => str_contains($message, 'Ehdotus: kirjaa 1,5 h laskuriin Käyttötunnit.'))
->call('saveUsage', $hours->id)
->assertHasNoErrors();
expect(MachineCounterReadingModel::query()->sole()->reading_milli)->toBe(1_500);
});
it('saves hours written as ",5" without a counter suggestion it cannot read', function () {
$hours = MachineCounterModel::query()->create(['machine_id' => $this->machine->id, 'name' => 'Käyttötunnit', 'unit' => 'hour', 'sort_order' => 1]);
console($this)
->call('selectRow', MachineQueueEntryModel::query()->sole()->id)
->set('hours', ',5')
->call('saveHours')
->assertHasNoErrors()
->assertSet('hours', '')
->assertSet('usage', []);
expect(TimeEntryModel::query()->sole()->hours)->toEqual(0.5)
->and(MachineCounterReadingModel::query()->where('machine_counter_id', $hours->id)->exists())->toBeFalse();
});
it('changes the console\'s user with a PIN code', function () {
$console = route('machines.console', $this->machine->id);
$other = User::factory()->employee()->create(['name' => 'Olli Operaattori', 'pin_hash' => Hash::make('2468')]);
User::factory()->employee()->create(['name' => 'Ei PINiä']);
Livewire::test('users::pin-switch', ['returnTo' => $console])
->assertSee('Olli Operaattori')
->assertDontSee('Ei PINiä')
->set('userId', (string) $other->id)
->set('pin', '1111')
->call('switchUser')
->assertHasErrors('pin')
->set('pin', '2468')
->call('switchUser')
->assertRedirect($console);
expect(auth()->id())->toBe($other->id);
});
it('locks a user\'s PIN code after five wrong attempts', function () {
$other = User::factory()->employee()->create(['pin_hash' => Hash::make('2468')]);
$component = Livewire::test('users::pin-switch', ['returnTo' => route('machines.console', $this->machine->id)])->set('userId', (string) $other->id);
foreach (range(1, 5) as $attempt) {
$component->set('pin', '0000')->call('switchUser');
}
$component->set('pin', '2468')->call('switchUser')
->assertHasErrors('pin')
->assertSee('Liian monta väärää yritystä');
expect(auth()->id())->toBe($this->employee->id);
});
it('does not let a deactivated user in with a PIN code or return to another site', function () {
$other = User::factory()->employee()->inactive()->create(['pin_hash' => Hash::make('2468')]);
Livewire::test('users::pin-switch', ['returnTo' => route('machines.console', $this->machine->id)])
->assertDontSee($other->name)
->set('userId', (string) $other->id)
->set('pin', '2468')
->call('switchUser')
->assertHasErrors('userId');
Livewire::test('users::pin-switch', ['returnTo' => 'https://example.com/phish'])->assertNotFound();
});