1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Libraries;
7
8use App\Libraries\ReplayFile;
9use App\Models\Beatmap;
10use App\Models\Score\Best;
11use App\Models\User;
12use Tests\TestCase;
13
14class ReplayFileTest extends TestCase
15{
16 public function testEndChunk()
17 {
18 // known good end chunk.
19 $known = 'd75c989400000000';
20
21 $replayFile = new ReplayFile($this->knownScore());
22
23 $this->assertSame($known, bin2hex($replayFile->endChunk()));
24 }
25
26 public function testHeaderChunk()
27 {
28 // known good header.
29 $known = '0056ef33010b2039323536623735613064663334356262623237396533353438303732396631340b08492e522e5265616c0b2033393336326435393239313738633162626265373465323130646639316232399e000600000016000500000059911f00460101784200000b008014b73dec8dd508';
30
31 $replayFile = new ReplayFile($this->knownScore());
32
33 $this->assertSame($known, bin2hex($replayFile->headerChunk()));
34 }
35
36 public function testHeaderChunkDefaultVersion()
37 {
38 $known = '00bc7b33010b2039323536623735613064663334356262623237396533353438303732396631340b08492e522e5265616c0b2033393336326435393239313738633162626265373465323130646639316232399e000600000016000500000059911f00460101784200000b008014b73dec8dd508';
39
40 $replayFile = new ReplayFile($this->knownScore(true, null));
41
42 $this->assertSame($known, bin2hex($replayFile->headerChunk()));
43 }
44
45 public function testHeaderChunkMissingReplayRecord()
46 {
47 $known = '00bc7b33010b2039323536623735613064663334356262623237396533353438303732396631340b08492e522e5265616c0b2033393336326435393239313738633162626265373465323130646639316232399e000600000016000500000059911f00460101784200000b008014b73dec8dd508';
48
49 $replayFile = new ReplayFile($this->knownScore(false));
50
51 $this->assertSame($known, bin2hex($replayFile->headerChunk()));
52 }
53
54 private function knownScore($hasReplayRecord = true, ?int $version = 20180822)
55 {
56 $beatmapId = 1103293;
57 $beatmap = Beatmap::find($beatmapId) ?? Beatmap::factory()->create(['beatmap_id' => $beatmapId]);
58 $beatmap->fill(['checksum' => '9256b75a0df345bbb279e35480729f14'])->saveOrExplode();
59
60 $userId = 6258604;
61 $user = User::find($userId) ?? User::factory()->create(['user_id' => $userId]);
62 $user->fill(['username' => 'I.R.Real'])->saveOrExplode(['skipValidations' => true]);
63
64 $score = Best\Osu::make([
65 'score_id' => 2493013207,
66 'beatmap_id' => $beatmapId,
67 'user_id' => $userId,
68 'score' => 2068825,
69 'maxcombo' => 326,
70 'rank' => 'SH',
71 'count50' => 0,
72 'count100' => 6,
73 'count300' => 158,
74 'countmiss' => 0,
75 'countgeki' => 22,
76 'countkatu' => 5,
77 'perfect' => true,
78 'enabled_mods' => 17016, // [HD, HR, NC, PF] + implied mods
79 'date' => '2018-03-19 22:53:33',
80 'pp' => 68.41,
81 'replay' => true,
82 'hidden' => 0,
83 'country_acronym' => 'DE',
84 ]);
85
86 if ($hasReplayRecord) {
87 $score->setRelation('replayViewCount', $score->replayViewCount()->make([
88 'score_id' => 2493013207,
89 'play_count' => 1,
90 'version' => $version,
91 ]));
92 }
93
94 return $score;
95 }
96}