MCPcopy Create free account
hub / github.com/coder/agentapi / TestStatePersistence

Function TestStatePersistence

lib/screentracker/pty_conversation_test.go:573–1194  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

571}
572
573func TestStatePersistence(t *testing.T) {
574 t.Run("SaveState creates file with correct structure", func(t *testing.T) {
575 ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
576 t.Cleanup(cancel)
577
578 // Create temp directory for state file
579 tmpDir := t.TempDir()
580 stateFile := tmpDir + "/state.json"
581
582 mClock := quartz.NewMock(t)
583 agent := &testAgent{screen: "initial"}
584 cfg := st.PTYConversationConfig{
585 Clock: mClock,
586 SnapshotInterval: 100 * time.Millisecond,
587 ScreenStabilityLength: 200 * time.Millisecond,
588 AgentIO: agent,
589 Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
590 StatePersistenceConfig: st.StatePersistenceConfig{
591 StateFile: stateFile,
592 LoadState: false,
593 SaveState: true,
594 },
595 InitialPrompt: []st.MessagePart{st.MessagePartText{Content: "test prompt"}},
596 }
597
598 c := st.NewPTY(ctx, cfg, &testEmitter{})
599 c.Start(ctx)
600
601 // Generate some conversation
602 agent.setScreen("hello")
603 advanceFor(ctx, t, mClock, 300*time.Millisecond)
604
605 // Save state
606 err := c.SaveState()
607 require.NoError(t, err)
608
609 // Read and verify the saved file
610 data, err := os.ReadFile(stateFile)
611 require.NoError(t, err)
612
613 var agentState st.AgentState
614 err = json.Unmarshal(data, &agentState)
615 require.NoError(t, err)
616
617 assert.Equal(t, 1, agentState.Version)
618 assert.Equal(t, "test prompt", agentState.InitialPrompt)
619 assert.NotEmpty(t, agentState.Messages)
620 })
621
622 t.Run("SaveState creates valid JSON", func(t *testing.T) {
623 ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
624 t.Cleanup(cancel)
625
626 tmpDir := t.TempDir()
627 stateFile := tmpDir + "/state.json"
628
629 mClock := quartz.NewMock(t)
630 fixedTime := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)

Callers

nothing calls this directly

Calls 6

setScreenMethod · 0.95
advanceForFunction · 0.85
advanceUntilFunction · 0.85
StartMethod · 0.65
SaveStateMethod · 0.65
MessagesMethod · 0.65

Tested by

no test coverage detected