Files
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

821 lines
28 KiB
Go

package builtinapps
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
workspacebiz "github.com/tutti-os/tutti/services/tuttid/biz/workspace"
)
func TestCatalogLoadsRemoteAppsFromFile(t *testing.T) {
t.Setenv(remoteCatalogURLEnv, "")
remoteApp := remoteCatalogAppForVersionTest("remote-design", "0.1.0")
remoteApp.Manifest.Name = "Remote Design"
remoteApp.Manifest.Description = "Design workspace"
remoteApp.Manifest.Icon.Src = "icon.svg"
remoteApp.Manifest.Runtime.HealthcheckPath = "/healthz"
remoteApp.Distribution.ArtifactURL = "https://cdn.example.test/apps/remote-design/remote-design.zip"
remoteApp.Distribution.ArtifactSHA256 = "abc123"
remoteApp.Distribution.IconURL = "https://cdn.example.test/apps/remote-design/icon.svg"
remoteApp.Localizations = []workspacebiz.AppManifestLocalization{{
Locale: "zh-CN", Name: "远程设计", Description: "设计工作区", Tags: []string{"设计", "工作区"},
}}
setRemoteCatalogFileForTest(t, remoteCatalogDocumentForTest(remoteApp))
apps, err := Catalog()
if err != nil {
t.Fatalf("Catalog() error = %v", err)
}
app := findCatalogAppForTest(apps, "remote-design")
if app == nil {
t.Fatalf("remote app missing from catalog: %#v", apps)
}
if app.Distribution.Kind != DistributionRemote || app.Distribution.IconURL == "" {
t.Fatalf("remote app distribution = %#v", app.Distribution)
}
if len(app.Localizations) != 1 || app.Localizations[0].Locale != "zh-CN" || app.Localizations[0].Name != "远程设计" {
t.Fatalf("remote app localizations = %#v", app.Localizations)
}
}
func TestCatalogReturnsExplicitFileErrors(t *testing.T) {
t.Setenv(remoteCatalogURLEnv, "")
t.Setenv(remoteCatalogFileEnv, filepath.Join(t.TempDir(), "missing-catalog.json"))
if _, err := Catalog(); err == nil {
t.Fatal("Catalog() error = nil, want missing catalog file error")
}
}
func TestCatalogLoadsRemoteAppsFromURL(t *testing.T) {
t.Setenv(remoteCatalogFileEnv, "")
server := remoteCatalogServerForTest(t, remoteCatalogDocumentForTest(remoteCatalogAppForVersionTest("remote-tool", "1.2.3")))
t.Setenv(remoteCatalogURLEnv, server.URL+"/catalog.json")
snapshot, err := Snapshot()
if err != nil {
t.Fatalf("Snapshot() error = %v", err)
}
if snapshot.RemoteCatalog.Status != RemoteCatalogLoadStatusLoading {
t.Fatalf("remote catalog status = %q, want loading", snapshot.RemoteCatalog.Status)
}
if app := findCatalogAppForTest(snapshot.Apps, "remote-tool"); app != nil {
t.Fatalf("remote app loaded synchronously: %#v", app)
}
snapshot = waitForCatalogStatusForTest(t, RemoteCatalogLoadStatusReady)
if snapshot.RemoteCatalog.LastError != "" {
t.Fatalf("remote catalog last error = %q, want empty", snapshot.RemoteCatalog.LastError)
}
if app := findCatalogAppForTest(snapshot.Apps, "remote-tool"); app == nil {
t.Fatalf("remote app missing from catalog: %#v", snapshot.Apps)
}
}
func TestCatalogRetriesRemoteURLFetch(t *testing.T) {
disableRemoteCatalogRetrySleepForTest(t)
t.Setenv(remoteCatalogFileEnv, "")
var requests atomic.Int32
data := remoteCatalogDataForTest(t, remoteCatalogDocumentForTest(remoteCatalogAppForVersionTest("retry-tool", "1.2.3")))
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
if requests.Add(1) < remoteCatalogFetchAttempts {
http.Error(writer, "unavailable", http.StatusServiceUnavailable)
return
}
writeRemoteCatalogResponseForTest(writer, data)
}))
t.Cleanup(server.Close)
t.Setenv(remoteCatalogURLEnv, server.URL+"/catalog.json")
snapshot, err := snapshot(true, CatalogHost{})
if err != nil {
t.Fatalf("snapshot(true) error = %v", err)
}
if snapshot.RemoteCatalog.Status != RemoteCatalogLoadStatusLoading {
t.Fatalf("remote catalog status = %q, want loading", snapshot.RemoteCatalog.Status)
}
snapshot = waitForCatalogStatusForTest(t, RemoteCatalogLoadStatusReady)
if got := requests.Load(); got != remoteCatalogFetchAttempts {
t.Fatalf("remote catalog requests = %d, want %d", got, remoteCatalogFetchAttempts)
}
if app := findCatalogAppForTest(snapshot.Apps, "retry-tool"); app == nil {
t.Fatalf("retry-loaded remote app missing from catalog: %#v", snapshot.Apps)
}
}
func TestRefreshRemoteCatalogAndWaitReturnsReadyCatalog(t *testing.T) {
t.Setenv(remoteCatalogFileEnv, "")
releaseResponse := make(chan struct{})
var releaseOnce sync.Once
release := func() {
releaseOnce.Do(func() {
close(releaseResponse)
})
}
t.Cleanup(release)
data := remoteCatalogDataForTest(t, remoteCatalogDocumentForTest(remoteCatalogAppForVersionTest("wait-tool", "1.2.3")))
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
<-releaseResponse
writeRemoteCatalogResponseForTest(writer, data)
}))
t.Cleanup(server.Close)
t.Setenv(remoteCatalogURLEnv, server.URL+"/catalog.json")
resultCh := make(chan struct {
snapshot CatalogSnapshot
err error
}, 1)
go func() {
snapshot, err := RefreshRemoteCatalogAndWaitForHost(context.Background(), CatalogHost{})
resultCh <- struct {
snapshot CatalogSnapshot
err error
}{snapshot: snapshot, err: err}
}()
select {
case result := <-resultCh:
t.Fatalf("RefreshRemoteCatalogAndWaitForHost() returned before response: %#v", result)
case <-time.After(50 * time.Millisecond):
}
release()
select {
case result := <-resultCh:
if result.err != nil {
t.Fatalf("RefreshRemoteCatalogAndWaitForHost() error = %v", result.err)
}
if result.snapshot.RemoteCatalog.Status != RemoteCatalogLoadStatusReady {
t.Fatalf("remote catalog status = %q, want ready", result.snapshot.RemoteCatalog.Status)
}
if app := findCatalogAppForTest(result.snapshot.Apps, "wait-tool"); app == nil {
t.Fatalf("remote app missing from waited snapshot: %#v", result.snapshot.Apps)
}
case <-time.After(time.Second):
t.Fatal("RefreshRemoteCatalogAndWaitForHost() did not return after response")
}
}
func TestRefreshRemoteCatalogAndWaitReturnsFailedCatalog(t *testing.T) {
disableRemoteCatalogRetrySleepForTest(t)
t.Setenv(remoteCatalogFileEnv, "")
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
http.Error(writer, "unavailable", http.StatusServiceUnavailable)
}))
t.Cleanup(server.Close)
t.Setenv(remoteCatalogURLEnv, server.URL+"/catalog.json")
snapshot, err := RefreshRemoteCatalogAndWaitForHost(context.Background(), CatalogHost{})
if err != nil {
t.Fatalf("RefreshRemoteCatalogAndWaitForHost() error = %v", err)
}
if snapshot.RemoteCatalog.Status != RemoteCatalogLoadStatusFailed {
t.Fatalf("remote catalog status = %q, want failed", snapshot.RemoteCatalog.Status)
}
if snapshot.RemoteCatalog.LastError == "" {
t.Fatal("remote catalog last error is empty, want failure details")
}
}
func TestCatalogReturnsEmbeddedCatalogWhenRemoteURLFails(t *testing.T) {
disableRemoteCatalogRetrySleepForTest(t)
t.Setenv(remoteCatalogFileEnv, "")
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
http.Error(writer, "unavailable", http.StatusServiceUnavailable)
}))
t.Cleanup(server.Close)
t.Setenv(remoteCatalogURLEnv, server.URL+"/catalog.json")
apps, err := Catalog()
if err != nil {
t.Fatalf("Catalog() error = %v", err)
}
if app := findCatalogAppForTest(apps, "tutti-onboarding"); app == nil {
t.Fatalf("Catalog() apps = %#v, want embedded onboarding", apps)
}
snapshot := waitForCatalogStatusForTest(t, RemoteCatalogLoadStatusFailed)
if snapshot.RemoteCatalog.LastError == "" {
t.Fatal("remote catalog last error is empty, want failure details")
}
if app := findCatalogAppForTest(snapshot.Apps, "tutti-onboarding"); app == nil {
t.Fatalf("failed snapshot apps = %#v, want embedded onboarding", snapshot.Apps)
}
}
func TestEmbeddedOnboardingArchiveMatchesCatalog(t *testing.T) {
app := findCatalogAppForTest(embeddedCatalog(), "tutti-onboarding")
if app == nil {
t.Fatal("embedded catalog missing tutti-onboarding")
}
if app.Distribution.Kind != DistributionEmbeddedArchive {
t.Fatalf("onboarding distribution kind = %q, want embedded-archive", app.Distribution.Kind)
}
manifestData, err := os.ReadFile(filepath.Join("tutti-onboarding", "tutti-package", "tutti.app.json"))
if err != nil {
t.Fatalf("read source manifest: %v", err)
}
sourceManifest, _, err := workspacebiz.ParseAppManifestJSON(manifestData)
if err != nil {
t.Fatalf("parse source manifest: %v", err)
}
assertCatalogManifestMatchesSource(t, app.Manifest, sourceManifest)
archiveData, err := files.ReadFile(app.Distribution.EmbeddedArtifactPath)
if err != nil {
t.Fatalf("read embedded archive %q: %v", app.Distribution.EmbeddedArtifactPath, err)
}
archive, err := zip.NewReader(bytes.NewReader(archiveData), int64(len(archiveData)))
if err != nil {
t.Fatalf("open embedded archive: %v", err)
}
requireZipEntryForTest(t, archive, "tutti.app.json")
requireZipEntryForTest(t, archive, "tutti.cli.json")
requireZipEntryForTest(t, archive, "tutti-guide.md")
requireZipEntryForTest(t, archive, "bootstrap.sh")
requireZipEntryForTest(t, archive, "dist/index.html")
requireZipEntryForTest(t, archive, "bin/darwin-arm64/tutti-onboarding-server")
requireZipEntryForTest(t, archive, "bin/darwin-amd64/tutti-onboarding-server")
requireZipEntryForTest(t, archive, "bin/windows-amd64/tutti-onboarding-server.exe")
archiveManifestData := readZipEntryForTest(t, archive, "tutti.app.json")
archiveManifest, _, err := workspacebiz.ParseAppManifestJSON(archiveManifestData)
if err != nil {
t.Fatalf("parse archive manifest: %v", err)
}
assertCatalogManifestMatchesSource(t, app.Manifest, archiveManifest)
}
func TestRemoteCatalogURLDefaultsToPublishedCatalog(t *testing.T) {
previousValue, hadPreviousValue := os.LookupEnv(remoteCatalogURLEnv)
t.Cleanup(func() {
if hadPreviousValue {
_ = os.Setenv(remoteCatalogURLEnv, previousValue)
return
}
_ = os.Unsetenv(remoteCatalogURLEnv)
})
_ = os.Unsetenv(remoteCatalogURLEnv)
if got := remoteCatalogURL(); got != defaultRemoteCatalogURL {
t.Fatalf("remoteCatalogURL() = %q, want %q", got, defaultRemoteCatalogURL)
}
_ = os.Setenv(remoteCatalogURLEnv, "")
if got := remoteCatalogURL(); got != "" {
t.Fatalf("remoteCatalogURL() with empty override = %q, want empty", got)
}
override := "https://cdn.example.test/catalog.json"
_ = os.Setenv(remoteCatalogURLEnv, override)
if got := remoteCatalogURL(); got != override {
t.Fatalf("remoteCatalogURL() with override = %q, want %q", got, override)
}
}
func TestMergeCatalogsKeepsEmbeddedAppBeforeRemoteAppWithSameID(t *testing.T) {
embedded := embeddedCatalog()
if len(embedded) == 0 {
t.Fatal("embedded catalog is empty")
}
remoteManifest := embedded[0].Manifest
remoteManifest.Version = "9.9.9"
apps, err := mergeCatalogs(embedded, []App{
{
Manifest: remoteManifest,
Distribution: Distribution{
Kind: DistributionRemote,
ArtifactURL: "https://cdn.example.test/tutti-onboarding.zip",
ArtifactSHA256: "abc123",
IconURL: "https://cdn.example.test/tutti-onboarding.webp",
},
},
})
if err != nil {
t.Fatalf("mergeCatalogs() error = %v", err)
}
app := findCatalogAppForTest(apps, "tutti-onboarding")
if app == nil {
t.Fatalf("merged apps = %#v, want embedded onboarding", apps)
}
if app.Manifest.Version != "0.1.0" || app.Distribution.Kind != DistributionEmbeddedArchive {
t.Fatalf("merged onboarding = %#v, want embedded version", app)
}
}
func TestCatalogLoadsRemoteAutomationWhenProvidedByCatalog(t *testing.T) {
t.Setenv(remoteCatalogURLEnv, "")
manifest := remoteCatalogManifestForTest("automation")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{{
Manifest: manifest,
Distribution: remoteDistribution{
Kind: string(DistributionRemote),
ArtifactURL: "https://cdn.example.test/automation.zip",
ArtifactSHA256: "abc123",
IconURL: "https://cdn.example.test/automation.png",
},
}},
}
setRemoteCatalogFileForTest(t, document)
apps, err := Catalog()
if err != nil {
t.Fatalf("Catalog() error = %v", err)
}
var matchingApps []App
for _, app := range apps {
if app.Manifest.AppID == "automation" {
matchingApps = append(matchingApps, app)
}
}
if len(matchingApps) != 1 {
t.Fatalf("automation entries = %#v, want exactly one remote entry", matchingApps)
}
if matchingApps[0].Distribution.Kind != DistributionRemote {
t.Fatalf("automation distribution = %#v, want remote", matchingApps[0].Distribution)
}
}
func TestCatalogNormalizesLegacyNodeStaticRuntimeProfile(t *testing.T) {
t.Setenv(remoteCatalogURLEnv, "")
manifest := remoteCatalogManifestForTest("legacy-node-static")
manifest.Runtime.Profile = "node-static"
setRemoteCatalogFileForTest(t, remoteCatalogDocumentForTest(remoteCatalogApp{
Manifest: manifest,
Distribution: remoteDistribution{
Kind: string(DistributionRemote),
ArtifactURL: "https://cdn.example.test/legacy-node-static.zip",
ArtifactSHA256: strings.Repeat("a", 64),
IconURL: "https://cdn.example.test/legacy-node-static.png",
},
}))
apps, err := Catalog()
if err != nil {
t.Fatalf("Catalog() error = %v", err)
}
app := findCatalogAppForTest(apps, "legacy-node-static")
if app == nil {
t.Fatal("legacy-node-static app missing from catalog")
}
if app.Manifest.Runtime.Profile != "connector-node-static" {
t.Fatalf("runtime profile = %q, want connector-node-static", app.Manifest.Runtime.Profile)
}
}
func TestParseRemoteCatalogSelectsHighestCompatibleAppVersion(t *testing.T) {
legacy := remoteCatalogAppForVersionTest("versioned-app", "1.0.0")
compatible := remoteCatalogAppForVersionTest("versioned-app", "1.1.0")
newest := remoteCatalogAppForVersionTest("versioned-app", "2.0.0")
newOnly := remoteCatalogAppForVersionTest("new-only-app", "1.0.0")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{legacy},
Compatibility: &remoteCatalogCompatibility{Apps: map[string][]remoteCatalogCompatibilityEntry{
"versioned-app": {
{MinTuttiVersion: "0.0.0", App: compatible},
{MinTuttiVersion: "0.12.0", App: newest},
},
"new-only-app": {
{MinTuttiVersion: "0.12.0", App: newOnly},
},
}},
}
data := remoteCatalogDataForTest(t, document)
legacyApps, err := parseRemoteCatalogForTuttiVersion(data, "")
if err != nil {
t.Fatalf("parse legacy catalog: %v", err)
}
if app := findCatalogAppForTest(legacyApps, "versioned-app"); app == nil || app.Manifest.Version != "1.0.0" {
t.Fatalf("legacy versioned app = %#v, want 1.0.0", app)
}
if app := findCatalogAppForTest(legacyApps, "new-only-app"); app != nil {
t.Fatalf("legacy new-only app = %#v, want omitted", app)
}
lowApps, err := parseRemoteCatalogForTuttiVersion(data, "0.11.0")
if err != nil {
t.Fatalf("parse low-version catalog: %v", err)
}
if app := findCatalogAppForTest(lowApps, "versioned-app"); app == nil || app.Manifest.Version != "1.1.0" {
t.Fatalf("low-version app = %#v, want 1.1.0", app)
}
highApps, err := parseRemoteCatalogForTuttiVersion(data, "0.12.0")
if err != nil {
t.Fatalf("parse high-version catalog: %v", err)
}
if app := findCatalogAppForTest(highApps, "versioned-app"); app == nil || app.Manifest.Version != "2.0.0" {
t.Fatalf("high-version app = %#v, want 2.0.0", app)
}
if app := findCatalogAppForTest(highApps, "new-only-app"); app == nil || app.Manifest.Version != "1.0.0" {
t.Fatalf("high-version new-only app = %#v, want 1.0.0", app)
}
}
func TestParseRemoteCatalogSelectsHighestCompatibleTuttiVersionTier(t *testing.T) {
legacy := remoteCatalogAppForVersionTest("automation", "0.1.0+f279cf1f96ab")
current := remoteCatalogAppForVersionTest("automation", "0.1.0+954ca3ea7534")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{legacy},
Compatibility: &remoteCatalogCompatibility{Apps: map[string][]remoteCatalogCompatibilityEntry{
"automation": {
{MinTuttiVersion: "0.0.0", App: legacy},
{MinTuttiVersion: "0.1.18", App: current},
},
}},
}
data := remoteCatalogDataForTest(t, document)
apps, err := parseRemoteCatalogForTuttiVersion(data, "0.2.4-rc.1-14-g5cbafc453-dirty")
if err != nil {
t.Fatalf("parse catalog: %v", err)
}
if app := findCatalogAppForTest(apps, "automation"); app == nil || app.Manifest.Version != "0.1.0+954ca3ea7534" {
t.Fatalf("automation app = %#v, want current compatibility tier", app)
}
}
func TestParseRemoteCatalogSelectsCapabilityCompatibleAppVersion(t *testing.T) {
legacy := remoteCatalogAppForVersionTest("capability-app", "1.0.0")
versionCompatible := remoteCatalogAppForVersionTest("capability-app", "1.1.0")
capabilityCompatible := remoteCatalogAppForVersionTest("capability-app", "2.0.0")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{legacy},
Compatibility: &remoteCatalogCompatibility{
Apps: map[string][]remoteCatalogCompatibilityEntry{
"capability-app": {
{MinTuttiVersion: "0.0.0", App: versionCompatible},
},
},
CapabilityApps: map[string][]remoteCatalogCapabilityEntry{
"capability-app": {
{
RequiredTuttiCapabilities: []string{"managed-model-cli-v1"},
App: capabilityCompatible,
},
},
},
},
}
data := remoteCatalogDataForTest(t, document)
legacyApps, err := parseRemoteCatalogForTuttiVersion(data, "0.12.0")
if err != nil {
t.Fatalf("parse legacy wrapper catalog: %v", err)
}
if app := findCatalogAppForTest(legacyApps, "capability-app"); app == nil || app.Manifest.Version != "1.1.0" {
t.Fatalf("legacy capability app = %#v, want 1.1.0", app)
}
unsupportedApps, err := parseRemoteCatalogForHost(data, CatalogHost{
TuttiVersion: "0.12.0",
})
if err != nil {
t.Fatalf("parse unsupported capability catalog: %v", err)
}
if app := findCatalogAppForTest(unsupportedApps, "capability-app"); app == nil || app.Manifest.Version != "1.1.0" {
t.Fatalf("unsupported capability app = %#v, want 1.1.0", app)
}
capableApps, err := parseRemoteCatalogForHost(data, CatalogHost{
TuttiVersion: "0.12.0",
Capabilities: []string{"managed-model-cli-v1"},
})
if err != nil {
t.Fatalf("parse capable catalog: %v", err)
}
if app := findCatalogAppForTest(capableApps, "capability-app"); app == nil || app.Manifest.Version != "2.0.0" {
t.Fatalf("capable app = %#v, want 2.0.0", app)
}
}
func TestParseRemoteCatalogRejectsEligibleMalformedCapabilityPayload(t *testing.T) {
legacy := remoteCatalogAppForVersionTest("capability-app", "1.0.0")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{legacy},
Compatibility: &remoteCatalogCompatibility{
Apps: map[string][]remoteCatalogCompatibilityEntry{},
CapabilityApps: map[string][]remoteCatalogCapabilityEntry{
"capability-app": {
{
RequiredTuttiCapabilities: []string{"managed-model-cli-v1"},
App: remoteCatalogApp{
Manifest: workspacebiz.AppManifest{Version: "not-a-manifest"},
},
},
},
},
},
}
data := remoteCatalogDataForTest(t, document)
if _, err := parseRemoteCatalogForHost(data, CatalogHost{
Capabilities: []string{"managed-model-cli-v1"},
}); err == nil {
t.Fatal("parse eligible malformed capability payload error = nil")
}
}
func TestParseRemoteCatalogRejectsInvalidCompatibility(t *testing.T) {
entry := remoteCatalogAppForVersionTest("versioned-app", "1.0.0")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{entry},
Compatibility: &remoteCatalogCompatibility{Apps: map[string][]remoteCatalogCompatibilityEntry{
"versioned-app": {
{MinTuttiVersion: "not-semver", App: entry},
},
}},
}
data := remoteCatalogDataForTest(t, document)
if _, err := parseRemoteCatalogForTuttiVersion(data, "0.12.0"); err == nil {
t.Fatal("parse compatibility error = nil, want invalid semver")
}
}
func TestParseRemoteCatalogIgnoresFutureCompatibilityPayload(t *testing.T) {
legacy := remoteCatalogAppForVersionTest("versioned-app", "1.0.0")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{legacy},
Compatibility: &remoteCatalogCompatibility{Apps: map[string][]remoteCatalogCompatibilityEntry{
"versioned-app": {
{
MinTuttiVersion: "99.0.0",
App: remoteCatalogApp{
Manifest: workspacebiz.AppManifest{Version: "future-invalid-payload"},
},
},
},
}},
}
data := remoteCatalogDataForTest(t, document)
apps, err := parseRemoteCatalogForTuttiVersion(data, "0.12.0")
if err != nil {
t.Fatalf("parse catalog: %v", err)
}
if app := findCatalogAppForTest(apps, "versioned-app"); app == nil || app.Manifest.Version != "1.0.0" {
t.Fatalf("versioned app = %#v, want legacy 1.0.0", app)
}
}
func TestCompareCatalogAppVersionsPrefersSemver(t *testing.T) {
t.Parallel()
valid := App{Manifest: workspacebiz.AppManifest{Version: "1.0.0"}}
invalid := App{Manifest: workspacebiz.AppManifest{Version: "beta"}}
if comparison := compareCatalogAppVersions(valid, invalid); comparison <= 0 {
t.Fatalf("valid vs invalid comparison = %d, want positive", comparison)
}
if comparison := compareCatalogAppVersions(invalid, valid); comparison >= 0 {
t.Fatalf("invalid vs valid comparison = %d, want negative", comparison)
}
}
func remoteCatalogAppForVersionTest(appID string, version string) remoteCatalogApp {
manifest := remoteCatalogManifestForTest(appID)
manifest.Version = version
return remoteCatalogApp{
Manifest: manifest,
Distribution: remoteDistribution{
Kind: string(DistributionRemote),
ArtifactURL: "https://cdn.example.test/" + appID + "/" + version + ".zip",
ArtifactSHA256: strings.Repeat("a", 64),
IconURL: "https://cdn.example.test/" + appID + "/" + version + ".png",
},
}
}
func remoteCatalogDocumentForTest(apps ...remoteCatalogApp) remoteCatalogDocument {
return remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: apps,
}
}
func remoteCatalogDataForTest(t *testing.T, document remoteCatalogDocument) []byte {
t.Helper()
data, err := json.Marshal(document)
if err != nil {
t.Fatalf("marshal catalog: %v", err)
}
return data
}
func setRemoteCatalogFileForTest(t *testing.T, document remoteCatalogDocument) {
t.Helper()
catalogPath := filepath.Join(t.TempDir(), "catalog.json")
if err := os.WriteFile(catalogPath, remoteCatalogDataForTest(t, document), 0o644); err != nil {
t.Fatalf("write catalog: %v", err)
}
t.Setenv(remoteCatalogFileEnv, catalogPath)
}
func remoteCatalogServerForTest(t *testing.T, document remoteCatalogDocument) *httptest.Server {
t.Helper()
data := remoteCatalogDataForTest(t, document)
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
writeRemoteCatalogResponseForTest(writer, data)
}))
t.Cleanup(server.Close)
return server
}
func writeRemoteCatalogResponseForTest(writer http.ResponseWriter, data []byte) {
writer.Header().Set("Content-Type", "application/json")
_, _ = writer.Write(data)
}
func TestParseRemoteCatalogRequiresIconURLAndManifestIcon(t *testing.T) {
manifest := remoteCatalogManifestForTest("remote-tool")
document := remoteCatalogDocument{
SchemaVersion: remoteCatalogSchemaVersionV1,
Apps: []remoteCatalogApp{{
Manifest: manifest,
Distribution: remoteDistribution{
Kind: string(DistributionRemote),
ArtifactURL: "https://cdn.example.test/remote-tool.zip",
ArtifactSHA256: "abc123",
},
}},
}
data := remoteCatalogDataForTest(t, document)
if _, err := parseRemoteCatalog(data); err == nil {
t.Fatal("parseRemoteCatalog() error = nil, want missing iconUrl error")
}
manifest.Icon = workspacebiz.AppManifestIcon{}
document.Apps[0].Manifest = manifest
document.Apps[0].Distribution.IconURL = "https://cdn.example.test/icon.png"
data = remoteCatalogDataForTest(t, document)
if _, err := parseRemoteCatalog(data); err == nil {
t.Fatal("parseRemoteCatalog() error = nil, want missing manifest icon error")
}
}
func assertCatalogManifestMatchesSource(t *testing.T, catalog workspacebiz.AppManifest, source workspacebiz.AppManifest) {
t.Helper()
if catalog.SchemaVersion != source.SchemaVersion {
t.Fatalf("schemaVersion = %q, want %q", catalog.SchemaVersion, source.SchemaVersion)
}
if catalog.AppID != source.AppID {
t.Fatalf("appId = %q, want %q", catalog.AppID, source.AppID)
}
if catalog.Version != source.Version {
t.Fatalf("version = %q, want %q", catalog.Version, source.Version)
}
if catalog.Name != source.Name {
t.Fatalf("name = %q, want %q", catalog.Name, source.Name)
}
if catalog.Description != source.Description {
t.Fatalf("description = %q, want %q", catalog.Description, source.Description)
}
if !reflect.DeepEqual(catalog.Icon, source.Icon) {
t.Fatalf("icon = %#v, want %#v", catalog.Icon, source.Icon)
}
if !reflect.DeepEqual(catalog.Runtime, source.Runtime) {
t.Fatalf("runtime = %#v, want %#v", catalog.Runtime, source.Runtime)
}
if !reflect.DeepEqual(catalog.CLI, source.CLI) {
t.Fatalf("cli = %#v, want %#v", catalog.CLI, source.CLI)
}
if !reflect.DeepEqual(catalog.Window, source.Window) {
t.Fatalf("window = %#v, want %#v", catalog.Window, source.Window)
}
if !reflect.DeepEqual(catalog.Launch, source.Launch) {
t.Fatalf("launch = %#v, want %#v", catalog.Launch, source.Launch)
}
if !reflect.DeepEqual(catalog.LocalizationInfo, source.LocalizationInfo) {
t.Fatalf("localizationInfo = %#v, want %#v", catalog.LocalizationInfo, source.LocalizationInfo)
}
if !reflect.DeepEqual(catalog.Author, source.Author) {
t.Fatalf("author = %#v, want %#v", catalog.Author, source.Author)
}
if !reflect.DeepEqual(catalog.Authors, source.Authors) {
t.Fatalf("authors = %#v, want %#v", catalog.Authors, source.Authors)
}
if !reflect.DeepEqual(catalog.Source, source.Source) {
t.Fatalf("source = %#v, want %#v", catalog.Source, source.Source)
}
if !reflect.DeepEqual(catalog.Tags, source.Tags) {
t.Fatalf("tags = %#v, want %#v", catalog.Tags, source.Tags)
}
}
func requireZipEntryForTest(t *testing.T, archive *zip.Reader, name string) {
t.Helper()
for _, file := range archive.File {
if file.Name == name {
return
}
}
t.Fatalf("zip missing %q", name)
}
func readZipEntryForTest(t *testing.T, archive *zip.Reader, name string) []byte {
t.Helper()
for _, file := range archive.File {
if file.Name != name {
continue
}
reader, err := file.Open()
if err != nil {
t.Fatalf("open zip entry %q: %v", name, err)
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("read zip entry %q: %v", name, err)
}
return data
}
t.Fatalf("zip missing %q", name)
return nil
}
func remoteCatalogManifestForTest(appID string) workspacebiz.AppManifest {
return workspacebiz.AppManifest{
SchemaVersion: workspacebiz.AppManifestSchemaVersionV1,
AppID: appID,
Version: "0.1.0",
Name: "Remote App",
Description: "Remote app",
Icon: workspacebiz.AppManifestIcon{
Type: "asset",
Src: "icon.png",
},
Runtime: workspacebiz.AppManifestRuntime{
Bootstrap: "bootstrap.sh",
HealthcheckPath: "/",
},
Authors: []workspacebiz.AppManifestAuthor{
{
Name: "Tutti Developer",
URL: "https://github.com/tutti-os",
AvatarURL: "https://github.com/tutti-os.png",
},
},
Source: &workspacebiz.AppManifestSource{
Type: "github",
URL: "https://github.com/tutti-os/remote-app",
},
}
}
func findCatalogAppForTest(apps []App, appID string) *App {
for index := range apps {
if apps[index].Manifest.AppID == appID {
return &apps[index]
}
}
return nil
}
func waitForCatalogStatusForTest(t *testing.T, status RemoteCatalogLoadStatus) CatalogSnapshot {
t.Helper()
deadline := time.Now().Add(2 * time.Second)
for {
snapshot, err := Snapshot()
if err != nil {
t.Fatalf("Snapshot() error = %v", err)
}
if snapshot.RemoteCatalog.Status == status {
return snapshot
}
if time.Now().After(deadline) {
t.Fatalf("remote catalog status = %q, want %q", snapshot.RemoteCatalog.Status, status)
}
time.Sleep(10 * time.Millisecond)
}
}
func disableRemoteCatalogRetrySleepForTest(t *testing.T) {
t.Helper()
previous := sleepRemoteCatalogRetry
sleepRemoteCatalogRetry = func(time.Duration) {}
t.Cleanup(func() {
sleepRemoteCatalogRetry = previous
})
}