package gio import ( "errors" "testing" "softbox.local/core/application" ) func TestCatalogEventsUpdateSnapshotAndRetainItOnFailure(t *testing.T) { shell := NewAppShell("Legacy") items := []application.CatalogListItem{{ID: "json-tool", Name: "JSON Tool", Version: "1.0.0", Category: "工具"}} if err := shell.ApplyEvent(application.Event{ Type: application.EventCatalogRefreshed, Payload: application.CatalogEvent{Type: application.EventCatalogRefreshed, Source: application.CatalogSourceCache, Items: items}, }); err != nil { t.Fatalf("ApplyEvent(refresh) error = %v", err) } if shell.model.TotalCount() != 1 || shell.catalogState != catalogStateReady { t.Fatalf("snapshot count/state = %d/%q", shell.model.TotalCount(), shell.catalogState) } if err := shell.ApplyEvent(application.Event{ Type: application.EventCatalogRejected, Payload: application.CatalogEvent{Type: application.EventCatalogRejected, FailureCode: application.CatalogFailureLoadFailed}, }); err != nil { t.Fatalf("ApplyEvent(reject) error = %v", err) } if shell.model.TotalCount() != 1 || shell.catalogState != catalogStateLoadFailed { t.Fatalf("failure cleared snapshot or state = %d/%q", shell.model.TotalCount(), shell.catalogState) } } func TestCatalogEventsExposeStableEmptyStatesAndRejectBadPayload(t *testing.T) { shell := NewAppShell("Legacy") if shell.catalogState != catalogStateLoading { t.Fatalf("initial state = %q, want loading", shell.catalogState) } if err := shell.ApplyEvent(application.Event{ Type: application.EventCatalogRejected, Payload: application.CatalogEvent{Type: application.EventCatalogRejected, FailureCode: application.CatalogFailureSourceUnconfigured}, }); err != nil { t.Fatalf("ApplyEvent(unconfigured) error = %v", err) } if shell.catalogState != catalogStateUnconfigured || shell.catalogStatusText() != "目录状态:Catalog 来源尚未配置" { t.Fatalf("unconfigured state/status = %q/%q", shell.catalogState, shell.catalogStatusText()) } if nodes := adapterContractLayout(shell, adapterContractViewport); !adapterContractHasSemantic(nodes, "Catalog 来源尚未配置") { t.Fatal("unconfigured state was not visible") } if err := shell.ApplyEvent(application.Event{ Type: application.EventCatalogRejected, Payload: application.CatalogEvent{Type: application.EventCatalogRejected, FailureCode: application.CatalogFailureLoadFailed}, }); err != nil { t.Fatalf("ApplyEvent(load failed) error = %v", err) } if nodes := adapterContractLayout(shell, adapterContractViewport); !adapterContractHasSemantic(nodes, "Catalog 加载失败") { t.Fatal("load-failed state was not visible") } if err := shell.ApplyEvent(application.Event{ Type: application.EventCatalogRefreshed, Payload: application.CatalogEvent{Type: application.EventCatalogRefreshed, Source: application.CatalogSourceRemote}, }); err != nil { t.Fatalf("ApplyEvent(empty refreshed) error = %v", err) } if nodes := adapterContractLayout(shell, adapterContractViewport); !adapterContractHasSemantic(nodes, "Catalog 暂无可显示软件") { t.Fatal("loaded-empty state was not visible") } err := shell.ApplyEvent(application.Event{Type: application.EventCatalogRefreshed, Payload: "raw error"}) if !errors.Is(err, application.ErrCatalogEventPayload) { t.Fatalf("bad payload error = %v", err) } }