Files
zzet--gortex/internal/resolver/spring_event_calls_test.go
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

77 lines
3.0 KiB
Go

package resolver
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zzet/gortex/internal/graph"
)
func springListener(g *graph.Graph, id, file, evType string) {
g.AddNode(&graph.Node{ID: id, Kind: graph.KindMethod, Name: lastSeg(id), FilePath: file, Language: "java",
Meta: map[string]any{"spring_listener_type": evType}})
}
func springPublish(g *graph.Graph, fromID, file, evType string) {
if g.GetNode(fromID) == nil {
g.AddNode(&graph.Node{ID: fromID, Kind: graph.KindMethod, Name: lastSeg(fromID), FilePath: file, Language: "java"})
}
g.AddEdge(&graph.Edge{From: fromID, To: "unresolved::*." + evType, Kind: graph.EdgeCalls, FilePath: file,
Meta: map[string]any{"via": springEventVia, "spring_event_type": evType}})
}
func synthSpringEdge(g graph.Store, from, to string) *graph.Edge {
for e := range g.EdgesByKind(graph.EdgeCalls) {
if e == nil || e.From != from || e.To != to || e.Meta == nil {
continue
}
if by, _ := e.Meta[MetaSynthesizedBy].(string); by == SynthSpringEvent {
return e
}
}
return nil
}
func TestResolveSpringEventCalls_TypeKeyedFanout(t *testing.T) {
g := graph.New()
springListener(g, "App.java::MailListener.on", "App.java", "OrderPlaced")
springListener(g, "App.java::AuditListener.onApplicationEvent", "App.java", "OrderPlaced")
springPublish(g, "App.java::OrderService.place", "App.java", "OrderPlaced")
n := ResolveSpringEventCalls(g)
require.Equal(t, 2, n, "both listeners receive an edge")
a := synthSpringEdge(g, "App.java::OrderService.place", "App.java::MailListener.on")
require.NotNil(t, a)
assert.Equal(t, ConfidenceTyped, a.Confidence)
assert.Equal(t, ProvenanceFramework, a.Meta[MetaProvenance])
assert.NotNil(t, synthSpringEdge(g, "App.java::OrderService.place", "App.java::AuditListener.onApplicationEvent"))
}
func TestResolveSpringEventCalls_SubtypeReachesBaseListener(t *testing.T) {
// A listener for the base event type receives a published subtype when
// the type-hierarchy edge is present.
g := graph.New()
springListener(g, "App.java::Base.on", "App.java", "OrderEvent")
springPublish(g, "App.java::Svc.fire", "App.java", "OrderPlaced")
// OrderPlaced extends OrderEvent.
g.AddNode(&graph.Node{ID: "App.java::OrderPlaced", Kind: graph.KindType, Name: "OrderPlaced", FilePath: "App.java"})
g.AddEdge(&graph.Edge{From: "App.java::OrderPlaced", To: "unresolved::OrderEvent", Kind: graph.EdgeExtends, FilePath: "App.java"})
n := ResolveSpringEventCalls(g)
require.Equal(t, 1, n)
assert.NotNil(t, synthSpringEdge(g, "App.java::Svc.fire", "App.java::Base.on"),
"subtype publish reaches the base-type listener")
}
func TestResolveSpringEventCalls_NoListenerStaysPlaceholder(t *testing.T) {
g := graph.New()
springListener(g, "App.java::L.on", "App.java", "SomeEvent")
springPublish(g, "App.java::S.fire", "App.java", "OtherEvent")
assert.Equal(t, 0, ResolveSpringEventCalls(g))
assert.Nil(t, synthSpringEdge(g, "App.java::S.fire", "App.java::L.on"))
}