@@ -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 { deterministicFileId , 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,127 @@ 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+
140+ test ( "keyed file IDs are UUIDs that work through lookup, content reads and attachments" , async ( ) => {
141+ const owner = "local-user" ;
142+ const sample = await createSamplePdf ( ) ;
143+ const source = await agent . files . import ( owner , "form.pdf" , sample , "test" ) ;
144+ const fields = { participant_name : "Sample Student" , permission_granted : true } ;
145+ const filled = await agent . files . fill ( owner , source . id , fields , "task-1:fill_pdf" ) ;
146+ const imported = await agent . files . import (
147+ owner ,
148+ "a.pdf" ,
149+ sample ,
150+ "test" ,
151+ undefined ,
152+ "attachment:1" ,
153+ ) ;
154+ assert . equal ( ( await agent . files . fill ( owner , source . id , fields , "task-1:fill_pdf" ) ) . id , filled . id ) ;
155+ for ( const id of [ filled . id , imported . id ] ) {
156+ assert . match ( id , / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - 5 [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / ) ;
157+ assert . equal ( ( await agent . files . get ( owner , id ) ) . id , id ) ;
158+ const content = await app . request ( `/api/files/${ id } /content` , { headers : headers ( ) } ) ;
159+ assert . equal ( content . status , 200 ) ;
160+ assert . equal (
161+ Buffer . from ( await content . arrayBuffer ( ) )
162+ . subarray ( 0 , 5 )
163+ . toString ( ) ,
164+ "%PDF-" ,
165+ ) ;
166+ const proposal = await app . request ( "/api/actions" , {
167+ method : "POST" ,
168+ headers : headers ( ) ,
169+ body : JSON . stringify ( {
170+ kind : "email.send" ,
171+ data : {
172+ to : [ "sample@example.com" ] ,
173+ subject : "Attached" ,
174+ body : "See attached." ,
175+ attachmentIds : [ id ] ,
176+ } ,
177+ } ) ,
178+ } ) ;
179+ assert . equal ( proposal . status , 201 ) ;
180+ }
181+ assert . equal ( deterministicFileId ( owner , "x" ) , deterministicFileId ( owner , "x" ) ) ;
182+ assert . notEqual ( deterministicFileId ( owner , "x" ) , deterministicFileId ( "other" , "x" ) ) ;
183+ } ) ;
184+
61185test ( "sample workspace serves a real PDF and filling creates a new version" , async ( ) => {
62186 const response = await app . request ( "/api/workspace" , { headers : headers ( ) } ) ;
63187 assert . equal ( response . status , 200 ) ;
0 commit comments