|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/docker/docker-agent/pkg/agent" |
| 10 | + "github.com/docker/docker-agent/pkg/hooks" |
| 11 | + "github.com/docker/docker-agent/pkg/team" |
| 12 | +) |
| 13 | + |
| 14 | +// runtimeWithRecordedSessionResume mirrors runtimeWithRecordedAgentSwitch |
| 15 | +// for the on_session_resume event. Same pattern: register a recording |
| 16 | +// builtin on the runtime's private registry post-construction so the |
| 17 | +// test can assert on dispatched input without exposing a runtime |
| 18 | +// option that production callers shouldn't reach for. |
| 19 | +func runtimeWithRecordedSessionResume(t *testing.T) (*LocalRuntime, *recordingBuiltin) { |
| 20 | + t.Helper() |
| 21 | + |
| 22 | + rb := &recordingBuiltin{} |
| 23 | + prov := &mockProvider{id: "test/mock-model", stream: &mockStream{}} |
| 24 | + a := agent.New("root", "instructions", |
| 25 | + agent.WithModel(prov), |
| 26 | + agent.WithHooks(&hooks.Config{ |
| 27 | + OnSessionResume: []hooks.Hook{{ |
| 28 | + Type: hooks.HookTypeBuiltin, |
| 29 | + Command: "test_record_session_resume", |
| 30 | + }}, |
| 31 | + }), |
| 32 | + ) |
| 33 | + tm := team.New(team.WithAgents(a)) |
| 34 | + |
| 35 | + r, err := NewLocalRuntime(tm, WithModelStore(mockModelStore{})) |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + require.NoError(t, r.hooksRegistry.RegisterBuiltin("test_record_session_resume", rb.hook)) |
| 39 | + r.buildHooksExecutors() |
| 40 | + |
| 41 | + return r, rb |
| 42 | +} |
| 43 | + |
| 44 | +// TestExecuteOnSessionResumeHooks_ForwardsLimits pins the contract: |
| 45 | +// PreviousMaxIterations and NewMaxIterations both reach the hook |
| 46 | +// verbatim. Audit pipelines compute the granted-runtime delta from |
| 47 | +// these directly without rebuilding it from the iteration counter. |
| 48 | +func TestExecuteOnSessionResumeHooks_ForwardsLimits(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + r, rb := runtimeWithRecordedSessionResume(t) |
| 52 | + a := r.CurrentAgent() |
| 53 | + require.NotNil(t, a) |
| 54 | + |
| 55 | + r.executeOnSessionResumeHooks(t.Context(), a, "session-y", 5, 15) |
| 56 | + |
| 57 | + got := rb.snapshot() |
| 58 | + require.Len(t, got, 1) |
| 59 | + in := got[0] |
| 60 | + assert.Equal(t, "session-y", in.SessionID) |
| 61 | + assert.Equal(t, 5, in.PreviousMaxIterations) |
| 62 | + assert.Equal(t, 15, in.NewMaxIterations) |
| 63 | +} |
| 64 | + |
| 65 | +// TestExecuteOnSessionResumeHooks_NoopWhenNoHookRegistered keeps the |
| 66 | +// cheap-when-unused property symmetric with on_agent_switch: no |
| 67 | +// dispatch, no panic, no error when no hook is configured. |
| 68 | +func TestExecuteOnSessionResumeHooks_NoopWhenNoHookRegistered(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + |
| 71 | + prov := &mockProvider{id: "test/mock-model", stream: &mockStream{}} |
| 72 | + a := agent.New("root", "instructions", agent.WithModel(prov)) |
| 73 | + tm := team.New(team.WithAgents(a)) |
| 74 | + |
| 75 | + r, err := NewLocalRuntime(tm, WithModelStore(mockModelStore{})) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + r.executeOnSessionResumeHooks(t.Context(), a, "s", 5, 15) |
| 79 | +} |
0 commit comments