| 1 | package extension |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | // TestValidatePriorityBounds: the documented range is [-1000, 1000], |
| 8 | // inclusive. Unbounded priorities would let one contributor permanently |
| 9 | // outrank every future interceptor. |
| 10 | func TestValidatePriorityBounds(t *testing.T) { |
| 11 | for _, p := range []int{-1000, -1, 0, 1, 1000} { |
| 12 | if err := ValidatePriority(p); err != nil { |
| 13 | t.Errorf("ValidatePriority(%d) = %v, want ok", p, err) |
| 14 | } |
| 15 | } |
| 16 | for _, p := range []int{-1001, 1001, -100000, 100000} { |
| 17 | if err := ValidatePriority(p); err == nil { |
| 18 | t.Errorf("ValidatePriority(%d) succeeded, want error", p) |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // TestSortInterceptors pins the exact execution order: priority ascending, |
| 24 | // then plugin ID ascending, then per-contributor registration order |
| 25 | // ascending. The input slice must not be mutated — chains are shared state. |
| 26 | func TestSortInterceptors(t *testing.T) { |
| 27 | in := []Contribution{ |
| 28 | {Kind: KindInterceptor, ID: string(PointToolBefore), Priority: 10, Source: src(ScopePlugin, "plug-a", "plugin"), Order: 0}, |
| 29 | {Kind: KindInterceptor, ID: string(PointToolBefore), Priority: -5, Source: src(ScopePlugin, "plug-b", "plugin"), Order: 2}, |
| 30 | {Kind: KindInterceptor, ID: string(PointToolBefore), Priority: -5, Source: src(ScopePlugin, "plug-a", "plugin"), Order: 7}, |
| 31 | {Kind: KindInterceptor, ID: string(PointToolBefore), Priority: -5, Source: src(ScopePlugin, "plug-a", "plugin"), Order: 3}, |
| 32 | {Kind: KindInterceptor, ID: string(PointToolBefore), Priority: 0, Source: src(ScopeProject, "", "project"), Order: 1}, |
| 33 | } |
| 34 | got := SortInterceptors(in) |
| 35 | type key struct { |
| 36 | priority int |
| 37 | plugin string |
| 38 | order int |
| 39 | } |
| 40 | want := []key{ |
| 41 | {-5, "plug-a", 3}, |
| 42 | {-5, "plug-a", 7}, |
| 43 | {-5, "plug-b", 2}, |
| 44 | {0, "", 1}, |
| 45 | {10, "plug-a", 0}, |
| 46 | } |
| 47 | if len(got) != len(want) { |
| 48 | t.Fatalf("sorted %d entries, want %d", len(got), len(want)) |
| 49 | } |
| 50 | for i, w := range want { |
| 51 | g := got[i] |
| 52 | if g.Priority != w.priority || g.Source.PluginID != w.plugin || g.Order != w.order { |
| 53 | t.Fatalf("position %d: got (p=%d, plugin=%s, order=%d), want %+v", |
| 54 | i, g.Priority, g.Source.PluginID, g.Order, w) |
| 55 | } |
| 56 | } |
| 57 | // Input untouched: position 0 must still hold the priority-10 entry. |
| 58 | if in[0].Priority != 10 { |
| 59 | t.Fatal("SortInterceptors mutated its input") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // TestInterceptorChainAssembly: the builder groups interceptors by point and |
| 64 | // orders each chain per SortInterceptors; points with no interceptors are |
| 65 | // absent from the map. |
| 66 | func TestInterceptorChainAssembly(t *testing.T) { |
| 67 | b := NewBuilder() |
| 68 | b.AddContributor(staticContributor("i", |
| 69 | Contribution{Kind: KindInterceptor, ID: string(PointToolBefore), Priority: 20, Source: src(ScopePlugin, "pb", "plugin"), Payload: "late"}, |
| 70 | Contribution{Kind: KindInterceptor, ID: string(PointToolBefore), Priority: -1, Source: src(ScopePlugin, "pa", "plugin"), Payload: "early"}, |
| 71 | Contribution{Kind: KindInterceptor, ID: string(PointProviderResponse), Priority: 0, Source: src(ScopeProject, "", "project"), Payload: "resp"}, |
| 72 | )) |
| 73 | snap, _, err := b.Build(t.Context()) |
| 74 | if err != nil { |
| 75 | t.Fatalf("Build failed: %v", err) |
| 76 | } |
| 77 | chains := snap.InterceptorChain() |
| 78 | if len(chains) != 2 { |
| 79 | t.Fatalf("chain points = %v, want tool.before + provider.response", chains) |
| 80 | } |
| 81 | before := chains[PointToolBefore] |
| 82 | if len(before) != 2 || before[0].Payload != "early" || before[1].Payload != "late" { |
| 83 | t.Fatalf("tool.before chain = %v, want early then late", before) |
| 84 | } |
| 85 | if _, present := chains[PointSessionStart]; present { |
| 86 | t.Fatal("empty point present in chain map") |
| 87 | } |
| 88 | } |
| 89 |