Files
coruna-lab/tests/Unit/MnemonicDetectorTest.php
T
2026-09-13 00:31:54 +08:00

149 lines
4.9 KiB
PHP

<?php
namespace Tests\Unit;
use App\Services\Chain\Bip44;
use App\Services\MnemonicDetector;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class MnemonicDetectorTest extends TestCase
{
#[Test]
public function detects_english_phrase_among_noise(): void
{
$phrase = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
$hit = (new MnemonicDetector)->detect("Recovery sheet\n1. ".$phrase."\nbackup 2024");
$this->assertNotNull($hit);
$this->assertSame($phrase, $hit['candidate']);
$this->assertSame(12, $hit['word_count']);
$this->assertTrue($hit['confirmed']);
}
#[Test]
public function ignores_short_unrelated_text(): void
{
$this->assertNull((new MnemonicDetector)->detect('hello world this is just a note'));
}
#[Test]
public function ignores_chinese_wallet_ui_copy(): void
{
$text = '备份助记词 请按顺序抄写助记词,确保备份正确。妥善保管助记词至隔离网络的安全地方。';
$this->assertNull((new MnemonicDetector)->detect($text));
}
#[Test]
public function marks_dense_ocr_holes_as_suspected_not_confirmed(): void
{
$ocr = <<<'TXT'
palance cousin endorse
refuse private dog
solution puddy april
picture father jab
TXT;
$hit = (new MnemonicDetector)->detect($ocr);
$this->assertNotNull($hit);
$this->assertFalse($hit['confirmed']);
$this->assertSame(9, $hit['word_count']);
}
#[Test]
public function eight_consecutive_hits_are_suspected(): void
{
$text = 'cousin endorse refuse private dog solution april picture';
$hit = (new MnemonicDetector)->detect($text);
$this->assertNotNull($hit);
$this->assertFalse($hit['confirmed']);
$this->assertSame(8, $hit['word_count']);
}
#[Test]
public function ignores_sparse_english_prose(): void
{
$ocr = '7134 bom CA i a ating Heath Mag Daly craving If You\'re Craving Sugar '
.'Your body may need protein + stable blood sugar What\'s happening: Blood sugar spikes '
.'crash more cravings what eat instead cogs chicken';
$this->assertNull((new MnemonicDetector)->detect($ocr));
}
#[Test]
public function two_ocr_holes_with_ten_hits_are_suspected(): void
{
$ocr = 'cousin endorse refuse private dog solution puddy april picture father palance lab';
$hit = (new MnemonicDetector)->detect($ocr);
$this->assertNotNull($hit);
$this->assertFalse($hit['confirmed']);
$this->assertSame(10, $hit['word_count']);
}
#[Test]
public function ignores_repetitive_plumbing_copy(): void
{
$text = 'PVC pipe 200 1.5 cable coil cable coil cable coil cable way';
$this->assertNull((new MnemonicDetector)->detect($text, true));
}
#[Test]
public function notes_ignore_partial_ocr_style_hits(): void
{
$ocr = <<<'TXT'
palance cousin endorse
refuse private dog
solution puddy april
picture father jab
TXT;
$this->assertNull((new MnemonicDetector)->detect($ocr, true));
}
#[Test]
public function notes_keep_numbered_valid_phrase(): void
{
$lines = [];
foreach (explode(' ', 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about') as $i => $word) {
$lines[] = ($i + 1).'. '.$word;
}
$hit = (new MnemonicDetector)->detect(implode("\n", $lines), true);
$this->assertNotNull($hit);
$this->assertTrue($hit['confirmed']);
$this->assertSame(12, $hit['word_count']);
}
#[Test]
public function prefers_english_phrase_over_chinese_ui(): void
{
$phrase = 'arctic floor lamp patrol connect mixture student enough mandate region whisper hint';
$hit = (new MnemonicDetector)->detect("正确抄写12个助记词\n".$phrase."\n我已抄好,去验证");
$this->assertNotNull($hit);
$this->assertSame($phrase, $hit['candidate']);
$this->assertTrue($hit['confirmed']);
}
#[Test]
public function slides_past_ui_card_when_checksum_needs_the_last_word(): void
{
$phrase = 'across artwork strike near sadness federal group soccer alert equal valley ask';
$ocr = 'a SS KS 备份 助 记 词 手 抄 备份 KeyPal Card at 助 记 词 (12 个 单词 ) '
.'across artwork strike near sadness federal group soccer alert 10 equal valley ask';
$hit = (new MnemonicDetector)->detect($ocr);
$this->assertNotNull($hit);
$this->assertSame($phrase, $hit['candidate']);
$this->assertTrue($hit['confirmed']);
$this->assertTrue(Bip44::isUsableMnemonic($phrase));
$this->assertFalse(Bip44::isUsableMnemonic(
'card across artwork strike near sadness federal group soccer alert equal valley'
));
}
}