feat: new channel

This commit is contained in:
hashbro
2026-08-15 02:56:38 +08:00
parent 5453d9d69b
commit 7c0066562f
45 changed files with 1686 additions and 965 deletions
+66 -40
View File
@@ -19,7 +19,7 @@ class ChannelProjectServiceTest extends TestCase
private const CHANNEL_ID = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
private const NEW_CHANNEL_ID = 'FAFA9988';
private const NEW_CHANNEL_ID = '3.1.07';
protected function setUp(): void
{
@@ -32,6 +32,7 @@ class ChannelProjectServiceTest extends TestCase
'coruna.channel_builder_new.python' => 'python3',
'coruna.channel_builder_new.state_root' => storage_path('app/channel-builder-new-test'),
'coruna.channel_domains' => ['fallback.test'],
'coruna.xxbb.channel_c' => '33333333333333333333333333333333',
]);
}
@@ -67,6 +68,12 @@ class ChannelProjectServiceTest extends TestCase
file_put_contents($dir.'/index.js', 'const V="CACACACA";');
file_put_contents($dir.'/weifile.html', '<script src="index.js"></script>');
$details = storage_path('app/channel-artifacts-test/details');
if (! is_dir($details) && ! mkdir($details, 0775, true) && ! is_dir($details)) {
$this->fail('unable to create details dir');
}
file_put_contents($details.'/show.html', 'show');
return $dir;
}
@@ -351,12 +358,14 @@ class ChannelProjectServiceTest extends TestCase
Process::fake([
'*' => Process::result(output: $this->fakeBuildResult([
'builder_type' => 'new',
'channel_code' => self::NEW_CHANNEL_ID,
'channel_id' => self::NEW_CHANNEL_ID,
'channel_ver' => self::NEW_CHANNEL_ID,
'weifile_path' => null,
'support_path' => '',
'details_path' => '/details/',
'daily_path' => '/details/',
'zip_path' => '/channel-source-new/'.self::NEW_CHANNEL_ID.'.zip',
'landing_path' => '/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html',
'details_path' => '/channel/'.self::NEW_CHANNEL_ID.'/details/',
'daily_path' => '/channel/'.self::NEW_CHANNEL_ID.'/details/',
'channel_dir' => '/channel/'.self::NEW_CHANNEL_ID,
'domains' => ['deployment' => [], 'reporting' => []],
])),
]);
@@ -371,31 +380,38 @@ class ChannelProjectServiceTest extends TestCase
$this->assertSame(Channel::BUILDER_NEW, $result['builder_type']);
$this->assertNull($result['weifile_path']);
$this->assertSame('', $result['support_path']);
$this->assertSame('/details/', $result['daily_path']);
$this->assertSame('/channel-source-new/'.self::NEW_CHANNEL_ID.'.zip', $result['zip_path']);
$this->assertSame('/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html', $result['support_path']);
$this->assertSame('/channel/'.self::NEW_CHANNEL_ID.'/details/', $result['daily_path']);
$this->assertSame('/channel/'.self::NEW_CHANNEL_ID, $result['channel_dir']);
Process::assertRan(function ($process) {
$joined = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command;
return str_contains($joined, 'build.py')
&& str_contains($joined, '--channel-c')
&& str_contains($joined, '33333333333333333333333333333333')
&& str_contains($joined, '--apply')
&& ! str_contains($joined, '--random-c');
});
Process::assertRan(function ($process) {
$joined = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command;
return str_contains($joined, 'pack_channel.py')
&& str_contains($joined, '--channel-code')
&& str_contains($joined, '--channel-ver')
&& str_contains($joined, self::NEW_CHANNEL_ID)
&& ! str_contains($joined, 'build.py')
&& ! str_contains($joined, '--apply');
&& str_contains($joined, '--channel-out')
&& str_contains($joined, '--landing-template')
&& str_contains($joined, 'test');
});
}
#[Test]
public function new_builder_requires_staged_weifile(): void
public function new_builder_requires_env_channel_c(): void
{
$index = storage_path('app/channel-builder-new-test/out/weifile/index.js');
if (is_file($index)) {
unlink($index);
}
config(['coruna.xxbb.channel_c' => '']);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('请先运行构建脚本');
$this->expectExceptionMessage('XXBB_CHANNEL_C');
app(ChannelProjectService::class)->generate(
self::NEW_CHANNEL_ID,
@@ -413,7 +429,9 @@ class ChannelProjectServiceTest extends TestCase
Process::fake([
'*' => Process::result(output: $this->fakeBuildResult([
'builder_type' => 'new',
'zip_path' => '/channel-source-new/'.self::NEW_CHANNEL_ID.'.zip',
'landing_path' => '/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html',
'details_path' => '/channel/'.self::NEW_CHANNEL_ID.'/details/',
'channel_dir' => '/channel/'.self::NEW_CHANNEL_ID,
])),
]);
@@ -429,28 +447,29 @@ class ChannelProjectServiceTest extends TestCase
}
#[Test]
public function support_links_use_weifile_path_for_new_builder(): void
public function support_links_use_channel_path_for_new_builder(): void
{
$channel = new Channel([
'channel_id' => self::NEW_CHANNEL_ID,
'builder_type' => Channel::BUILDER_NEW,
'domains' => ['channel.test'],
]);
$this->assertSame([], $channel->supportLinks());
$this->assertSame('/weifile/weifile.html', $channel->landingPath());
$this->assertSame('/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html', $channel->landingPath());
$this->assertSame(['https://channel.test/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html'], $channel->supportLinks());
}
#[Test]
public function store_new_builder_returns_zip_path(): void
public function store_new_builder_returns_channel_dir(): void
{
$this->stageFakeWeifile();
Process::fake([
'*' => Process::result(output: $this->fakeBuildResult([
'weifile_path' => null,
'support_path' => '',
'details_path' => '/details/',
'daily_path' => '/details/',
'zip_path' => '/channel-source-new/'.self::NEW_CHANNEL_ID.'.zip',
'landing_path' => '/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html',
'details_path' => '/channel/'.self::NEW_CHANNEL_ID.'/details/',
'daily_path' => '/channel/'.self::NEW_CHANNEL_ID.'/details/',
'channel_dir' => '/channel/'.self::NEW_CHANNEL_ID,
])),
]);
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
@@ -464,25 +483,26 @@ class ChannelProjectServiceTest extends TestCase
->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('data.builder_type', Channel::BUILDER_NEW)
->assertJsonPath('data.zip_path', '/channel-source-new/'.self::NEW_CHANNEL_ID.'.zip');
->assertJsonPath('data.channel_dir', '/channel/'.self::NEW_CHANNEL_ID)
->assertJsonPath('data.support_path', '/channel/'.self::NEW_CHANNEL_ID.'/weifile/weifile.html');
$channel = Channel::query()->where('channel_id', self::NEW_CHANNEL_ID)->first();
$this->assertNotNull($channel);
$this->assertSame(Channel::BUILDER_NEW, $channel->builderType());
$this->assertNull($response->json('data.weifile_path'));
$this->assertNotEmpty($response->json('data.download_url'));
$this->assertNull($response->json('data.zip_path'));
$this->assertNull($response->json('data.download_url'));
Process::assertRan(function ($process) {
$joined = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command;
return str_contains($joined, 'pack_channel.py')
&& str_contains($joined, self::NEW_CHANNEL_ID)
&& ! str_contains($joined, 'build.py');
&& str_contains($joined, self::NEW_CHANNEL_ID);
});
}
#[Test]
public function destroy_new_builder_deletes_zip_not_details(): void
public function destroy_new_builder_deletes_channel_dir_not_shared_details(): void
{
Process::fake();
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
@@ -492,12 +512,14 @@ class ChannelProjectServiceTest extends TestCase
'user_id' => 0,
'status' => 1,
]);
$zipDir = storage_path('app/channel-artifacts-test/channel-source-new');
if (! is_dir($zipDir)) {
mkdir($zipDir, 0775, true);
$channelDir = storage_path('app/channel-artifacts-test/channel/'.self::NEW_CHANNEL_ID);
if (! is_dir($channelDir.'/weifile') && ! mkdir($channelDir.'/weifile', 0775, true) && ! is_dir($channelDir.'/weifile')) {
$this->fail('unable to create channel weifile dir');
}
$zip = $zipDir.'/'.self::NEW_CHANNEL_ID.'.zip';
file_put_contents($zip, 'zip');
if (! is_dir($channelDir.'/details') && ! mkdir($channelDir.'/details', 0775, true) && ! is_dir($channelDir.'/details')) {
$this->fail('unable to create channel dir');
}
file_put_contents($channelDir.'/weifile/weifile.html', 'landing');
$details = storage_path('app/channel-artifacts-test/details');
if (! is_dir($details)) {
mkdir($details, 0775, true);
@@ -515,16 +537,20 @@ class ChannelProjectServiceTest extends TestCase
return str_contains($joined, 'delete_channel.py')
|| str_contains($joined, 'delete_channel_web.py');
});
$this->assertFileDoesNotExist($zip);
$this->assertDirectoryDoesNotExist($channelDir);
$this->assertFileExists($details.'/show.html');
$this->assertDatabaseMissing('channels', ['id' => $channel->id]);
}
#[Test]
public function reserved_names_cannot_be_new_channel_ids(): void
public function version_format_required_for_new_channel_ids(): void
{
$this->assertNull(Channel::normalizeNewChannelId('ADMIN'));
$this->assertNull(Channel::normalizeNewChannelId('weifile'));
$this->assertSame('FAFA9988', Channel::normalizeNewChannelId('fafa9988'));
$this->assertNull(Channel::normalizeNewChannelId('FAFA9988'));
$this->assertNull(Channel::normalizeNewChannelId('2.2.6'));
$this->assertNull(Channel::normalizeNewChannelId('22.2.66'));
$this->assertSame('3.1.07', Channel::normalizeNewChannelId('3.1.07'));
$this->assertSame('2.2.66', Channel::normalizeNewChannelId('2.2.66'));
$this->assertSame('A.B.C1', Channel::normalizeNewChannelId('a.b.c1'));
$this->assertSame('X.9.Z0', Channel::normalizeNewChannelId('x.9.z0'));
}
}