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/NativeComponents/CommitDetail.php b/app/NativeComponents/CommitDetail.php index 5f285779..4dbfafab 100644 --- a/app/NativeComponents/CommitDetail.php +++ b/app/NativeComponents/CommitDetail.php @@ -225,15 +225,6 @@ class CommitDetail extends NativeComponent */ 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 mount(): void { $this->focusReply = $this->data('reply', false) === true; diff --git a/app/NativeComponents/Conversation.php b/app/NativeComponents/Conversation.php index 9324fb2c..464f5f56 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 276688e9..19ec6ca5 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']; /** 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 fb12ccf9..fdc7d9d8 100644 --- a/app/NativeComponents/Organization.php +++ b/app/NativeComponents/Organization.php @@ -53,11 +53,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 b82daf61..cee28387 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 9f15bbc5..92bf354f 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 d05cd1a9..e66bf30a 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/tests/Feature/Screens/LogsTest.php b/tests/Feature/Screens/LogsTest.php index b072768d..e4a44294 100644 --- a/tests/Feature/Screens/LogsTest.php +++ b/tests/Feature/Screens/LogsTest.php @@ -5,9 +5,14 @@ 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; @@ -355,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..45bfa771 --- /dev/null +++ b/tests/Feature/Screens/PartialFramesTest.php @@ -0,0 +1,508 @@ + '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 and type a reply', 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' => []]], + ]); + + $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'), + ]; + + $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/Pest.php b/tests/Pest.php index 670154f7..de1d0bb7 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; @@ -1115,3 +1116,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([]); +});