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

Function TestWorker

coderd/x/gitsync/worker_test.go:936–1013  ·  view source on GitHub ↗

TestWorker exercises the worker tick against a real PostgreSQL database to verify that the SQL queries, foreign key constraints, and upsert logic work end-to-end.

(t *testing.T)

Source from the content-addressed store, hash-verified

934// real PostgreSQL database to verify that the SQL queries, foreign key
935// constraints, and upsert logic work end-to-end.
936func TestWorker(t *testing.T) {
937 t.Parallel()
938 ctx := testutil.Context(t, testutil.WaitLong)
939
940 // 1. Real database store.
941 db, _ := dbtestutil.NewDB(t)
942
943 // 2. Create a user and an organization (FKs for chats).
944 user := dbgen.User(t, db, database.User{})
945 org := dbgen.Organization(t, db, database.Organization{})
946
947 // 3. Set up FK chain: ai_providers -> chat_model_configs -> chats.
948 _ = dbgen.ChatProvider(t, db, database.ChatProvider{})
949
950 modelCfg := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{
951 Model: "test-model",
952 ContextLimit: 100000,
953 })
954
955 chat := dbgen.Chat(t, db, database.Chat{
956 OrganizationID: org.ID,
957 OwnerID: user.ID,
958 LastModelConfigID: modelCfg.ID,
959 Title: "integration-test",
960 })
961
962 // 4. Seed a stale diff status row so the worker picks it up.
963 _, err := db.UpsertChatDiffStatusReference(ctx, database.UpsertChatDiffStatusReferenceParams{
964 ChatID: chat.ID,
965 GitBranch: "feature",
966 GitRemoteOrigin: "https://github.com/o/r",
967 StaleAt: time.Now().Add(-time.Minute),
968 Url: sql.NullString{},
969 })
970 require.NoError(t, err)
971
972 // 5. Mock refresher returns a canned PR status.
973 mClock := quartz.NewMock(t)
974 refresher := newTestRefresher(t, mClock)
975
976 // 6. Track publish calls.
977 var publishCount atomic.Int32
978 tickDone := make(chan struct{})
979 pub := func(_ context.Context, chatID uuid.UUID) error {
980 assert.Equal(t, chat.ID, chatID)
981 if publishCount.Add(1) == 1 {
982 close(tickDone)
983 }
984 return nil
985 }
986
987 // 7. Create and run the worker for one tick.
988 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
989 worker := gitsync.NewWorker(db, refresher, pub, mClock, logger)
990
991 tickOnce(ctx, t, mClock, worker, tickDone)
992
993 // 8. Assert publisher was called.

Callers

nothing calls this directly

Calls 15

ContextFunction · 0.92
NewDBFunction · 0.92
UserFunction · 0.92
OrganizationFunction · 0.92
ChatProviderFunction · 0.92
ChatModelConfigFunction · 0.92
ChatFunction · 0.92
NewWorkerFunction · 0.92
newTestRefresherFunction · 0.85
tickOnceFunction · 0.85
AddMethod · 0.65

Tested by

no test coverage detected