|
| 1 | +package router_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/vagnercazarotto/verifhir-gateway/internal/channel" |
| 8 | + "github.com/vagnercazarotto/verifhir-gateway/internal/model" |
| 9 | + "github.com/vagnercazarotto/verifhir-gateway/internal/router" |
| 10 | +) |
| 11 | + |
| 12 | +func mustAdd(t *testing.T, reg *channel.Registry, ch channel.Channel) { |
| 13 | + t.Helper() |
| 14 | + if err := reg.Add(ch); err != nil { |
| 15 | + t.Fatalf("add channel %s: %v", ch.ID, err) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func samplePayload(score float64) model.RoutedPayload { |
| 20 | + return model.RoutedPayload{ |
| 21 | + Resource: model.FHIRResource{ResourceType: "Bundle", ID: "msg-1"}, |
| 22 | + Quality: model.QualityReport{Score: score}, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestRouteWithNilRegistryReturnsNil(t *testing.T) { |
| 27 | + r := router.New(nil) |
| 28 | + if got := r.Route(context.Background(), samplePayload(0.9)); got != nil { |
| 29 | + t.Fatalf("expected nil decisions, got %+v", got) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestRouteWithNoChannelsReturnsNil(t *testing.T) { |
| 34 | + r := router.New(channel.NewRegistry()) |
| 35 | + if got := r.Route(context.Background(), samplePayload(0.9)); got != nil { |
| 36 | + t.Fatalf("expected nil decisions for empty registry, got %+v", got) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestRouteSkipsDisabledChannels(t *testing.T) { |
| 41 | + reg := channel.NewRegistry() |
| 42 | + mustAdd(t, reg, channel.Channel{ |
| 43 | + ID: "off", URL: "http://example.test", Enabled: false, |
| 44 | + }) |
| 45 | + got := router.New(reg).Route(context.Background(), samplePayload(1.0)) |
| 46 | + if len(got) != 1 { |
| 47 | + t.Fatalf("decisions: got %d want 1", len(got)) |
| 48 | + } |
| 49 | + if got[0].Status != "skipped" || got[0].Reason != "channel disabled" { |
| 50 | + t.Fatalf("unexpected decision: %+v", got[0]) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestRouteEnforcesQualityThreshold(t *testing.T) { |
| 55 | + reg := channel.NewRegistry() |
| 56 | + mustAdd(t, reg, channel.Channel{ |
| 57 | + ID: "strict", URL: "http://example.test", |
| 58 | + Enabled: true, MinQualityScore: 0.8, |
| 59 | + }) |
| 60 | + got := router.New(reg).Route(context.Background(), samplePayload(0.5)) |
| 61 | + if len(got) != 1 || got[0].Status != "skipped" { |
| 62 | + t.Fatalf("expected skipped decision, got %+v", got) |
| 63 | + } |
| 64 | + if got[0].Reason != "quality score below channel threshold" { |
| 65 | + t.Fatalf("unexpected reason: %q", got[0].Reason) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestRoutePassesQualifyingMessages(t *testing.T) { |
| 70 | + reg := channel.NewRegistry() |
| 71 | + mustAdd(t, reg, channel.Channel{ |
| 72 | + ID: "primary", URL: "http://hapi:8080/fhir", |
| 73 | + Enabled: true, MinQualityScore: 0.7, |
| 74 | + }) |
| 75 | + got := router.New(reg).Route(context.Background(), samplePayload(0.9)) |
| 76 | + if len(got) != 1 || got[0].Status != "pending" { |
| 77 | + t.Fatalf("expected pending decision, got %+v", got) |
| 78 | + } |
| 79 | + if got[0].ChannelID != "primary" || got[0].URL != "http://hapi:8080/fhir" { |
| 80 | + t.Fatalf("decision metadata mismatch: %+v", got[0]) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestRouteFanOutToMultipleChannels(t *testing.T) { |
| 85 | + reg := channel.NewRegistry() |
| 86 | + mustAdd(t, reg, channel.Channel{ |
| 87 | + ID: "a", URL: "http://a", Enabled: true, MinQualityScore: 0.8, |
| 88 | + }) |
| 89 | + mustAdd(t, reg, channel.Channel{ |
| 90 | + ID: "b", URL: "http://b", Enabled: true, MinQualityScore: 0.8, |
| 91 | + }) |
| 92 | + mustAdd(t, reg, channel.Channel{ |
| 93 | + ID: "off", URL: "http://off", Enabled: false, |
| 94 | + }) |
| 95 | + |
| 96 | + got := router.New(reg).Route(context.Background(), samplePayload(0.95)) |
| 97 | + if len(got) != 3 { |
| 98 | + t.Fatalf("decisions: got %d want 3", len(got)) |
| 99 | + } |
| 100 | + |
| 101 | + pending, skipped := 0, 0 |
| 102 | + for _, d := range got { |
| 103 | + switch d.Status { |
| 104 | + case "pending": |
| 105 | + pending++ |
| 106 | + case "skipped": |
| 107 | + skipped++ |
| 108 | + } |
| 109 | + } |
| 110 | + if pending != 2 || skipped != 1 { |
| 111 | + t.Fatalf("unexpected breakdown: pending=%d skipped=%d", pending, skipped) |
| 112 | + } |
| 113 | +} |
0 commit comments