|
1 | 1 | import { |
| 2 | + claimWatchDelivery, |
2 | 3 | createChat, |
3 | 4 | createDashboardAgentDb, |
| 5 | + createWatch, |
| 6 | + deleteTerminalWatchesOlderThan, |
| 7 | + getWatch, |
4 | 8 | listChats, |
| 9 | + releaseWatchDelivery, |
5 | 10 | softDeleteChat, |
| 11 | + softDeleteChatsForOrganization, |
| 12 | + transitionWatchCondition, |
6 | 13 | type DashboardAgentDb, |
7 | 14 | type DashboardAgentDbClient, |
8 | 15 | } from "@internal/dashboard-agent-db"; |
@@ -98,3 +105,97 @@ describe("softDeleteChat tenant isolation", () => { |
98 | 105 | 30_000 |
99 | 106 | ); |
100 | 107 | }); |
| 108 | + |
| 109 | +/** |
| 110 | + * Both delivery sweeps skip a watch whose chat is deleted, and retention only deletes rows |
| 111 | + * whose delivery is settled — so a wake still owed when the chat goes would keep its snapshot |
| 112 | + * until the chat's hard delete (30 days) instead of the much shorter watch retention cutoff. |
| 113 | + */ |
| 114 | +describe("deleting a chat settles the wakes it still owes", () => { |
| 115 | + async function firedWatch(db: DashboardAgentDb, chatId: string) { |
| 116 | + const created = await createWatch(db, { |
| 117 | + chatId, |
| 118 | + identity: `run_finished:run_${chatId}`, |
| 119 | + spec: { |
| 120 | + kind: "run_finished", |
| 121 | + runId: `run_${chatId}`, |
| 122 | + checkEveryMinutes: 1, |
| 123 | + maxHours: 2, |
| 124 | + } as never, |
| 125 | + organizationId: ORG, |
| 126 | + projectId: "proj_1", |
| 127 | + environmentId: "env_1", |
| 128 | + userId: USER, |
| 129 | + expiresAt: new Date(Date.now() + 60 * 60 * 1000), |
| 130 | + }); |
| 131 | + if (!created.ok) throw new Error(`the watch wasn't created: ${created.error}`); |
| 132 | + |
| 133 | + const fired = await transitionWatchCondition(db, { |
| 134 | + id: created.watch.id, |
| 135 | + resolution: "condition_met", |
| 136 | + lastResult: { runs: 1 }, |
| 137 | + }); |
| 138 | + expect(fired?.deliveryStatus).toBe("pending"); |
| 139 | + return created.watch.id; |
| 140 | + } |
| 141 | + |
| 142 | + postgresTest( |
| 143 | + "softDeleteChat settles a fired watch's pending delivery, so retention can reclaim it", |
| 144 | + async ({ prisma, postgresContainer }) => { |
| 145 | + const db = await boot(prisma, postgresContainer.getConnectionUri()); |
| 146 | + await createChat(db, { id: "chat_1", organizationId: ORG, userId: USER }); |
| 147 | + const watchId = await firedWatch(db, "chat_1"); |
| 148 | + |
| 149 | + await softDeleteChat(db, { chatId: "chat_1", userId: USER, organizationId: ORG }); |
| 150 | + |
| 151 | + expect((await getWatch(db, { id: watchId }))?.deliveryStatus).toBe("not_required"); |
| 152 | + expect( |
| 153 | + await deleteTerminalWatchesOlderThan(db, { before: new Date(Date.now() + 60_000) }) |
| 154 | + ).toBe(1); |
| 155 | + }, |
| 156 | + 30_000 |
| 157 | + ); |
| 158 | + |
| 159 | + postgresTest( |
| 160 | + "a delivery claimed in flight is settled too, claim and all", |
| 161 | + async ({ prisma, postgresContainer }) => { |
| 162 | + const db = await boot(prisma, postgresContainer.getConnectionUri()); |
| 163 | + await createChat(db, { id: "chat_1", organizationId: ORG, userId: USER }); |
| 164 | + const watchId = await firedWatch(db, "chat_1"); |
| 165 | + |
| 166 | + // Deleted while a deliverer holds the claim: no sweep can reach the row afterwards, so |
| 167 | + // it would sit in `delivering` for as long as the chat does. |
| 168 | + const claim = await claimWatchDelivery(db, { |
| 169 | + id: watchId, |
| 170 | + staleBefore: new Date(Date.now() - 60_000), |
| 171 | + }); |
| 172 | + expect(claim?.watch.deliveryStatus).toBe("delivering"); |
| 173 | + |
| 174 | + await softDeleteChat(db, { chatId: "chat_1", userId: USER, organizationId: ORG }); |
| 175 | + |
| 176 | + const settled = await getWatch(db, { id: watchId }); |
| 177 | + expect(settled?.deliveryStatus).toBe("not_required"); |
| 178 | + expect(settled?.deliveryClaimId).toBeNull(); |
| 179 | + expect(settled?.deliveryClaimedAt).toBeNull(); |
| 180 | + |
| 181 | + // The deliverer that still holds the old claim can no longer move the row either way. |
| 182 | + await releaseWatchDelivery(db, { id: watchId, claimId: claim!.claimId }); |
| 183 | + expect((await getWatch(db, { id: watchId }))?.deliveryStatus).toBe("not_required"); |
| 184 | + }, |
| 185 | + 30_000 |
| 186 | + ); |
| 187 | + |
| 188 | + postgresTest( |
| 189 | + "softDeleteChatsForOrganization settles them too", |
| 190 | + async ({ prisma, postgresContainer }) => { |
| 191 | + const db = await boot(prisma, postgresContainer.getConnectionUri()); |
| 192 | + await createChat(db, { id: "chat_1", organizationId: ORG, userId: USER }); |
| 193 | + const watchId = await firedWatch(db, "chat_1"); |
| 194 | + |
| 195 | + expect(await softDeleteChatsForOrganization(db, { organizationId: ORG })).toBe(1); |
| 196 | + |
| 197 | + expect((await getWatch(db, { id: watchId }))?.deliveryStatus).toBe("not_required"); |
| 198 | + }, |
| 199 | + 30_000 |
| 200 | + ); |
| 201 | +}); |
0 commit comments