@@ -6,11 +6,14 @@ import { after, before, test } from "node:test";
66import { EventType } from "@ag-ui/core" ;
77import { lastValueFrom , toArray } from "rxjs" ;
88import { createApp } from "../apps/server/src/app.ts" ;
9+ import { Auth } from "../apps/server/src/auth.ts" ;
910import type { Config } from "../apps/server/src/config.ts" ;
1011import { createStore , type Store } from "../apps/server/src/db.ts" ;
1112import { ConversationAgent } from "../apps/server/src/engine/conversation.ts" ;
1213import type { AgentService } from "../apps/server/src/engine/service.ts" ;
14+ import { Files } from "../apps/server/src/files.ts" ;
1315import type { ActionProposal , Artifact , Workspace } from "../packages/domain/src/index.ts" ;
16+ import { createSamplePdf } from "../packages/integrations/src/pdf.ts" ;
1417
1518let db : Store ,
1619 app : Awaited < ReturnType < typeof createApp > > [ "app" ] ,
@@ -58,6 +61,82 @@ test("API protects private data and rejects unrelated web origins", async () =>
5861 403 ,
5962 ) ;
6063} ) ;
64+ test ( "file operations recover a published PDF when metadata persistence fails" , async ( t ) => {
65+ const owner = "file-recovery" ;
66+ const bytes = await createSamplePdf ( ) ;
67+ const insert = db . insertIfAbsent . bind ( db ) ;
68+ let publishedId = "" ;
69+ t . mock . method ( db , "insertIfAbsent" , async ( ...args : Parameters < Store [ "insertIfAbsent" ] > ) => {
70+ if ( args [ 0 ] === owner && args [ 1 ] === "files" && ! publishedId ) {
71+ publishedId = args [ 2 ] . id ;
72+ throw new Error ( "metadata unavailable" ) ;
73+ }
74+ return insert ( ...args ) ;
75+ } ) ;
76+ await assert . rejects (
77+ agent . files . import ( owner , "sample.pdf" , bytes , "fixture" , undefined , "import-once" ) ,
78+ / m e t a d a t a u n a v a i l a b l e / ,
79+ ) ;
80+ assert . ok ( publishedId ) ;
81+ assert . equal ( ( await agent . files . list ( owner ) ) . length , 0 ) ;
82+ const restarted = new Files ( db , config , new Auth ( db , config , "sample-signing-key" ) ) ;
83+ const recovered = await restarted . import (
84+ owner ,
85+ "sample.pdf" ,
86+ bytes ,
87+ "fixture" ,
88+ undefined ,
89+ "import-once" ,
90+ ) ;
91+ assert . equal ( recovered . id , publishedId ) ;
92+ assert . deepEqual ( new Uint8Array ( await restarted . bytes ( owner , recovered . id ) ) , bytes ) ;
93+ assert . equal ( ( await restarted . list ( owner ) ) . length , 1 ) ;
94+ } ) ;
95+
96+ test ( "file operation identity isolates owners, inputs and independent requests" , async ( ) => {
97+ const owner = "file-identities" ;
98+ const bytes = await createSamplePdf ( ) ;
99+ const source = await agent . files . import ( owner , "sample.pdf" , bytes , "fixture" ) ;
100+ const fields = { participant_name : "Sample Student" , permission_granted : true } ;
101+ const [ first , concurrent ] = await Promise . all ( [
102+ agent . files . fill ( owner , source . id , fields , "task-fill" ) ,
103+ agent . files . fill ( owner , source . id , fields , "task-fill" ) ,
104+ ] ) ;
105+ assert . equal ( first . id , concurrent . id ) ;
106+ assert . equal ( first . createdAt , concurrent . createdAt ) ;
107+ assert . equal ( ( await agent . files . list ( owner ) ) . filter ( ( file ) => file . parentId ) . length , 1 ) ;
108+ const reordered = await agent . files . fill (
109+ owner ,
110+ source . id ,
111+ {
112+ permission_granted : true ,
113+ participant_name : "Sample Student" ,
114+ } ,
115+ "task-fill" ,
116+ ) ;
117+ assert . equal ( reordered . id , first . id ) ;
118+ assert . notEqual ( ( await agent . files . fill ( owner , source . id , fields , "another-task" ) ) . id , first . id ) ;
119+ assert . notEqual (
120+ ( await agent . files . fill ( owner , source . id , { participant_name : "Another Student" } , "task-fill" ) )
121+ . id ,
122+ first . id ,
123+ ) ;
124+ const manual = await agent . files . fill ( owner , source . id , fields ) ;
125+ assert . notEqual ( ( await agent . files . fill ( owner , source . id , fields ) ) . id , manual . id ) ;
126+ const otherOwner = await agent . files . import (
127+ "other-file-owner" ,
128+ "sample.pdf" ,
129+ bytes ,
130+ "fixture" ,
131+ undefined ,
132+ "owner-scoped" ,
133+ ) ;
134+ assert . notEqual (
135+ ( await agent . files . import ( owner , "sample.pdf" , bytes , "fixture" , undefined , "owner-scoped" ) ) . id ,
136+ otherOwner . id ,
137+ ) ;
138+ } ) ;
139+
61140test ( "sample workspace serves a real PDF and filling creates a new version" , async ( ) => {
62141 const response = await app . request ( "/api/workspace" , { headers : headers ( ) } ) ;
63142 assert . equal ( response . status , 200 ) ;
0 commit comments