nolint:paralleltest // It uses LockIDDBPurge.
(t *testing.T)
| 2674 | |
| 2675 | //nolint:paralleltest // It uses LockIDDBPurge. |
| 2676 | func TestAutoArchiveInactiveChats(t *testing.T) { |
| 2677 | now := time.Date(2025, 6, 15, 12, 0, 0, 0, time.UTC) |
| 2678 | |
| 2679 | tests := []struct { |
| 2680 | name string |
| 2681 | run func(t *testing.T) |
| 2682 | }{ |
| 2683 | { |
| 2684 | name: "AutoArchiveDisabled", |
| 2685 | run: func(t *testing.T) { |
| 2686 | h := newArchiveHarness(t, now) |
| 2687 | ctx, clk, db, rawDB, logger, deps := h.ctx, h.clk, h.db, h.rawDB, h.logger, h.deps |
| 2688 | |
| 2689 | require.Zero(t, codersdk.DefaultChatAutoArchiveDays) |
| 2690 | require.NoError(t, db.UpsertChatAutoArchiveDays(ctx, codersdk.DefaultChatAutoArchiveDays)) |
| 2691 | |
| 2692 | // Chat older than any reasonable cutoff. |
| 2693 | staleChat := createArchiveChat(ctx, t, db, rawDB, deps, "stale-chat", now.Add(-365*24*time.Hour)) |
| 2694 | |
| 2695 | auditor := audit.NewMock() |
| 2696 | auditorPtr := mockAuditorPtr(auditor) |
| 2697 | enqueuer := notificationstest.NewFakeEnqueuer() |
| 2698 | done := awaitDoTick(ctx, t, clk) |
| 2699 | closer := dbpurge.New(ctx, logger, db, &codersdk.DeploymentValues{}, prometheus.NewRegistry(), auditorPtr, dbpurge.WithNotificationsEnqueuer(enqueuer), dbpurge.WithClock(clk)) |
| 2700 | defer closer.Close() |
| 2701 | testutil.TryReceive(ctx, t, done) |
| 2702 | |
| 2703 | // Not archived, no audits, no digests. |
| 2704 | refreshed, err := db.GetChatByID(ctx, staleChat.ID) |
| 2705 | require.NoError(t, err) |
| 2706 | require.False(t, refreshed.Archived, "chat should stay active when auto-archive is disabled") |
| 2707 | |
| 2708 | require.Empty(t, auditor.AuditLogs(), "no audit log entries expected") |
| 2709 | require.Empty(t, enqueuer.Sent(), "no digest notifications expected") |
| 2710 | }, |
| 2711 | }, |
| 2712 | { |
| 2713 | name: "ArchivesInactiveRoot", |
| 2714 | run: func(t *testing.T) { |
| 2715 | h := newArchiveHarness(t, now) |
| 2716 | ctx, clk, db, rawDB, logger, deps := h.ctx, h.clk, h.db, h.rawDB, h.logger, h.deps |
| 2717 | |
| 2718 | // Regression guard: ensure that both auto-archive and retention |
| 2719 | // are both set to a distinct non-zero value. |
| 2720 | require.NoError(t, db.UpsertChatAutoArchiveDays(ctx, int32(90))) |
| 2721 | require.NoError(t, db.UpsertChatRetentionDays(ctx, int32(30))) |
| 2722 | |
| 2723 | // Inactive root: newest message 100 days old. |
| 2724 | staleChat := createArchiveChat(ctx, t, db, rawDB, deps, "stale-chat", now.Add(-120*24*time.Hour)) |
| 2725 | insertTextMessage(ctx, t, db, rawDB, staleChat.ID, deps.user.ID, deps.modelConfig.ID, now.Add(-100*24*time.Hour)) |
| 2726 | |
| 2727 | // Active root: message 10 days old, within cutoff. |
| 2728 | activeChat := createArchiveChat(ctx, t, db, rawDB, deps, "active-chat", now.Add(-120*24*time.Hour)) |
| 2729 | insertTextMessage(ctx, t, db, rawDB, activeChat.ID, deps.user.ID, deps.modelConfig.ID, now.Add(-10*24*time.Hour)) |
| 2730 | |
| 2731 | auditor := audit.NewMock() |
| 2732 | auditorPtr := mockAuditorPtr(auditor) |
| 2733 | enqueuer := notificationstest.NewFakeEnqueuer() |
nothing calls this directly
no test coverage detected