109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package updater
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewPlatformAdapterSupportsAllDeclaredPlatforms(t *testing.T) {
|
|
for _, platform := range []string{"linux", "windows", "macos", "android"} {
|
|
adapter, err := NewPlatformAdapter(platform)
|
|
if err != nil {
|
|
t.Fatalf("创建 %s 适配器失败: %v", platform, err)
|
|
}
|
|
if adapter.Platform() != platform {
|
|
t.Fatalf("适配器平台错误: got %s want %s", adapter.Platform(), platform)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNativeAdapterValidatesEntrypointAndInstall(t *testing.T) {
|
|
releaseDir := t.TempDir()
|
|
entrypoint := filepath.Join(releaseDir, "demo")
|
|
if err := os.WriteFile(entrypoint, []byte("demo"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
adapter, err := NewNativeAdapter("linux")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
release := ReleasePointer{
|
|
Version: "1.0.0",
|
|
Path: releaseDir,
|
|
Entrypoint: "demo",
|
|
Platform: "linux",
|
|
Architecture: "amd64",
|
|
}
|
|
if err := adapter.Validate(release); err != nil {
|
|
t.Fatalf("入口校验失败: %v", err)
|
|
}
|
|
if err := adapter.Install(context.Background(), release); err != nil {
|
|
t.Fatalf("桌面平台安装处理失败: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAndroidAdapterRequiresHostInstallation(t *testing.T) {
|
|
adapter, err := NewPlatformAdapter("android")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = adapter.Install(context.Background(), ReleasePointer{Path: t.TempDir()})
|
|
if !errors.Is(err, ErrPlatformInstallRequired) {
|
|
t.Fatalf("Android 应返回宿主安装要求: %v", err)
|
|
}
|
|
if _, err := adapter.Launch(context.Background(), LaunchRequest{}); err == nil {
|
|
t.Fatal("Android 不应由默认适配器直接启动")
|
|
}
|
|
}
|
|
|
|
type recordingAdapter struct {
|
|
installed bool
|
|
}
|
|
|
|
func (adapter *recordingAdapter) Platform() string {
|
|
return "linux"
|
|
}
|
|
|
|
func (adapter *recordingAdapter) Validate(ReleasePointer) error {
|
|
return nil
|
|
}
|
|
|
|
func (adapter *recordingAdapter) Install(_ context.Context, _ ReleasePointer) error {
|
|
adapter.installed = true
|
|
return nil
|
|
}
|
|
|
|
func (adapter *recordingAdapter) Launch(context.Context, LaunchRequest) (*exec.Cmd, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func TestManagerInstallDelegatesToInjectedAdapter(t *testing.T) {
|
|
api := newTestClient(t)
|
|
adapter := &recordingAdapter{}
|
|
manager, err := New(Config{
|
|
Client: api,
|
|
Adapter: adapter,
|
|
PackageName: "demo-core",
|
|
RootDir: t.TempDir(),
|
|
Platform: "linux",
|
|
Architecture: "amd64",
|
|
CurrentVersion: "1.0.0",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(manager.filePath("pending.json"), []byte(`{"version":"1.1.0","path":"/tmp/release","entrypoint":"demo","platform":"linux"}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := manager.Install(context.Background()); err != nil {
|
|
t.Fatalf("注入适配器安装失败: %v", err)
|
|
}
|
|
if !adapter.installed {
|
|
t.Fatal("Manager.Install 没有调用注入的适配器")
|
|
}
|
|
}
|