Files
petriandClaude Opus 5.5 9eac4c4d4e Remember for each user which Kanban cards have their task list open
The open cards are kept in the user's board preferences (expanded_cards,
at most 500), saved without redrawing the board and restored on return.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 11:25:58 +02:00

264 lines
14 KiB
PHP

<?php
use App\Modules\Jobs\Application\DTOs\BoardCard;
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\DTOs\TaskData;
use App\Modules\Jobs\Application\Services\BoardLayout;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Application\Services\TaskPlanner;
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
use App\Modules\Jobs\Application\UseCases\AddTask;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\MoveBoardCard;
use App\Modules\Jobs\Application\UseCases\PinTaskToBoard;
use App\Modules\Jobs\Domain\Enums\BoardColumn;
use App\Modules\Jobs\Domain\Enums\BoardView;
use App\Modules\Jobs\Domain\Enums\ColumnSort;
use App\Modules\Jobs\Domain\Enums\JobKind;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Enums\UrgentThreshold;
use App\Modules\Jobs\Domain\Events\TaskCompleted;
use App\Modules\Jobs\Domain\Exceptions\CardCannotMoveThere;
use App\Modules\Jobs\Domain\Exceptions\TaskCannotBePinned;
use App\Modules\Jobs\Domain\ValueObjects\BoardPreferences;
use App\Modules\Shared\Application\UseCases\DailyCapacity;
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
use App\Modules\Shared\Domain\ValueObjects\HexColor;
use Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeEventDispatcher;
use Tests\Fakes\FakeSettings;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\InMemoryBoardRepository;
use Tests\Fakes\InMemoryCustomerJobRepository;
use Tests\Fakes\InMemoryHolidayOverrides;
use Tests\Fakes\InMemoryTaskBookingRepository;
use Tests\Fakes\InMemoryTaskRepository;
function boardCard(int $jobId, JobStatus $status, Priority $priority = Priority::Normal, ?string $dueOn = null, int $position = 0, ?int $taskId = null, ?TaskStatus $taskStatus = null): BoardCard
{
return new BoardCard(
jobId: $jobId, taskId: $taskId, number: '2026-'.$jobId, title: 'Työ '.$jobId, jobName: 'Työ '.$jobId,
customerId: 1, customerName: 'Mainos Oy', customerHeaderColor: null, jobStatus: $status, taskStatus: $taskStatus,
priority: $priority, kind: JobKind::OneOff, dueOn: $dueOn, isLate: false, responsibleName: 'Eero Esimerkki',
serviceUnitName: null, estimatedHours: null, loggedQuarters: 0, boardPosition: $position, billerUserId: null,
);
}
/**
* @param array<string, list<BoardCard>> $columns
* @return array<string, list<string>>
*/
function boardKeys(array $columns): array
{
return array_map(fn (array $cards): array => array_map(fn (BoardCard $card): string => $card->key(), $cards), $columns);
}
describe('board layout', function () {
it('gathers the quote statuses in the daily view and keeps them apart in the sales view', function () {
$cards = [boardCard(1, JobStatus::Prospect), boardCard(2, JobStatus::Tentative), boardCard(3, JobStatus::QuoteRequest), boardCard(4, JobStatus::Order)];
$layout = new BoardLayout;
expect(boardKeys($layout->arrange($cards, BoardView::Production, BoardPreferences::defaults())))
->toMatchArray(['quotes' => ['job-1', 'job-2', 'job-3'], 'order' => ['job-4']])
->and(array_keys($layout->arrange($cards, BoardView::Sales, BoardPreferences::defaults())))->toBe(['prospect', 'tentative', 'quote_request'])
->and(boardKeys($layout->arrange($cards, BoardView::Sales, BoardPreferences::defaults())))->toBe(['prospect' => ['job-1'], 'tentative' => ['job-2'], 'quote_request' => ['job-3']]);
});
it('splits jobs in production by the user\'s urgent threshold', function (UrgentThreshold $threshold, array $urgent) {
$cards = [
boardCard(1, JobStatus::InProduction, Priority::Normal),
boardCard(2, JobStatus::InProduction, Priority::First),
boardCard(3, JobStatus::InProduction, Priority::Urgent),
boardCard(4, JobStatus::InProduction, Priority::Immediate),
];
$columns = boardKeys((new BoardLayout)->arrange($cards, BoardView::Production, BoardPreferences::defaults()->withUrgentThreshold($threshold)));
expect($columns['in_production_urgent'])->toBe($urgent)
->and([...$columns['in_production'], ...$columns['in_production_urgent']])->toHaveCount(4);
})->with([
'only immediate' => [UrgentThreshold::Immediate, ['job-4']],
'urgent and immediate' => [UrgentThreshold::Urgent, ['job-3', 'job-4']],
'first, urgent and immediate' => [UrgentThreshold::First, ['job-2', 'job-3', 'job-4']],
]);
it('keeps continuing jobs in their own column and places pinned tasks by their status', function () {
$cards = [
boardCard(1, JobStatus::Ongoing, Priority::Urgent),
boardCard(1, JobStatus::Ongoing, taskId: 10, taskStatus: TaskStatus::Waiting),
boardCard(1, JobStatus::Ongoing, taskId: 11, taskStatus: TaskStatus::InReview),
boardCard(1, JobStatus::Ongoing, taskId: 12, taskStatus: TaskStatus::Done),
];
expect(boardKeys((new BoardLayout)->arrange($cards, BoardView::Production, BoardPreferences::defaults())))->toMatchArray([
'ongoing' => ['job-1'],
'order' => ['task-10'],
'in_production' => ['task-11'],
'in_production_urgent' => [],
'done' => ['task-12'],
]);
});
it('orders a column by drag order or by due date with undated cards last', function () {
$cards = [
boardCard(1, JobStatus::Order, dueOn: '2026-12-10', position: 10),
boardCard(2, JobStatus::Order, dueOn: null, position: 20),
boardCard(3, JobStatus::Order, dueOn: '2026-12-01', position: 30),
];
$layout = new BoardLayout;
$byDate = BoardPreferences::defaults()->withColumnSort(BoardColumn::Order, ColumnSort::DueDate);
expect(boardKeys($layout->arrange($cards, BoardView::Production, BoardPreferences::defaults()))['order'])->toBe(['job-1', 'job-2', 'job-3'])
->and(boardKeys($layout->arrange($cards, BoardView::Production, $byDate))['order'])->toBe(['job-3', 'job-1', 'job-2']);
});
it('gives each column the design colour until the user picks one', function () {
$preferences = BoardPreferences::defaults()->withColumnColor(BoardColumn::Order, new HexColor('#B91C1C'));
expect($preferences->colorOf(BoardColumn::Order)->value)->toBe('#b91c1c')
->and($preferences->colorOf(BoardColumn::Ongoing)->value)->toBe('#3b2d5e')
->and($preferences->withColumnColor(BoardColumn::Order, null)->colorOf(BoardColumn::Order)->value)->toBe('#23476a');
});
it('drops unknown columns and invalid values from stored settings', function () {
$preferences = BoardPreferences::fromStored('huge', 'sometimes', ['order' => '#14532d', 'nope' => '#ffffff', 'done' => 'red'], ['order' => 'due_date', 'done' => 'random']);
expect($preferences->colorValues())->toBe(['order' => '#14532d'])
->and($preferences->sortValues())->toBe(['order' => 'due_date'])
->and($preferences->density->value)->toBe('normal')
->and($preferences->urgentThreshold)->toBe(UrgentThreshold::Urgent);
});
});
describe('moving cards', function () {
beforeEach(function () {
$this->jobs = new InMemoryCustomerJobRepository;
$this->tasks = new InMemoryTaskRepository;
$this->board = new InMemoryBoardRepository;
$this->events = new FakeEventDispatcher;
$customers = new FakeCustomerQueries;
$catalog = new FakeCatalogQueries;
$users = new FakeUserAccountQueries;
$transactions = new FakeTransactionManager;
$clock = new FakeClock(new DateTimeImmutable('2026-11-30'));
$customers->customers = [1 => 'Mainos Oy'];
$catalog->addServiceUnit(1, 'Markkinointi');
$catalog->addWorkType(11, 1, 'graafinen työ');
$users->addUser(10, 'Eero');
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $catalog, $users), $transactions, $clock, $this->events);
$this->create = fn (string $status, string $priority = 'normal'): int => $createJob->execute(new CustomerJobData('Kampanja', 1, [1], 10, 'one_off', $status, $priority), 10);
$planner = new TaskPlanner(new InMemoryTaskBookingRepository($this->tasks), new HolidayCalendar(new InMemoryHolidayOverrides), new DailyCapacity(new FakeSettings));
$this->addTask = new AddTask($this->jobs, $this->tasks, new TaskReferenceChecker($catalog, $users), $planner, $transactions, $this->events);
$this->move = new MoveBoardCard($this->jobs, $this->tasks, $this->board, $transactions, $this->events, $clock);
$this->pin = new PinTaskToBoard($this->jobs, $this->tasks, $this->board, $transactions);
});
it('changes the status, records it and stores the dropped order', function () {
$jobId = ($this->create)('order');
$otherId = ($this->create)('in_production');
$this->move->execute($jobId, null, BoardColumn::InProduction, ["job-{$otherId}", "job-{$jobId}"], UrgentThreshold::Urgent, 10);
expect($this->jobs->findById($jobId)?->details()->status)->toBe(JobStatus::InProduction)
->and($this->jobs->changes)->not->toBeEmpty()
->and($this->board->jobPositions)->toBe([$otherId => 10, $jobId => 20]);
});
it('raises the priority to the threshold in the urgent column and lowers it in the normal one', function () {
$jobId = ($this->create)('in_production');
$this->move->execute($jobId, null, BoardColumn::InProductionUrgent, ["job-{$jobId}"], UrgentThreshold::First, 10);
expect($this->jobs->findById($jobId)?->details()->priority)->toBe(Priority::First);
$this->move->execute($jobId, null, BoardColumn::InProduction, ["job-{$jobId}"], UrgentThreshold::First, 10);
expect($this->jobs->findById($jobId)?->details()->priority)->toBe(Priority::Normal);
});
it('keeps a higher priority when the card is already urgent enough', function () {
$jobId = ($this->create)('in_production', 'immediate');
$changesBefore = count($this->jobs->changes);
$this->move->execute($jobId, null, BoardColumn::InProductionUrgent, ["job-{$jobId}"], UrgentThreshold::Urgent, 10);
expect($this->jobs->findById($jobId)?->details()->priority)->toBe(Priority::Immediate)
->and($this->jobs->changes)->toHaveCount($changesBefore);
});
it('keeps a quote status when moved inside the quotes column', function () {
$jobId = ($this->create)('tentative');
$this->move->execute($jobId, null, BoardColumn::Quotes, ["job-{$jobId}"], UrgentThreshold::Urgent, 10);
expect($this->jobs->findById($jobId)?->details()->status)->toBe(JobStatus::Tentative);
$orderId = ($this->create)('order');
$this->move->execute($orderId, null, BoardColumn::Quotes, ["job-{$orderId}"], UrgentThreshold::Urgent, 10);
expect($this->jobs->findById($orderId)?->details()->status)->toBe(JobStatus::QuoteRequest);
});
it('never moves continuing jobs out of their column or other jobs into it', function (string $status, BoardColumn $target) {
$jobId = ($this->create)($status);
$this->move->execute($jobId, null, $target, [], UrgentThreshold::Urgent, 10);
})->throws(CardCannotMoveThere::class)->with([
'continuing out' => ['ongoing', BoardColumn::InProduction],
'other in' => ['order', BoardColumn::Ongoing],
]);
it('sets a pinned task\'s status by its new column and publishes its completion', function () {
$jobId = ($this->create)('ongoing');
$taskId = $this->addTask->execute($jobId, new TaskData('Ottelu 1', 11, 'waiting'), 10);
$this->pin->execute($jobId, $taskId, true);
$this->move->execute($jobId, $taskId, BoardColumn::Done, ["task-{$taskId}"], UrgentThreshold::Urgent, 10);
expect($this->tasks->findForJob($jobId, $taskId)?->status())->toBe(TaskStatus::Done)
->and($this->board->pinnedTasks)->toBe([$taskId => true])
->and($this->board->taskPositions)->toBe([$taskId => 10])
->and(array_filter($this->events->dispatched, fn (object $event): bool => $event instanceof TaskCompleted))->toHaveCount(1);
});
it('keeps pinned tasks out of columns that are not task columns', function () {
$jobId = ($this->create)('ongoing');
$taskId = $this->addTask->execute($jobId, new TaskData('Ottelu 1', 11, 'waiting'), 10);
$this->move->execute($jobId, $taskId, BoardColumn::OnHold, [], UrgentThreshold::Urgent, 10);
})->throws(CardCannotMoveThere::class);
it('pins only tasks of continuing jobs', function () {
$jobId = ($this->create)('in_production');
$taskId = $this->addTask->execute($jobId, new TaskData('Taitto', 11, 'waiting'), 10);
$this->pin->execute($jobId, $taskId, true);
})->throws(TaskCannotBePinned::class);
});
describe('open cards', function () {
it('remembers which cards are open', function () {
$preferences = BoardPreferences::defaults()
->withCardExpanded('job-12', true)
->withCardExpanded('task-34', true)
->withCardExpanded('job-12', false)
->withCardExpanded('nonsense', true);
expect($preferences->isCardExpanded('task-34'))->toBeTrue()
->and($preferences->isCardExpanded('job-12'))->toBeFalse()
->and($preferences->expandedCards)->toBe(['task-34']);
});
it('keeps only valid keys and the latest cards from stored settings', function () {
$stored = array_map(fn (int $id): string => 'job-'.$id, range(1, BoardPreferences::MAX_EXPANDED_CARDS + 10));
$preferences = BoardPreferences::fromStored(null, null, [], [], [...$stored, 'drop table', 42, 'job-1']);
expect($preferences->expandedCards)->toHaveCount(BoardPreferences::MAX_EXPANDED_CARDS)
->and($preferences->isCardExpanded('job-1'))->toBeFalse()
->and($preferences->isCardExpanded('job-'.(BoardPreferences::MAX_EXPANDED_CARDS + 10)))->toBeTrue()
->and($preferences->isCardExpanded('drop table'))->toBeFalse();
});
});