55 */
66import { db } from '@sim/db'
77import { permissions , user , workspace , workspaceFiles } from '@sim/db/schema'
8- import { explainCaptured } from '@sim/db/testing/explain'
98import { assertDisposableTestDatabaseUrl } from '@sim/db/testing/test-infrastructure'
109import { deleteWorkspaceFixture , insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
1110import { generateId } from '@sim/utils/id'
@@ -175,11 +174,9 @@ describe('bounded workspace file reads in PostgreSQL', () => {
175174 INSERT INTO folder (id, name, user_id, workspace_id, resource_type, parent_id)
176175 VALUES (${ folders . deep } , 'Deep', ${ alice } , ${ workspaceId } , 'file', ${ folders . sub } )` )
177176
178- /**
179- * Ties on every sort key: the same name in different folders, equal sizes, and timestamps
180- * equal to the microsecond or distinct only below the millisecond — where ordering on the raw
181- * column and ordering on its millisecond truncation disagree.
182- */
177+ // Ties on every sort key: the same name in different folders, equal sizes, and timestamps
178+ // equal to the microsecond or distinct only below the millisecond — where ordering on the raw
179+ // column and ordering on its millisecond truncation disagree.
183180 const t = '2026-03-01T10:00:00.123'
184181 for ( const seed of [
185182 {
@@ -293,14 +290,6 @@ describe('bounded workspace file reads in PostgreSQL', () => {
293290 ] )
294291 expect ( [ ...firstPage . map ( ( row ) => row . id ) , ...resumed ] ) . toEqual ( legacyOrder )
295292 } )
296-
297- it ( 'rejects a cursor value the sort cannot hold as a validation error' , async ( ) => {
298- await expect (
299- page ( { sortBy : 'uploadedAt' , after : [ 'not-a-date' , 'wf_x' ] } )
300- ) . rejects . toMatchObject ( {
301- code : 'validation' ,
302- } )
303- } )
304293 } )
305294
306295 describe ( 'folder filters by id' , ( ) => {
@@ -358,45 +347,6 @@ describe('bounded workspace file reads in PostgreSQL', () => {
358347 } )
359348 ) . rejects . toMatchObject ( { code : 'validation' } )
360349 } )
361-
362- /**
363- * The folder predicate must be spelled on `coalesce(folder_id, '')`: that is the key the
364- * `(workspace_id, coalesce(folder_id, ''), original_name)` index leads with, and a name-sorted
365- * folder page that cannot seek it reads every live file the workspace holds.
366- */
367- it ( 'seeks the folder name index for a name-sorted root or folder page' , async ( ) => {
368- await db . execute ( sql `
369- INSERT INTO ${ workspaceFiles } (id, key, user_id, workspace_id, folder_id, context, original_name, content_type, size_bytes)
370- SELECT 'wf_pad_' || n || '_' || ${ workspaceId } , 'pad/' || n || '/' || ${ workspaceId } , ${ alice } ,
371- ${ workspaceId } , CASE WHEN n % 2 = 0 THEN ${ folders . media } END, 'workspace', 'pad-' || n || '.txt',
372- 'text/plain', 1
373- FROM generate_series(1, 3000) AS n` )
374- await db . execute ( sql `ANALYZE ${ workspaceFiles } ` )
375- try {
376- for ( const folderId of [ null , folders . media ] ) {
377- const nodes = await explainCaptured (
378- db ,
379- ( ) =>
380- queryWorkspaceFiles ( workspaceId , {
381- folderId,
382- sortBy : 'name' ,
383- sortOrder : 'asc' ,
384- limit : 50 ,
385- after : [ 'pad-1500.txt' , 'wf_x' ] ,
386- } ) ,
387- { match : / o r d e r b y / }
388- )
389- const scan = nodes . find (
390- ( node ) => node [ 'Index Name' ] === 'workspace_files_workspace_folder_name_active_unique'
391- )
392- expect ( scan ?. [ 'Index Cond' ] ) . toMatch ( / C O A L E S C E \( f o l d e r _ i d .* o r i g i n a l _ n a m e > = / )
393- }
394- } finally {
395- await db . execute (
396- sql `DELETE FROM ${ workspaceFiles } WHERE id LIKE 'wf_pad_%' AND workspace_id = ${ workspaceId } `
397- )
398- }
399- } )
400350 } )
401351
402352 describe ( 'name search' , ( ) => {
@@ -474,13 +424,14 @@ describe('bounded workspace file reads in PostgreSQL', () => {
474424 } )
475425
476426 it ( 'rolls each child folder subtree up, excluding deleted files' , async ( ) => {
477- expect ( ( await stats ( null ) ) . folders ) . toEqual (
427+ const atRoot = ( await stats ( null ) ) . folders
428+ expect ( atRoot ) . toHaveLength ( 2 )
429+ expect ( atRoot ) . toEqual (
478430 expect . arrayContaining ( [
479431 { folderId : folders . docs , fileCount : 4 , sizeBytes : 100 + 200 + 400 + 1000 } ,
480432 { folderId : folders . media , fileCount : 1 , sizeBytes : 50 } ,
481433 ] )
482434 )
483- expect ( ( await stats ( null ) ) . folders ) . toHaveLength ( 2 )
484435 expect ( ( await stats ( folders . docs ) ) . folders ) . toEqual ( [
485436 { folderId : folders . sub , fileCount : 2 , sizeBytes : 1400 } ,
486437 ] )
@@ -495,20 +446,21 @@ describe('bounded workspace file reads in PostgreSQL', () => {
495446
496447 describe ( 'authorization' , ( ) => {
497448 it ( 'serves a read member and refuses a non-member on every read' , async ( ) => {
498- await expect ( page ( { } , { userId : bob } ) ) . resolves . toBeDefined ( )
499- await expect ( page ( { } , { userId : carol } ) ) . rejects . toBeDefined ( )
449+ expect ( ( await page ( { } , { userId : bob } ) ) . files ) . toHaveLength ( files . length )
450+ const refused = { code : 'forbidden' }
451+ await expect ( page ( { } , { userId : carol } ) ) . rejects . toMatchObject ( refused )
500452 await expect (
501453 lookupWorkspaceFiles . execute ( {
502454 principal : session ( carol ) ,
503455 input : { workspaceId, ids : [ 'wf_x' ] } ,
504456 } )
505- ) . rejects . toBeDefined ( )
457+ ) . rejects . toMatchObject ( refused )
506458 await expect (
507459 getWorkspaceFileFolderStats . execute ( {
508460 principal : session ( carol ) ,
509461 input : { workspaceId, parentId : null } ,
510462 } )
511- ) . rejects . toBeDefined ( )
463+ ) . rejects . toMatchObject ( refused )
512464 } )
513465 } )
514466} )
0 commit comments