package testweknora import ( "context" "knowledge-governance/internal/platform/weknora" ) // TaskConnector is implemented by the production client. BeginTask resolves the // endpoint/key/binding exactly once, verifies the object's owning tenant through // that connection and returns a context that pins the same immutable connection // for every later call of this job (including recovery and read-back). type TaskConnector interface { BeginTask(context.Context, string) (context.Context, weknora.InstanceIdentity, error) } // IdentityProvider remains for narrow fakes/tests that only expose identity. type IdentityProvider interface { TargetIdentity(context.Context, string) (weknora.InstanceIdentity, error) } // resolveIdentity prefers BeginTask so upload/poll/validate/remove pass the // pinned context to every follow-up call. Returning the original ctx for // identity-only fakes keeps the unit seam simple; production never takes that // path. func resolveIdentity(ctx context.Context, client any, knowledgeBaseID string) (context.Context, weknora.InstanceIdentity, bool, error) { if connector, ok := client.(TaskConnector); ok { bound, identity, err := connector.BeginTask(ctx, knowledgeBaseID) if err != nil { return ctx, weknora.InstanceIdentity{}, true, err } identity.BaseURL = weknora.NormalizeInstanceURL(identity.BaseURL) return bound, identity, true, nil } provider, ok := client.(IdentityProvider) if !ok { return ctx, weknora.InstanceIdentity{}, false, nil } identity, err := provider.TargetIdentity(ctx, knowledgeBaseID) if err != nil { return ctx, weknora.InstanceIdentity{}, true, err } identity.BaseURL = weknora.NormalizeInstanceURL(identity.BaseURL) return ctx, identity, true, nil } // identityMatches requires BOTH the instance address and the owning tenant to // match. A cloned instance can expose the same knowledge base id and tenant id; // an empty stored identity is never treated as "matches anything". func identityMatches(storedURL string, storedTenant uint64, current weknora.InstanceIdentity) bool { if storedURL == "" || storedTenant == 0 { return false } return weknora.NormalizeInstanceURL(storedURL) == weknora.NormalizeInstanceURL(current.BaseURL) && storedTenant == current.TenantID }