@@ -3,8 +3,8 @@ import { db, dbFor } from '@sim/db'
33import { copilotChats , copilotMessages , organization , user } from '@sim/db/schema'
44import { generateId } from '@sim/utils/id'
55import { compareStrings } from '@sim/utils/string'
6- import { eq , inArray } from 'drizzle-orm'
7- import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest'
6+ import { eq } from 'drizzle-orm'
7+ import { afterAll , beforeAll , describe , expect , it } from 'vitest'
88import { deletableChatFiles } from '@/lib/cleanup/chat-cleanup'
99import { type ChatFileRef , collectChatFiles } from '@/lib/cleanup/chat-files'
1010
@@ -13,8 +13,7 @@ function attachment(key: string) {
1313}
1414
1515const userId = generateId ( )
16- /** One organization with chats, and one whose only chats were the deleted ones. */
17- const organizationIds = { withChats : generateId ( ) , empty : generateId ( ) }
16+ const organizationId = generateId ( )
1817
1918beforeAll ( async ( ) => {
2019 const now = new Date ( )
@@ -26,20 +25,18 @@ beforeAll(async () => {
2625 createdAt : now ,
2726 updatedAt : now ,
2827 } )
29- await db . insert ( organization ) . values (
30- Object . values ( organizationIds ) . map ( ( id ) => ( {
31- id,
32- name : 'Chat cleanup fixture' ,
33- slug : id ,
34- createdAt : now ,
35- updatedAt : now ,
36- } ) )
37- )
28+ await db . insert ( organization ) . values ( {
29+ id : organizationId ,
30+ name : 'Chat cleanup fixture' ,
31+ slug : organizationId ,
32+ createdAt : now ,
33+ updatedAt : now ,
34+ } )
3835} )
3936
4037afterAll ( async ( ) => {
4138 await db . delete ( copilotChats ) . where ( eq ( copilotChats . userId , userId ) )
42- await db . delete ( organization ) . where ( inArray ( organization . id , Object . values ( organizationIds ) ) )
39+ await db . delete ( organization ) . where ( eq ( organization . id , organizationId ) )
4340 await db . delete ( user ) . where ( eq ( user . id , userId ) )
4441 await Promise . all ( [ db . $client . end ( ) , dbFor ( 'cleanup' ) . $client . end ( ) ] )
4542} )
@@ -80,6 +77,7 @@ describe('chat cleanup file collection in PostgreSQL', () => {
8077 'not an attachment' ,
8178 null ,
8279 { key : '' } ,
80+ { key : 42 } ,
8381 ] ,
8482 } ,
8583 {
@@ -149,11 +147,11 @@ describe('deletable chat files against PostgreSQL message rows', () => {
149147 await seedChat (
150148 [ { role : 'user' , content : 'fork' , fileAttachments : [ attachment ( shared ) , 'x' , { key : 7 } ] } ] ,
151149 [ ] ,
152- organizationIds . withChats
150+ organizationId
153151 )
154152 // Attached only by a chat outside the scope, which cannot be a fork of the deleted one.
155153 await seedChat ( [ { role : 'user' , content : 'other' , fileAttachments : [ attachment ( elsewhere ) ] } ] )
156- const scope = { kind : 'organization' , id : organizationIds . withChats } as const
154+ const scope = { kind : 'organization' , id : organizationId } as const
157155
158156 const deletable = await deletableChatFiles ( [
159157 ref ( shared , scope ) ,
@@ -166,27 +164,14 @@ describe('deletable chat files against PostgreSQL message rows', () => {
166164 )
167165 } )
168166
169- it ( 'scans no messages for a scope with no chat left' , async ( ) => {
170- const scan = vi . spyOn ( dbFor ( 'cleanup' ) , 'execute' )
171- const key = `copilot/${ generateId ( ) } /gone.png`
172-
173- const deletable = await deletableChatFiles ( [
174- ref ( key , { kind : 'organization' , id : organizationIds . empty } ) ,
175- ] )
176-
177- expect ( deletable . map ( ( file ) => file . key ) ) . toEqual ( [ key ] )
178- expect ( scan ) . not . toHaveBeenCalled ( )
179- scan . mockRestore ( )
180- } )
181-
182167 it ( 'places a legacy ref by its key, keeping one it cannot place' , async ( ) => {
183- const referenced = `assistant/${ organizationIds . withChats } /${ userId } /u1/kept.png`
184- const unreferenced = `assistant/${ organizationIds . withChats } /${ userId } /u2/gone.png`
168+ const referenced = `assistant/${ organizationId } /${ userId } /u1/kept.png`
169+ const unreferenced = `assistant/${ organizationId } /${ userId } /u2/gone.png`
185170 const unplaceable = `copilot/${ generateId ( ) } /legacy.png`
186171 await seedChat (
187172 [ { role : 'user' , content : 'fork' , fileAttachments : [ attachment ( referenced ) ] } ] ,
188173 [ ] ,
189- organizationIds . withChats
174+ organizationId
190175 )
191176
192177 const deletable = await deletableChatFiles (
0 commit comments