travelTo(CarbonImmutable::parse('2026-11-26 14:00', 'Europe/Helsinki')); $this->user = User::factory()->create(); $this->member = User::factory()->create(); $this->job = CustomerJobModel::factory()->create(['status' => 'order', 'number' => '2026-0042', 'name' => 'Messuvideo']); }); function notificationFor(User $user, array $overrides = []): NotificationModel { static $sequence = 0; $sequence++; return NotificationModel::query()->create([ 'user_id' => $user->id, 'type' => 'task_starts_soon', 'params' => ['task_id' => 7, 'task_title' => 'Kuvauspäivä', 'job_id' => 3, 'job_number' => '2026-0042', 'job_name' => 'Messuvideo', 'starts_on' => '2026-11-30'], 'dedupe_key' => 'test:'.$sequence, 'created_at' => now(), ...$overrides, ]); } it('finds tasks starting in the period with their assignee and members, without done tasks and archived jobs', function () { $task = TaskModel::factory()->assignedTo([$this->user->id, $this->member->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä', 'planned_from' => '2026-11-27', 'planned_to' => '2026-11-27']); TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'status' => 'done', 'planned_from' => '2026-11-27']); TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => CustomerJobModel::factory()->create(['status' => 'archived'])->id, 'planned_from' => '2026-11-27']); TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'planned_from' => '2026-11-26']); $tasks = app(UpcomingTasks::class)->startingBetween('2026-11-26', '2026-11-27'); expect($tasks)->toHaveCount(1) ->and($tasks[0]->taskId)->toBe($task->id) ->and($tasks[0]->jobNumber)->toBe('2026-0042') ->and($tasks[0]->plannedFrom)->toBe('2026-11-27') ->and($tasks[0]->recipientIds)->toBe([$this->user->id, $this->member->id]); }); it('notifies with the daily command, scheduled at 14.00 Finnish time', function () { $task = TaskModel::factory()->assignedTo([$this->user->id, $this->member->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä', 'planned_from' => '2026-11-27']); $this->artisan('notifications:task-starts')->expectsOutputToContain('2 notifications added.')->assertSuccessful(); $this->artisan('notifications:task-starts')->expectsOutputToContain('0 notifications added.'); expect(NotificationModel::query()->where('user_id', $this->member->id)->sole()->params['task_id'])->toBe($task->id); $event = collect(app(Schedule::class)->events())->first(fn (Event $event): bool => str_contains((string) $event->command, 'notifications:task-starts')); expect($event)->not->toBeNull() ->and($event->expression)->toBe('0 14 * * *') ->and($event->timezone)->toBe('Europe/Helsinki'); }); it('shows the unseen count of the user\'s own notifications of the last 7 days', function () { notificationFor($this->user); notificationFor($this->user, ['created_at' => now()->subDays(8)]); notificationFor($this->member); $this->actingAs($this->user); Livewire::test('notifications::bell') ->assertSee('1 lukematon ilmoitus') ->assertSee('Tehtävä Kuvauspäivä (työ 2026-0042 Messuvideo) alkaa ma 30.11.2026.') ->assertSee(route('jobs.show', 3).'#task-7', false) ->assertSee('26.11.2026 klo 14.00'); }); it('marks the notifications seen when the panel is opened, keeping them marked new for now', function () { $notification = notificationFor($this->user); $this->actingAs($this->user); Livewire::test('notifications::bell') ->call('open') ->assertSee('Ei lukemattomia ilmoituksia') ->assertSet('newIds', [$notification->id]) ->assertSee('Uusi'); expect($notification->fresh()->seen_at)->not->toBeNull(); Livewire::test('notifications::bell')->assertDontSee('Uusi'); }); it('tells when there are no notifications, and in the user\'s language', function () { $this->actingAs($this->user); Livewire::test('notifications::bell')->assertSee('Ei ilmoituksia viimeiseltä 7 päivältä.'); notificationFor($this->member); $this->member->forceFill(['locale' => 'en'])->save(); $this->actingAs($this->member); app()->setLocale('en'); Livewire::test('notifications::bell') ->assertSee('Task Kuvauspäivä (job 2026-0042 Messuvideo) starts on Mon 30.11.2026.') ->assertSee('1 unread notification'); }); it('shows the bell in the header of signed-in pages', function () { $this->actingAs($this->user); $this->get(route('jobs.index')) ->assertOk() ->assertSee('data-notifications-bell', false); }); it('notifies people at once when someone else adds them to a task, not the one who added', function () { $marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole(); DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $marketing->id]); $this->actingAs($this->user); Livewire::test('jobs::task-form', ['job' => $this->job->id]) ->set('form.title', 'Mainoskuvat') ->set('form.work_type_id', (string) WorkTypeModel::query()->where('name', 'graafinen työ')->value('id')) ->set('form.member_user_ids', [(string) $this->member->id]) ->call('save') ->assertHasNoErrors(); expect(NotificationModel::query()->where('user_id', $this->user->id)->count())->toBe(0) ->and(NotificationModel::query()->where('user_id', $this->member->id)->sole()->type->value)->toBe('task_assigned'); $this->actingAs($this->member); Livewire::test('notifications::bell') ->assertSee('Sinut lisättiin tehtävän Mainoskuvat jäseneksi (työ 2026-0042 Messuvideo).') ->assertSee(route('jobs.show', $this->job->id).'#task-'.TaskModel::query()->sole()->id, false); }); it('notifies people at once about their new roles on a job', function () { $jobId = app(CreateCustomerJob::class)->execute(new CustomerJobData( name: 'Esite', customerId: CustomerModel::factory()->create()->id, serviceUnitIds: [ServiceUnitModel::query()->value('id')], responsibleUserId: $this->member->id, kind: 'one_off', status: 'order', priority: 'normal', memberUserIds: [$this->member->id, $this->user->id], ), $this->user->id); $this->actingAs($this->member); Livewire::test('notifications::bell') ->assertSee('Sinut lisättiin työn '.CustomerJobModel::query()->find($jobId)->number.' Esite vastuuhenkilöksi ja jäseneksi.') ->assertSee(route('jobs.show', $jobId), false); expect(NotificationModel::query()->where('user_id', $this->user->id)->count())->toBe(0); });