diff --git a/app/Devnull/Data/Activity.php b/app/Devnull/Data/Activity.php index 3a802ed2..1ca9be78 100644 --- a/app/Devnull/Data/Activity.php +++ b/app/Devnull/Data/Activity.php @@ -31,7 +31,7 @@ public function __construct(public string $scale = 'year', public array $buckets */ public static function fromArray(mixed $data): self { - if (! is_array($data) || ! array_key_exists($data['scale'] ?? null, self::Windows) || ! is_array($data['buckets'] ?? null)) { + if (! is_array($data) || ! is_array($data['buckets'] ?? null)) { return new self; } @@ -43,7 +43,7 @@ public static function fromArray(mixed $data): self } } - return new self($data['scale'], $buckets); + return new self(in_array($data['scale'] ?? null, array_keys(self::Windows), true) ? $data['scale'] : 'year', $buckets); } public function total(): int diff --git a/tests/Unit/Devnull/ActivityTest.php b/tests/Unit/Devnull/ActivityTest.php index 944ebca8..b0760a0b 100644 --- a/tests/Unit/Devnull/ActivityTest.php +++ b/tests/Unit/Devnull/ActivityTest.php @@ -81,3 +81,11 @@ function activityJson(string $scale, CarbonImmutable $from, string $step, array expect(Activity::fromArray(activityJson('week', CarbonImmutable::parse('2026-10-09'), 'days', array_fill(0, 7, 0)))->isEmpty())->toBeTrue() ->and(Activity::fromArray(activityJson('year', CarbonImmutable::parse('2025-10-12'), 'days', array_fill(0, 7, 0)))->isEmpty())->toBeFalse(); }); + +it('draws a scale it does not know as a year rather than dropping the commits', function () { + $activity = Activity::fromArray(activityJson('quarter', CarbonImmutable::parse('2026-08-23'), 'days', [1, ...array_fill(0, 89, 0), 2])); + + expect($activity->scale)->toBe('year') + ->and($activity->cells())->toHaveCount(13) + ->and($activity->total())->toBe(3); +});