MCPcopy
hub / github.com/docker/docker-agent / TestUpdateSession_LazyCreation

Function TestUpdateSession_LazyCreation

pkg/session/store_test.go:385–432  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

383}
384
385func TestUpdateSession_LazyCreation(t *testing.T) {
386 t.Parallel()
387
388 tempDB := filepath.Join(t.TempDir(), "test_lazy.db")
389
390 store, err := NewSQLiteSessionStore(t.Context(), tempDB)
391 require.NoError(t, err)
392 defer store.(*SQLiteSessionStore).Close()
393
394 testAgent := agent.New("test-agent", "test prompt")
395
396 // Create a session but don't add it to the store (simulating lazy creation)
397 session := &Session{
398 ID: "lazy-session",
399 CreatedAt: time.Now(),
400 }
401
402 // Verify session doesn't exist yet
403 _, err = store.GetSession(t.Context(), "lazy-session")
404 require.ErrorIs(t, err, ErrNotFound)
405
406 // UpdateSession creates the session (upsert) but does NOT persist messages
407 // Messages must be added separately via AddMessage
408 err = store.UpdateSession(t.Context(), session)
409 require.NoError(t, err)
410
411 // Session exists but has no messages yet
412 retrieved, err := store.GetSession(t.Context(), "lazy-session")
413 require.NoError(t, err)
414 assert.Empty(t, retrieved.Messages)
415
416 // Add messages via AddMessage (the proper way)
417 _, err = store.AddMessage(t.Context(), "lazy-session", UserMessage("Hello"))
418 require.NoError(t, err)
419
420 _, err = store.AddMessage(t.Context(), "lazy-session", NewAgentMessage(testAgent.Name(), &chat.Message{
421 Role: chat.MessageRoleAssistant,
422 Content: "Hi there!",
423 }))
424 require.NoError(t, err)
425
426 // Now the session should have messages
427 retrieved, err = store.GetSession(t.Context(), "lazy-session")
428 require.NoError(t, err)
429 assert.Len(t, retrieved.Messages, 2)
430 assert.Equal(t, "Hello", retrieved.Messages[0].Message.Message.Content)
431 assert.Equal(t, "Hi there!", retrieved.Messages[1].Message.Message.Content)
432}
433
434func TestUpdateSession_LazyCreation_InMemory(t *testing.T) {
435 t.Parallel()

Callers

nothing calls this directly

Calls 12

GetSessionMethod · 0.95
UpdateSessionMethod · 0.95
AddMessageMethod · 0.95
NewFunction · 0.92
NewSQLiteSessionStoreFunction · 0.85
NewAgentMessageFunction · 0.85
ContextMethod · 0.80
NowMethod · 0.80
UserMessageFunction · 0.70
CloseMethod · 0.65
NameMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected