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
44 lines
1.9 KiB
Go
44 lines
1.9 KiB
Go
package mcp
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestTokenStats_RecordFansOutToParent pins the fix for graph_stats's
|
|
// always-zero token_savings counter: every per-session record() must
|
|
// aggregate into the parent (process-wide) tokenStats so the shared
|
|
// default surfaces daemon-wide live activity instead of always
|
|
// reporting zero. Without the fanout, a fresh session that asked
|
|
// graph_stats first saw token_savings.calls_counted == 0 even when
|
|
// the daemon had served thousands of source-fetching calls — the
|
|
// shared default never received observations because every real call
|
|
// carried a session ID and went to the per-session counter.
|
|
func TestTokenStats_RecordFansOutToParent(t *testing.T) {
|
|
parent := &tokenStats{}
|
|
child := &tokenStats{parent: parent}
|
|
|
|
child.record(nil, "get_symbol_source", 100, 500)
|
|
child.record(nil, "smart_context", 50, 200)
|
|
|
|
require.Equal(t, int64(2), child.callCount, "child counter must record both calls")
|
|
require.Equal(t, int64(150), child.tokensReturned, "child tokens_returned must accumulate")
|
|
require.Equal(t, int64(550), child.tokensSaved, "child tokens_saved must accumulate (400+150)")
|
|
|
|
require.Equal(t, int64(2), parent.callCount, "parent must aggregate child calls")
|
|
require.Equal(t, int64(150), parent.tokensReturned, "parent tokens_returned must aggregate")
|
|
require.Equal(t, int64(550), parent.tokensSaved, "parent tokens_saved must aggregate")
|
|
}
|
|
|
|
// TestTokenStats_RootHasNoParent confirms the recursion floor: the
|
|
// process-wide root tokenStats (constructed by NewServer) must not
|
|
// have a parent pointer, otherwise record() would loop or push into
|
|
// an unrelated counter.
|
|
func TestTokenStats_RootHasNoParent(t *testing.T) {
|
|
root := &tokenStats{}
|
|
root.record(nil, "get_symbol_source", 100, 500)
|
|
require.Equal(t, int64(1), root.callCount)
|
|
require.Nil(t, root.parent, "root counter must keep parent nil")
|
|
}
|