rob-revisited/ide/tests/TextIndexTest.php

164 lines
8.2 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Search\TextIndex;
use JumPHP\Ide\Workspace\MemoryWorkspace;
use PHPUnit\Framework\TestCase;
final class TextIndexTest extends TestCase
{
private function project(): MemoryWorkspace
{
return new MemoryWorkspace([
'src/Scene/PlayScene.php' => "<?php\nfinal class PlayScene\n{\n public function render(\$r): void\n {\n \$r->fillRect(0, 0, 10, 10, '#fff');\n }\n}\n",
'src/Entity/Player.php' => "<?php\nfinal class Player\n{\n private int \$jumpHeight = 3;\n public function jump(): void { \$this->jumpHeight++; }\n}\n",
'src/Net/NetSession.php' => "<?php\n// sends a message to the other players\nfinal class NetSession { public function send_message(): void {} }\n",
'README.md' => "# The game\n\nNever block the frame: count dt in update().\nThe player jumps over the frame.\n",
'docs/notes.txt' => "frame block never\nplayer\n",
'vendor/lib/Player.php' => "<?php class Player {}\n",
'assets/hero.png' => "\x89PNG\0\0binary",
]);
}
private function built(MemoryWorkspace $ws): TextIndex
{
$index = new TextIndex($ws);
self::assertTrue($index->refresh());
return $index;
}
/** @return list<string> */
private function paths(TextIndex $index, string $query): array
{
return array_column($index->query($query)['results'], 'path');
}
public function testWordsAreLowercasedAndCompoundIdentifiersIndexedWholeAndByParts(): void
{
self::assertSame(['findpanel', 'find', 'panel'], TextIndex::wordTokens('FindPanel'));
self::assertSame(['is_skipped', 'is', 'skipped'], TextIndex::wordTokens('is_skipped'));
self::assertSame(['htmlparser', 'html', 'parser'], TextIndex::wordTokens('HTMLParser'));
self::assertSame(['utf8decode', 'utf', 'decode'], TextIndex::wordTokens('utf8Decode'));
self::assertSame(['été'], TextIndex::wordTokens('Été'));
self::assertSame([], TextIndex::wordTokens('x'));
self::assertSame([], TextIndex::wordTokens('3f2a9c0b7d1e4f5a6b7c8d9e'), 'a hash is not a word');
$counts = TextIndex::tokenCounts('$this->jumpHeight = jumpHeight + 1; // Jump');
self::assertSame(2, $counts['jumpheight']);
self::assertSame(3, $counts['jump']);
self::assertSame(2, $counts['height']);
self::assertSame(1, $counts['this']);
}
public function testTheIndexHoldsTheTextFilesOnly(): void
{
$index = $this->built($this->project());
self::assertSame(5, $index->status()['files'], 'not vendor/, not the PNG');
self::assertTrue($index->status()['complete']);
self::assertNotContains('vendor/lib/Player.php', $this->paths($index, 'player'));
}
public function testAllTheWordsMustBeThereAndThePartsOfAnIdentifierMatch(): void
{
$index = $this->built($this->project());
self::assertSame(['src/Entity/Player.php'], $this->paths($index, 'jump height'));
self::assertSame(['src/Entity/Player.php'], $this->paths($index, 'jumpHeight'));
self::assertSame(['src/Net/NetSession.php'], $this->paths($index, 'send message'));
self::assertSame([], $this->paths($index, 'jump message'));
}
public function testPhrasesPrefixesExclusionsAndFilters(): void
{
$index = $this->built($this->project());
self::assertSame(['README.md'], $this->paths($index, '"never block the frame"'), 'notes.txt has the words, not the phrase');
self::assertEqualsCanonicalizing(['README.md', 'docs/notes.txt'], $this->paths($index, 'never block frame'));
self::assertEqualsCanonicalizing(['src/Entity/Player.php', 'README.md'], $this->paths($index, 'jum*'));
self::assertSame(['docs/notes.txt'], $this->paths($index, 'player -jumps -class'));
self::assertSame(['src/Entity/Player.php'], $this->paths($index, 'player path:src/'));
self::assertSame(['src/Entity/Player.php'], $this->paths($index, 'player ext:php'));
self::assertEqualsCanonicalizing(['README.md', 'docs/notes.txt'], $this->paths($index, 'player ext:md,txt'));
self::assertSame(['src/Scene/PlayScene.php'], $this->paths($index, 'r->fillRect'), 'two words together: a phrase (r is too short for the index, the text has it)');
}
public function testFileNamesAndDeclarationsRankFirst(): void
{
$index = $this->built($this->project());
// "player" is in four files: the one named after it, which declares it, comes first
self::assertSame('src/Entity/Player.php', $this->paths($index, 'player')[0]);
$result = $index->query('fillRect')['results'][0];
self::assertSame('src/Scene/PlayScene.php', $result['path']);
self::assertSame(6, $result['line']);
self::assertSame(13, $result['col']);
self::assertSame(" \$r->fillRect(0, 0, 10, 10, '#fff');", $result['text']);
self::assertSame([[12, 8]], $result['ranges']);
}
public function testAFileChangedOrDeletedIsSeenAndTheDeadOnesAreCompacted(): void
{
$ws = $this->project();
$index = $this->built($ws);
$ws->write('README.md', "Rainbows and frames\n");
$index->update('README.md');
self::assertSame(['README.md'], $this->paths($index, 'rainbows'));
self::assertSame(['docs/notes.txt'], $this->paths($index, '"frame block"'));
self::assertSame(['docs/notes.txt'], $this->paths($index, 'never'));
// changed outside the IDE (git, Claude): seen by the next refresh
$ws->write('docs/notes.txt', "nothing here\n");
$ws->delete('src/Net/NetSession.php');
$ws->write('src/New.php', "<?php // never\n");
$index->invalidate();
self::assertTrue($index->refresh());
self::assertSame(['src/New.php'], $this->paths($index, 'never'));
self::assertSame([], $this->paths($index, 'message'));
// many changes: the dead ids leave the lists
for ($i = 0; $i < 250; $i++) {
$index->update('src/New.php');
}
self::assertGreaterThan(200, $index->status()['dead']);
self::assertTrue($index->refresh());
self::assertSame(0, $index->status()['dead']);
self::assertSame(['src/New.php'], $this->paths($index, 'never'));
self::assertSame(5, $index->status()['files']);
}
public function testTheIndexStaysSmall(): void
{
$files = [];
$words = ['alpha', 'beta', 'gamma', 'delta', 'render', 'update', 'scene', 'player', 'entity', 'collision'];
for ($i = 0; $i < 400; $i++) {
$text = "<?php\nfinal class Thing$i\n{\n";
for ($j = 0; $j < 60; $j++) {
$text .= ' public function ' . $words[($i + $j) % 10] . ucfirst($words[$j % 10]) . "$j(): void { \$this->{$words[$j % 10]}++; }\n";
}
$files["src/Thing$i.php"] = $text . "}\n";
}
$ws = new MemoryWorkspace($files);
$before = memory_get_usage();
$index = $this->built($ws);
$used = memory_get_usage() - $before;
// about 1 MB of PHP: the index is a fraction of it
self::assertLessThan(1_500_000, $used);
self::assertLessThan(1_500_000, $index->memory());
$time = microtime(true);
$result = $index->query('render scene*', 20);
self::assertLessThan(50, (microtime(true) - $time) * 1000);
self::assertSame(20, count($result['results']));
}
public function testTheIdeKeepsItUpToDateAndAnswersSearchEngine(): void
{
$ws = $this->project();
$ide = new Ide($ws);
for ($i = 0; $i < 50 && !$ide->handle('search.engine.status', [])['complete']; $i++) {
$ide->tick(1.0);
}
self::assertTrue($ide->handle('search.engine.status', [])['complete']);
self::assertSame('src/Entity/Player.php', $ide->handle('search.engine', ['query' => 'jumpHeight'])['results'][0]['path']);
$ide->handle('file.write', ['path' => 'src/Entity/Player.php', 'content' => "<?php // empty now\n"]);
self::assertSame([], $ide->handle('search.engine', ['query' => 'jumpHeight'])['results']);
self::assertSame(['src/Entity/Player.php'], array_column($ide->handle('search.engine', ['query' => 'empty'])['results'], 'path'));
}
}