feat(platform): add the notification inbox and deterministic match search
This commit is contained in:
1 parent
5edff21d2d
commit
0b11021b16
169 files changed
+17083
-443
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// ErrNotDemoPolicy reports a latest product policy that is not a demonstration
|
||||
// version. The operator command never builds on a calibrated version.
|
||||
var ErrNotDemoPolicy = errors.New("the latest product policy is not a demonstration version")
|
||||
|
||||
// ErrPolicyChanged reports that another publication took the next version
|
||||
// number while the section was being added. Nothing was stored; the command
|
||||
// can be run again.
|
||||
var ErrPolicyChanged = errors.New("the product policy changed during publication, run the command again")
|
||||
|
||||
// MatchingResult reports what EnsureDemoMatchingSection did. Version is the
|
||||
// version that carries the matching section: the new one when Created, the
|
||||
// latest one otherwise.
|
||||
type MatchingResult struct {
|
||||
Created bool
|
||||
Version domain.PolicyVersion
|
||||
}
|
||||
|
||||
// EnsureDemoMatchingSection makes the latest demonstration version carry the
|
||||
// demo matching section. A version that already has the section is left alone.
|
||||
// Otherwise it publishes the next version: a copy of the latest one with
|
||||
// domain.DemoMatchingPolicy added, so no other value changes. It refuses a
|
||||
// latest version that is not a demonstration version and a family without a
|
||||
// version, because the section is only an addition to a policy that exists.
|
||||
func (publisher *Publisher) EnsureDemoMatchingSection(ctx context.Context, author domain.PolicyAuthor) (MatchingResult, error) {
|
||||
const location = "Publisher.EnsureDemoMatchingSection"
|
||||
latest, err := publisher.policies.GetLatest(ctx, domain.ProductPolicyKey)
|
||||
if errors.Is(err, outbound.ErrPolicyVersionNotFound) {
|
||||
return MatchingResult{}, fmt.Errorf("%s - the product policy has no version to extend: %w", location, err)
|
||||
}
|
||||
if err != nil {
|
||||
return MatchingResult{}, fmt.Errorf("%s - read latest version: %w", location, err)
|
||||
}
|
||||
current := latest.Policy()
|
||||
if current.Matching != nil {
|
||||
return MatchingResult{Version: latest}, nil
|
||||
}
|
||||
if latest.Kind() != domain.PolicyKindDemo {
|
||||
return MatchingResult{}, fmt.Errorf("%s - latest version %d is %s: %w", location, latest.Version(), latest.Kind(), ErrNotDemoPolicy)
|
||||
}
|
||||
matching := domain.DemoMatchingPolicy()
|
||||
current.Matching = &matching
|
||||
// The new version is built on the version that was read, so it takes the
|
||||
// next number exactly once. A taken number means the base is no longer the
|
||||
// latest, and the operator decides again instead of the command retrying.
|
||||
draft, err := domain.NewPolicyVersionDraft(domain.NewPolicyVersionDraftInput{
|
||||
Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: author, Policy: current,
|
||||
})
|
||||
if err != nil {
|
||||
return MatchingResult{}, fmt.Errorf("%s - build the extended version: %w", location, err)
|
||||
}
|
||||
numbered, err := draft.WithVersion(latest.Version() + 1)
|
||||
if err != nil {
|
||||
return MatchingResult{}, fmt.Errorf("%s - number the extended version: %w", location, err)
|
||||
}
|
||||
published, err := publisher.policies.Create(ctx, numbered)
|
||||
if errors.Is(err, outbound.ErrPolicyVersionConflict) {
|
||||
return MatchingResult{}, fmt.Errorf("%s - version %d is taken: %w", location, numbered.Version(), ErrPolicyChanged)
|
||||
}
|
||||
if err != nil {
|
||||
return MatchingResult{}, fmt.Errorf("%s - store the extended version: %w", location, err)
|
||||
}
|
||||
return MatchingResult{Created: true, Version: published}, nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func seedVersion(t *testing.T, store *memoryPolicies, kind domain.PolicyKind, product domain.ProductPolicy) {
|
||||
t.Helper()
|
||||
publisher := NewPublisher(store, testClock)
|
||||
if _, err := publisher.Publish(context.Background(), PublishInput{
|
||||
Key: domain.ProductPolicyKey, Kind: kind, Author: domain.DemoPolicySeedAuthor, Policy: product,
|
||||
}); err != nil {
|
||||
t.Fatalf("Publish() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDemoMatchingSectionAddsOnlyTheSection(t *testing.T) {
|
||||
store := &memoryPolicies{}
|
||||
seedVersion(t, store, domain.PolicyKindDemo, domain.DemoProductPolicyV2())
|
||||
publisher := NewPublisher(store, testClock)
|
||||
|
||||
result, err := publisher.EnsureDemoMatchingSection(context.Background(), "operator:night")
|
||||
if err != nil || !result.Created || result.Version.Version() != 2 {
|
||||
t.Fatalf("EnsureDemoMatchingSection() = %+v, %v, want version 2 created", result, err)
|
||||
}
|
||||
extended := result.Version.Policy()
|
||||
want := domain.DemoProductPolicyV2()
|
||||
if extended.Matching == nil || extended.Support == nil || extended.Assessment.Attempt == nil ||
|
||||
extended.Invitations.MaxPendingPerNeed != want.Invitations.MaxPendingPerNeed {
|
||||
t.Fatalf("extended policy = %+v, want the earlier sections kept and the matching section added", extended)
|
||||
}
|
||||
if result.Version.Kind() != domain.PolicyKindDemo || string(result.Version.Author()) != "operator:night" {
|
||||
t.Fatalf("version kind/author = %s/%s", result.Version.Kind(), result.Version.Author())
|
||||
}
|
||||
|
||||
again, err := publisher.EnsureDemoMatchingSection(context.Background(), "operator:night")
|
||||
if err != nil || again.Created || again.Version.Version() != 2 || len(store.versions) != 2 {
|
||||
t.Fatalf("second call = %+v, %v with %d versions, want no new version", again, err, len(store.versions))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDemoMatchingSectionRefusesWhatItCannotExtend(t *testing.T) {
|
||||
empty := &memoryPolicies{}
|
||||
if _, err := NewPublisher(empty, testClock).EnsureDemoMatchingSection(context.Background(), "operator:night"); !errors.Is(err, outbound.ErrPolicyVersionNotFound) {
|
||||
t.Fatalf("empty family error = %v, want ErrPolicyVersionNotFound", err)
|
||||
}
|
||||
calibrated := &memoryPolicies{}
|
||||
product := domain.DemoProductPolicyV2()
|
||||
product.Assessment.Calibration = domain.AssessmentCalibrationCalibrated
|
||||
seedVersion(t, calibrated, domain.PolicyKindCalibrated, product)
|
||||
if _, err := NewPublisher(calibrated, testClock).EnsureDemoMatchingSection(context.Background(), "operator:night"); !errors.Is(err, ErrNotDemoPolicy) {
|
||||
t.Fatalf("calibrated family error = %v, want ErrNotDemoPolicy", err)
|
||||
}
|
||||
if len(calibrated.versions) != 1 {
|
||||
t.Fatalf("versions = %d, want the calibrated family untouched", len(calibrated.versions))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDemoMatchingSectionDoesNotRetryAfterAConcurrentPublication(t *testing.T) {
|
||||
store := &memoryPolicies{}
|
||||
seedVersion(t, store, domain.PolicyKindDemo, domain.DemoProductPolicyV2())
|
||||
store.beforeCreate = func(domain.PolicyVersionDraft) {
|
||||
store.beforeCreate = nil
|
||||
// Another operator publishes version 2 first.
|
||||
if _, err := NewPublisher(store, testClock).Publish(context.Background(), PublishInput{
|
||||
Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: "operator:other", Policy: domain.DemoProductPolicyV2(),
|
||||
}); err != nil {
|
||||
t.Errorf("concurrent Publish() error = %v", err)
|
||||
}
|
||||
}
|
||||
_, err := NewPublisher(store, testClock).EnsureDemoMatchingSection(context.Background(), "operator:night")
|
||||
if !errors.Is(err, ErrPolicyChanged) {
|
||||
t.Fatalf("EnsureDemoMatchingSection() error = %v, want ErrPolicyChanged", err)
|
||||
}
|
||||
if len(store.versions) != 2 || store.versions[1].Policy().Matching != nil {
|
||||
t.Fatalf("versions = %d, want only the concurrent publication stored and no built-on-stale version", len(store.versions))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user