Files
sub2api-cn-relay-manager/internal/provision/preview_service_test.go
phamnazage-jpg 71cbaf5fa6 test(project): achieve ≥70% package coverage across all internal packages
- store/sqlite: 75.4% (repos + db coverage)
- host/sub2api: 80.8% (httptest mock server, pure function tests)
- app: 74.2% (handler error paths, NewActionSet closures)
- pack: 72.4%
- provision: 75.2%
- access: 77.3%
- config: 94.7% (lookup mock tests)

All tests pass: build, vet, race, coverage gates.
2026-05-15 19:26:25 +08:00

88 lines
2.9 KiB
Go

package provision
import (
"context"
"reflect"
"testing"
"sub2api-cn-relay-manager/internal/host/sub2api"
)
func TestSuggestResourceNames(t *testing.T) {
provider := sampleProviderManifest()
names := SuggestResourceNames(provider)
want := ResourceNames{
Group: "crm-deepseek-group",
Channel: "crm-deepseek-channel",
Plan: "crm-deepseek-plan",
}
if !reflect.DeepEqual(names, want) {
t.Fatalf("SuggestResourceNames() = %#v, want %#v", names, want)
}
}
func TestPreviewServiceReportsCreateActionsWhenHostHasNoResources(t *testing.T) {
host := &fakePreviewHost{}
svc := NewPreviewService(host)
report, err := svc.PreviewImport(context.Background(), PreviewRequest{
Provider: sampleProviderManifest(),
Mode: ImportModeStrict,
Keys: []string{" key-1 ", "key-2", "key-1"},
})
if err != nil {
t.Fatalf("PreviewImport() error = %v", err)
}
if !reflect.DeepEqual(report.AcceptedKeys, []string{"key-1", "key-2"}) {
t.Fatalf("AcceptedKeys = %#v, want normalized deduped keys", report.AcceptedKeys)
}
if got := report.Decisions["group"]; got.Action != PreviewActionCreate {
t.Fatalf("group action = %q, want %q", got.Action, PreviewActionCreate)
}
if got := report.Decisions["channel"]; got.Action != PreviewActionCreate {
t.Fatalf("channel action = %q, want %q", got.Action, PreviewActionCreate)
}
if got := report.Decisions["plan"]; got.Action != PreviewActionCreate {
t.Fatalf("plan action = %q, want %q", got.Action, PreviewActionCreate)
}
}
func TestPreviewServiceReportsReuseAndConflict(t *testing.T) {
host := &fakePreviewHost{snapshot: sub2api.ManagedResourceSnapshot{
Groups: []sub2api.NamedResource{{ID: "group_1", Name: "crm-deepseek-group"}},
Channels: []sub2api.NamedResource{{ID: "channel_1", Name: "crm-deepseek-channel"}, {ID: "channel_2", Name: "crm-deepseek-channel"}},
Plans: []sub2api.NamedResource{{ID: "plan_1", Name: "crm-deepseek-plan"}},
}}
svc := NewPreviewService(host)
report, err := svc.PreviewImport(context.Background(), PreviewRequest{
Provider: sampleProviderManifest(),
Mode: ImportModePartial,
Keys: []string{"key-1"},
})
if err != nil {
t.Fatalf("PreviewImport() error = %v", err)
}
if got := report.Decisions["group"]; got.Action != PreviewActionReuse || got.ExistingID != "group_1" {
t.Fatalf("group decision = %#v, want reuse group_1", got)
}
if got := report.Decisions["plan"]; got.Action != PreviewActionReuse || got.ExistingID != "plan_1" {
t.Fatalf("plan decision = %#v, want reuse plan_1", got)
}
if got := report.Decisions["channel"]; got.Action != PreviewActionConflict {
t.Fatalf("channel decision = %#v, want conflict", got)
}
}
type fakePreviewHost struct {
snapshot sub2api.ManagedResourceSnapshot
}
func (f *fakePreviewHost) ListManagedResources(context.Context, sub2api.ListManagedResourcesRequest) (sub2api.ManagedResourceSnapshot, error) {
return f.snapshot, nil
}