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

Function TestDeleteOldChatFiles

coderd/database/dbpurge/dbpurge_test.go:2129–2587  ·  view source on GitHub ↗

nolint:paralleltest // It uses LockIDDBPurge.

(t *testing.T)

Source from the content-addressed store, hash-verified

2127
2128//nolint:paralleltest // It uses LockIDDBPurge.
2129func TestDeleteOldChatFiles(t *testing.T) {
2130 now := time.Date(2025, 6, 15, 12, 0, 0, 0, time.UTC)
2131
2132 // createChatFile inserts a chat file and backdates created_at.
2133 createChatFile := func(ctx context.Context, t *testing.T, db database.Store, rawDB *sql.DB, ownerID, orgID uuid.UUID, createdAt time.Time) uuid.UUID {
2134 t.Helper()
2135 row, err := db.InsertChatFile(ctx, database.InsertChatFileParams{
2136 OwnerID: ownerID,
2137 OrganizationID: orgID,
2138 Name: "test.png",
2139 Mimetype: "image/png",
2140 Data: []byte("fake-image-data"),
2141 })
2142 require.NoError(t, err)
2143 _, err = rawDB.ExecContext(ctx, "UPDATE chat_files SET created_at = $1 WHERE id = $2", createdAt, row.ID)
2144 require.NoError(t, err)
2145 return row.ID
2146 }
2147
2148 // createChat inserts a chat and optionally archives it, then
2149 // backdates updated_at to control the "archived since" window.
2150 createChat := func(ctx context.Context, t *testing.T, db database.Store, rawDB *sql.DB, ownerID, orgID, modelConfigID uuid.UUID, archived bool, updatedAt time.Time) database.Chat {
2151 t.Helper()
2152 chat := dbgen.Chat(t, db, database.Chat{
2153 OrganizationID: orgID,
2154 OwnerID: ownerID,
2155 LastModelConfigID: modelConfigID,
2156 Title: "test-chat",
2157 })
2158 if archived {
2159 _, err := db.ArchiveChatByID(ctx, chat.ID)
2160 require.NoError(t, err)
2161 }
2162 _, err := rawDB.ExecContext(ctx, "UPDATE chats SET updated_at = $1 WHERE id = $2", updatedAt, chat.ID)
2163 require.NoError(t, err)
2164 return chat
2165 }
2166 // setupChatDeps creates the common dependencies needed for
2167 // chat-related tests: user, org, org member, provider, model config.
2168 type chatDeps struct {
2169 user database.User
2170 org database.Organization
2171 modelConfig database.ChatModelConfig
2172 }
2173 setupChatDeps := func(t *testing.T, db database.Store) chatDeps {
2174 t.Helper()
2175 user := dbgen.User(t, db, database.User{})
2176 org := dbgen.Organization(t, db, database.Organization{})
2177 _ = dbgen.OrganizationMember(t, db, database.OrganizationMember{UserID: user.ID, OrganizationID: org.ID})
2178 _ = dbgen.ChatProvider(t, db, database.ChatProvider{
2179 Provider: "openai",
2180 DisplayName: "OpenAI",
2181 })
2182 mc := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{
2183 Provider: "openai",
2184 Model: "test-model",
2185 ContextLimit: 8192,
2186 })

Callers

nothing calls this directly

Calls 15

ChatFunction · 0.92
UserFunction · 0.92
OrganizationFunction · 0.92
OrganizationMemberFunction · 0.92
ChatProviderFunction · 0.92
ChatModelConfigFunction · 0.92
ContextFunction · 0.92
NewDBWithSQLDBFunction · 0.92
WithDumpOnFailureFunction · 0.92
NewFunction · 0.92
WithClockFunction · 0.92
TryReceiveFunction · 0.92

Tested by

no test coverage detected