Maintain local ⭤ remote in sync with automatic AT Protocol parity for Laravel (alpha & unstable)
at dev 139 lines 4.7 kB view raw
1<?php 2 3namespace SocialDept\AtpParity\Tests\Unit\Import; 4 5use SocialDept\AtpParity\Import\ImportResult; 6use SocialDept\AtpParity\Tests\TestCase; 7 8class ImportResultTest extends TestCase 9{ 10 public function test_success_creates_completed_result(): void 11 { 12 $result = ImportResult::success( 13 did: 'did:plc:test123', 14 collection: 'app.bsky.feed.post', 15 synced: 50, 16 skipped: 5, 17 failed: 2 18 ); 19 20 $this->assertTrue($result->isSuccess()); 21 $this->assertFalse($result->isPartial()); 22 $this->assertFalse($result->isFailed()); 23 $this->assertTrue($result->completed); 24 $this->assertNull($result->error); 25 $this->assertSame(50, $result->recordsSynced); 26 $this->assertSame(5, $result->recordsSkipped); 27 $this->assertSame(2, $result->recordsFailed); 28 } 29 30 public function test_partial_creates_incomplete_result(): void 31 { 32 $result = ImportResult::partial( 33 did: 'did:plc:test123', 34 collection: 'app.bsky.feed.post', 35 synced: 100, 36 cursor: 'abc123' 37 ); 38 39 $this->assertFalse($result->isSuccess()); 40 $this->assertTrue($result->isPartial()); 41 $this->assertFalse($result->isFailed()); 42 $this->assertFalse($result->completed); 43 $this->assertSame('abc123', $result->cursor); 44 $this->assertNull($result->error); 45 } 46 47 public function test_failed_creates_error_result(): void 48 { 49 $result = ImportResult::failed( 50 did: 'did:plc:test123', 51 collection: 'app.bsky.feed.post', 52 error: 'Connection failed' 53 ); 54 55 $this->assertFalse($result->isSuccess()); 56 $this->assertFalse($result->isPartial()); // no records synced 57 $this->assertTrue($result->isFailed()); 58 $this->assertSame('Connection failed', $result->error); 59 } 60 61 public function test_failed_with_partial_progress(): void 62 { 63 $result = ImportResult::failed( 64 did: 'did:plc:test123', 65 collection: 'app.bsky.feed.post', 66 error: 'Connection lost', 67 synced: 50, 68 cursor: 'xyz789' 69 ); 70 71 $this->assertTrue($result->isFailed()); 72 $this->assertTrue($result->isPartial()); // has synced records 73 $this->assertSame(50, $result->recordsSynced); 74 $this->assertSame('xyz789', $result->cursor); 75 } 76 77 public function test_total_processed_sums_all_records(): void 78 { 79 $result = ImportResult::success( 80 did: 'did:plc:test123', 81 collection: 'app.bsky.feed.post', 82 synced: 50, 83 skipped: 10, 84 failed: 5 85 ); 86 87 $this->assertSame(65, $result->totalProcessed()); 88 } 89 90 public function test_aggregate_combines_multiple_results(): void 91 { 92 $results = [ 93 ImportResult::success('did:plc:test', 'app.bsky.feed.post', synced: 50), 94 ImportResult::success('did:plc:test', 'app.bsky.feed.like', synced: 100, failed: 5), 95 ]; 96 97 $aggregate = ImportResult::aggregate('did:plc:test', $results); 98 99 $this->assertTrue($aggregate->isSuccess()); 100 $this->assertSame('*', $aggregate->collection); 101 $this->assertSame(150, $aggregate->recordsSynced); 102 $this->assertSame(5, $aggregate->recordsFailed); 103 $this->assertNull($aggregate->error); 104 } 105 106 public function test_aggregate_marks_incomplete_when_any_incomplete(): void 107 { 108 $results = [ 109 ImportResult::success('did:plc:test', 'app.bsky.feed.post', synced: 50), 110 ImportResult::partial('did:plc:test', 'app.bsky.feed.like', synced: 100, cursor: 'abc'), 111 ]; 112 113 $aggregate = ImportResult::aggregate('did:plc:test', $results); 114 115 $this->assertFalse($aggregate->completed); 116 } 117 118 public function test_aggregate_combines_errors(): void 119 { 120 $results = [ 121 ImportResult::failed('did:plc:test', 'app.bsky.feed.post', error: 'Error 1'), 122 ImportResult::failed('did:plc:test', 'app.bsky.feed.like', error: 'Error 2'), 123 ]; 124 125 $aggregate = ImportResult::aggregate('did:plc:test', $results); 126 127 $this->assertTrue($aggregate->isFailed()); 128 $this->assertStringContainsString('app.bsky.feed.post: Error 1', $aggregate->error); 129 $this->assertStringContainsString('app.bsky.feed.like: Error 2', $aggregate->error); 130 } 131 132 public function test_aggregate_with_empty_array(): void 133 { 134 $aggregate = ImportResult::aggregate('did:plc:test', []); 135 136 $this->assertTrue($aggregate->completed); 137 $this->assertSame(0, $aggregate->recordsSynced); 138 } 139}