diff --git a/app/Devnull/CachedRow.php b/app/Devnull/CachedRow.php index 7f337124..c9d59a16 100644 --- a/app/Devnull/CachedRow.php +++ b/app/Devnull/CachedRow.php @@ -19,6 +19,11 @@ * when none of its ids is already in the frame. Otherwise, and for a row with no * keyed parent, whose ids would come from the frame's running count, the row * makes its node afresh. + * + * With EDGE's subtree reuse on, the row goes out as EDGE sends any node: a part + * of it the phone already holds, by id and content hash, is sent as a reuse + * marker and taken from the phone's last tree. So a row that hasn't changed + * costs the frame one marker, and a starred one only the parts the star touched. */ final class CachedRow extends Element { @@ -65,45 +70,63 @@ public function toArray( && array_intersect_key($this->nodeIds, $emittedIds) === []) { $emittedIds += $this->nodeIds; - return $this->node; + return self::send($this->node, $lastNodeHashes); } - $node = $this->row->toArray($registry, $nextId, $parentKeyPath, $indexInParent, $emittedIds, $lastNodeHashes); + // Built against no hashes, the row's node is whole, so it can be kept and sent again in any later frame. + $sentNothing = []; + $node = $this->row->toArray($registry, $nextId, $parentKeyPath, $indexInParent, $emittedIds, $sentNothing); $ids = []; + self::collectIds($node, $ids); + + $this->node = $node; + $this->nodePlace = $place; + $this->nodeRegistry = $registry; + $this->nodeIds = $ids; - if (self::collectFullIds($node, $ids)) { - $this->node = $node; - $this->nodePlace = $place; - $this->nodeRegistry = $registry; - $this->nodeIds = $ids; - } else { - $this->node = null; + return self::send($node, $lastNodeHashes); + } + + /** + * A node as this frame sends it: a reuse marker where the phone already holds + * it, by its id and hash, as EDGE's own walk does, and otherwise in full, its + * children each sent the same way, and its hash noted as what the phone holds + * next. With subtree reuse off the hashes start empty every frame, so every + * node goes in full. + * + * @param array $node + * @param array $lastNodeHashes + * @return array + */ + private static function send(array $node, array &$lastNodeHashes): array + { + if (($lastNodeHashes[$node['id']] ?? null) === $node['_hash']) { + return ['id' => $node['id'], 'type' => $node['type'], 'flags' => 1, '_hash' => $node['_hash']]; + } + + $lastNodeHashes[$node['id']] = $node['_hash']; + + if (isset($node['children'])) { + foreach ($node['children'] as $i => $child) { + $node['children'][$i] = self::send($child, $lastNodeHashes); + } } return $node; } /** - * Collects a node's ids, and its children's. False when any of them is a - * reuse marker, which only stands for a node the phone got in an earlier frame. + * Collects a node's ids, and its children's. * * @param array $node * @param array $ids */ - private static function collectFullIds(array $node, array &$ids): bool + private static function collectIds(array $node, array &$ids): void { - if ((($node['flags'] ?? 0) & 1) !== 0) { - return false; - } - $ids[$node['id']] = true; foreach ($node['children'] ?? [] as $child) { - if (! self::collectFullIds($child, $ids)) { - return false; - } + self::collectIds($child, $ids); } - - return true; } } diff --git a/app/Devnull/RowInputs.php b/app/Devnull/RowInputs.php index d96cbca2..41b62a77 100644 --- a/app/Devnull/RowInputs.php +++ b/app/Devnull/RowInputs.php @@ -26,15 +26,18 @@ final class RowInputs */ public static function for(Commit $commit, array $scope): array { - $viewer = $scope['viewer'] ?? app(Account::class)->user(); + // The screen's own answer, null included: null is a real answer (signed out, pastéis mode off), so it's + // only read from the account when the screen gave none. Reading it again cost a query a row on a phone. + $viewer = array_key_exists('viewer', $scope) ? $scope['viewer'] : app(Account::class)->user(); $ownership = $scope['ownership'] ?? app(Account::class)->ownership(); return [ 'viewer' => $viewer?->id, 'organizations' => $ownership->organizationIds, - 'pasteis' => $scope['pasteis'] ?? app(Account::class)->pasteis(), - 'stashed' => $scope['stashed'] ?? [], - 'unfolded' => $scope['unfolded'] ?? [], + 'pasteis' => array_key_exists('pasteis', $scope) ? $scope['pasteis'] : app(Account::class)->pasteis(), + // Only this commit's own drafts and unfolding: a draft started anywhere used to build every row again. + 'stashed' => [in_array('quote-'.$commit->id, $scope['stashed'] ?? [], true), in_array('reply-'.$commit->id, $scope['stashed'] ?? [], true)], + 'unfolded' => in_array($commit->id, $scope['unfolded'] ?? [], true), 'expandable' => isset($scope['threads']), 'opened' => array_key_exists($commit->id, $scope['threads'] ?? []) && ! in_array($commit->id, $scope['closingThreads'] ?? [], true), 'faded' => in_array($commit->id, $scope['fadedRows'] ?? [], true), @@ -46,6 +49,8 @@ public static function for(Commit $commit, array $scope): array 'poll' => [$scope['pollChoices'][$commit->id] ?? null, in_array($commit->id, $scope['voting'] ?? [], true)], 'selected' => ($scope['selectedCommit'] ?? null) === $commit->id, 'video' => $commit->linkPreview?->embed === null ? null : VideoDetails::card($commit->linkPreview), + // A poll's Vote button wears your lane colour. + 'colour' => $scope['myColour'] ?? null, ]; } diff --git a/app/NativeComponents/CommitDetail.php b/app/NativeComponents/CommitDetail.php index d103ecc6..a061e7cb 100644 --- a/app/NativeComponents/CommitDetail.php +++ b/app/NativeComponents/CommitDetail.php @@ -289,19 +289,12 @@ class CommitDetail extends NativeComponent public string $openedId = ''; /** - * The replies from the last frame, so a render only builds the replies that changed, as the Log does its rows. + * The page's rows from the last frame, so a render only builds the ones that changed, as the Log does its rows: + * the commit, what it replies to and the rest of its series, its replies, and the related commits. Typing a reply + * draws a frame a few times a second, and each used to build every commit above the reply bar again. */ private ?RowCache $replyRows = null; - /** - * Every frame goes in full, as on the Log (see Log::$forceFullFrames). A - * tap on the reply field mounts the thread's scroll view again under a new - * key, and EDGE's subtree reuse sent its unchanged rows as references to - * nodes the phone dropped with the old one, so the thread went blank as - * you started typing. - */ - protected bool $forceFullFrames = true; - public function data(string $key, mixed $default = null): mixed { return $this->embedded ? ($this->conversationData[$key] ?? $default) : parent::data($key, $default); diff --git a/app/NativeComponents/Conversation.php b/app/NativeComponents/Conversation.php index a65a36f2..93b01241 100644 --- a/app/NativeComponents/Conversation.php +++ b/app/NativeComponents/Conversation.php @@ -175,12 +175,6 @@ class Conversation extends NativeComponent public const int PinnedLines = 3; - /** - * Every frame goes in full, as on the Log (see Log::$forceFullFrames): messages come in at the bottom and their - * reactions change in place, which EDGE's subtree reuse would otherwise miss. - */ - protected bool $forceFullFrames = true; - /** * Who the thread is with, from the route: a username, or an org's id. */ diff --git a/app/NativeComponents/Log.php b/app/NativeComponents/Log.php index 2ef269c0..d63cbb06 100644 --- a/app/NativeComponents/Log.php +++ b/app/NativeComponents/Log.php @@ -114,14 +114,6 @@ class Log extends NativeComponent use TalksToDevnull; use WatchesForCalls; - /** - * Every frame goes in full. EDGE's subtree reuse sometimes refers to rows - * the phone never got once a list has grown or reordered, and the phone - * drops them, which drew the log blank under a pull to refresh. EDGE's own - * answer for a screen whose lists change often is to send it in full. - */ - protected bool $forceFullFrames = true; - public const array Logs = ['following', 'latest', 'trending']; /** @@ -251,6 +243,14 @@ class Log extends NativeComponent */ private array $rowCaches = []; + /** + * The logs beside yours as they were last drawn, by log: what was kept + * for them, hashed, and what they showed (see previewState()). + * + * @var array}> + */ + private array $previews = []; + /** * Which rows of each log are drawn. * @@ -438,6 +438,10 @@ private function openAfresh(): void * A log you're not on: the first few commits kept for it, as it will * open, or skeleton rows with nothing kept, or while it's two swipes away. * + * Every frame draws it, so its commits are only made again when what's + * kept for them changes. Made afresh, they cost the frame as much again + * to lay out and to check against the rows kept for them. + * * @return array */ private function previewState(string $log): array @@ -445,19 +449,25 @@ private function previewState(string $log): array $beside = abs(array_search($log, self::Logs, true) - array_search($this->log, self::Logs, true)) === 1; $json = $beside && ! $this->skeletonFrame ? $this->cached('log/'.$log) : null; $kept = is_array($json['data'] ?? null) ? $json['data'] : []; - $page = $json === null ? null : Page::fromArray(['data' => array_slice($kept, 0, self::PreviewCommits)] + $json, Commit::fromArray(...)); + $shown = $json === null ? null : [array_slice($kept, 0, self::PreviewCommits), count($kept) > self::PreviewCommits, array_diff_key($json, ['data' => true])]; + $key = hash('xxh3', serialize($shown)); + + if (($this->previews[$log][0] ?? null) !== $key) { + $page = $json === null ? null : Page::fromArray(['data' => array_slice($kept, 0, self::PreviewCommits)] + $json, Commit::fromArray(...)); + + $this->previews[$log] = [$key, [ + 'commits' => $page->items ?? [], + // Rows past the preview are left out, with a skeleton row for them, as for the next page. + 'nextCursor' => count($kept) > self::PreviewCommits ? 'preview' : $page?->nextCursor, + 'threads' => [], + 'latestCommits' => [], + 'suggestions' => array_values(array_map(Commit::fromArray(...), array_filter(is_array($json['suggestions'] ?? null) ? $json['suggestions'] : [], is_array(...)))), + 'loaded' => $page !== null, + 'error' => null, + ]]; + } - return [ - 'commits' => $page->items ?? [], - // Rows past the preview are left out, with a skeleton row for them, as for the next page. - 'nextCursor' => count($kept) > self::PreviewCommits ? 'preview' : $page?->nextCursor, - 'threads' => [], - 'latestCommits' => [], - 'suggestions' => array_values(array_map(Commit::fromArray(...), array_filter(is_array($json['suggestions'] ?? null) ? $json['suggestions'] : [], is_array(...)))), - 'loaded' => $page !== null, - 'lastVisitAt' => $this->lastVisitSeenAt($log), - 'error' => null, - ]; + return [...$this->previews[$log][1], 'lastVisitAt' => $this->lastVisitSeenAt($log)]; } public function isRanked(): bool diff --git a/app/NativeComponents/Notifications.php b/app/NativeComponents/Notifications.php index a96fb82e..cd6a66d2 100644 --- a/app/NativeComponents/Notifications.php +++ b/app/NativeComponents/Notifications.php @@ -58,11 +58,6 @@ class Notifications extends NativeComponent use WatchesForCalls; use WritesInBackground; - /** - * Every frame goes in full, as on the Log (see Log::$forceFullFrames). - */ - protected bool $forceFullFrames = true; - /** * How long a group's notifications take to fade in or out, and its * chevron to turn, in milliseconds: as long as replies take on the Log. diff --git a/app/NativeComponents/Organization.php b/app/NativeComponents/Organization.php index 78bf193d..3ed3fa4b 100644 --- a/app/NativeComponents/Organization.php +++ b/app/NativeComponents/Organization.php @@ -54,11 +54,6 @@ class Organization extends NativeComponent use ShowsCommits; use TalksToDevnull; - /** - * Every frame goes in full, as on a profile (see Profile::$forceFullFrames). - */ - protected bool $forceFullFrames = true; - public const array Tabs = ['commits', 'mentions']; /** diff --git a/app/NativeComponents/Profile.php b/app/NativeComponents/Profile.php index f9ad023b..bafdcc21 100644 --- a/app/NativeComponents/Profile.php +++ b/app/NativeComponents/Profile.php @@ -82,13 +82,6 @@ class Profile extends NativeComponent use TalksToDevnull; use WatchesForCalls; - /** - * Every frame goes in full, as on the Log (see Log::$forceFullFrames). A tab - * tapped again goes back to the top in a list with a new key and the same - * rows, which EDGE's subtree reuse would otherwise never send. - */ - protected bool $forceFullFrames = true; - public string $username = ''; public const array Tabs = ['commits', 'replies']; diff --git a/app/NativeComponents/Remotes.php b/app/NativeComponents/Remotes.php index 32a670cf..1e93e4bb 100644 --- a/app/NativeComponents/Remotes.php +++ b/app/NativeComponents/Remotes.php @@ -88,12 +88,6 @@ class Remotes extends NativeComponent public const string LeaveAlert = 'remotes-leave'; - /** - * Every frame goes in full, as on the Log (see Log::$forceFullFrames), since a row moves to the top when it - * has a new message. - */ - protected bool $forceFullFrames = true; - /** @var list */ public array $conversations = []; diff --git a/app/NativeComponents/Search.php b/app/NativeComponents/Search.php index afbc04dd..ab3fdf1e 100644 --- a/app/NativeComponents/Search.php +++ b/app/NativeComponents/Search.php @@ -63,11 +63,6 @@ class Search extends NativeComponent */ public const int ShortestCommitTerm = 3; - /** - * Every frame goes in full, as on Starred, since a new term replaces every row. - */ - protected bool $forceFullFrames = true; - public string $query = ''; /** diff --git a/app/NativeComponents/Starred.php b/app/NativeComponents/Starred.php index 98fd4021..35dcc24c 100644 --- a/app/NativeComponents/Starred.php +++ b/app/NativeComponents/Starred.php @@ -46,11 +46,6 @@ class Starred extends NativeComponent } use TalksToDevnull; - /** - * Every frame goes in full, as on the Log (see Log::$forceFullFrames), since rows leave the list from the middle. - */ - protected bool $forceFullFrames = true; - /** @var list */ public array $commits = []; diff --git a/resources/views/native/commit-detail.blade.php b/resources/views/native/commit-detail.blade.php index d5f4c550..a6b0a73f 100644 --- a/resources/views/native/commit-detail.blade.php +++ b/resources/views/native/commit-detail.blade.php @@ -62,6 +62,16 @@ + @elseif ($pageRow['partial'] === 'native.partials.commit-row') + {{-- + The commits are kept like the replies, so typing a reply only builds the reply bar. A row's + data says where its commit sits in the tree: its rail, its place in its author's thread, and + for a commit above, how many replies its button counts. RowInputs has whether those are open. + The commit the page is about also shows its own replies' toggle and count, and its time in your + timezone, which only the page knows. + --}} + @php($rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])) + @php($replyRows->include($pageRow['key'], [...\App\Devnull\RowInputs::for($pageRow['data']['commit'], $rowScope), 'focused' => $pageRow['data']['focused'] ? [$repliesShown, $replyCount, app(\App\Devnull\Timezone::class)->yours()] : null], $pageRow['partial'], $pageRow['data'], $rowScope)) @elseif ($pageRow['reply'] === null) @include($pageRow['partial'], $pageRow['data']) @else diff --git a/resources/views/native/partials/commit-actions.blade.php b/resources/views/native/partials/commit-actions.blade.php index b8371dc7..5118560f 100644 --- a/resources/views/native/partials/commit-actions.blade.php +++ b/resources/views/native/partials/commit-actions.blade.php @@ -22,7 +22,9 @@ A commit with a quote or a reply you started and didn't send marks that button, as the web does with stash@{0} beside its label. That doesn't fit the row, so here it's a dot after the label, inside the button. - The screen passes the names of the drafts it has kept ($stashed, see App\Devnull\Stash). + The screen passes the names of the drafts it has kept ($stashed, see App\Devnull\Stash). It passes you ($viewer) + and your pastéis ($pasteis) too, and null is an answer: a row only asks the account when the screen gave neither, + since asking is a query on a phone, and with pastéis mode off every row asked again. Each action is at least 44pt wide and 36pt tall to tap, as Apple asks. The row has no negative vertical margins: Android draws a negative margin as an offset and keeps the space, so the row sat 6pt lower there than on iOS, and @@ -52,7 +54,7 @@ @php($repliesCount ??= $commit->repliesCount) @php($iconSize = $compact ? 14 : 16) @php($labelSize = $compact ? 'text-xs' : 'text-[13px]') -@php($viewer ??= app(\App\Devnull\Account::class)->user()) +@php($viewer = array_key_exists('viewer', get_defined_vars()) ? $viewer : app(\App\Devnull\Account::class)->user()) @php($ownership ??= app(\App\Devnull\Account::class)->ownership()) @php($stashed ??= []) @php($hasQuoteStash = in_array('quote-'.$commit->id, $stashed, true)) @@ -63,7 +65,7 @@ @php($endDot ??= null) @php($starPop = \App\Devnull\Pop::transform($popping['star-'.$commit->id] ?? null, 'star')) @php($pastelPop = \App\Devnull\Pop::transform($popping['pastel-'.$commit->id] ?? null, 'pastel')) -@php($pasteis ??= app(\App\Devnull\Account::class)->pasteis()) +@php($pasteis = array_key_exists('pasteis', get_defined_vars()) ? $pasteis : app(\App\Devnull\Account::class)->pasteis()) @php($showsPastel = $pasteis !== null && ! $commit->author->isOrganization) @php($canGivePastel = $showsPastel && $viewer !== null && ! $isOwn && $commit->isApproved()) @php($pastelLabel = \App\Devnull\Display::count($commit->pasteisCount, 'pastel', 'pastéis')) diff --git a/resources/views/native/partials/feed.blade.php b/resources/views/native/partials/feed.blade.php index 5a9ebb31..c594571b 100644 --- a/resources/views/native/partials/feed.blade.php +++ b/resources/views/native/partials/feed.blade.php @@ -6,8 +6,8 @@ The commit's rail runs on past them when its author's run goes on below. A commit its author carried on in replies to themselves shows "Show thread". - Given a $rowCache (see App\Devnull\RowCache), commits and replies that haven't changed since the last frame are - drawn from it rather than built again. Given a $rowWindow (see App\Devnull\RowWindow), rows far from the screen + Given a $rowCache (see App\Devnull\RowCache), commits, replies and markers that haven't changed since the last frame + are drawn from it rather than built again. Given a $rowWindow (see App\Devnull\RowWindow), rows far from the screen go as placeholders with the keys their partials give them, so the phone keeps each one's place and height. --}} @php @@ -17,11 +17,18 @@ @endphp @foreach ($rows as $feedIndex => $row) @if ($row->isDayMarker()) - @if ($rowWindow?->place($feedIndex) ?? true) + @php + $markerDraws = $rowWindow?->place($feedIndex) ?? true; + @endphp + @if (! $markerDraws) + @php + $rowWindow->placeholder('day-'.$row->label); + @endphp + @elseif ($rowCache === null) @include('native.partials.day-marker', ['label' => $row->label, 'lane' => $row->previousLane]) @else @php - $rowWindow->placeholder('day-'.$row->label); + $rowCache->include('day-'.$row->label, [], 'native.partials.day-marker', ['label' => $row->label, 'lane' => $row->previousLane], []); @endphp @endif @elseif ($row->isWhoToFollow()) @@ -33,11 +40,18 @@ @endphp @endif @elseif ($row->isLastVisitMarker()) - @if ($rowWindow?->place($feedIndex) ?? true) + @php + $markerDraws = $rowWindow?->place($feedIndex) ?? true; + @endphp + @if (! $markerDraws) + @php + $rowWindow->placeholder('last-visit'); + @endphp + @elseif ($rowCache === null) @include('native.partials.last-visit-marker', ['lane' => $row->previousLane]) @else @php - $rowWindow->placeholder('last-visit'); + $rowCache->include('last-visit', [], 'native.partials.last-visit-marker', ['lane' => $row->previousLane], []); @endphp @endif @else diff --git a/resources/views/native/partials/log-page.blade.php b/resources/views/native/partials/log-page.blade.php index 1e2073d9..4b18be87 100644 --- a/resources/views/native/partials/log-page.blade.php +++ b/resources/views/native/partials/log-page.blade.php @@ -26,7 +26,9 @@ class="w-full flex-1 bg-theme-surface {{ $embedded ? '' : \App\Devnull\Display:: > @if (! $page['loaded'] && $page['error'] === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']], []); + @endphp @endforeach @endif @@ -45,9 +47,13 @@ class="w-full flex-1 bg-theme-surface {{ $embedded ? '' : \App\Devnull\Display:: @if ($page['lastCommit']) @if ($page['nextCursor'] !== null) - @include('native.partials.rail-loading', ['pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-more', [], 'native.partials.rail-loading', ['pulse' => $page['showing']], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $page['lastCommit']->lane(), 'message' => "That's the root commit. There's nothing before it."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $page['lastCommit']->lane(), 'message' => "That's the root commit. There's nothing before it."], []); + @endphp @endif @endif diff --git a/resources/views/native/partials/organization-page.blade.php b/resources/views/native/partials/organization-page.blade.php index 234d444d..52f38ad7 100644 --- a/resources/views/native/partials/organization-page.blade.php +++ b/resources/views/native/partials/organization-page.blade.php @@ -21,7 +21,9 @@ class="w-full flex-1 bg-theme-surface" ]) @elseif (! $page['loaded']) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']], []); + @endphp @endforeach @elseif ($page['isEmpty']) @if ($page['tab'] === 'mentions') @@ -36,11 +38,17 @@ class="w-full flex-1 bg-theme-surface" @if ($page['loaded'] && ! $page['isEmpty']) @if ($page['nextCursor'] !== null) - @include('native.partials.rail-loading', ['pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-more', [], 'native.partials.rail-loading', ['pulse' => $page['showing']], []); + @endphp @elseif ($page['tab'] === 'mentions') - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's the first mention of {$user->handle()}."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's the first mention of {$user->handle()}."], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's {$user->handle()}'s root commit."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's {$user->handle()}'s root commit."], []); + @endphp @endif @endif @endif diff --git a/resources/views/native/partials/profile-page.blade.php b/resources/views/native/partials/profile-page.blade.php index 9b1c5f66..80d0cadb 100644 --- a/resources/views/native/partials/profile-page.blade.php +++ b/resources/views/native/partials/profile-page.blade.php @@ -4,6 +4,8 @@ The pager draws the header and the tabs at the top of this list, so they scroll with it. As on the Log, each tab has its own list, a new $page['listKey'] puts it back at the top, and only the tab you're on asks for its next page. + The pinned commit, the skeleton rows and the row at the end are kept in the tab's row cache with its commits, so + a frame only builds what changed. --}} @if (! $page['loaded'] && $page['error'] === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']], []); + @endphp @endforeach @endif @@ -39,18 +43,27 @@ class="w-full flex-1 bg-theme-surface" Pinned - @include('native.partials.commit-row', ['commit' => $page['pinnedCommit'], 'previousLane' => null, 'continuesRun' => false, 'rowKey' => 'pinned-'.$page['pinnedCommit']->id]) + @php + $rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]); + $page['rowCache']->include('pinned-'.$page['pinnedCommit']->id, \App\Devnull\RowInputs::for($page['pinnedCommit'], $rowScope), 'native.partials.commit-row', ['commit' => $page['pinnedCommit'], 'previousLane' => null, 'continuesRun' => false, 'rowKey' => 'pinned-'.$page['pinnedCommit']->id], $rowScope); + @endphp @endif @include('native.partials.feed', ['rows' => $page['rows'], 'threads' => $page['threads'], 'rowCache' => $page['rowCache']]) @if (! $page['isEmpty'] || $page['pinnedCommit']) @if ($page['nextCursor'] !== null) - @include('native.partials.rail-loading', ['pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-more', [], 'native.partials.rail-loading', ['pulse' => $page['showing']], []); + @endphp @elseif ($page['tab'] === 'replies') - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first reply.' : "That's {$user->handle()}'s first reply."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first reply.' : "That's {$user->handle()}'s first reply."], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first commit. Everyone starts somewhere.' : "That's {$user->handle()}'s root commit."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first commit. Everyone starts somewhere.' : "That's {$user->handle()}'s root commit."], []); + @endphp @endif @endif diff --git a/resources/views/native/partials/related-commits.blade.php b/resources/views/native/partials/related-commits.blade.php index af215186..2ada9d41 100644 --- a/resources/views/native/partials/related-commits.blade.php +++ b/resources/views/native/partials/related-commits.blade.php @@ -2,7 +2,8 @@ Other threads, in relevance order, on a fresh rail below this thread as on the web. They start closed under a header with their count, which opens them in place and closes them again, its chevron turning as they do (see CommitDetail::toggleRelated()). Each commit fades and slides into place a little after the one above it, and fades - out together as they close. The header is the whole row, so it's an easy target at the foot of the page. + out together as they close. The header is the whole row, so it's an easy target at the foot of the page. Their rows + are kept with the page's other rows (see CommitDetail::$replyRows), so a frame only builds the ones that changed. --}} @php($relatedOpen = $relatedShown && ! $relatedClosing) @php($relatedLabel = \App\Devnull\Display::count(count($relatedRows), 'related commit', 'related commits')) @@ -18,7 +19,8 @@ @if ($relatedShown) @foreach ($relatedRows as $relatedRow) - @include('native.partials.commit-row', [ + @php($rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])) + @php($replyRows->include('related-'.$relatedRow->commit->id, \App\Devnull\RowInputs::for($relatedRow->commit, $rowScope), 'native.partials.commit-row', [ 'commit' => $relatedRow->commit, 'previousLane' => $relatedRow->previousLane, 'continuesRun' => $relatedRow->continuesRun, @@ -29,7 +31,7 @@ 'threadBelow' => false, 'withTail' => ! $loop->last, 'expandable' => false, - ]) + ], $rowScope)) @endforeach @endif diff --git a/resources/views/native/search.blade.php b/resources/views/native/search.blade.php index 663f1740..581e42eb 100644 --- a/resources/views/native/search.blade.php +++ b/resources/views/native/search.blade.php @@ -50,7 +50,9 @@ @if ($commitsFoundFor === null && $error === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Searching commits']) + @php + $rowCache->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Searching commits'], []); + @endphp @endforeach @elseif ($commitsFoundFor !== null && $commits === []) @include('native.partials.empty', [ diff --git a/resources/views/native/starred.blade.php b/resources/views/native/starred.blade.php index 20ce9d17..de2b723b 100644 --- a/resources/views/native/starred.blade.php +++ b/resources/views/native/starred.blade.php @@ -23,7 +23,9 @@ @if (! $loaded && $error === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading starred commits']) + @php + $rowCache->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading starred commits'], []); + @endphp @endforeach @endif @@ -40,9 +42,13 @@ @if ($lastCommit) @if ($nextCursor !== null) - @include('native.partials.rail-loading') + @php + $rowCache->include('loading-more', [], 'native.partials.rail-loading', [], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $lastCommit->lane(), 'message' => "You've reached the end of your starred commits."]) + @php + $rowCache->include('root-commit', [], 'native.partials.rail-end', ['lane' => $lastCommit->lane(), 'message' => "You've reached the end of your starred commits."], []); + @endphp @endif @endif diff --git a/tests/Feature/Screens/LogRowCacheTest.php b/tests/Feature/Screens/LogRowCacheTest.php index 8dc278df..aeb27bac 100644 --- a/tests/Feature/Screens/LogRowCacheTest.php +++ b/tests/Feature/Screens/LogRowCacheTest.php @@ -3,9 +3,13 @@ use App\Devnull\Account; use App\Devnull\Data\Pasteis; use App\Devnull\Data\StashedDraft; +use App\Devnull\Data\User; use App\Devnull\ImageCache; use App\Devnull\Stash; use App\NativeComponents\Log; +use Illuminate\Cache\Events\CacheHit; +use Illuminate\Cache\Events\CacheMissed; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\View; use Native\Mobile\Testing\Native; @@ -24,6 +28,11 @@ 'GET /api/v1/log/following' => [pageJson([ commitJson(['id' => 'c3', 'body' => $long, 'preview_segments' => [['type' => 'text', 'text' => substr($long, 0, 270).'…']]]), commitJson(['id' => 'c2', 'replies_count' => 1, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada', 'avatar_url' => 'https://avatars.githubusercontent.com/u/2?v=4'])]), + commitJson(['id' => 'p1', 'body' => 'Tabs or spaces?', 'author' => $grace, 'poll' => [ + 'id' => 'poll1', 'closes_at' => now()->addDay()->toIso8601ZuluString(), 'is_closed' => false, 'has_voted' => false, + 'options' => [['id' => 'o1', 'label' => 'Tabs'], ['id' => 'o2', 'label' => 'Spaces']], + ]]), + commitJson(['id' => 'g1', 'body' => 'From the organization', 'author' => userJson(['type' => 'organization', 'id' => 'o1', 'handle' => 'acme', 'name' => 'Acme'])]), commitJson(['id' => 'c1', 'body' => 'The oldest commit', 'author' => $grace]), ])], 'GET /api/v1/commits/c2/replies' => [pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c2', 'body' => 'A reply in place'])])], @@ -69,6 +78,16 @@ function expectKeptRowsToMatchFreshOnes(TestableComponent $screen, array $before app(Account::class)->rememberPasteis(new Pasteis); $screen->call('isRanked'); }, + // An organization's commit is yours once you're in it, whoever posted it. + 'joining an organization' => function () use ($screen) { + app(Account::class)->rememberOrganizations([User::fromArray(userJson(['type' => 'organization', 'id' => 'o1', 'handle' => 'acme', 'name' => 'Acme']))]); + $screen->call('isRanked'); + }, + // A poll's Vote button wears your lane colour. + 'your colour' => function () use ($screen) { + app(Account::class)->remember(User::fromArray(userJson(['lane' => 5]))); + $screen->call('isRanked'); + }, ]; foreach ($changes as $change) { @@ -108,4 +127,50 @@ function expectKeptRowsToMatchFreshOnes(TestableComponent $screen, array $before $screen->tap('star-c1'); expect($built)->toBe(['c1']); + + // A draft marks the button of the commit it's for, and only that row. + $built = []; + app(Stash::class)->put(app(Account::class)->user()?->id, 'reply-c1', new StashedDraft('Half a reply')); + $screen->call('isRanked'); + expect($built)->toBe(['c1']); +}); + +it('asks the account for you and your pastéis once a frame, not once a row', function () { + $screen = Native::visit('/log')->tap('replies-c2'); + $reads = []; + Event::listen([CacheHit::class, CacheMissed::class], function (CacheHit|CacheMissed $event) use (&$reads) { + $reads[$event->key] = ($reads[$event->key] ?? 0) + 1; + }); + + // Pastéis mode is off: null is the answer, not a reason to ask again. + $screen->call('isRanked'); + expect($reads['devnull.pasteis'] ?? 0)->toBe(1); + + // Every row built afresh asks no more often. + $reads = []; + Closure::bind(fn () => $this->rowCaches = [], $screen->instance(), Log::class)(); + $screen->call('isRanked'); + expect($reads['devnull.pasteis'] ?? 0)->toBe(1); +}); + +it('keeps the skeleton, loading and day rows, and draws them as building them again would', function () { + $built = []; + View::composer(['native.partials.rail-loading', 'native.partials.day-marker'], function ($view) use (&$built) { + $built[] = $view->name(); + }); + + // Latest is beside Following and has nothing kept, so it shows skeleton rows; Following ends in a loading row. + keep('log/following', pageJson([commitJson(['id' => 'c9', 'created_at' => now()->subDays(2)->toIso8601ZuluString()])], 'next')); + $screen = Native::visit('/log'); + $built = []; + + $screen->call('isRanked'); + expect($built)->toBe([]); + + // Swiping to Latest stops the loading row pulsing on Following, and starts the skeletons pulsing on Latest. + foreach (['latest', 'following'] as $log) { + $before = $screen->tree(); + tapLogTab($screen, $log); + expectKeptRowsToMatchFreshOnes($screen, $before); + } }); diff --git a/tests/Feature/Screens/LogsTest.php b/tests/Feature/Screens/LogsTest.php index 615b43c6..e4a44294 100644 --- a/tests/Feature/Screens/LogsTest.php +++ b/tests/Feature/Screens/LogsTest.php @@ -2,11 +2,17 @@ use App\Devnull\Client; use App\Devnull\FirstFrame; +use App\Devnull\ResponseCache; use App\Devnull\Timeline; use App\NativeComponents\CommitDetail; +use App\NativeComponents\Conversation; use App\NativeComponents\Log; use App\NativeComponents\Notifications; +use App\NativeComponents\Organization; use App\NativeComponents\Profile; +use App\NativeComponents\Remotes; +use App\NativeComponents\Search; +use App\NativeComponents\Starred; use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Http; use Native\Mobile\Events\Async\AsyncTaskFailed; @@ -277,6 +283,26 @@ function ada(): array } }); +it('keeps the commits shown for the log beside yours until what was kept for it changes', function () { + holdRefreshes(); + keep('log/latest', pageJson([commitJson(['id' => 'l1', 'body' => 'Latest 1', 'author' => ada()])])); + + $screen = Native::visit('/log'); + $previews = fn (): array => Closure::bind(fn (): array => $this->previews, $screen->instance(), Log::class)(); + $shown = $previews()['latest'][1]['commits'][0]; + + // Drawn again with nothing changed, the commit is the one made last frame, so its row is the one kept for it. + $screen->call('isRanked'); + expect($previews()['latest'][1]['commits'][0])->toBe($shown); + + // A star given on another screen changes what was kept for Latest, and its preview shows it. + app(ResponseCache::class)->starChanged('l1', true); + $screen->call('isRanked'); + + expect($previews()['latest'][1]['commits'][0])->not->toBe($shown) + ->and(nodeByRef($screen, 'star-l1')['props']['a11y_label'])->toStartWith('Unstar commit'); +}); + it('leaves the log two swipes away as skeleton rows until you are beside it', function () { holdRefreshes(); keep('log/trending', pageJson(array_map(fn (int $n): array => commitJson(['id' => "t{$n}", 'body' => "Trending {$n}"]), range(1, 5)))); @@ -334,13 +360,12 @@ function ada(): array ->and(nodeByRef($screen, 'commit-c1')['id'])->toBe($row); }); -it('sends the Log, Notifications, profiles and commit pages in full every frame, as EDGE advises for lists that change often', function () { +it('sends only what changed on the timeline screens, not every frame in full', function () { Http::fake(['devnull.test/*' => Http::response(pageJson([]))]); + $sendsInFull = fn (string $screen): bool => (fn (): bool => $this->forceFullFrames)->call(Native::test($screen)->instance()); - foreach ([Log::class, Notifications::class, Profile::class, CommitDetail::class] as $screen) { - $instance = Native::test($screen)->instance(); - - expect((fn (): bool => $this->forceFullFrames)->call($instance))->toBeTrue(); + foreach ([Log::class, Profile::class, Organization::class, CommitDetail::class, Starred::class, Notifications::class, Remotes::class, Search::class, Conversation::class] as $screen) { + expect($sendsInFull($screen))->toBeFalse(); } }); diff --git a/tests/Feature/Screens/PartialFramesTest.php b/tests/Feature/Screens/PartialFramesTest.php new file mode 100644 index 00000000..988742d4 --- /dev/null +++ b/tests/Feature/Screens/PartialFramesTest.php @@ -0,0 +1,523 @@ + 'http://devnull.test']); + signIn(); + + // Frozen, so a relative time can't move between the phone's frame and the full one it's checked against. + $this->freezeSecond(); +}); + +/** + * A page of the log's commits, newest first, from "{$prefix}{$first}" on. + * + * @return list> + */ +function partialFrameCommits(string $prefix, int $first, int $count): array +{ + return array_map(fn (int $n): array => commitJson([ + 'id' => "{$prefix}{$n}", + 'body' => "Commit {$prefix}{$n}", + // Ada's, but c7 is yours, so you can delete it. + 'author' => "{$prefix}{$n}" === 'c7' ? userJson() : userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada']), + // One with a photo, for the image viewer. + ...($n === 5 ? ['images' => [['id' => "{$prefix}{$n}-i1", 'url' => "https://images.example.com/{$prefix}{$n}.jpg", 'width' => 1200, 'height' => 900, 'description' => null]]] : []), + ]), range($first, $first + $count - 1)); +} + +it('keeps the Log on the phone in step through refreshes, pages, threads, swipes and deletes', function () { + sendPartialFrames(); + holdRefreshes(); + + $state = ['new' => [], 'deleted' => []]; + serveApi(function (Request $request) use (&$state) { + $path = parse_url($request->url(), PHP_URL_PATH); + parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query); + $served = fn (array $commits): array => array_values(array_filter($commits, fn (array $commit): bool => ! in_array($commit['id'], $state['deleted'], true))); + + return match (true) { + $path === '/api/v1/log/following' && isset($query['cursor']) => Http::response(pageJson($served(partialFrameCommits('c', 20, 20)))), + // As devnull answers ?since=: the first page, and how many on it are newer than the commit named. + $path === '/api/v1/log/following' => Http::response([ + ...pageJson($first = $served([...$state['new'], ...partialFrameCommits('c', 0, 20)]), 'page2'), + 'new_commits_count' => isset($query['since']) ? max(0, (int) array_search($query['since'], array_column($first, 'id'), true)) : 0, + ]), + $path === '/api/v1/log/latest' => Http::response(pageJson(partialFrameCommits('l', 0, 20))), + $path === '/api/v1/commits/c3/replies' => Http::response(pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c3', 'body' => 'A reply'])])), + $request->method() === 'POST' && $path === '/api/v1/commits/c4/star' => Http::response(['data' => [...partialFrameCommits('c', 4, 1)[0], 'starred' => true, 'stars_count' => 1]]), + $request->method() === 'DELETE' && str_starts_with($path, '/api/v1/commits/') => (function () use (&$state, $path) { + $state['deleted'][] = substr($path, strlen('/api/v1/commits/')); + + return Http::response(null, 204); + })(), + $path === '/api/v1/me' => Http::response(['data' => userWithCountsJson(), 'unread_notifications_count' => 0]), + default => Http::response(['message' => "No fake for {$path}."], 404), + }; + }); + + $screen = finishRefreshes(Native::visit('/log')); + expectPhoneInStep($screen); + + // A star sends the starred row's changes, and every other row as a marker. + finishRefreshes($screen->call('toggleStar', 'c4')); + expectPhoneInStep($screen); + expect(nodesSentInFull($screen))->toBeLessThan(40); + + $steps = [ + 'open a thread' => fn () => finishRefreshes($screen->call('toggleReplies', 'c3')), + 'next page' => fn () => finishRefreshes($screen->call('loadMore')), + 'pull to refresh with new commits on top' => function () use ($screen, &$state) { + $state['new'] = partialFrameCommits('n', 0, 3); + finishRefreshes($screen->call('load')); + }, + 'back to the top in a new list' => fn () => $screen->call('backToTop', 0), + 'delete from the middle' => fn () => finishRefreshes($screen->call('deleteCommit', 'c7')), + 'new commits in the pill' => function () use ($screen, &$state) { + $state['new'] = [...partialFrameCommits('m', 0, 2), ...$state['new']]; + finishRefreshes($screen->call('checkForNewCommits')); + }, + 'tap the pill' => fn () => finishRefreshes($screen->call('showNewCommits')), + 'close the thread' => fn () => finishRefreshes($screen->call('toggleReplies', 'c3')), + 'open the image viewer' => fn () => $screen->call('openImage', 'c5', 0)->assertSet('viewerOpen', true), + 'close it' => fn () => $screen->call('closeImageViewer'), + 'swipe to Latest' => fn () => finishRefreshes($screen->changeTab('showPage', 1)), + 'swipe back' => fn () => finishRefreshes($screen->changeTab('showPage', 0)), + ]; + + foreach ($steps as $name => $step) { + $step(); + expectPhoneInStep($screen, $name); + } +}); + +/** + * Commits on a profile or an organization's page, by the author given, newest first. + * + * @param array $author + * @return list> + */ +function partialFramePageCommits(string $prefix, array $author): array +{ + return array_map(fn (int $n): array => commitJson([ + 'id' => "{$prefix}{$n}", + 'body' => "Commit {$prefix}{$n}", + 'author' => $author, + 'replies_count' => $n === 3 ? 1 : 0, + ]), range(0, 19)); +} + +/** + * The API for a page of commits that can be starred and have a thread: $routes answers the rest by path. + * + * @param array $routes + */ +function servePartialFramePage(array $routes, string $starred): void +{ + serveApi(function (Request $request) use ($routes, $starred) { + $path = parse_url($request->url(), PHP_URL_PATH); + $thread = str_replace('4', '3', $starred); + + return match (true) { + $request->method() === 'POST' && $path === "/api/v1/commits/{$starred}/star" => Http::response(['data' => commitJson(['id' => $starred, 'body' => "Commit {$starred}", 'starred' => true, 'stars_count' => 1])]), + $path === "/api/v1/commits/{$thread}/replies" => Http::response(pageJson([replyJson(['id' => 'r1', 'parent_id' => $thread, 'body' => 'A reply'])])), + $path === '/api/v1/me' => Http::response(['data' => userWithCountsJson(), 'unread_notifications_count' => 0]), + default => is_callable($routes[$path] ?? null) ? $routes[$path]($request) : Http::response($routes[$path] ?? ['message' => "No fake for {$path}."], isset($routes[$path]) ? 200 : 404), + }; + }); +} + +it('keeps a profile on the phone in step through stars, threads, its tabs and back to the top', function () { + sendPartialFrames(); + holdRefreshes(); + + $ada = userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada Lovelace', 'lane' => 1]); + servePartialFramePage([ + '/api/v1/users/ada' => [ + 'data' => userWithCountsJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada Lovelace', 'lane' => 1], commits: 20), + 'is_following' => false, 'activity' => null, 'streaks' => ['longest' => 0, 'current' => 0], 'pinned_commit' => null, + ], + '/api/v1/users/ada/commits' => fn (Request $request) => Http::response(pageJson(partialFramePageCommits(str_contains($request->url(), 'replies') ? 'r' : 'p', $ada))), + ], starred: 'p4'); + + $screen = finishRefreshes(Native::visit('/log/users/ada')); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'a star' => fn () => finishRefreshes($screen->call('toggleStar', 'p4')), + 'open a thread' => fn () => finishRefreshes($screen->call('toggleReplies', 'p3')), + 'the tab tapped again, back to the top in a new list' => fn () => tapProfileTab($screen, 'commits'), + 'swipe to Replies' => fn () => finishRefreshes(tapProfileTab($screen, 'replies')), + 'swipe back' => fn () => finishRefreshes(tapProfileTab($screen, 'commits')), + 'close the thread' => fn () => finishRefreshes($screen->call('toggleReplies', 'p3')), + ]; + + foreach ($steps as $name => $step) { + $step(); + expectPhoneInStep($screen, $name); + } + + expect(nodesSentInFull($screen))->toBeLessThan(40); +}); + +it('keeps an organization on the phone in step through stars, threads, its tabs and back to the top', function () { + sendPartialFrames(); + holdRefreshes(); + + $laravel = ['type' => 'organization', 'id' => 'o-laravel', 'handle' => 'laravel', 'name' => 'Laravel', 'lane' => 0]; + servePartialFramePage([ + '/api/v1/organizations/laravel' => [ + 'data' => [ + 'handle' => 'laravel', 'name' => 'Laravel', 'avatar_url' => 'https://avatars.githubusercontent.com/u/958072', 'bio' => 'The PHP framework for web artisans.', + 'location' => null, 'website' => 'https://laravel.com', 'github_url' => 'https://github.com/laravel', 'html_url' => 'http://devnull.test/@laravel', + 'lane' => 0, 'followers_count' => 45, 'admins' => [userJson(['id' => 'u-taylor', 'handle' => 'taylorotwell', 'name' => 'Taylor Otwell'])], + ], + 'is_following' => false, 'can_claim' => false, 'settings_url' => null, + ], + '/api/v1/organizations/laravel/commits' => pageJson(partialFramePageCommits('o', $laravel)), + '/api/v1/organizations/laravel/mentions' => pageJson(partialFramePageCommits('m', userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada']))), + ], starred: 'o4'); + + $screen = finishRefreshes(Native::visit('/log/organizations/laravel')); + expectPhoneInStep($screen, 'opening'); + $tab = fn (string $tab) => $screen->changeTab($screen->get('tab') === $tab ? 'backToTop' : 'showPage', (int) array_search($tab, Organization::Tabs, true)); + + $steps = [ + 'a star' => fn () => finishRefreshes($screen->call('toggleStar', 'o4')), + 'open a thread' => fn () => finishRefreshes($screen->call('toggleReplies', 'o3')), + 'the tab tapped again, back to the top in a new list' => fn () => $tab('commits'), + 'swipe to Mentions' => fn () => finishRefreshes($tab('mentions')), + 'swipe back' => fn () => finishRefreshes($tab('commits')), + 'close the thread' => fn () => finishRefreshes($screen->call('toggleReplies', 'o3')), + ]; + + foreach ($steps as $name => $step) { + $step(); + expectPhoneInStep($screen, $name); + } + + expect(nodesSentInFull($screen))->toBeLessThan(40); +}); + +it('keeps the commit page on the phone in step while you open the thread, type a reply and move around its tree', function () { + sendPartialFrames(); + $grace = userJson(['id' => 'u3', 'handle' => 'grace', 'name' => 'Grace']); + fakeApi([ + 'GET /api/v1/commits/c2' => [commitPageJson( + commitJson(['id' => 'c2', 'parent_id' => 'c1', 'body' => 'The focused commit', 'replies_count' => 3]), + [ + commitJson(['id' => 'c0', 'body' => 'The root commit', 'author' => $grace]), + commitJson(['id' => 'c1', 'parent_id' => 'c0', 'body' => 'The parent commit', 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada'])]), + ], + )], + 'GET /api/v1/commits/c2/replies' => [pageJson(array_map(fn (int $n): array => replyJson(['id' => "r{$n}", 'parent_id' => 'c2', 'body' => "Reply {$n}", 'author' => $grace]), range(1, 3)))], + 'GET /api/v1/commits/c2/related' => [['data' => []]], + 'GET /api/v1/commits/c0/replies' => [pageJson([ + commitJson(['id' => 'c1', 'parent_id' => 'c0', 'body' => 'The parent commit', 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada'])]), + replyJson(['id' => 'x1', 'parent_id' => 'c0', 'body' => 'Another reply to the root']), + ])], + 'GET /api/v1/commits/c1/replies' => [pageJson([commitJson(['id' => 'c2', 'parent_id' => 'c1', 'body' => 'The focused commit', 'replies_count' => 3])])], + 'GET /api/v1/commits/c1/related' => [['data' => []]], + 'GET /api/v1/commits/r2/replies' => [pageJson([])], + 'GET /api/v1/commits/r2/related' => [['data' => []]], + 'GET /api/v1/commits/x1/replies' => [pageJson([])], + ]); + + $screen = Native::visit('/log/commits/c2'); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'the earlier commits shown' => fn () => $screen->call('showEarlier'), + 'the replies opened' => fn () => $screen->call('toggleReplies'), + // The scroll view mounts again at the top, under a new key. + 'a tap into the reply field' => fn () => $screen->call('replyTapped'), + 'a letter typed' => fn () => $screen->set('reply', 'N'), + 'another' => fn () => $screen->set('reply', 'Ni'), + 'the replies hidden' => fn () => $screen->call('toggleReplies'), + 'the reply field tapped again' => fn () => $screen->call('replyTapped'), + 'the replies opened again' => fn () => $screen->call('toggleReplies'), + // The page moves to a commit above, keeping its scroll view and the commits it shows, and back down. + 'the page moved up' => fn () => $screen->call('openCommit', 'c1')->assertSet('id', 'c1'), + 'the replies above opened' => fn () => $screen->call('toggleReplies', 'c0')->assertSee('Another reply to the root'), + 'the replies above folded' => fn () => $screen->call('toggleReplies', 'c0')->assertDontSee('Another reply to the root'), + 'the page moved back down' => fn () => $screen->call('openCommit', 'c2')->assertSet('id', 'c2'), + 'the page moved down to a reply' => fn () => $screen->call('openCommit', 'r2')->assertSet('id', 'r2'), + ]; + + $sentInFull = []; + foreach ($steps as $name => $step) { + $step(); + $sentInFull[$name] = nodesSentInFull($screen); + expectPhoneInStep($screen, $name); + } + + // A letter typed sends the field and what it changes, not the thread above it. + expect($sentInFull['another'])->toBeLessThan(40); +}); + +it('keeps Starred on the phone in step as rows leave from the middle', function () { + sendPartialFrames(); + $starred = fn (int $n, bool $starred = true): array => commitJson(['id' => "s{$n}", 'body' => "Starred s{$n}", 'starred' => $starred, 'stars_count' => (int) $starred, 'replies_count' => $n === 6 ? 1 : 0, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'lane' => 1])]); + $unstarred = []; + serveApi(function (Request $request) use ($starred, &$unstarred) { + $path = parse_url($request->url(), PHP_URL_PATH); + + if ($request->method() === 'DELETE' && preg_match('#^/api/v1/commits/(s\d+)/star$#', $path, $m)) { + $unstarred[] = $m[1]; + + return Http::response(['data' => $starred((int) substr($m[1], 1), false)]); + } + + return match ($path) { + '/api/v1/starred' => Http::response(pageJson(array_values(array_filter(array_map($starred, range(12, 1, -1)), fn (array $commit): bool => ! in_array($commit['id'], $unstarred, true))), null, 'http://devnull.test/api/v1/starred')), + '/api/v1/commits/s6/replies' => Http::response(pageJson([replyJson(['id' => 'r1', 'parent_id' => 's6', 'body' => 'A reply'])])), + default => Http::response(['message' => "No fake for {$path}."], 404), + }; + }); + + $screen = finishRefreshes(Native::visit('/profile/starred')); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'one unstarred from the middle' => fn () => finishRefreshes($screen->call('toggleStar', 's8')), + 'a thread opened under it' => fn () => finishRefreshes($screen->call('toggleReplies', 's6')), + 'another unstarred above the thread' => fn () => finishRefreshes($screen->call('toggleStar', 's7')), + 'pull to refresh' => fn () => finishRefreshes($screen->call('load')), + 'the top one unstarred' => fn () => finishRefreshes($screen->call('toggleStar', 's12')), + ]; + + $sentInFull = []; + foreach ($steps as $name => $step) { + $step(); + $sentInFull[$name] = nodesSentInFull($screen); + expectPhoneInStep($screen, $name); + } + + expect($sentInFull['one unstarred from the middle'])->toBeLessThan(40); +}); + +/** + * A page of notifications, as devnull sends them. + * + * @param list> $groups + * @return array + */ +function partialFrameInbox(array $groups, ?string $cursor = null): array +{ + return [...pageJson($groups, $cursor, path: 'http://devnull.test/api/v1/notifications'), 'filters' => ['type' => 'all'], 'read_at' => now()->toIso8601ZuluString()]; +} + +it('keeps Notifications on the phone in step as new ones come in, groups open and the kinds change', function () { + sendPartialFrames(); + holdRefreshes(); + + $notification = fn (string $id, string $type = 'starred', int $events = 1): array => notificationJson($type, [ + 'id' => $id, + 'event_count' => $events, + 'actor' => userJson(['id' => "u-{$id}", 'handle' => "person{$id}", 'name' => "Person {$id}", 'lane' => 2]), + 'commit' => notificationCommitJson(['id' => "c-{$id}", 'body' => "About {$id}"]), + ]); + $state = ['new' => []]; + serveApi(function (Request $request) use ($notification, &$state) { + $path = parse_url($request->url(), PHP_URL_PATH); + $all = [...$state['new'], $notification('g1', events: 3), ...array_map(fn (int $n): array => $notification("n{$n}"), range(1, 15))]; + + return match (true) { + $path === '/api/v1/notifications' && str_contains($request->url(), 'cursor=page2') => Http::response(partialFrameInbox(array_map(fn (int $n): array => $notification("o{$n}"), range(1, 10)))), + $path === '/api/v1/notifications' => Http::response(partialFrameInbox($all, 'page2')), + $path === '/api/v1/notifications/g1/group' => Http::response(pageJson([$notification('g1-older'), $notification('g1-oldest')])), + $path === '/api/v1/me' => Http::response(['data' => userWithCountsJson(), 'unread_notifications_count' => 0]), + default => Http::response(['message' => "No fake for {$path}."], 404), + }; + }); + + $screen = finishRefreshes(Native::visit('/notifications')); + expect(treeShows(phoneTree($screen), 'About n15'))->toBeTrue(); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'a group opened' => fn () => finishRefreshes($screen->call('toggleGroup', 'g1')), + 'two new ones on top' => function () use ($screen, &$state, $notification) { + $state['new'] = [$notification('new2', 'followed'), $notification('new1', 'mentioned')]; + finishRefreshes($screen->call('load')); + }, + 'the next page' => fn () => finishRefreshes($screen->call('loadMore')), + 'the group closed' => fn () => finishRefreshes($screen->call('toggleGroup', 'g1')), + 'every kind shown again, in a new list' => fn () => finishRefreshes($screen->call('showAll')), + ]; + + $sentInFull = []; + foreach ($steps as $name => $step) { + $step(); + $sentInFull[$name] = nodesSentInFull($screen); + expectPhoneInStep($screen, $name); + } + + expect(treeShows(phoneTree($screen), 'Person new1'))->toBeTrue() + ->and($sentInFull['the group closed'])->toBeLessThan(40); +}); + +it('keeps Remotes on the phone in step as a conversation with a new message moves to the top', function () { + sendPartialFrames(); + $this->travelTo('2026-10-19 15:30:00'); + app(Account::class)->rememberRemotes(new Remotes(requests: true)); + + // Ten conversations, the newest message first. A new message puts its conversation on top. + $latest = array_combine(range(1, 10), array_map(fn (int $n): string => sprintf('2026-10-19T%02d:00:00.000000Z', 14 - $n % 10), range(1, 10))); + $conversation = fn (int $n): array => conversationJson([ + 'id' => "conv-{$n}", + 'partner' => userJson(['id' => "u{$n}", 'handle' => "person{$n}", 'name' => "Person {$n}", 'lane' => $n % 7]), + 'last_message' => remoteMessageJson(['id' => $n, 'body' => "Message for {$n} at {$latest[$n]}"]), + 'last_message_at' => $latest[$n], + ]); + serveApi(function (Request $request) use (&$latest, $conversation) { + parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query); + $ids = array_keys($latest); + usort($ids, fn (int $a, int $b): int => strcmp($latest[$b], $latest[$a])); + + return Http::response(remotesInboxJson(($query['tab'] ?? 'open') === 'open' ? array_map($conversation, $ids) : [])); + }); + + $screen = finishRefreshes(Native::visit('/remotes')); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'a message in the middle one, which moves to the top' => function () use ($screen, &$latest) { + $latest[6] = '2026-10-19T15:20:00.000000Z'; + finishRefreshes($screen->call('pollInbox')); + }, + 'another, from further down' => function () use ($screen, &$latest) { + $latest[9] = '2026-10-19T15:25:00.000000Z'; + finishRefreshes($screen->call('pollInbox')); + }, + 'the tab tapped again, back to the top in a new list' => fn () => $screen->call('backToTop', 0), + 'swipe to Fetched' => fn () => finishRefreshes($screen->changeTab('showPage', 1)), + 'swipe back' => fn () => finishRefreshes($screen->changeTab('showPage', 0)), + 'a poll with nothing new' => fn () => finishRefreshes($screen->call('pollInbox')), + ]; + + foreach ($steps as $name => $step) { + $step(); + expectPhoneInStep($screen, $name); + } + + $rows = findNode(phoneTree($screen), fn (array $node): bool => ($node['ref'] ?? null) === 'remotes')['children']; + expect(array_slice(array_column($rows, 'ref'), 0, 3))->toBe(['remote-conv-9', 'remote-conv-6', 'remote-conv-10']) + ->and($rows)->toHaveCount(10); +}); + +it('keeps Search on the phone in step as each term replaces every row', function () { + sendPartialFrames(); + serveApi(function (Request $request) { + parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query); + $term = (string) ($query['q'] ?? ''); + $found = fn (int $count, string $prefix): array => array_map(fn (int $n): array => commitJson(['id' => "{$prefix}{$n}", 'body' => "{$term} result {$n}"]), range(1, $count)); + + return match (parse_url($request->url(), PHP_URL_PATH)) { + '/api/v1/search/users' => Http::response(['data' => [userJson(['id' => 'id-ada', 'handle' => 'ada', 'name' => 'Ada', 'is_followed' => false, 'follows_you' => false])]]), + // The same commits come back for two terms, in a different order, as a search can find them. + '/api/v1/search/commits' => Http::response(['data' => str_starts_with($term, 'php') ? $found(8, 'c') : array_reverse($found(8, 'c'))]), + default => Http::response(pageJson([])), + }; + }); + + $screen = Native::visit('/log/search'); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'a term picked' => fn () => finishRefreshes($screen->call('pick', 'php')), + 'a letter more' => fn () => finishRefreshes($screen->set('query', 'phpx')), + 'another term, the same commits in another order' => function () use ($screen) { + finishRefreshes($screen->set('query', 'laravel')); + expect(treeShows(phoneTree($screen), 'laravel result 8'))->toBeTrue(); + }, + 'back to the first term' => fn () => finishRefreshes($screen->set('query', 'php')), + 'the field cleared' => fn () => finishRefreshes($screen->set('query', '')), + ]; + + foreach ($steps as $name => $step) { + $step(); + expectPhoneInStep($screen, $name); + } +}); + +it('keeps a Remotes thread on the phone in step as messages come in at the bottom and reactions change in place', function () { + sendPartialFrames(); + $this->travelTo('2026-10-19 15:30:00'); + $viewer = '0199e5c4-0000-7000-8000-000000000001'; + + $message = fn (int $id, bool $mine = false): array => remoteMessageJson([ + 'id' => $id, + 'user_id' => $mine ? $viewer : 'u2', + 'client_id' => sprintf('9f0f6f3e-0000-4000-8000-%012d', $id), + 'body' => "Message {$id}", + 'created_at' => sprintf('2026-10-19T15:%02d:00Z', $id), + ]); + $state = ['messages' => array_map(fn (int $id): array => $message($id, $id % 3 === 0), range(11, 22)), 'changes' => []]; + serveApi(function (Request $request) use (&$state, $message, $viewer) { + $path = parse_url($request->url(), PHP_URL_PATH); + parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query); + + return match (true) { + $request->method() === 'GET' && $path === '/api/v1/remotes/@ada/messages' && isset($query['before']) => Http::response(threadJson(array_map($message, range(1, 10)))), + $request->method() === 'GET' && $path === '/api/v1/remotes/@ada/messages' && isset($query['after']) => Http::response(threadJson( + array_values(array_filter($state['messages'], fn (array $m): bool => $m['id'] > (int) $query['after'])), + ['reactions_version' => count($state['changes']), 'reaction_changes' => $state['changes']], + )), + $request->method() === 'GET' && $path === '/api/v1/remotes/@ada/messages' => Http::response(threadJson($state['messages'], ['has_older' => true, 'read_through_id' => 22])), + $request->method() === 'POST' && $path === '/api/v1/remotes/@ada/messages' => Http::response(['data' => remoteMessageJson([ + 'id' => 99, 'user_id' => $viewer, 'client_id' => json_decode($request->body(), true)['client_id'], 'body' => json_decode($request->body(), true)['body'], + ])], 201), + str_ends_with($path, '/reactions') && $request->method() === 'POST' => Http::response(['reactions' => [['user_id' => $viewer, 'emoji' => json_decode($request->body(), true)['emoji']]]]), + $path === '/api/v1/me' => Http::response(currentUserJson()), + default => Http::response(['usage' => []], $request->method() === 'GET' ? 200 : 204), + }; + }); + + $screen = finishRefreshes(Native::visit('/remotes/conversations/ada')); + expectPhoneInStep($screen, 'opening'); + + $steps = [ + 'a message in at the bottom, and a reaction on one above' => function () use ($screen, &$state, $message) { + $state['messages'][] = $message(23); + $state['changes'] = [['id' => 15, 'reactions' => [['user_id' => 'u2', 'emoji' => '🚀']]]]; + finishRefreshes($screen->call('poll')); + }, + 'a letter typed' => fn () => $screen->set('draft', 'o'), + 'a few more' => function () use ($screen) { + $screen->set('draft', 'on my way'); + expect(treeShows(phoneTree($screen), 'on my way'))->toBeTrue(); + }, + 'a heart on one of theirs' => fn () => finishRefreshes($screen->call('heart', 20)), + 'a reply begun' => fn () => $screen->call('reply', 21), + 'sent' => fn () => finishRefreshes($screen->call('send')), + 'older messages above' => fn () => finishRefreshes($screen->call('loadOlder')), + 'another in, and the reaction taken off' => function () use ($screen, &$state, $message) { + $state['messages'][] = $message(24); + $state['changes'] = [...$state['changes'], ['id' => 15, 'reactions' => []]]; + finishRefreshes($screen->call('poll')); + }, + ]; + + $sentInFull = []; + foreach ($steps as $name => $step) { + $step(); + $sentInFull[$name] = nodesSentInFull($screen); + expectPhoneInStep($screen, $name); + } + + expect(treeShows(phoneTree($screen), 'Message 24'))->toBeTrue() + ->and(treeShows(phoneTree($screen), 'Message 1"'))->toBeTrue() + ->and($sentInFull['a few more'])->toBeLessThan(40); +}); diff --git a/tests/Feature/Screens/RowCacheScreensTest.php b/tests/Feature/Screens/RowCacheScreensTest.php index d7c547cd..f050b9fd 100644 --- a/tests/Feature/Screens/RowCacheScreensTest.php +++ b/tests/Feature/Screens/RowCacheScreensTest.php @@ -1,5 +1,6 @@ tap('replies-c2')->assertSee('Second reply'); - // The commits above the replies read what the screen shows of the thread, so they're built every time. "Show - // more" is a row of its own, so the last reply of a page stays as it was when the next page comes. - expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe(['c2']) - ->and(rowsBuiltBy(fn () => $screen->call('loadMoreReplies')->assertSee('Third reply')))->toBe(['c2', 'r3']); + // The commits above the replies are kept like the replies, and "Show more" is a row of its own, so the last reply + // of a page stays as it was when the next page comes. Only the reply that page brings is built. + expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe([]) + ->and(rowsBuiltBy(fn () => $screen->call('loadMoreReplies')->assertSee('Third reply')))->toBe(['r3']); expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); }); + +it("keeps a commit page's commits between renders, and builds the commit again when what it shows of its thread changes", function () { + fakeApi([ + 'GET /api/v1/commits/c2' => [commitPageJson(commitJson(['id' => 'c2', 'replies_count' => 1, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada'])]), [commitJson(['id' => 'c1', 'body' => 'What it replies to'])])], + 'GET /api/v1/commits/c2/replies' => [pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c2', 'body' => 'First reply'])])], + 'GET /api/v1/commits/c2/related' => [['data' => [commitJson(['id' => 'x1', 'body' => 'Another thread'])]]], + 'POST /api/v1/commits/c2/star' => [['data' => commitJson(['id' => 'c2', 'replies_count' => 1, 'stars_count' => 1, 'starred' => true, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada'])])]], + ]); + $screen = finishRefreshes(Native::visit('/log/commits/c2'))->tap('related-commits-toggle'); + + // Typing a reply only builds the commit it replies to once, when the draft is first kept, which its row marks. + expect(rowsBuiltBy(fn () => $screen->input('reply-input', 'N')))->toBe(['c2']) + ->and(rowsBuiltBy(fn () => $screen->input('reply-input', 'Nice')))->toBe([]); + + $changes = [ + 'its replies opened' => fn () => $screen->tap('replies-c2')->assertSee('First reply'), + 'a star' => fn () => $screen->tap('star-c2'), + 'its replies closed' => fn () => $screen->tap('replies-c2'), + 'the time' => function () use ($screen) { + $this->travel(2)->hours(); + $screen->call('navTitle')->assertSee('2h'); + }, + 'your timezone' => function () use ($screen) { + app(Account::class)->rememberTimezone('Asia/Tokyo'); + $screen->call('navTitle'); + }, + ]; + + foreach ($changes as $change) { + $change(); + expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); + } +}); + +it("keeps a commit page's commits right as the replies of a commit above open and fold, and as the page moves", function () { + $person = fn (string $handle, int $lane): array => userJson(['id' => 'u-'.$handle, 'handle' => $handle, 'name' => ucfirst($handle), 'lane' => $lane]); + $c1 = commitJson(['id' => 'c1', 'body' => 'The commit above', 'replies_count' => 2, 'author' => $person('ada', 1)]); + $c2 = commitJson(['id' => 'c2', 'parent_id' => 'c1', 'body' => 'The focused commit', 'replies_count' => 1, 'author' => $person('grace', 2)]); + $x1 = replyJson(['id' => 'x1', 'parent_id' => 'c1', 'body' => 'Another reply above', 'replies_count' => 1, 'author' => $person('linus', 4)]); + $x2 = replyJson(['id' => 'x2', 'parent_id' => 'x1', 'body' => 'A reply to the other reply', 'author' => $person('alan', 5)]); + $r1 = replyJson(['id' => 'r1', 'parent_id' => 'c2', 'body' => 'A reply below', 'author' => $person('ken', 6)]); + fakeApi([ + 'GET /api/v1/commits/c2' => [commitPageJson($c2, [$c1])], + 'GET /api/v1/commits/c1' => [commitPageJson($c1)], + 'GET /api/v1/commits/r1' => [commitPageJson($r1, [$c1, $c2])], + 'GET /api/v1/commits/c1/related' => [['data' => []]], + 'GET /api/v1/commits/c2/related' => [['data' => []]], + 'GET /api/v1/commits/r1/related' => [['data' => []]], + 'GET /api/v1/commits/c1/replies' => [pageJson([$c2, $x1])], + 'GET /api/v1/commits/c2/replies' => [pageJson([$r1])], + 'GET /api/v1/commits/x1/replies' => [pageJson([$x2])], + 'GET /api/v1/commits/r1/replies' => [pageJson([])], + 'GET /api/v1/commits/x2/replies' => [pageJson([])], + ]); + // Held, so the frame while the replies above fade out is drawn too. + holdRefreshes(); + $screen = finishRefreshes(Native::visit('/log/commits/c2', ['replies' => true]))->assertSee('A reply below'); + + finishRefreshes($screen->tap('replies-c1'))->assertSee('Another reply above'); + expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); + + // With the replies above open, a frame builds none of the commits, and typing a reply only builds the commit it + // replies to, once, when the draft is first kept. + expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe([]) + ->and(rowsBuiltBy(fn () => $screen->input('reply-input', 'N')))->toBe(['c2']) + ->and(rowsBuiltBy(fn () => $screen->input('reply-input', 'Nice')))->toBe([]); + + $changes = [ + 'a reply above opens its own' => fn () => finishRefreshes($screen->tap('replies-x1'))->assertSee('A reply to the other reply'), + 'the replies above fading out' => fn () => expect($screen->tap('replies-c1')->get('closingThreads'))->toBe(['c1']), + 'the replies above folded' => fn () => finishRefreshes($screen)->assertDontSee('Another reply above'), + 'the page moved up to the commit above' => fn () => finishRefreshes($screen->tap('commit-c1'))->assertSet('id', 'c1'), + 'the page moved back down' => fn () => finishRefreshes($screen->tap('thread-reply-c2'))->assertSet('id', 'c2'), + 'the page moved down to a reply' => fn () => finishRefreshes($screen->tap('thread-reply-r1'))->assertSet('id', 'r1'), + 'the replies above opened over the reply' => fn () => finishRefreshes($screen->tap('replies-c1'))->assertSee('Another reply above'), + ]; + + foreach ($changes as $change) { + $change(); + expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); + } +}); + +it("keeps a commit page's commits right on a thread its author wrote as replies to themselves", function () { + $ada = userJson(['id' => 'u-ada', 'handle' => 'ada', 'name' => 'Ada', 'lane' => 2]); + $part = fn (string $id, ?string $parent, int $minutes, array $overrides = []): array => commitJson(['id' => $id, 'parent_id' => $parent, 'body' => "Ada's {$id}", 'author' => $ada, 'created_at' => now()->subMinutes($minutes)->toIso8601ZuluString(), ...$overrides]); + fakeApi([ + 'GET /api/v1/commits/c1' => [commitPageJson($part('c1', null, 30, ['replies_count' => 2]))], + 'GET /api/v1/commits/p2' => [commitPageJson($part('p2', 'c1', 25), [$part('c1', null, 30, ['replies_count' => 2])])], + 'GET /api/v1/commits/c1/related' => [['data' => []]], + 'GET /api/v1/commits/p2/related' => [['data' => []]], + 'GET /api/v1/commits/c1/replies' => [pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c1', 'body' => 'Grace answers', 'created_at' => now()->subMinutes(28)->toIso8601ZuluString()]), $part('p2', 'c1', 25)])], + 'GET /api/v1/commits/p2/replies' => [pageJson([replyJson(['id' => 'x1', 'parent_id' => 'p2', 'body' => 'Under part two']), $part('p3', 'p2', 20)])], + 'GET /api/v1/commits/p3/replies' => [pageJson([replyJson(['id' => 'y1', 'parent_id' => 'p3', 'body' => 'Under the last part'])])], + 'GET /api/v1/commits/r1/replies' => [pageJson([])], + 'GET /api/v1/commits/x1/replies' => [pageJson([])], + 'GET /api/v1/commits/y1/replies' => [pageJson([])], + ]); + $screen = finishRefreshes(Native::visit('/log/commits/c1'))->assertSee("Ada's p3")->assertSee('Under the last part'); + + // Typing a reply builds the commit the page is about once, for its draft, and none of the parts after it. + expect(rowsBuiltBy(fn () => $screen->input('reply-input', 'N')))->toBe(['c1']) + ->and(rowsBuiltBy(fn () => $screen->input('reply-input', 'Nice')))->toBe([]); + + $changes = [ + 'its replies opened' => fn () => $screen->tap('replies-c1')->assertSee('Grace answers'), + 'its replies closed' => fn () => finishRefreshes($screen->tap('replies-c1')), + 'the time' => function () use ($screen) { + $this->travel(2)->hours(); + $screen->call('navTitle'); + }, + 'the page moved to a later part' => fn () => finishRefreshes($screen->tap('commit-p2')->assertSet('id', 'p2')), + ]; + + foreach ($changes as $change) { + $change(); + expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); + } +}); diff --git a/tests/Pest.php b/tests/Pest.php index 38cc75fc..6af30cc4 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -15,6 +15,7 @@ use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Http; use Native\Mobile\AsyncTask; +use Native\Mobile\Edge\NativeComponent; use Native\Mobile\Events\Async\AsyncTaskFailed; use Native\Mobile\Events\Async\AsyncTaskFinished; use Native\Mobile\Events\Gallery\MediaSelected; @@ -1208,3 +1209,109 @@ function skipWithoutSkia(): void test()->markTestSkipped('needs the Skia plugin installed'); } } + +/** + * Turns EDGE's subtree reuse on for the screens a test opens next, as every phone has it: a frame sends a node the + * phone already holds as a reuse marker. Skips the test where the installed nativephp/mobile can't keep the phone's + * tree in step with that (anything before NativePHP/mobile-air#699, whose reader asks for a full frame when it + * can't), since partial frames would draw lists blank there. + */ +function sendPartialFrames(): FakeBridge +{ + if (! defined(NativeComponent::class.'::EVENT_FULL_FRAME')) { + test()->markTestSkipped('needs nativephp/mobile with NativePHP/mobile-air#699'); + } + + $bridge = FakeBridge::enable(); + $bridge->runtimeFlags = 0x02; + + return $bridge; +} + +/** + * The tree the phone holds after every frame the screen has published, built as EDGE's reader builds it: a reuse + * marker takes the node with that id from the tree before. A marker for a node the phone doesn't hold fails the test. + * + * @return array + */ +function phoneTree(TestableComponent $screen): array +{ + $tree = null; + + foreach ($screen->bridge()->publishes as $n => $frame) { + $held = []; + $index = function (array $node) use (&$index, &$held): void { + $held[$node['id']] = $node; + foreach ($node['children'] ?? [] as $child) { + $index($child); + } + }; + if ($tree !== null) { + $index($tree); + } + + $build = function (array $node) use (&$build, $held, $n): array { + if ((($node['flags'] ?? 0) & 1) === 1) { + expect(array_key_exists($node['id'], $held))->toBeTrue("Frame {$n} reuses node {$node['id']} ({$node['type']}), which the phone doesn't hold."); + + return $held[$node['id']]; + } + + if (isset($node['children'])) { + $node['children'] = array_map($build, $node['children']); + } + + return $node; + }; + + $tree = $build($frame); + } + + expect($tree)->not->toBeNull('The screen has published nothing.'); + + return $tree; +} + +/** + * Expects the phone to hold what the screen would send it in full right now, so partial frames left nothing stale or + * missing. Draws one full frame to compare with, which the phone never gets. + */ +function expectPhoneInStep(TestableComponent $screen, string $after = ''): void +{ + $onPhone = phoneTree($screen); + $bridge = $screen->bridge(); + $flags = $bridge->runtimeFlags; + $sent = count($bridge->publishes); + + $bridge->runtimeFlags = 0; + $full = $screen->call('navTitle')->tree(); + $bridge->runtimeFlags = $flags; + $bridge->publishes = array_slice($bridge->publishes, 0, $sent); + + $strip = function (array $node) use (&$strip): array { + unset($node['_hash']); + if (isset($node['children'])) { + $node['children'] = array_map($strip, $node['children']); + } + + return $node; + }; + + expect($strip($onPhone))->toBe($strip($full), "The phone isn't in step after: {$after}"); +} + +/** + * How many nodes the screen's last frame sent in full, rather than as reuse markers. + */ +function nodesSentInFull(TestableComponent $screen): int +{ + $count = function (array $node) use (&$count): int { + if ((($node['flags'] ?? 0) & 1) === 1) { + return 0; + } + + return 1 + array_sum(array_map($count, $node['children'] ?? [])); + }; + + return $count(end($screen->bridge()->publishes)); +} diff --git a/tests/Unit/Devnull/CachedRowTest.php b/tests/Unit/Devnull/CachedRowTest.php new file mode 100644 index 00000000..531ecf59 --- /dev/null +++ b/tests/Unit/Devnull/CachedRowTest.php @@ -0,0 +1,106 @@ + $hashes + * @return array + */ +function cachedRowFrame(Element $list, CallbackRegistry $registry, array &$hashes): array +{ + $nextId = 1; + $emitted = []; + $frame = $list->toArray($registry, $nextId, '', 0, $emitted, $hashes); + $hashes = array_intersect_key($hashes, $emitted); + + return $frame; +} + +/** + * Each node of a frame, by id, and whether it went as a reuse marker. + * + * @param array $node + * @return array + */ +function cachedRowMarkers(array $node): array +{ + $markers = [$node['id'] => (($node['flags'] ?? 0) & 1) === 1]; + + foreach ($node['children'] ?? [] as $child) { + $markers += cachedRowMarkers($child); + } + + return $markers; +} + +function keptRow(string $key, string $body, string $time = '5m'): CachedRow +{ + return new CachedRow(Column::make(Text::make($body), Text::make($time))->key($key)); +} + +it('sends a kept row the phone already holds as a reuse marker', function () { + $registry = new CallbackRegistry; + $hashes = []; + $rows = [keptRow('c1', 'First'), keptRow('c2', 'Second')]; + + $first = cachedRowFrame(Column::make(...$rows)->key('list'), $registry, $hashes); + expect(array_filter(cachedRowMarkers($first)))->toBe([]); + + // A row goes on top: the list goes again, the two rows under it as markers. + $second = cachedRowFrame(Column::make(keptRow('c3', 'Third'), ...$rows)->key('list'), $registry, $hashes); + [$new, $c1, $c2] = $second['children']; + + expect($second['flags'] ?? 0)->toBe(0) + ->and($new['flags'] ?? 0)->toBe(0) + ->and($c1)->toBe(['id' => $first['children'][0]['id'], 'type' => 'column', 'flags' => 1, '_hash' => $first['children'][0]['_hash']]) + ->and($c2['flags'])->toBe(1); +}); + +it('sends only the parts of a row that changed', function () { + $registry = new CallbackRegistry; + $hashes = []; + + $first = cachedRowFrame(Column::make(keptRow('c1', 'First', '5m'))->key('list'), $registry, $hashes); + $second = cachedRowFrame(Column::make(keptRow('c1', 'First', '6m'))->key('list'), $registry, $hashes); + [$body, $time] = $second['children'][0]['children']; + + expect($second['children'][0]['flags'] ?? 0)->toBe(0) + ->and($body['flags'])->toBe(1) + ->and($body['id'])->toBe($first['children'][0]['children'][0]['id']) + ->and($time['flags'] ?? 0)->toBe(0) + ->and($time['props'] ?? null)->not->toBeNull(); +}); + +it('sends a kept row in full when the phone has lost it, and as a marker the frame after', function () { + $registry = new CallbackRegistry; + $hashes = []; + $list = Column::make(keptRow('c1', 'First'), keptRow('c2', 'Second'))->key('list'); + + $first = cachedRowFrame($list, $registry, $hashes); + + // EDGE drops every hash when the phone may have lost its tree, as on its heartbeat. + $hashes = []; + expect(cachedRowFrame($list, $registry, $hashes))->toBe($first); + + $third = cachedRowFrame($list, $registry, $hashes); + expect($third['flags'])->toBe(1); +}); + +it('sends every frame in full with subtree reuse off', function () { + $registry = new CallbackRegistry; + $list = Column::make(keptRow('c1', 'First'))->key('list'); + + $throwaway = []; + $first = cachedRowFrame($list, $registry, $throwaway); + $throwaway = []; + + expect(cachedRowFrame($list, $registry, $throwaway))->toBe($first) + ->and(array_filter(cachedRowMarkers($first)))->toBe([]); +});