|
1 | 1 | package tui |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -65,3 +66,62 @@ func TestTLSHeader_PeerOnly(t *testing.T) { |
65 | 66 | t.Errorf("tlsHeader unexpectedly included version/cipher on peer-only state\ngot:\n%s", got) |
66 | 67 | } |
67 | 68 | } |
| 69 | + |
| 70 | +// eventScopedToPlugin must restrict both the Invocations slices and the |
| 71 | +// per-plugin Plugins map to the selected plugin, and must NOT mutate the |
| 72 | +// original event (the shallow copy aliases the slices/map otherwise). |
| 73 | +func TestEventScopedToPlugin_FiltersToSelectedPlugin(t *testing.T) { |
| 74 | + ev := &pipeline.SessionEvent{ |
| 75 | + Invocations: &pipeline.Invocations{ |
| 76 | + Inbound: []pipeline.Invocation{ |
| 77 | + {Plugin: "jwt-validation", Action: pipeline.ActionAllow}, |
| 78 | + {Plugin: "a2a-parser", Action: pipeline.ActionObserve}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + Plugins: map[string]json.RawMessage{ |
| 82 | + "jwt-validation": json.RawMessage("{}"), |
| 83 | + "a2a-parser": json.RawMessage("{}"), |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + scoped := eventScopedToPlugin(ev, "jwt-validation") |
| 88 | + |
| 89 | + if got := len(scoped.Invocations.Inbound); got != 1 { |
| 90 | + t.Fatalf("scoped inbound invocations = %d, want 1", got) |
| 91 | + } |
| 92 | + if got := scoped.Invocations.Inbound[0].Plugin; got != "jwt-validation" { |
| 93 | + t.Errorf("scoped invocation plugin = %q, want %q", got, "jwt-validation") |
| 94 | + } |
| 95 | + if got := len(scoped.Plugins); got != 1 { |
| 96 | + t.Fatalf("scoped Plugins entries = %d, want 1", got) |
| 97 | + } |
| 98 | + if _, ok := scoped.Plugins["jwt-validation"]; !ok { |
| 99 | + t.Errorf("scoped Plugins missing jwt-validation key: %v", scoped.Plugins) |
| 100 | + } |
| 101 | + if _, ok := scoped.Plugins["a2a-parser"]; ok { |
| 102 | + t.Errorf("scoped Plugins unexpectedly retained a2a-parser") |
| 103 | + } |
| 104 | + |
| 105 | + // Original event must be untouched (no aliasing of slice/map). |
| 106 | + if got := len(ev.Invocations.Inbound); got != 2 { |
| 107 | + t.Errorf("original inbound invocations mutated: = %d, want 2", got) |
| 108 | + } |
| 109 | + if got := len(ev.Plugins); got != 2 { |
| 110 | + t.Errorf("original Plugins map mutated: = %d, want 2", got) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// An empty plugin string means "no specific invocation" — the helper returns |
| 115 | +// the event unchanged (same pointer) so old whole-event behavior is preserved. |
| 116 | +func TestEventScopedToPlugin_EmptyPluginReturnsUnchanged(t *testing.T) { |
| 117 | + ev := &pipeline.SessionEvent{ |
| 118 | + Invocations: &pipeline.Invocations{ |
| 119 | + Inbound: []pipeline.Invocation{ |
| 120 | + {Plugin: "jwt-validation", Action: pipeline.ActionAllow}, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | + if got := eventScopedToPlugin(ev, ""); got != ev { |
| 125 | + t.Errorf("eventScopedToPlugin(ev, \"\") = %p, want original %p", got, ev) |
| 126 | + } |
| 127 | +} |
0 commit comments