MCPcopy Index your code
hub / github.com/coder/coder / TestSubscribeNoPubsubNoDuplicateMessageParts

Function TestSubscribeNoPubsubNoDuplicateMessageParts

coderd/x/chatd/chatd_test.go:4891–4938  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

4889}
4890
4891func TestSubscribeNoPubsubNoDuplicateMessageParts(t *testing.T) {
4892 t.Parallel()
4893
4894 // Use nil pubsub to force the no-pubsub path.
4895 db, _ := dbtestutil.NewDB(t)
4896 replica := newStartedTestServer(t, db, nil, uuid.New())
4897
4898 ctx := testutil.Context(t, testutil.WaitLong)
4899 user, org, model := seedChatDependencies(t, db)
4900
4901 chat, err := replica.CreateChat(ctx, chatd.CreateOptions{
4902 OrganizationID: org.ID,
4903 OwnerID: user.ID,
4904 Title: "no-dup-parts",
4905 ModelConfigID: model.ID,
4906 InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("hello")},
4907 })
4908 require.NoError(t, err)
4909
4910 // Wait for any wake-triggered processing to settle before
4911 // subscribing, so the snapshot captures the final state.
4912 // The wake signal may trigger processOnce which will fail
4913 // (no LLM configured) and set the chat to error status.
4914 // Poll until the chat reaches a terminal state (not pending
4915 // and not running), then wait for the goroutine to finish.
4916 waitForChatProcessed(ctx, t, db, chat.ID, replica)
4917
4918 snapshot, events, cancel, ok := replica.Subscribe(ctx, chat.ID, nil, 0)
4919 require.True(t, ok)
4920 t.Cleanup(cancel)
4921
4922 // Snapshot should have events (at minimum: status + message).
4923 require.NotEmpty(t, snapshot)
4924
4925 // The events channel should NOT immediately produce any
4926 // events. The snapshot already contained everything. Before
4927 // the fix, localSnapshot was replayed into the channel,
4928 // causing duplicates.
4929 require.Never(t, func() bool {
4930 select {
4931 case <-events:
4932 return true
4933 default:
4934 return false
4935 }
4936 }, 200*time.Millisecond, testutil.IntervalFast,
4937 "expected no duplicate events after snapshot")
4938}
4939
4940func TestSubscribeAfterMessageID(t *testing.T) {
4941 t.Parallel()

Callers

nothing calls this directly

Calls 11

NewDBFunction · 0.92
ContextFunction · 0.92
ChatMessageTextFunction · 0.92
newStartedTestServerFunction · 0.85
waitForChatProcessedFunction · 0.85
NotEmptyMethod · 0.80
seedChatDependenciesFunction · 0.70
NewMethod · 0.65
CreateChatMethod · 0.65
SubscribeMethod · 0.65
CleanupMethod · 0.65

Tested by

no test coverage detected